张量变形。
所谓张量的变形,就是改变张量的形状。形状包括:张量的维度,不同维度方向上的长度。 一般情况下,变形前后,张量的总元素的个数和内容不变。
常见的变形操作如下:
import torch
print("===torch.reshape===")
a = torch.rand(2, 3)
print(a)
out = torch.reshape(a, (3, 2))
print(out)
print("===torch.t===")
print(torch.t(out))
print("===torch.transpose===")
print(out)
print(torch.transpose(out, 0, 1))
a = torch.rand(1, 2, 3)
print(a)
print(a.shape)
out = torch.transpose(a, 0, 1)
print(out)
print(out.shape)
print("===torch.squeeze===")
print(a)
print(a.shape)
out = torch.squeeze(a)
print(out)
print(out.shape)
print("===torch.unsqueeze===")
print(a)
print(a.shape)
out = torch.unsqueeze(a, -1)
print(out)
print(out.shape)
print("===torch.unbind===")
print(a)
print(a.shape)
out = torch.unbind(a, dim=2)
print(out)
print("===torch.flip===")
print(a)
print(torch.flip(a, dims=[2, 1]))
print("===torch.rot90===")
print(a)
print(a.shape)
out = torch.rot90(a, -1, dims=[0, 2])
print(out)
print(out.shape)