How to use node features which is 2 dimension? #2813
Unanswered
zzzzzzzzzzzx
asked this question in
Q&A
Replies: 1 comment 3 replies
-
The In your case, I would simply reshape your feature representation to a matrix, since this will result in the computation that I think you are trying to achieve anyway. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the pyg data format, x=[num_nodes, num_node_features], in all the explanation and the code I saw, this 'num_node_features' is a one-dimensional vector, that is, the node feature is one-dimensional data, but my node features is a two-dimensional matrix, how can i implement the node features in the pyg data format?
here is an example:
x = torch.tensor([[[2,1,1,3,5,6],[2,1,1,3,5,6]],[[5,6,1,3,5,6],[2,1,1,3,5,6]],[[3,7,1,3,5,6],[2,1,1,3,5,6]],[[12,0,1,3,5,6],[2,1,1,3,5,6]]],dtype=torch.float) y = torch.tensor([0,1,0,1], dtype=torch.float) edge_index = torch.tensor([[0,1,2,1,0,3,3,2], [1,0,1,2,3,0,2,3]],dtype=torch.long) mydata = Data(x=x,y=y,edge_index=edge_index) print(mydata)
The output is
Data(edge_index=[2, 5], x=[4, 2, 6], y=[4])
then i use a simple model to train this data:
class Net(torch.nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = GCNConv(mydata.num_node_features, 2) def forward(self, data): x, edge_index = data.x, data.edge_index x = self.conv1(x, edge_index) return F.log_softmax(x, dim=1)
The whole code is below:
`import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
from torch_geometric.data import Data
from torch_geometric.data import DataLoader
x = torch.tensor([[[2,1,1,3,5,6],[2,1,1,3,5,6]],[[5,6,1,3,5,6],[2,1,1,3,5,6]],[[3,7,1,3,5,6],[2,1,1,3,5,6]],[[12,0,1,3,5,6],[2,1,1,3,5,6]]],dtype=torch.float)
y = torch.tensor([0,1,0,1], dtype=torch.float)
edge_index = torch.tensor([[0,1,2,1,0,3,3,2],
[1,0,1,2,3,0,2,3]],dtype=torch.long)
mydata = Data(x=x,y=y,edge_index=edge_index)
print(mydata)
class Net(torch.nn.Module):
def init(self):
super(Net, self).init()
self.conv1 = GCNConv(mydata.num_node_features, 2)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Net().to(device)
data = mydata.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
model.train()
for epoch in range(10):
print('epoch',epoch)
optimizer.zero_grad()
out = model(data)
loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
loss.backward()
optimizer.step()
model.eval()
_, pred = model(data).max(dim=1)
correct = int(pred[data.test_mask].eq(data.y[data.test_mask]).sum().item())
acc = correct / int(data.test_mask.sum())
print('Accuracy: {:.4f}'.format(acc))`
This code report an error, which is "RuntimeError: size mismatch, m1: [8 x 6], m2: [2 x 2] at /opt/conda/conda-bld/pytorch_1579022034529/work/aten/src/THC/generic/THCTensorMathBlas.cu:290", Obviously this is because of my node feature. SO how can i use the node features which is 2 dimension? THANKS !
Beta Was this translation helpful? Give feedback.
All reactions