如何在 Tensorflow 中计算 R^2 [英] How to Calculate R^2 in Tensorflow
本文介绍了如何在 Tensorflow 中计算 R^2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 Tensorflow 中进行回归.我不确定我是否正确计算了 R^2,因为 Tensorflow 给了我与 sklearn.metrics.r2_score
不同的答案有人可以看看我下面的代码,让我知道我是否实现了图中的方程正确.谢谢
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
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)))
推荐答案
你在计算什么R^2"是
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.
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))
这篇关于如何在 Tensorflow 中计算 R^2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文