gpt4 book ai didi

python - 我如何使用 torch.stack?

转载 作者:太空狗 更新时间:2023-10-29 22:05:19 26 4
gpt4 key购买 nike

如何使用 torch.stack 堆叠两个形状为 a.shape = (2, 3, 4)b.shape = ( 2, 3) 没有就地操作?

最佳答案

堆叠需要相同数量的维度。一种方法是取消挤压和堆叠。例如:

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

关于python - 我如何使用 torch.stack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52288635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com