What is the correct way to assert equality with tensor objects?

Hey y’all,

I am writing test for my code and need to know the best way to assert the equality of tensor objects using pytensor.

A simple comparison like assert pt.as_tensor_variable(0) == pt.as_tensor_variable(0) doesn’t work.

I can do a comparison like assert str(pt.as_tensor_variable(0)) == str(pt.as_tensor_variable(0)) which works, but isn’t totally ideal.

But do y’all have any utility functions (like for example numpy.testing’s assert_array_equals

1 Like

Welcome!

For debugging purposes, you are probably looking for the evaluated tensors, not the tensors themselves.
So this:

pt.as_tensor_variable(0).eval() == pt.as_tensor_variable(0).eval()

returns True. So does:

pt.eq(pt.as_tensor_variable(0), pt.as_tensor_variable(0)).eval()

This is also an option to deal with floating point values:

pt.isclose(pt.as_tensor_variable(0), pt.as_tensor_variable(0+10e-20)).eval()

pt.allclose() may get you something similar to assert_array_equals(). @ricardoV94 may have other suggestions.

Can you provide more info on what are you trying to test exactly?