如何使用 torch.stack 功能 [英] How to use torch.stack function
本文介绍了如何使用 torch.stack 功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个关于 torch.stack 的问题
I have a question about torch.stack
我有 2 个张量,a.shape=(2, 3, 4) 和 b.shape=(2, 3).如何在没有就地操作的情况下堆叠它们?
I have 2 tensors, a.shape=(2, 3, 4) and b.shape=(2, 3). How to stack them without in-place operation?
推荐答案
堆叠需要相同数量的维度.一种方法是解压和堆叠.例如:
Stacking requires same number of dimensions. One way would be to unsqueeze and stack. For example:
a.size() # 2, 3, 4
b.size() # 2, 3
b = torch.unsqueeze(b, dim=2) # 2, 3, 1
# torch.unsqueeze(b, dim=-1) does the same thing
torch.stack([a, b], dim=2) # 2, 3, 5
这篇关于如何使用 torch.stack 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文