图神经网络回归

3

我正在尝试在图神经网络上实现回归。我看到的大部分示例都是关于分类的,在这个领域没有看到过回归的例子。我看到一个分类的例子如下:

from torch_geometric.nn import GCNConv

class GCN(torch.nn.Module):
def __init__(self, hidden_channels):
    super(GCN, self).__init__()
    torch.manual_seed(12345)
    self.conv1 = GCNConv(dataset.num_features, hidden_channels)
    self.conv2 = GCNConv(hidden_channels, dataset.num_classes)

def forward(self, x, edge_index):
    x = self.conv1(x, edge_index)
    x = x.relu()
    x = F.dropout(x, p=0.5, training=self.training)
    x = self.conv2(x, edge_index)
    return x

model = GCN(hidden_channels=16)
print(model)

我正在尝试修改它以适应我的任务,基本上包括对具有30个节点的网络执行回归,每个节点具有3个特征,边缘具有一个特征。

如果有人能够为我提供相同的示例,那将非常有帮助。

1个回答

2

添加一个线性层,不要忘记使用回归损失函数。

class GCN(torch.nn.Module):
    def __init__(self, hidden_channels):
        super(GCN, self).__init__()
        torch.manual_seed(12345)
        self.conv1 = GCNConv(dataset.num_features, hidden_channels)
        self.conv2 = GCNConv(hidden_channels, dataset.num_classes)
        self.linear1 = torch.nn.Linear(100,1)
    def forward(self, x, edge_index):
        x = self.conv1(x, edge_index)
        x = x.relu()
        x = F.dropout(x, p=0.5, training=self.training)
        x = self.conv2(x, edge_index)
        x = self.linear1(x)
        return x

你曾经为节点回归任务训练过GNN吗?我相信在那个任务中添加一个线性层并没有太大的帮助。 - Cuong Truong Huy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接