pytorch的unsqueeze

就是在指定维度前再插入一个新的维度。

import torch
import numpy as np
x=np.arange(24).reshape((2,3,4))
x = torch.tensor(x)
print(x)
y=x.permute((2, 0, 1))
print(y.shape)
print(y.unsqueeze(0).shape)
print(y.unsqueeze(0).float())

tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],

[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]], dtype=torch.int32)
torch.Size([4, 2, 3])
torch.Size([1, 4, 2, 3])

posted @ 2022-12-24 08:05  祥瑞哈哈哈  阅读(54)  评论(0)    收藏  举报