Pytorch: Node2Vec: TypeError: tuple indices must be integers or slices, not tuple

Pytorch: Node2Vec: TypeError: tuple indices must be integers or slices, not tuple

我正在尝试 运行 来自 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

感谢您的帮助。

错误是由于 torch.ops.torch_cluster.random_walk 返回元组而不是 array/tensor。我通过使用替换 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