Fix Python – Pytorch tensor to numpy array

Question

Asked By – DukeLover

I have a pytorch Tensor of shape [4, 3, 966, 1296]. I want to convert it to numpy array using the following code:

imgs = imgs.numpy()[:, ::-1, :, :]

How does that code work?

Now we will see solution for issue: Pytorch tensor to numpy array


Answer

There are 4 dimensions of the tensor you want to convert.

[:, ::-1, :, :] 

: means that the first dimension should be copied as it is and converted, same goes for the third and fourth dimension.

::-1 means that for the second axes it reverses the the axes

This question is answered By – Maaz Bin Musa

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0