Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new dim to a a pytorch tensor?

Tags:

python

pytorch

In NumPy, I would do

a = np.zeros((4, 5, 6))
a = a[:, :, np.newaxis, :]
assert a.shape == (4, 5, 1, 6)

How to do the same in PyTorch?

like image 845
Gulzar Avatar asked Dec 14 '22 07:12

Gulzar


1 Answers

a = torch.zeros(4, 5, 6)
a = a[:, :, None, :]
assert a.shape == (4, 5, 1, 6)
like image 165
Gulzar Avatar answered Dec 30 '22 22:12

Gulzar