一次迭代两个Pytorch张量?

inf*_*nge 5 python pytorch tensor

我有两个Pytorch张量(实际上只是一维列表)t1t2。是否可以并行地遍历它们,即执行类似的操作

for a,b in zip(t1,t2)

谢谢。

Que*_*ker 7

对我来说(Python 3.7.3 版和 PyTorch 1.0.0 版)zip 函数与 PyTorch 张量一起按预期工作:

>>> import torch
>>> t1 = torch.ones(3)
>>> t2 = torch.zeros(3)
>>> list(zip(t1, t2))
[(tensor(1.), tensor(0.)), (tensor(1.), tensor(0.)), (tensor(1.), tensor(0.))]
Run Code Online (Sandbox Code Playgroud)

list是只需要调用显示结果。迭代zip正常工作。


Jaj*_*aja 6

您可以尝试:\n torch.stack(seq, dim=0, out=None) \xe2\x86\x92 Tensor,\n
有关详细信息,请参阅pytoch 文档

\n


gmd*_*mds 1

将它们与;连接起来会更有意义。torch.cat(dim=1)然后,您可以迭代新的张量。