Negative strides in tensor error

Not the values in the array/tensor are negative but the strides, which are used to access elements in the tensor via indexing.
Negative strides are not supported in PyTorch, so you would have to create a copy first:

x = torch.randn(10, 10)
x[:, ::-1]
# > ValueError: step must be greater than zero
x.numpy()[:, ::-1] # works

a = np.random.randn(10, 10)
a.strides
# > (80, 8)
a = np.flip(a)
a.strides
# > (-80, -8)

x = torch.from_numpy(a)
# > ValueError: At least one stride in the given numpy array is negative, and tensors with negative strides are not currently supported. (You can probably work around this by making a copy of your array  with array.copy().) 

x = torch.from_numpy(a.copy()) # works
x.stride()
# > (10, 1)
1 Like