Train a pyg model with edge features only, no node level feature vector

338 views Asked by At

With a dataset containing only edge features. This graph has nodes with no X features. This dataset is a set of edges about a network and its network flow. Ex: ip-address1, ip-address2 are the source & target nodes ... the edge_attr

To build a model in pytorch geometric (pyg) how do I pass the x (features) to the Data object?

Here is what I do now, but is not working:

X = np.zeros(shape=(len(node_indices),0))
x = torch.tensor(X, dtype=torch.double)
...
data = Data(x=x, edge_index=edge_index, edge_attr=edge_attr, y=y)

print data: Data(x=[1784, 0], edge_index=[2, 1087823], edge_attr=[1087823, 6], y=[1087823, 1])

P.S. A similar question is in this link

1

There are 1 answers

2
Renyi On

A straightforward way to deal with graphs without node features is to give all the nodes the same feature, as shown in the following code. Here I suggest using torch.ones instead of torch.zeros as in some GNNs the message is multiplied with node embeddings.

X = torch.ones(size=(len(node_indices),1))
...
data = Data(x=X, edge_index=edge_index, edge_attr=edge_attr, y=y)

You also can create some node features based on the graph information, for example, using the Positional Encoding methods. In pytorch geometric, you can directly apply these methods by using transforms AddLaplacianEigenvectorPE and AddRandomWalkPE.