PyTorch:作为张量操作的动态编程 [英] PyTorch: Dynamic Programming as Tensor Operation
本文介绍了PyTorch:作为张量操作的动态编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以通过Tensor操作获得以下循环?
Is it possible to get the following loop with a Tensor operation?
a = torch.Tensor([1, 0, 0, 0])
b = torch.Tensor([1, 2, 3, 4])
for i in range(1, a.shape[0]):
a[i] = b[i] + a[i-1]
print(a) # [1, 3, 6, 10]
该操作取决于 a
中的先前值和在途中(以动态编程方式)计算出的值.
The operation depends on the previous values in a
and the values that are computed on the way (in a dynamic programming fashion).
是否可以通过张量运算获得这种顺序计算?
Is it possible to get this type of sequential computation with a tensor operation?
推荐答案
您可以使用累计和:
b.cumsum(0)
这篇关于PyTorch:作为张量操作的动态编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文