我有几个图,我使用这些图来训练模型,希望能够识别每个节点的类别。我有三个类别,这是我的一个图表的公式(使用数据对象):
Data(x=[100,64],pos=[100,2],edge_index=[2,546],y=[100],edge_weight=[546])
x是每个节点的embedding,这里有100个节点,每个节点有一个64维的特征,pos是每个节点的坐标。 edge_index 中的二维向量表示两点之间的边缘。而edge_weight就是每条边对应的权重。
这是我的 GCN 模型:
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
# GCN model with 2 layers
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = GCNConv(data.num_features, 16)
self.conv2 = GCNConv(16, int(data.num_classes))
def forward(self):
x, edge_index = data.x, data.edge_index
x = F.relu(self.conv1(x, edge_index))
x = F.dropout(x, training=self.training)
x = self.conv2(x, edge_index)
return F.log_softmax(x, dim=1)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
data = data.to(device)
model = Net().to(device)
这是我的训练代码:
for epoch in range(epoches):
for img_path in img_paths: #in order to build the graph
data=build_graph(img_path)
model.train()
optimizer.zero_grad()
output=GCNmodel(data)
F.nll_loss(output,target).backward()
optimizer.step()
但是使用这段代码,一个图中的每个节点都有相同的输出,尤其是负数。
注意:每张图片中的类别1和类别2是平衡的,每张图片中的类别3较小。但数据不应该成为这个结果的主要原因
这让我很困惑,如果你能帮助我,我非常感激。
这可能是过度平滑的情况,也是 GNN 中使用的消息传递范例的自然可能结果。它覆盖得很好这里