torch.unsqueeze()用法

torch.unsqueeze()这个函数主要是对数据维度进行扩充。第二个参数为0数据为行方向扩,为1列方向扩,再大错误。

例子:
import torch
a = torch.tensor([1,2,3,4])
b = torch.unsqueeze(a,0)
print('b.size(): ',b.size())
print('b: ',b)
c = torch.unsqueeze(a,1)
print('c.size(): ',c.size())
print('c: ',c)

结果:
b.size(): torch.Size([1, 4])
b: tensor([[1, 2, 3, 4]])
c.size(): torch.Size([4, 1])
c: tensor([[1],
[2],
[3],
[4]])

猜你喜欢

转载自blog.csdn.net/weixin_43255962/article/details/84348054