将自定义知识图数据加载到pytorch几何

问题描述 投票:0回答:1

我是 PyTorch 几何的新手,想知道如何将我们自己的知识图数据集加载到 PyTorch 几何 DataLoader 中。我的数据位于 CSV 文件中,例如:

数据集由 1000 个这样的三元组组成
我浏览了 PyTorch 文档,但无法理解如何将此类数据与 Pytorch 几何一起使用。
我之前使用 Ampligraph 来使用这些数据进行链接预测,并考虑使用 GNN(PyTorch 几何)进行尝试。
任何帮助!

python pytorch knowledge-graph pytorch-geometric
1个回答
0
投票

我不明白你的数据格式,但如果知识图是你正在寻找的,它可以实现如下:

from torch_geometric.data import Data
import torch

example_node_labels = ["cat", "dog", "horse"]
example_edge_labels = ["example0", "example1", "example2", "example3", "example4"]

example_node_label_references = torch.tensor([
    0, 2, 1
])
example_edge_label_references = torch.tensor([
    1, 3, 4
])

example_edges = torch.tensor([
    [0, 1, 2], # start nodes
    [1, 2, 0]  # end nodes
])

knowledge_graph = Data(
    x = example_node_label_references,
    edge_index = example_edges,
    edge_attr = example_edge_label_references
)

这里需要注意的是,pytorch 不能处理字符串,只能处理数字数据。因此,您需要将字符串编码为数字。在此示例中,数字是指向字符串数据列表中正确字符串的索引。

另外,请阅读 this 了解有关 PyTorch Geometric 中表示图形的数据对象的更多信息!

© www.soinside.com 2019 - 2024. All rights reserved.