4

There are few key parameters associated with Linear Regression e.g. Adjusted R Square, Coefficients, P-value, R square, Multiple R etc. While using google Tensorflow API to implement Linear Regression how are these parameter mapped? Is there any way we can get the value of these parameters after/during model execution

4 Answers4

3

From my experience, if you want to have these values while your model runs then you have to hand code them using tensorflow functions. If you want them after the model has run you can use scipy or other implementations. Below are some examples of how you might go about coding R^2, MAPE, RMSE...

enter image description here

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(tf.div(total_error, unexplained_error),1.0)
R = tf.mul(tf.sign(R_squared),tf.sqrt(tf.abs(unexplained_error)))

MAPE = tf.reduce_mean(tf.abs(tf.div(tf.sub(y, prediction), y)))

RMSE = tf.sqrt(tf.reduce_mean(tf.square(tf.sub(y, prediction))))
Matt Camp
  • 1,448
  • 3
  • 17
  • 38
0

I believe the formula for R2 should be the following. Note that it would go negative when the network is so bad that it does a worse job than the mere average as a predictor:

total_error = tf.reduce_sum(tf.square(tf.subtract(y, tf.reduce_mean(y))))

unexplained_error = tf.reduce_sum(tf.square(tf.subtract(y, pred)))

R_squared = tf.subtract(1.0, tf.divide(unexplained_error, total_error)) 
Pang
  • 9,564
  • 146
  • 81
  • 122
Pierre
  • 1
0

Adjusted_R_squared = 1 - [ (1-R_squared)*(n-1)/(n-k-1) ]

whereas n is the number of observations and k is the number of features.

Inna
  • 663
  • 8
  • 12
0

You should not use a formula for R Squared. This exists in Tensorflow Addons. You will only need to extend it to Adjusted R Squared.

I would strongly recommend against using a recipe to calculate r-squared itself! The examples I've found do not produce consistent results, especially with just one target variable. This gave me enormous headaches!

The correct thing to do is to use tensorflow_addons.metrics.RQsquare(). Tensorflow Add Ons is on PyPi here and the documentation is a part of Tensorflow here. All you have to do is set y_shape to the shape of your output, often it is (1,) for a single output variable.

Then you can use what RSquare() returns in your own metric that handled the adjustments.

rjurney
  • 4,824
  • 5
  • 41
  • 62