For comparing tensors you can do elemental:
torch.eq :
torch.eq(torch.tensor([[1., 2.], [3., 4.]]), torch.tensor([[1., 1.], [4., 4.]])) tensor([[True, False], [False, True]])
Or torch.equal for the whole tensor:
torch.equal(torch.tensor([[1., 2.], [3, 4.]]), torch.tensor([[1., 1.], [4., 4.]])) # False torch.equal(torch.tensor([[1., 2.], [3., 4.]]), torch.tensor([[1., 2.], [3., 4.]])) # True
But then you can get lost, because at some point there are small differences that you would like to ignore. For example, floats 1.0 and 1.0000000001 pretty close, and you can assume that they are equal. For such a comparison, you have torch.allclose .
torch.allclose(torch.tensor([[1., 2.], [3., 4.]]), torch.tensor([[1., 2.000000001], [3., 4.]])) # True
At some point, it may be important to check how many elements are equal compared to the total number of elements. If you have two tensors dt1 and dt2 , you will get the number of dt1 elements as dt1.nelement()
And with this formula you will get a percentage:
print(torch.sum(torch.eq(dt1, dt2)).item()/dt1.nelement())