Equivalent of np.reshape() in pyTorch?
Kalamaya
<SCODE>>>> import torch
>>> t = torch.ones((2, 3, 4))
>>> t.size()
torch.Size([2, 3, 4])
>>> t.view(-1, 12).size()
torch.Size([2, 12])<ECODE>
rasbt
Kalamaya
A caveat is that it has to be contiguous, but that matches numpy as far as I know.
apaszke
tachim
Since people have asked about this several times – yes. You just have to define the module version yourself: <SCODE>class View(nn.Module):
    def __init__(self, *shape):
        super(View, self).__init__()
        self.shape = shape
    def forward(self, input):
        return input.view(*shape)

sequential_model = nn.Sequential([Linear(10, 20), View(-1, 5, 4)])
<ECODE>
jekbradbury
mderakhshani
You can use permute in pytorch to specify its order of reshaping. <SCODE>t = torch.rand((2, 3, 4))
t = t.permute(1, 0, 2)
<ECODE> this can reshape its order
SherlockLiao
cosmozhang1988
colesbury
Thank you!!!
cosmozhang1988
why is this not called reshape?
Brando_Miranda
My guess is that it’s been done to be consistent with torch rather than numpy, which makes sense. However, yeah, some of the naming conventions are a bit annoying for people coming from NumPy, not Torch
rasbt
I think the torchTensor.view() is same as to np.reshape() after my experiment. And torchTesnor.permute is same as to np.transpose. But I have a question about what is the use of np.reshape. Because in image processing domain, reshape will change the structure information of the image and that is fatal.
Eric_K
In image processing field, we should use permute in my opinion then what is the meaning of view()'s existence? Thank you!
Eric_K
<SCODE>linear_input = conv_output.view(batch_size, channels_out*height_out*width_out)
<ECODE>
jpeg729
Elias_Vansteenkiste