pythontensorflowregression

How to Calculate R^2 in Tensorflow


I am trying to do regression in Tensorflow. I'm not positive I am calculating R^2 correctly as Tensorflow gives me a different answer than sklearn.metrics.r2_score Can someone please look at my below code and let me know if I implemented the pictured equation correctly. Thanks

The formula I am attempting to create in TF

total_error = tf.square(tf.sub(y, tf.reduce_mean(y)))
unexplained_error = tf.square(tf.sub(y, prediction))
R_squared = tf.reduce_mean(tf.sub(tf.div(unexplained_error, total_error), 1.0))
R = tf.mul(tf.sign(R_squared),tf.sqrt(tf.abs(R_squared)))

Solution

  • What you are computing the "R^2" is

    R^2_{\text{wrong}} = \operatorname{mean}_i \left( \frac{(y_i-\hat y_i)^2}{(y_i-\mu)^2} - 1\right)1

    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.

    unexplained_error = tf.reduce_sum(tf.square(tf.sub(y, prediction)))
    total_error = tf.reduce_sum(tf.square(tf.sub(y, tf.reduce_mean(y))))
    R_squared = tf.sub(1, tf.div(unexplained_error, total_error))