torch.view()
实现reshape的功能(以行优先的方式)
torch.view(参数a,-1),则表示根据a自动补齐另一个维度
expand 和 expand_as
Pytorch中tensor.expand()和tensor.expand_as()函数
Yale曼陀罗的博客-CSDN博客_tensor.expand
# 只能expand (n, 1) 或 (1, n)的tensor
a = torch.tensor([[1,2,3]]) # a.shape:[1,3]
b = a.expand(2, -1) # 不扩充的维度可设为-1,即保持原维度
print (b.shape) # a的shape保持不变,而b的shape变为:[2,3]
# expand_as
c = a.expand_as(b) # 即a.expand(b.shape)
print (c.shape) # expand_as之后,a和b的shape均保持不变