How do you check the equivalence of a numpy.int64 value stored in a tf.Tensor dataset object in Python 3?

154 views Asked by At

I would like to know whether there is an easy way to check the equivalence of an int32 value stored in a tf.Tensor dataset object of class 'numpy.int64'. The integer value will change over time, and I would like to get a logical indicator of 'True' when the value is 2, and 'False' when it is not. I am working with Python 3 and TensorFlow 2.5.0.

Here are some specifics. When I call:

print(class(token))

on my dataset object (named 'token'), I see:

tf.Tensor([[2]], shape=(1, 1), dtype=int32)
<class 'numpy.int64'>

I would like to check whether the integer value stored in 'token' equals 2, and receive a logical indicator 'True' or 'False' as the value changes relative to the reference value of 2. It seems like this should be easy, but no matter what sort of indexing or function call I try, I cannot seem to access the integer stored in 'token'. How can I do this?

When I call:

print(token)

I see:

0

When I try:

print(tf.math.equal(token,[[2]]))

I see:

tf.Tensor([[False]], shape=(1, 1), dtype=bool)

Thank you!

1

There are 1 answers

4
Anurag Dhadse On BEST ANSWER

Run this

tf.squeeze(tf.math.equal(token,[[2]])).numpy()

This should output True.