import torch
import matplotlib.pyplot as plt
a = torch.randn(2, 3) # 标准正态分布生成随机数
print("a:", a)
print("a.shape:", a.shape) # torch.Size([2, 3])
# unsqueeze:扩充数据维度,在0起的指定位置N加上维数为一的维度
b = torch.unsqueeze(a, 1) # [2, 3]中在位置1,就是=3的位置增加维度1,3向后串
print("b.shape:", b.shape) # torch.Size([2, 1, 3])
print("b:", b)
c = torch.unsqueeze(a, 0) # [2, 3]中在位置0,就是=1的位置增加维度1,2向后串
print("c.shape:", c.shape) # torch.Size([1, 2, 3])
# --------------------------------------------------------------#
f = torch.randn(3)
print("f:", f)
print("f.shape:", f.shape) # torch.Size([3])
g = f.unsqueeze(0) # [3]中在位置0,就是=3的位置增加维度1,3向后串
print("g.shape:", g.shape) # torch.Size([1, 3])
print("g:", g)
# --------------------------------------------------------------#
# squeeze:维度压缩,在0起的指定位置,去掉维数为1的的维度
print("c:", c)
print("c.shape:", c.shape)
d = torch.squeeze(c) # d=c.squeeze(0)
print("d:", d)
print(d.shape) # torch.Size([2, 3])
unsqueeze,squeeze
最新推荐文章于 2022-12-24 11:23:50 发布