Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch reshape tensor dimension

For example, I have 1D vector with dimension (5). I would like to reshape it into 2D matrix (1,5).

Here is how I do it with numpy

>>> import numpy as np >>> a = np.array([1,2,3,4,5]) >>> a.shape (5,) >>> a = np.reshape(a, (1,5)) >>> a.shape (1, 5) >>> a array([[1, 2, 3, 4, 5]]) >>>  

But how can I do that with Pytorch Tensor (and Variable). I don't want to switch back to numpy and switch to Torch variable again, because it will loss backpropagation information.

Here is what I have in Pytorch

>>> import torch >>> from torch.autograd import Variable >>> a = torch.Tensor([1,2,3,4,5]) >>> a   1  2  3  4  5 [torch.FloatTensor of size 5]  >>> a.size() (5L,) >>> a_var = variable(a) >>> a_var = Variable(a) >>> a_var.size() (5L,) .....do some calculation in forward function >>> a_var.size() (5L,) 

Now I want it size to be (1, 5). How can I resize or reshape the dimension of pytorch tensor in Variable without loss grad information. (because I will feed into another model before backward)

like image 232
Haha TTpro Avatar asked Apr 10 '17 16:04

Haha TTpro


People also ask

How can you reduce the size of tensor PyTorch?

To squeeze a tensor, we use the torch. squeeze() method. It returns a new tensor with all the dimensions of the input tensor but removes size 1. For example, if the shape of the input tensor is (M ☓ 1 ☓ N ☓ 1 ☓ P), then the squeezed tensor will have the shape (M ☓ M ☓ P).

How do you flatten tensor PyTorch?

flatten. Flattens input by reshaping it into a one-dimensional tensor. If start_dim or end_dim are passed, only dimensions starting with start_dim and ending with end_dim are flattened.


2 Answers

you might use

a.view(1,5) Out:    1  2  3  4  5 [torch.FloatTensor of size 1x5] 
like image 33
Lelik Avatar answered Oct 05 '22 14:10

Lelik


Use torch.unsqueeze(input, dim, out=None)

>>> import torch >>> a = torch.Tensor([1,2,3,4,5]) >>> a   1  2  3  4  5 [torch.FloatTensor of size 5]  >>> a = a.unsqueeze(0) >>> a   1  2  3  4  5 [torch.FloatTensor of size 1x5] 
like image 148
Haha TTpro Avatar answered Oct 05 '22 15:10

Haha TTpro