PyTorch reshape tensor dimension
I want to reshape a vector of shape (5,) into a matrix of shape (1, 5).
With numpy, I can do:
&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; a = np.array([1, 2, 3, 4, 5])
&gt;&gt;&gt; a.shape
(5,)
&gt;&gt;&gt; a = np.reshape(a, (1, 5))
&gt;&gt;&gt; a.shape
(1, 5)
&gt;&gt;&gt; a
array([[1, 2, 3, 4, 5]])

But how do I do this with PyTorch?

Use  torch.unsqueeze(input, dim, out=None):
&gt;&gt;&gt; import torch
&gt;&gt;&gt; a = torch.Tensor([1, 2, 3, 4, 5])
&gt;&gt;&gt; a

 1
 2
 3
 4
 5
[torch.FloatTensor of size 5]

&gt;&gt;&gt; a = a.unsqueeze(0)
&gt;&gt;&gt; a

 1  2  3  4  5
[torch.FloatTensor of size 1x5]

