【问题标题】:Pytorch: Node2Vec: TypeError: tuple indices must be integers or slices, not tuplePytorch:Node2Vec:TypeError:元组索引必须是整数或切片,而不是元组
【发布时间】:2021-02-24 01:05:56
【问题描述】:

我正在尝试从 torch_geometric.nn 库中运行 Node2Vec。作为参考,我在关注this 示例。

在运行 train() 函数时,我不断收到TypeError: tuple indices must be integers or slices, not tuple

我正在使用torch version 1.6.0CUDA 10.1 以及最新版本的torch-scattertorch-sparsetorch-clustertorch-spline-convtorch-geometric

这里是详细的错误:

Part 1 of the Error

Part 2 of the Error

感谢您的帮助。

【问题讨论】:

    标签: python pytorch pytorch-dataloader


    【解决方案1】:

    错误是由于torch.ops.torch_cluster.random_walk 返回一个元组而不是数组/张量。我通过用这些替换torch_geometric.nn.Node2Vec 中的函数pos_sampleneg_sample 来修复它。

    def pos_sample(self, batch):
        batch = batch.repeat(self.walks_per_node)
        rowptr, col, _ = self.adj.csr()
        rw = random_walk(rowptr, col, batch, self.walk_length, self.p, self.q)
        if not isinstance(rw, torch.Tensor):
            rw = rw[0]
    
        walks = []
        num_walks_per_rw = 1 + self.walk_length + 1 - self.context_size
        for j in range(num_walks_per_rw):
            walks.append(rw[:, j:j + self.context_size])
        return torch.cat(walks, dim=0)
    
    
    def neg_sample(self, batch):
        batch = batch.repeat(self.walks_per_node * self.num_negative_samples)
    
        rw = torch.randint(self.adj.sparse_size(0),
                           (batch.size(0), self.walk_length))
        rw = torch.cat([batch.view(-1, 1), rw], dim=-1)
    
        walks = []
        num_walks_per_rw = 1 + self.walk_length + 1 - self.context_size
        for j in range(num_walks_per_rw):
            walks.append(rw[:, j:j + self.context_size])
        return torch.cat(walks, dim=0)
    

    参考 PyTorch Node2Vec documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2019-11-23
      • 2017-10-02
      相关资源
      最近更新 更多