pytorch 判断两个 tensor 是否相等
2020-10-11 本文已影响0人
潘旭
不用循环,用 pytorch 的基本函数, 非常简洁. 代码如下:
import torch
x = torch.tensor([[1, 2], [3, 4]])
y = torch.tensor([[1, 2], [3, 4]])
assert 0 == ((x != y).sum())
xx = torch.tensor([[1, 2], [3, 4]])
yy = torch.tensor([[2, 2], [3, 3]])
assert 0 == (xx != yy).sum()