Reputation: 655
In the following code, op1
and op2
should be identical. The only difference is in the order the addends appear in the sum. However, np.array_equal(res1, res2)
returns False
. Why does this happen? (I'm using TensorFlow version 0.12.1).
import numpy as np
import tensorflow as tf
V = tf.constant([[-3.47429895, -5.99409866],
[-4.84888363, 5.3685813],
[8.32318401, 0.62552071]], dtype=tf.float32)
rs = tf.reduce_sum(V, axis=1, keep_dims=True)
m = -tf.matmul(V, V, transpose_b=True)
op1 = m + rs + tf.transpose(rs)
op2 = rs + tf.transpose(rs) + m
with tf.Session() as session:
res1 = session.run(op1)
res2 = session.run(op2)
assert np.array_equal(res1, res2) # False, why?
Upvotes: 1
Views: 77
Reputation: 2897
It's happening because floating point operations are not accurate and changing the order can change the results albeit, the discrepancy is not significant.
The problem here is with np.array_equal
, the error between res1
and res2
is more than it can tolerate. Try printing res1 - res2
to get a better idea. You might want to use numpy.allclose with properly chosen tolerance values.
Upvotes: 2