Here’s another way that should work more generally:
In [180]: x = torch.randn(3, 3)

In [181]: x
Out[181]: 

 1.6177  0.5082  1.3817
-0.5801 -0.4657  0.8086
-0.4783 -0.3208  1.4459
[torch.FloatTensor of size 3x3]

In [182]: x[torch.triu(torch.ones(3, 3)) == 1]
Out[182]: 

 1.6177
 0.5082
 1.3817
-0.4657
 0.8086
 1.4459
[torch.FloatTensor of size 6]

And you can do the reverse similarly,
In [183]: vec = x[torch.triu(torch.ones(3, 3)) == 1]

In [184]: y = torch.zeros(3, 3)

In [185]: y[torch.triu(torch.ones(3, 3)) == 1] = vec

In [186]: y
Out[186]: 

 1.6177  0.5082  1.3817
 0.0000 -0.4657  0.8086
 0.0000  0.0000  1.4459
[torch.FloatTensor of size 3x3]