AssertionError: The HeteroNodeDataView has only one node type. please pass a tensor directly

Hi there,

I am trying a link prediction model on the following graph:

Graph(num_nodes={‘ent’: 12341},
num_edges={(‘ent’, ‘like’, ‘ent’): 11597, (‘ent’, ‘neutral’, ‘ent’): 58975, (‘ent’, ‘dislike’, ‘ent’): 8033},
metagraph=[(‘ent’, ‘ent’, ‘like’), (‘ent’, ‘ent’, ‘neutral’), (‘ent’, ‘ent’, ‘dislike’)])

My model is the following:

Define a Heterograph Conv model

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 are 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 Model(nn.Module):

def __init__(self, in_features, hidden_features, out_features, rel_names):

    super().__init__()

    self.sage = RGCN(in_features, hidden_features, out_features, rel_names)

    self.pred = HeteroDotProductPredictor()

def forward(self, g, neg_g, x, etype):

    h = self.sage(g, x)

    return self.pred(g, h, etype), self.pred(neg_g, h, etype)

When I try to compute loss with this:

def compute_loss(pos_score, neg_score):

# Margin loss

n_edges = pos_score.shape[0]

return (1 - pos_score.unsqueeze(1) + neg_score.view(n_edges, -1)).clamp(min=0).mean()

k = 5

num_nodes = g.num_nodes(‘ent’)

embeddings_dimensions = len(g.ndata[‘Feats’][1])

node_types = 1

model = Model(embeddings_dimensions, num_nodes, node_types, g.etypes)

node_feats = g.ndata[‘Feats’]

node_features = {‘ent’: node_feats}

opt = torch.optim.Adam(model.parameters())

for epoch in range(1):

  negative_graph = construct_negative_graph(g, k, ('ent', 'like, 'ent'))

  pos_score, neg_score = model(g, negative_graph, node_features, ('ent', 'like', 'ent'))

  loss = compute_loss(pos_score, neg_score)

  opt.zero_grad()

  loss.backward()

  opt.step()

  print(loss.item())

I get the following error:

AssertionError: The HeteroNodeDataView has only one node type. please pass a tensor directly

Could anybody tell which the problem is?

Thank you so much.

Do you know at which explicit line of code this error happened?

Hello and thank you again. It happens at:

pos_score, neg_score = model(g, negative_graph, node_features, (‘ent’, ‘like’, ‘ent’))

this is the rest of the trace.

As the error message suggested, you just need to replace node_features = {‘ent’: node_feats} by node_features = node_feats.

1 Like

Thanks again. I already tried that, but the next error happens:

RuntimeError: Tensor.contains only supports Tensor or scalar, but you passed in a <class ‘str’>.

It doesnt make sense, because when I print those feats:

node_feats = g.ndata[‘Feats’]
print(node_feats)
print(type(node_feats))

I get:

tensor([[ 0.0000, 1.0000, 0.0000, …, -0.4464, 4.3718, 3.4181],
[ 0.0000, 1.0000, 0.0000, …, 0.2697, -0.7329, -0.1157],
[ 0.0000, 1.0000, 0.0000, …, -0.0196, -0.0888, -1.4562],
…,
[ 0.0000, 0.0000, 1.0000, …, 0.4470, -0.1596, 1.2222],
[ 1.0000, 0.0000, 0.0000, …, 1.7735, 2.0091, 1.8494],
[ 1.0000, 0.0000, 0.0000, …, 1.5906, 1.9824, 1.3152]])
<class ‘torch.Tensor’>

This is the full snippet (i just changes the link name, nothing else)

Any ideas why? In theory all of these tensors should be valid since i was able to build the feats with them, but it looks like there is a string somewhere…

Sorry for the inconvenience and than you som much.

Can you provide a complete code snippet that is runnable along with a toy dataset?

1 Like

I believe the problem is in the score predictor. Since your graph is a heterogeneous graph with only one node type, g.ndata['h'] expects a tensor. However your HeteroDotProductPredictor (which I believe is from the user guide) expects a dictionary.

See also Edge Classification with one node type - #5 by BarclayII.

1 Like

Solved with this last tip, thanks @BarclayII

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.