Torch Tensor 再次展平、填充和展开

问题描述 投票:0回答:1

我有一个尺寸为 (2, 3) 的 torch.tensor:

1 2 3
4 5 6

我想创建尺寸为 (2, 5) 的张量,如下所示:

1 2 3 4 5
6 0 0 0 0

我怎样才能有效地做到这一点? 也用于向后变换。

pytorch padding tensor torch
1个回答
0
投票

这个怎么样?

import torch

a = torch.tensor([[1, 2, 3],
                  [4, 5, 6]])

b = torch.zeros(10, dtype=a.dtype)
b[0:a.numel()] = a.flatten()
b = b.reshape((2, 5))

© www.soinside.com 2019 - 2024. All rights reserved.