解决IndexError: Target 2 is out of bounds.
错误代码
# in_feats = 34
# hidden_size = 5
# num_classes = 2
class GCN(nn.Module):
def __init__(self, in_feats, hidden_size, num_classes):
super(GCN, self).__init__()
self.gcn1 = GCNLayer(in_feats, hidden_size)
self.gcn2 = GCNLayer(hidden_size, num_classes)
def forward(self, g, inputs):
h = self.gcn1(g, inputs)
h = torch.relu(h)
h = self.gcn2(g, h)
return h
# 以空手道俱乐部为例
# 第一层将34层的输入转化为隐层为5
# 第二层将隐层转化为最终的分类数2
net = GCN(450,5,2)
分类的类别数不匹配的问题,因此需要将分类的类数降为自己需要的维度,我需要64,改完后就正确了将代码改了之后为:
# in_feats = 34
# hidden_size = 5
# num_classes = 2
class GCN(nn.Module):
def __init__(self, in_feats, hidden_size, num_classes):
super(GCN, self).__init__()
self.gcn1 = GCNLayer(in_feats, hidden_size)
self.gcn2 = GCNLayer(hidden_size, num_classes)
def forward(self, g, inputs):
h = self.gcn1(g, inputs)
h = torch.relu(h)
h = self.gcn2(g, h)
return h
# 以空手道俱乐部为例
# 第一层将34层的输入转化为隐层为5
# 第二层将隐层转化为最终的分类数2
net = GCN(450,5,61)
改完之后出现结果了,虽然结果正确率还需要改正,好在问题解决了,希望能帮到你。