# Negative strides in tensor error

Hi, when I use numpy.flip() on my array and then i try to create from this array torch tensor with `torch.from_numpy(x)`
It says

``````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().)
``````

Why is this happening ? I see there are some solutions on internet but this never happened to me yet and I was using numpy.flip() a lot.
When I use min() max() on this array there is no negative number so why is this happening ?

example code:
Just normal numpy array, flip it and it crashes.

``````import numpy
import torch
x = numpy.zeros((12,3,48,48))

def _augmentation_flip(image_np, label_np):
aug_img = numpy.flip(image_np, 1)
aug_label = numpy.flip(label_np, 1)

return aug_img, aug_label

x,y = _augmentation_flip(x,x)

torch.from_numpy(x)
``````

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

Now I understand thank you a lot !