关于理解节点和边索引的澄清
通过访问 data
对象的属性, 我如何知道哪个节点功能属于哪个节点? 如果我理解正确的话,data.x
包含节点功能。 通过运行下面的 for 循环,我可以访问这些功能 但我怎么知道它是属于节点 0 还是节点 9 呢?
from torch_geometric.data import Data
edge_index = torch.tensor([[0, 1, 1, 2, 1, 9],
[1, 0, 2, 1, 8, 1]
], dtype=torch.long)
x = torch.tensor([[-5,7], [0,5], [0,9], [10,9]], dtype=torch.float)
data = Data(x=x, edge_index=edge_index)
for item in range(0, data.x.shape[0]):
print(item, data.x[item], data.edge_index.t()[item])
By accessing the data
object's properties,
how would I know which node feature belongs to what node?
If I understand it correctly data.x
holds the node features.
By running for loop below I can access the features
but how would I know that if it belongs to node 0 or node 9 for an example?
from torch_geometric.data import Data
edge_index = torch.tensor([[0, 1, 1, 2, 1, 9],
[1, 0, 2, 1, 8, 1]
], dtype=torch.long)
x = torch.tensor([[-5,7], [0,5], [0,9], [10,9]], dtype=torch.float)
data = Data(x=x, edge_index=edge_index)
for item in range(0, data.x.shape[0]):
print(item, data.x[item], data.edge_index.t()[item])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的代码中,通过定义
x
,Pytorch Geometric 推断(根据x
的形状)存在四个节点。这是在文档中指定的:您还指定了直到节点 9 的节点的边。如果您尝试在此代码上运行任何模型,我怀疑它会产生错误,因为预计只会出现四个节点。这是因为它将尝试访问 x 的第 9 个元素,这将返回索引错误。
最佳实践是定义存在的节点数量。如文档中所述:
In your code, by defining
x
as you have, Pytorch Geometric infers (from the shape ofx
) that four nodes exist. This is specified in the documentation:You have also specified edges for nodes up to node 9. If you try to run any models on this code, I suspect it would produce an error as there are only four nodes expected to be present. This is because it will try to access the 9th element of
x
which will return an index error.The best practice is to define the number of nodes present. As stated in the documentation: