用途unsqueeze()
:
input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
print(input.unsqueeze(0).size()) # prints - torch.size([1, 2, 4, 3])
用途view()
:
input = torch.Tensor(2, 4, 3) # input: 2 x 4 x 3
print(input.view(1, -1, -1, -1).size()) # prints - torch.size([1, 2, 4, 3])
根据文档,unsqueeze()
在作为参数给定的位置插入单例 dim,并view()
创建一个具有与tensor
.
什么view()
对我来说很清楚,但我无法将它与unsqueeze()
. 此外,我不明白何时使用view()
以及何时使用unsqueeze()
?
任何有良好解释的帮助将不胜感激!