我正在尝试在Tensorflow中进行回归。我不是很肯定我正在正确地计算R ^ 2,因为Tensorflow给我的答案与答案不同。sklearn.metrics.r2_score
有人可以看看下面的代码,让我知道我是否正确实现了图中的方程式。谢谢
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”是
与给定的表达式相比,您在错误的位置计算平均值。在进行除法运算时,您应该在计算误差时取平均值。
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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句