mberr/torch-ppr

RuntimeError torch.sparse.addmm different torch tensor shape

meatball1982 opened this issue · 4 comments

Dear torch-ppr

I installed torch-ppr on my Mac with python 3.9 and run the example code

>>> import torch
>>> edge_index = torch.as_tensor(data=[(0, 1), (1, 2), (1, 3), (2, 4)]).t()
>>> from torch_ppr import page_rank
>>> page_rank(edge_index)

I got a runtimeerror as

x = torch.sparse.addmm(input=x0, sparse=adj, dense=x, beta=alpha, alpha=beta)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (2x4 and 2x1)

I printed the shape of x0, adj and x

torch.Size([2, 1])
torch.Size([2, 4])
torch.Size([2, 1])

I believe that the shape of adj should be 2x2 or I might be wrong.
I find the define process of adj.

# convert to sparse matrix, shape: (n, n)
adj = edge_index_to_sparse_matrix(edge_index=edge_index, num_nodes=num_nodes)
adj = adj + adj.t()

The adj is symmect.

I wonder how to fix the runtimeError or any suggestions?
Thanks in advanced
meatball1982
12-May-2022 09:54:50

mberr commented

Hi @meatball1982 ,

thanks for finding the issue. In the example, edge_index should be passed as a keyword parameter, i.e.,

>>> from torch_ppr import page_rank
>>> page_rank(edge_index=edge_index)
tensor([0.1269, 0.3694, 0.2486, 0.1269, 0.1281])

I updated the README just now in 53079ec and the documentation in 3daee4c

mberr commented

I added some additional input validation, so the error messages are more meaningful.

mberr commented

You can install the improved version by

pip install -U torch_ppr

Hi.@mberr
Problem solved. Thanks a lot.
:)