Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: tensors used as indices must be long or byte tensors #2

Open
Supermaxman opened this issue Oct 18, 2018 · 1 comment · May be fixed by #3
Open

RuntimeError: tensors used as indices must be long or byte tensors #2

Supermaxman opened this issue Oct 18, 2018 · 1 comment · May be fixed by #3

Comments

@Supermaxman
Copy link

I am trying to run the examples.simple.main.py and I encounter the following exception:

RuntimeError: tensors used as indices must be long or byte tensors

I have the following packages installed with pip3 in python3.6:

neat-python==0.92
numpy==1.15.2+mkl
gym==0.10.5
click==6.7
torch==0.4.0
torchvision==0.2.1

This error occurs within pytorch_neat.recurrent_net in the dense_from_coo function:

def dense_from_coo(shape, conns, dtype=torch.float64):
    mat = torch.zeros(shape, dtype=dtype)
    idxs, weights = conns
    if len(idxs) == 0:
        return mat
    rows, cols = np.array(idxs).transpose()
    mat[torch.tensor(rows), torch.tensor(cols)] = torch.tensor(
        weights, dtype=dtype)
    return mat

The problem is that np.array is assuming int32 for the indexes, but torch wants int64.
Simple solution:

def dense_from_coo(shape, conns, dtype=torch.float64):
    mat = torch.zeros(shape, dtype=dtype)
    idxs, weights = conns
    if len(idxs) == 0:
        return mat
    rows, cols = np.array(idxs, dtype=np.int64).transpose()
    mat[torch.tensor(rows), torch.tensor(cols)] = torch.tensor(
        weights, dtype=dtype)
    return mat

The difference may be from the differing numpy versions, but I think this change makes sense regardless for torch tensor indexing.

@Supermaxman Supermaxman linked a pull request Oct 18, 2018 that will close this issue
@ai-nick
Copy link

ai-nick commented Apr 24, 2019

Hey supermax, I had the same issue and I solved it by instead changing mat[torch.tensor(rows), torch.tensor(cols)] to mat[torch.LongTensor(rows), torch.LongTensor(cols)] idk if this would alleviate the differences in numpy versions or not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants