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)
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).
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.
you might use
a.view(1,5) Out: 1 2 3 4 5 [torch.FloatTensor of size 1x5]
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With