1

我正在按照本指南从我自己的数据中创建用于图形分类的数据集:https ://docs.dgl.ai/en/0.6.x/new-tutorial/6_load_data.html

在那里,它们不会创建任何节点的特征,因为如果您要预测图形类,则没有必要。就我而言,它是相同的,我不想使用任何节点功能(还)进行分类。

为了训练 GNN,我正在学习本教程:https ://docs.dgl.ai/tutorials/blitz/5_graph_classification.html#sphx-glr-tutorials-blitz-5-graph-classification-py

两者都来自官方文档,但它们似乎不兼容,因为当我尝试将它们一起使用时,我收到了这个错误:

KeyError                                  Traceback (most recent call last) <ipython-input-39-8a94f1fa250d> in <module>
      4 for epoch in range(20):
      5     for batched_graph, labels in train_dataloader:
----> 6         pred = model(batched_graph, batched_graph.ndata['attr'].float())
      7         loss = F.cross_entropy(pred, labels)
      8         optimizer.zero_grad()

~/anaconda3/lib/python3.8/site-packages/dgl/view.py in
__getitem__(self, key)
     64             return ret
     65         else:
---> 66             return self._graph._get_n_repr(self._ntid, self._nodes)[key]
     67 
     68     def __setitem__(self, key, val):

~/anaconda3/lib/python3.8/site-packages/dgl/frame.py in
__getitem__(self, name)
    391             Column data.
    392         """
--> 393         return self._columns[name].data
    394 
    395     def __setitem__(self, name, data):

KeyError: 'attr'

而且我没有找到另一个不使用节点功能的情况下使用 DGl 训练 GNN 的示例。可能吗?我必须创建虚假属性吗?

谢谢!

4

1 回答 1

0

DGL 模型总是需要至少有一个特征。所以我通过使用分类器中的度数特征来解决它:

class Classifier(nn.Module):
    def __init__(self, in_dim, hidden_dim, n_classes):
        super(Classifier, self).__init__()
        self.conv1 = GraphConv(in_dim, hidden_dim)
        self.conv2 = GraphConv(hidden_dim, hidden_dim)
        self.classify = nn.Linear(hidden_dim, n_classes)

    def forward(self, g):
        # Use node degree as the initial node feature. For undirected graphs, the in-degree
        # is the same as the out_degree.
        h = g.in_degrees().view(-1, 1).float()
        # Perform graph convolution and activation function.
        h = F.relu(self.conv1(g, h))
        h = F.relu(self.conv2(g, h))
        g.ndata['h'] = h
        # Calculate graph representation by averaging all the node representations.
        hg = dgl.mean_nodes(g, 'h')
        return self.classify(hg)
于 2022-02-08T16:46:03.837 回答