How to calculate R ^ 2 in a tensor flow

I am trying to do a regression in Tensorflow. I am not sure if I am calculating R ^ 2 correctly, since Tensorflow gives me a different answer than sklearn.metrics.r2_scoreCan someone please take a look at my code below and let me know if I executed the equation correctly. Thanks

The formula I'm trying 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)))
+4
source share
3 answers

That you calculate "R ^ 2",

Compared to this expression, you are calculating the average in the wrong place. You must accept the average value when calculating errors before performing the separation.

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))
+5
source

. ,

0

, :

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))
0

Source: https://habr.com/ru/post/1670323/


All Articles