2017-02-20 311 views
3

我正試圖在Tensorflow中進行迴歸。我不積極我正在計算R^2正確,因爲Tensorflow給了我一個比sklearn.metrics.r2_score不同的答案有人可以看看我的下面的代碼,並讓我知道如果我正確實施了圖片方程。由於如何在Tensorflow中計算R^2

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

回答

4

你計算什麼 「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)) 
+0

在第三行tf.div,你有unexplained_error和total_error在錯誤的位置,他們需要進行切換。 –

0

它實際上應該是相反的rhs。不明原因的變化由總方差

+1

您可以添加代碼來幫助完成此答案嗎? – brennan

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)) 
相關問題