QBoard » Artificial Intelligence & ML » AI and ML - PyTorch » Pytorch reshape tensor dimension

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)

      September 17, 2020 2:44 PM IST
    0
  • 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]
     
      September 17, 2020 5:35 PM IST
    0
  • you might use

    a.view(1,5)
    Out: 
    
     1  2  3  4  5
    [torch.FloatTensor of size 1x5]
      September 17, 2020 5:36 PM IST
    0
  • you can use this, the '-1' means you don't have to specify the number of the elements.

    In [3]: a.view(1,-1)
    Out[3]:
    
     1  2  3  4  5
    [torch.FloatTensor of size 1x5]
      September 17, 2020 5:37 PM IST
    0
  • import torch
    >>>a = torch.Tensor([1,2,3,4,5])
    >>>a.size()
    torch.Size([5])
    #use view to reshape
    
    >>>b = a.view(1,a.shape[0])
    >>>b
    tensor([[1., 2., 3., 4., 5.]])
    >>>b.size()
    torch.Size([1, 5])
    >>>b.type()
    'torch.FloatTensor'
      September 17, 2020 5:38 PM IST
    0
  • For in-place modification of the shape of the tensor, you should use tensor.resize_():
    In [23]: a = torch.Tensor([1, 2, 3, 4, 5])
    
    In [24]: a.shape
    Out[24]: torch.Size([5])
    
    
    # tensor.resize_((`new_shape`))    
    In [25]: a.resize_((1,5))
    Out[25]: 
    
     1  2  3  4  5
    [torch.FloatTensor of size 1x5]
    
    In [26]: a.shape
    Out[26]: torch.Size([1, 5])

     

    In PyTorch, if there's an underscore at the end of an operation (like tensor.resize_()) then that operation does in-place modification to the original tensor.

    Also, you can simply use np.newaxis in a torch Tensor to increase the dimension. Here is an example:

    In [34]: list_ = range(5)
    In [35]: a = torch.Tensor(list_)
    In [36]: a.shape
    Out[36]: torch.Size([5])
    
    In [37]: new_a = a[np.newaxis, :]
    In [38]: new_a.shape
    Out[38]: torch.Size([1, 5])
      September 17, 2020 5:41 PM IST
    0
  • import torch
    t = torch.ones((2, 3, 4))
    t.size()
    
    >>torch.Size([2, 3, 4])
    
    a = t.view(-1,t.size()[1]*t.size()[2])
    a.size()
    
    >>torch.Size([2, 12])
      December 22, 2020 2:59 PM IST
    0