Pytorch reshape tensor dimension

前端 未结 10 665
忘掉有多难
忘掉有多难 2021-02-03 17:56

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 num         


        
10条回答
  •  不要未来只要你来
    2021-02-03 18:20

    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'
    

提交回复
热议问题