Hi there, I am trying to debug an issue for predicting a 25 value array for a given graph. For example, there could be 175 nodes with 300 edges, and this graph will give a tensor of 25 values. Also, there is only 1 type of node that has one feature, but there are 2 types of edges (beats, and loses_to). I have been trying to follow along to this tutorial.
class RGCN(nn.Module):
def __init__(self, in_feats, hid_feats, out_feats, rel_names):
super().__init__()
self.conv1 = dglnn.HeteroGraphConv({
rel: dglnn.GraphConv(in_feats, hid_feats)
for rel in rel_names}, aggregate='sum')
self.conv2 = dglnn.HeteroGraphConv({
rel: dglnn.GraphConv(hid_feats, out_feats)
for rel in rel_names}, aggregate='sum')
def forward(self, graph, inputs):
# inputs is features of nodes
h = self.conv1(graph, inputs)
h = {k: F.relu(v) for k, v in h.items()}
h = self.conv2(graph, h)
return h
class HeteroClassifier(nn.Module):
def __init__(self, in_dim, hidden_dim, n_outputs, rel_names):
super().__init__()
self.rgcn = RGCN(in_dim, hidden_dim, hidden_dim, rel_names)
self.classify = nn.Linear(hidden_dim, n_outputs)
def forward(self, g):
h = g.ndata['feat']
h = self.rgcn(g, h)
with g.local_scope():
g.ndata['h'] = h
# Calculate graph representation by average readout.
hg = 0
for ntype in g.ntypes:
hg = hg + dgl.mean_nodes(g, 'h', ntype=ntype)
return self.classify(hg)
Here is the training step:
# etypes is the list of edge types as strings.
model = HeteroClassifier(1, 20, 25, ['beats', 'loses_to'])
opt = torch.optim.Adam(model.parameters())
for epoch in range(20):
for batched_graph, labels in cf_dataloader:
#print(batched_graph, labels)
predictions = model(batched_graph)
loss = F.MSELoss(predictions, labels)
opt.zero_grad()
loss.backward()
opt.step()
However, the error I am receiving is this:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-185-457c3968e0c7> in <module>()
5 for batched_graph, labels in cf_dataloader:
6 #print(batched_graph, labels)
----> 7 predictions = model(batched_graph)
8 loss = F.MSELoss(predictions, labels)
9 opt.zero_grad()
6 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1129 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []
<ipython-input-170-34c06fd537f6> in forward(self, g)
26 def forward(self, g):
27 h = g.ndata['feat']
---> 28 h = self.rgcn(g, h)
29 with g.local_scope():
30 g.ndata['h'] = h['school']
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1129 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []
<ipython-input-170-34c06fd537f6> in forward(self, graph, inputs)
12 def forward(self, graph, inputs):
13 # inputs is features of nodes
---> 14 h = self.conv1(graph, inputs)
15 h = {k: F.relu(v) for k, v in h.items()}
16 h = self.conv2(graph, h)
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1129 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []
/usr/local/lib/python3.7/dist-packages/dgl/nn/pytorch/hetero.py in forward(self, g, inputs, mod_args, mod_kwargs)
185 if rel_graph.number_of_edges() == 0:
186 continue
--> 187 if stype not in inputs:
188 continue
189 dstdata = self.mods[etype](
/usr/local/lib/python3.7/dist-packages/torch/_tensor.py in __contains__(self, element)
784 raise RuntimeError(
785 "Tensor.__contains__ only supports Tensor or scalar, but you passed in a %s." %
--> 786 type(element)
787 )
788
RuntimeError: Tensor.__contains__ only supports Tensor or scalar, but you passed in a <class 'str'>.
Can anyone assist me in what I am doing wrong? This community has been so helpful and great, but I haven’t found anything related to what I am trying to accomplish.