Support for slicing with a step != 1?

Currently when attempting to slice a tensor or Variable object with a step different from 1 I get a Runtime Error:

RuntimeError: Trying to slice with a step of -1, but only a step of 1 is supported

Is support for this opperation coming anytime soon and what is the best way to reverse a tensor along an axis currently? Best I can come up with is doing it via index_select which is clunky and copies the tensor… Doing it via numpy is also not possible as from_numpy() doesn’t seem to work with negative strides.

Support for positive strides is added in this PR. Adding negative strides will need some additional work in our C backends, as they’re not ready for that yet.

I try to do image = image[:, :, ::-1], but it complains that ValueError: slice step has to be greater than 0. I try image = torch.from_numpy(image.numpy()[:, :, ::-1]), it still complains: RuntimeError: some of the strides of a given numpy array are negative. This is currently not supported, but will be added in future releases.

So how do I flip the image?


I decide to use image = torch.from_numpy(image.numpy()[:, :, ::-1].copy())


Strange it sometimes work, and sometimes doesn’t:

torch.reshape(weights, weights.shape[::-1])

            [ 1.2324],
            [ 0.1382],
            [-1.6822]]), torch.reshape(weights, weights[::-1]))

_ ValueError                                Traceback (most recent call last)_
_ <ipython-input-42-67cae86c60db> in <module>_, torch.reshape(weights, weights[::-1]))_
_ ValueError: negative step not yet supported_, weights.view(weights.shape[::-1]))

            [ 1.2324],
            [ 0.1382],

Hi guys,

I am also facing the same issue. Could anyone please confirm if the issue is resolved?

Which issue? negative strides? I’m afraid we do not support it.

detected_faces = face_detector.detect_from_image(data[..., ::-1].copy())

ValueError: negative step not yet supported

This is what I am getting.


I’m afraid we still do not.
But if you want a clone of the Tensor, you can use torch.flip(). Note that this will return new memory, but will have the exact same behavior as data[..., ::-1].copy().

1 Like