Python 索引器:torch_几何体的尺寸超出范围(预计在[-1,0]范围内,但得到-2)

Python 索引器:torch_几何体的尺寸超出范围(预计在[-1,0]范围内,但得到-2),python,pytorch,Python,Pytorch,我的GCN代码中的索引器有一些问题。 GCNConov层上发生错误。 我无法理解为什么self.node_dim包含在错误代码中显示的size函数中 我怎样才能解决这个问题? data.x是节点_特征,其中每个节点的特征数量仅为一个,节点数量为2058 提前谢谢 ''' GCN类编码器(火炬模块): def_uuu初始_uuuu(自、输入通道、输出通道): 超级(GCNEncoder,self)。\uuuu init\uuuuu() self.conv1=GCNConv(输入通道、输出通道、缓存

我的GCN代码中的索引器有一些问题。 GCNConov层上发生错误。 我无法理解为什么self.node_dim包含在错误代码中显示的size函数中

我怎样才能解决这个问题? data.x是节点_特征,其中每个节点的特征数量仅为一个,节点数量为2058

提前谢谢

'''

GCN类编码器(火炬模块):
def_uuu初始_uuuu(自、输入通道、输出通道):
超级(GCNEncoder,self)。\uuuu init\uuuuu()
self.conv1=GCNConv(输入通道、输出通道、缓存=真)
self.conv2=GCNConv(输出通道、输出通道、缓存=真)
def前进(自身、x、边缘索引):
x=self.conv1(x,边索引).relu()
返回self.conv2(x,边索引)
OurGAE类(火炬模块):
定义初始(自我、数字特征、输出通道、节点):
超级(乌尔盖,自我)。\uuuuu init\uuuuuuuu()
self.encoder=GCNEncoder(num_特性,out_通道)
self.linear=nn.linear(输出信道,len(节点))
def编码(自身、节点特征、边缘):
z=自编码器(节点特征、边缘)
返回z
def预测(自我,z):
预测的节点特征=自线性(z)
返回预测的\u节点\u特征
############列车模型#######
dataset=OurDataset(一个节点功能,超级边缘)
输出信道=16
num\u features=dataset.num\u features
model=OurGAE(num\u特性、out\u通道、节点)
device=torch.device('cuda'如果torch.cuda.is_可用(),则为'cpu')
模型=模型到(设备)
optimizer=torch.optim.Adam(model.parameters(),lr=1e-3)
纪元=100
标准=nn.MSELoss()
#模型列车()
对于范围内的历元(1,历元+1):
损失_ep=0
对于数据集中的数据:
optimizer.zero_grad()
x=数据.x.to(设备)
edge_lst=数据。edge_索引。到(设备)
z=模型。编码(x,边)
super_z=z[-1]
预测x=模型。预测(超级z)
损失=标准(x.重塑(-1),预测值x)
loss.backward()
optimizer.step()
loss_ep+=loss.cpu().detach().data.numpy()
如果历元%1==0:
打印(f“epoch:{epoch},loss:{loss_ep}”)“”
---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
在里面
71 edge_lst=数据。edge_索引。到(设备)
72
--->73 z=模型编码(x,边缘)
74
75 super_z=z[-1]
输入编码(自身、节点特征、边缘)
36
37 def编码(自身、节点特征、边缘):
--->38 z=自编码器(节点特征,边缘)
39返回z
40
~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in\u call\u impl(self,*input,**kwargs)
725结果=self.\u slow\u forward(*输入,**kwargs)
726其他:
-->727结果=自转发(*输入,**kwargs)
728用于itertools.chain中的挂钩(
729 _全局_向前_hooks.values(),
前进中(自身、x、边缘索引)
15
16 def前进(自身、x、边缘索引):
--->17 x=self.conv1(x,边索引).relu()
18返回self.conv2(x,边索引)
19
~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in\u call\u impl(self,*input,**kwargs)
725结果=self.\u slow\u forward(*输入,**kwargs)
726其他:
-->727结果=自转发(*输入,**kwargs)
728用于itertools.chain中的挂钩(
729 _全局_向前_hooks.values(),
~/anaconda3/lib/python3.8/site-packages/torch\u geometric/nn/conv/gcn\u conv.py前进(自身、x、边缘索引、边缘权重)
159如果缓存为无:
160边缘索引,边缘权重=gcn标准(#yapf:禁用
-->161边缘索引、边缘权重、x尺寸(自身节点尺寸),
162自我改进,自我添加(自我循环)
163如果self.cached:
索引器:维度超出范围(预期在[-1,0]范围内,但得到-2)
class GCNEncoder(torch.nn.Module):

    def __init__(self, in_channels, out_channels):
        super(GCNEncoder, self).__init__()
        self.conv1 = GCNConv(in_channels, out_channels, cached=True)
        self.conv2 = GCNConv(out_channels, out_channels, cached=True)

    def forward(self, x, edge_index):
        x = self.conv1(x, edge_index).relu()
        return self.conv2(x, edge_index)

class OurGAE(torch.nn.Module):

    def __init__(self, num_features, out_chennels, node):
        super(OurGAE, self).__init__()
        self.encoder = GCNEncoder(num_features, out_channels)
        
        self.linear = nn.Linear(out_channels, len(node))
    
    def encode(self, node_feature, edge_lst):
        z = self.encoder(node_feature, edge_lst)
        return z
    
    def predict(self, z):        
        predicted_node_features = self.linear(z)
        return predicted_node_features
    
    
############ train model #######

dataset = OurDataset(one_node_feature, super_edge_lst)

out_channels = 16
num_features = dataset.num_features

model = OurGAE(num_features, out_channels, node)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)

optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
epochs = 100

criterion = nn.MSELoss()

#model.train()

for epoch in range(1, epochs + 1):

    loss_ep = 0
    for data in dataset:    

        optimizer.zero_grad()
        
        x = data.x.to(device)
        edge_lst = data.edge_index.to(device)
        
        z = model.encode(x, edge_lst)
        
        super_z = z[-1]
        
        predicted_x = model.predict(super_z)
        
        loss = criterion(x.reshape(-1), predicted_x)
        
        loss.backward()
        optimizer.step()
        
        loss_ep += loss.cpu().detach().data.numpy()

    if epoch % 1 == 0:
        print(f"epoch: {epoch}, loss : {loss_ep}") '''

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-103-9dd243b81c8e> in <module>
     71         edge_lst = data.edge_index.to(device)
     72 
---> 73         z = model.encode(x, edge_lst)
     74 
     75         super_z = z[-1]

<ipython-input-103-9dd243b81c8e> in encode(self, node_feature, edge_lst)
     36 
     37     def encode(self, node_feature, edge_lst):
---> 38         z = self.encoder(node_feature, edge_lst)
     39         return z
     40 

~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-103-9dd243b81c8e> in forward(self, x, edge_index)
     15 
     16     def forward(self, x, edge_index):
---> 17         x = self.conv1(x, edge_index).relu()
     18         return self.conv2(x, edge_index)
     19 

~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

~/anaconda3/lib/python3.8/site-packages/torch_geometric/nn/conv/gcn_conv.py in forward(self, x, edge_index, edge_weight)
    159                 if cache is None:
    160                     edge_index, edge_weight = gcn_norm(  # yapf: disable
--> 161                         edge_index, edge_weight, x.size(self.node_dim),
    162                         self.improved, self.add_self_loops)
    163                     if self.cached:

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2)