问题描述
我的下张量具有以下形状
seq = dataset['features'][...]
print(f'shape of seq before unsequeeze {seq.shape}')
shape of seq before unsequeeze (461,1024)
我正在尝试在(461,512)中转换形状,如何在pytorch张量操作中实现这一点。
示例功能x如下
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
x = torch.tensor([[0.1,0.8,0.4,0.9,0.5,0.2,0.3,0.7,0.6,0.1,0.0,1.0,0.8],[0.5,0.2]]).to(
device,dtype=torch.int64
)
x.shape
torch.Size([2,1024])
我需要将特征尺寸减小到512,以保持第一个昏暗批次的尺寸不变,
x.shape
torch.Size([2,512])
谢谢
解决方法
z = torch.narrow(x,1,512)
z.shape
torch.Size([2,512])
如果您使用x.unsqueeze(0),则暗2将需要更改,
z = torch.narrow(x,2,512)