torch_geometric.transforms 中的 AttributeError
Posted
技术标签:
【中文标题】torch_geometric.transforms 中的 AttributeError【英文标题】:AttributeError in torch_geometric.transforms 【发布时间】:2021-07-07 21:02:57 【问题描述】:我有一个我无法理解的问题:即使模块“torch_geometric.transforms”根据documentation 具有属性“AddTrainValTestMask”,我也无法导入它。我不断收到错误AttributeError: module 'torch_geometric.transforms' has no attribute 'AddTrainValTestMask
我的 Pytorch 版本是 1.7.1
我从here获取代码
最小可重现示例:
import os.path as osp
import torch
import torch.nn.functional as F
from torch_geometric.datasets import Planetoid
import torch_geometric.transforms as T
from torch_geometric.nn import SplineConv
dataset = 'Cora'
transform = T.Compose([
T.AddTrainValTestMask('train_rest', num_val=500, num_test=500),
T.TargetIndegree(),
])
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', dataset)
dataset = Planetoid(path, dataset, transform=transform)
data = dataset[0]
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = SplineConv(dataset.num_features, 16, dim=1, kernel_size=2)
self.conv2 = SplineConv(16, dataset.num_classes, dim=1, kernel_size=2)
def forward(self):
x, edge_index, edge_attr = data.x, data.edge_index, data.edge_attr
x = F.dropout(x, training=self.training)
x = F.elu(self.conv1(x, edge_index, edge_attr))
x = F.dropout(x, training=self.training)
x = self.conv2(x, edge_index, edge_attr)
return F.log_softmax(x, dim=1)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model, data = Net().to(device), data.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-3)
def train():
model.train()
optimizer.zero_grad()
F.nll_loss(model()[data.train_mask], data.y[data.train_mask]).backward()
optimizer.step()
def test():
model.eval()
log_probs, accs = model(), []
for _, mask in data('train_mask', 'test_mask'):
pred = log_probs[mask].max(1)[1]
acc = pred.eq(data.y[mask]).sum().item() / mask.sum().item()
accs.append(acc)
return accs
for epoch in range(1, 201):
train()
log = 'Epoch: :03d, Train: :.4f, Test: :.4f'
print(log.format(epoch, *test()))
谁能给我解释一下这个问题?
【问题讨论】:
你能添加整个错误回溯吗?--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-57-3f3a1b763433> in <module> 9 dataset = 'Cora' 10 transform = T.Compose([ ---> 11 T.AddTrainValTestMask('train_rest', num_val=500, num_test=500), 12 T.TargetIndegree(), 13 ]) AttributeError: module 'torch_geometric.transforms' has no attribute 'AddTrainValTestMask'
【参考方案1】:
在最新版本的 torch_geometric 中已将其重命名为 RandomNodeSplit
。可以直接用RandomNodeSplit
替换。
【讨论】:
以上是关于torch_geometric.transforms 中的 AttributeError的主要内容,如果未能解决你的问题,请参考以下文章