PyG GCNConv Layer Shape Mismatch Error: Reshaping Input Tensor for Compatibility
The error occurs in the forward pass of the GCNConv layer in the GCN model. The input tensor x has a shape of (120, 40) and the weight matrix has a shape of (1600, 8), which cannot be multiplied together.
To fix this error, you need to adjust the shape of the input tensor x to match the shape expected by the GCNConv layer. This can be done by reshaping the tensor to have a shape of (batch_size * num_nodes, num_node_features) before passing it to the GCNConv layer.
Here's the updated code for the forward method in the GCN model:
class GCN(torch.nn.Module):
def __init__(self, num_node_features, num_classes):
super(GCN, self).__init__()
self.conv1 = GCNConv(num_node_features, 8)
self.conv2 = GCNConv(8, 16)
self.conv3 = GCNConv(16, num_classes)
self.cnn = CNN() # 添加CNN模型
def forward(self, data):
x, edge_index = data.x, data.edge_index
# 执行卷积神经网络前向传播
features = []
for i in range(x.shape[0]):
features.append(self.cnn(x[i].unsqueeze(0)))
features = torch.cat(features, dim=0).view(x.shape[0], -1)
# 替换PyG数据集的节点特征x
data.x = features
# Reshape the input tensor x
x = x.view(-1, x.shape[-1])
x = self.conv1(x, edge_index)
x = F.relu(x)
x = self.conv2(x, edge_index)
x = F.relu(x)
x = F.dropout(x, training=self.training)
x = self.conv3(x, edge_index)
return x
With this update, the error should be resolved.
原文地址: https://www.cveoy.top/t/topic/pepE 著作权归作者所有。请勿转载和采集!