My code to train the ML model is as follows:
import dgl
from dgl.nn import GraphConv
import torch
import torch.nn as nn
import torch.nn.functional as F
class GCN(nn.Module):
def __init__(self, in_feats, h_feats, num_classes):
super(GCN, self).__init__()
self.conv1 = GraphConv(in_feats, h_feats, allow_zero_in_degree=True)
self.conv2 = GraphConv(h_feats, num_classes, allow_zero_in_degree=True)
def forward(self, g, in_feat):
h = self.conv1(g, in_feat)
h = F.relu(h)
h = self.conv2(g, h)
g.ndata['h'] = h
h_mean = dgl.mean_nodes(g, 'h')
return h_mean
# Create the model
model = GCN(1, 4581, 1)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
# Training loop
for epoch in range(10):
for batched_graph, labels in train_data:
pred = model(batched_graph, batched_graph.ndata['cond'].float())
loss = F.cross_entropy(pred, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
I am using the DGL library and attempting to train a Convolutional Graph Neural Network. I am completely lost on why I am getting the following error after running my code:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x4581 and 1x4581)
Shouldn’t it be possible to multiply two matrices of the same shape?
I’ve ensured the shape of the input graphs is the same shape that the GraphConv layer is expecting.