我有以下毕火炬张量:
V1 = torch.tensor([[2, 4], [6, 4], [5, 3]])
我想做每一对数字的差值之和(应用绝对值),如下代码所示;
result.sum(abs(2-4), abs(6-4), abs(5-3))
我可以使用for语句来实现这一点:
total = 0
for i in range(0,vector.size(0)):
total = total + torch.abs(vector.data[i][1] - vector.data[i][0])
但我不想用for来做。
有办法吗?
发布于 2019-05-04 17:57:03
你能做到的
torch.abs(V1[:, 1]- V1[:, 0])
然后总结一下
torch.sum(torch.abs(V1[:, 1]- V1[:, 0]))
发布于 2019-05-05 00:37:07
您可以使用更通用的方法,如下面的代码段:
In [46]: torch.sum(torch.abs(V1[:, :-1] - V1[:, 1:]))
Out[46]: tensor(6)
https://stackoverflow.com/questions/55985336
复制相似问题