使用equal 张量比较
原型:equal(other)
比较两个张量是否相等–相等返回:True; 否则返回:False
import torch
a = torch.tensor([4, 2, 3, 4])
b = torch.tensor([4, 2, 3, 4])
print(a.equal(b)
# 返回结果:True
使用eq 逐元素判断
原型:eq(other)
比较两个张量tensor中,每一个对应位置上元素是否相等–对应位置相等,就返回一个1;否则返回一个0.
import torch
a = torch.tensor([2, 2, 2, 3])
b = torch.tensor([2, 2, 3, 3])
print(a.eq(b))
# 返回结果:tensor([True, True, True, True])
使用eq_ 将判断结果返回并替换原tensor
原型:eq_(other)
等价于tensor = tensor.eq(other);
即:将比较后的结果替换原张量的值
import torch
a = torch.tensor([1, 2, 3])
b = torch.tensor([1, 1, 3])
print(a.eq_(b))
print(a)
# 返回结果:tensor([1, 0, 1])
tensor([1, 0, 1])
# 可以看到a中的值已经被替换了