How to Calculate R^2 in Tensorflow
What you are computing the "R^2" is
compared to the given expression, you are computing the mean at the wrong place. You should take the mean when computing the errors, before doing the division.
total_error = tf.reduce_sum(tf.square(tf.sub(y, tf.reduce_mean(y))))
unexplained_error = tf.reduce_sum(tf.square(tf.sub(y, prediction)))
R_squared = tf.sub(1, tf.div(unexplained_error, total_error))
I would strongly recommend against using a recipe to calculate this! The examples I've found do not produce consistent results, especially with just one target variable. This gave me enormous headaches!
The correct thing to do is to use tensorflow_addons.metrics.RQsquare()
. Tensorflow Add Ons is on PyPi here and the documentation is a part of Tensorflow here. All you have to do is set y_shape
to the shape of your output, often it is (1,)
for a single output variable.