我是 pytorch 几何的新手,在运行我的模型时出现此错误:
RuntimeError: mat1 dim 1 必须匹配 mat2 dim 0
运行此代码时发生错误,它发生在z = model.encode(x, train_pos_edge_index)行
def train():
model.train()
optimizer.zero_grad()
z = model.encode(x, train_pos_edge_index)
loss = model.recon_loss(z, train_pos_edge_index)
loss = loss + (1 / data.num_nodes) * model.kl_loss() # new line
loss.backward()
optimizer.step()
return float(loss)
我生成模型的类如下:
class VariationalGCNEncoder(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super(VariationalGCNEncoder, self).__init__()
self.conv1 = GCNConv(in_channels, 2 * out_channels, cached=True) # cached only for transductive learning
self.conv_mu = GCNConv(2 * out_channels, out_channels, cached=True)
self.conv_logstd = GCNConv(2 * out_channels, out_channels, cached=True)
def forward(self, x, edge_index):
x = self.conv1(x, edge_index).relu()
return self.conv_mu(x, edge_index), self.conv_logstd(x, edge_index)
使用来自 torch_geometric.nn 的 VGAE,我创建模型如下:
out_channels = 2 #dimension of the embedding
num_features = 128
model = VGAE(VariationalGCNEncoder(num_features, out_channels))
变量的维度如下:
test_neg_edge_index=[2, 68],
test_pos_edge_index=[2, 68],
train_neg_adj_mask=[132, 132],
train_pos_edge_index=[2, 1166],
val_neg_edge_index=[2, 34],
val_pos_edge_index=[2, 34],
x=[132, 132]
模型中层的维数如下:
VGAE(
(encoder): VariationalGCNEncoder(
(conv1): GCNConv(128, 4)
(conv_mu): GCNConv(4, 2)
(conv_logstd): GCNConv(4, 2)
)
(decoder): InnerProductDecoder()
)
完整的错误回溯如下:
Traceback (most recent call last)
<ipython-input-20-af87b3233297> in <module>
2
3 for epoch in range(1, epochs + 1):
----> 4 loss = train()
5 auc, ap = test(data.test_pos_edge_index, data.test_neg_edge_index)
6 print('Epoch: {:03d}, AUC: {:.4f}, AP: {:.4f}'.format(epoch, auc, ap))
<ipython-input-19-e42a4a22847f> in train()
2 model.train()
3 optimizer.zero_grad()
----> 4 z = model.encode(x, train_pos_edge_index)
5 loss = model.recon_loss(z, train_pos_edge_index)
6
~\anaconda3\lib\site-packages\torch_geometric\nn\models\autoencoder.py in encode(self, *args, **kwargs)
153 def encode(self, *args, **kwargs):
154 """"""
--> 155 self.__mu__, self.__logstd__ = self.encoder(*args, **kwargs)
156 self.__logstd__ = self.__logstd__.clamp(max=MAX_LOGSTD)
157 z = self.reparametrize(self.__mu__, self.__logstd__)
~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
<ipython-input-16-d5c82de35dd4> in forward(self, x, edge_index)
7
8 def forward(self, x, edge_index):
----> 9 x = self.conv1(x, edge_index).relu()
10 return self.conv_mu(x, edge_index), self.conv_logstd(x, edge_index)
~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
~\anaconda3\lib\site-packages\torch_geometric\nn\conv\gcn_conv.py in forward(self, x, edge_index, edge_weight)
177 edge_index = cache
178
--> 179 x = x @ self.weight
180
181 # propagate_type: (x: Tensor, edge_weight: OptTensor)
RuntimeError: mat1 dim 1 must match mat2 dim 0