Skip to content

Instantly share code, notes, and snippets.

@arunmallya
Created August 14, 2017 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arunmallya/073d44d7e9a3bc925444d1e247c1e8da to your computer and use it in GitHub Desktop.
Save arunmallya/073d44d7e9a3bc925444d1e247c1e8da to your computer and use it in GitHub Desktop.
from __future__ import print_function
import torch
A = torch.rand(4, 4)
An = A.numpy()
idx = torch.ByteTensor([1, 0, 0, 1])
idxn = [True, False, False, True]
# Numpy indexing.
print('numpy')
print(An[idxn].shape)
print(An[:, idxn].shape)
# Torch indexing.
print('torch')
print(A[idx].size())
print(A[:, idx].size())
# Output.
"""
numpy
(2, 4)
(4, 2)
torch
Traceback (most recent call last):
File "bug.py", line 17, in <module>
print(A[idx].size())
RuntimeError: inconsistent tensor size, expected src [4 x 4] and mask [4] to have the same number of elements, but got 16 and 4 elements respectively at /Users/soumith/miniconda2/conda-bld/pytorch_1501999754274/work/torch/lib/TH/generic/THTensorMath.c:197
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment