Torch.from_numpy: The returned tensor is not resizable

Greetings from India!
I was referring the book: Deep Learning with PyTorch when I came across this function:
from_numpy
It says in the docstring that “the returned tensor is not resizable” but when I did something like this:
img = torch.from_numpy(img_arr)
img.resize_(3,720,1280)
It returned me a resized tensor. Can you please help?
I have just started with PyTorch and I am absolutely loving it though.

Hi,

The short answer I would give here is: "If you just started pytorch, you should not touch .resize_() :smiley: "
It is a fairly tricky function that actually resize the data storage for a Tensor and can contain un-initialized memory.

If you want to change the shape of a Tensor (without changing the number of elements) you should use .view() or .reshape() !

Thanks for the suggestion! My original ask was as per docstring the operation:
img.resize_(3,720,1280) should have resulted into error but it didnt. Why?
Am I missing something?

“resizable” here does not refer to the .resize_() function. But means you can’t change the size of the underlying data container.
What I guess happens here is that the size you give to .resize_() does not actually need to resize the underlying data container and so it is not an issue

1 Like

Thanks for the explanation!