将不同长度的 2D 张量列表转换为一个 3D 张量
我有一个包含 3 个张量的列表,其形状为: (8, 2), (8, 4), (8, 6)
我想将此列表转换为以下形状: ( 8, 3, x)
我该怎么做?我知道我需要使用 torch.cat
、torch.stack
和 torch.transpose
的某种组合,但我无法弄清楚。
提前致谢!
I have a list of 3 tensors with the shape: (8, 2), (8, 4), (8, 6)
And I want to turn this list into this shape: (8, 3, x)
How do I do this? I know I need to use some combination of torch.cat
, torch.stack
and torch.transpose
, but I can't figure it out.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所说,您需要使用
torch.cat
,还需要torch.reshape
。假设如下:并假设确实可以将张量重塑为
(8,3,-1)
形状,其中-1
代表只要需要的话:我会解释一下。由于
a,b,c
中的第一维不同,因此串联必须沿第一维进行,如变量d
中所示。然后,您可以重塑张量,如e
中所示,其中-1
代表“只要需要”。As you said, you need to use
torch.cat
, but alsotorch.reshape
. Assume the following:And assume that it is indeed possible to reshape the tensors to a
(8,3,-1)
shape, where-1
stands for as long as it need to be, then:I'll explain. Because the 1st dimension if different in
a,b,c
the concatenation has to be along the 1st dimension, as seen in variabled
. Then, you can reshape the tensor as seen ine
where the-1
stands for "as long as it needs to be".