如何拆分 Cora 数据集以仅在训练部分训练 GCN 模型?

plp*_*lpm 8 python validation neural-network python-3.x pytorch

我正在 Cora 数据集上训练 GCN(图形卷积网络)。

Cora 数据集具有以下属性:

Number of graphs: 1
Number of features: 1433
Number of classes: 7
Number of nodes: 2708
Number of edges: 10556
Number of training nodes: 140
Training node label rate: 0.05
Is undirected: True

Data(edge_index=[2, 10556], test_mask=[2708], train_mask=[2708], val_mask=[2708], x=[2708, 1433], y=[2708])
Run Code Online (Sandbox Code Playgroud)

由于我的代码很长,我只把代码的相关部分放在这里。首先,我将 Cora 数据集拆分如下:

def to_mask(index, size):
    mask = torch.zeros(size, dtype=torch.bool)
    mask[index] = 1
    return mask

def cora_splits(data, num_classes):
    indices = []

    for i in range(num_classes):
        # returns all indices of the elements = i from data.y tensor
        index = (data.y == i).nonzero().view(-1)

        # returns a random permutation of integers from 0 to index.size(0).
        index = index[torch.randperm(index.size(0))]

        # indices is a list of tensors and it has a length of 7
        indices.append(index)

    # select 20 nodes from each class for training
    train_index = torch.cat([i[:20] for i in indices], dim=0)

    rest_index = torch.cat([i[20:] for i in indices], dim=0)
    rest_index = rest_index[torch.randperm(len(rest_index))]

    data.train_mask = to_mask(train_index, size=data.num_nodes)
    data.val_mask = to_mask(rest_index[:500], size=data.num_nodes)
    data.test_mask = to_mask(rest_index[500:], size=data.num_nodes)

    return data
Run Code Online (Sandbox Code Playgroud)

train如下(摘自这里有一些修改):


def train(model, optimizer, data, epoch):
    t = time.time()
    model.train()
    optimizer.zero_grad()
    output = model(data)
    loss_train = F.nll_loss(output[data.train_mask], data.y[data.train_mask])
    acc_train = accuracy(output[data.train_mask], data.y[data.train_mask])
    loss_train.backward()
    optimizer.step()

    loss_val = F.nll_loss(output[data.val_mask], data.y[data.val_mask])
    acc_val = accuracy(output[data.val_mask], data.y[data.val_mask])

def accuracy(output, labels):
    preds = output.max(1)[1].type_as(labels)
    correct = preds.eq(labels).double()
    correct = correct.sum()
    return correct / len(labels)
Run Code Online (Sandbox Code Playgroud)

当我在 10 次运行中以 200 个 epoch 运行我的代码时,我获得了:

tensor([0.7690, 0.8030, 0.8530, 0.8760, 0.8600, 0.8550, 0.8850, 0.8580, 0.8940, 0.8830])

Val Loss: 0.5974, Test Accuracy: 0.854 ± 0.039
Run Code Online (Sandbox Code Playgroud)

其中张量中的每个值都属于每次运行的模型精度,所有这 10 次运行的平均精度为 0.854,std ± 0.039。

可以观察到,从第一次运行到第 10 次的准确率大幅提高。因此,我认为该模型过度拟合。过拟合的一个原因是,在代码中,模型在训练时间已经看到了测试数据,因为在train函数中,有一条线,output = model(data)所以模型是在整个数据上训练的。我打算做的是仅在部分数据(类似于data[data.train_mask])上训练我的模型,但问题是我无法通过data[data.train_mask],由于模型的forward功能GCN(来自此存储库):

def forward(self, data):
        x, edge_index = data.x, data.edge_index
        x = F.relu(self.conv1(x, edge_index))
        for conv in self.convs:
            x = F.relu(conv(x, edge_index))
        x = F.relu(self.lin1(x))
        x = F.dropout(x, p=0.5, training=self.training)
        x = self.lin2(x)
        return F.log_softmax(x, dim=-1)
Run Code Online (Sandbox Code Playgroud)

如果我通过data[data.train_mask]向GCN模型,然后在上述forward功能一致x, edge_index = data.x, data.edge_indexx并且edge_index不能从检索data[data.train_mask]。因此,我需要找到一种方法来拆分 Cora 数据集,以便我可以将其中的特定部分与节点、边缘索引和其他属性一起传递给模型。我的问题是怎么做?

