squeeze
从数组的形状中删除单维度条目,即把shape中为1的维度去掉
import numpy as np
a = [[[10, 2, 3]]]
a = np.array(a)
a_sque = np.squeeze(a)
print(a)
print(a_sque)
打印结果:
[[[10 2 3]]]
[10 2 3]
unsqueeze
unsqueeze()函数起升维的作用,参数表示在哪个地方加一个维度。
在第一个维度(中括号)的每个元素加中括号
0表示在张量最外层加一个中括号变成第一维。
import torch
input=torch.arange(0,6)
print(input)
print(input.shape)
打印结果:
tensor([0, 1, 2, 3, 4, 5])
torch.Size([6])
print(input.unsqueeze(0))
print(input.unsqueeze(0).shape)
打印结果:
tensor([[0, 1, 2, 3, 4, 5]])
torch.Size([1, 6])
print(input.unsqueeze(1))
print(input.unsqueeze(1).shape)
打印结果:
tensor([[0],
[1],
[2],
[3],
[4],
[5]])
torch.Size([6, 1])