此外,非常感谢有关 k 折交叉验证的任何建议。

Sai*_*ibō 2

我想您对转导学习的本质有点困惑,并且您提出的问题实际上并没有解决您面临的问题。

\n
\n

可以看出,从第一次运行到第十次运行,准确率大幅提高。因此,我认为该模型\n过度拟合。

\n
\n

不一定,当您的模型从训练样本中学习时,提高测试准确性可能是正常行为。由于损失函数的复杂性和非凸性,学习可以持续几十个epoch。判断过度拟合的最佳信号是当您的训练准确性提高但测试准确性显着下降时。

\n
\n

过度拟合的原因之一是,在代码中,模型在训练过程中\n已经看到了测试数据,\n因为在训练函数中\n有一行输出 = model(data),因此模型在整个\n上进行训练数据。

\n
\n

该模型确实在训练中看到了整个图(邻接矩阵),但它只看到了训练集中节点的标签,对测试集中节点的标签一无所知。这正是转导学习的作用。

\n

最后,如果您 100% 确定想要避免转导学习的范例,那么您可能需要编写自己的分割算法来实现这一目标。但我想提醒一下,在现实世界的用例中,转导是非常合适的。一个例子是预测社交网络用户之间的潜在链接,我们将整个网络结构作为输入,并希望简单地运行边缘预测 - >转导。因此,避免它没有多大意义。

\n
\n

根据您的任务,您可以查看 Stellargraph 的EdgeSplitter类(文档)和 scikit-learn\xe2\x80\x99strain_test_split函数(文档)如何实现拆分。

\n

节点分类

\n

如果您的任务是节点分类任务,那么使用图卷积网络 (GCN) 进行节点分类是如何加载数据和进行训练-测试-分割的一个很好的示例。以Cora数据集为例。最重要的步骤如下:

\n
dataset = sg.datasets.Cora()\ndisplay(HTML(dataset.description))\nG, node_subjects = dataset.load()\n\ntrain_subjects, test_subjects = model_selection.train_test_split(\n    node_subjects, train_size=140, test_size=None, stratify=node_subjects\n)\nval_subjects, test_subjects = model_selection.train_test_split(\n    test_subjects, train_size=500, test_size=None, stratify=test_subjects\n)\n\ntrain_gen = generator.flow(train_subjects.index, train_targets)\nval_gen = generator.flow(val_subjects.index, val_targets)\ntest_gen = generator.flow(test_subjects.index, test_targets)\n\n
Run Code Online (Sandbox Code Playgroud)\n

基本上,它与普通分类任务的训练-测试-分割相同,只不过我们这里分割的是节点。

\n

边缘分类

\n

如果您的任务是边缘分类,您可以查看此链接预测示例:Cora 引文数据集上的 GCN。训练测试分割最相关的代码是

\n
# Define an edge splitter on the original graph G:\nedge_splitter_test = EdgeSplitter(G)\n\n# Randomly sample a fraction p=0.1 of all positive links, and same number of negative links, from G, and obtain the\n# reduced graph G_test with the sampled links removed:\nG_test, edge_ids_test, edge_labels_test = edge_splitter_test.train_test_split(\n    p=0.1, method="global", keep_connected=True\n)\n\n# Define an edge splitter on the reduced graph G_test:\nedge_splitter_train = EdgeSplitter(G_test)\n\n# Randomly sample a fraction p=0.1 of all positive links, and same number of negative links, from G_test, and obtain the\n# reduced graph G_train with the sampled links removed:\nG_train, edge_ids_train, edge_labels_train = edge_splitter_train.train_test_split(\n    p=0.1, method="global", keep_connected=True\n)\n\n# For training we create a generator on the G_train graph, and make an \n# iterator over the training links using the generator\xe2\x80\x99s flow() method:\n\ntrain_gen = FullBatchLinkGenerator(G_train, method="gcn")\ntrain_flow = train_gen.flow(edge_ids_train, edge_labels_train)\ntest_gen = FullBatchLinkGenerator(G_test, method="gcn")\ntest_flow = train_gen.flow(edge_ids_test, edge_labels_test)\n\n
Run Code Online (Sandbox Code Playgroud)\n

EdgeSplitter这里class( docs )背后的分割算法更加复杂,它需要在分割时保持图结构,例如保持图的连通性。有关更多详细信息,请参见EdgeSplitter的源代码

\n