title: R-squared negative for a linear regression model - Tensorflow
body: <p>I built a neural network without an activation function, which is, therefore, a Linear regression Model:</p>
  
  <pre><code>def build_model():
    model = tf.keras.models.Sequential([
    tf.keras.layers.Input(shape=(1,)),
    tf.keras.layers.Dense(1)
    ])
  
    model.compile(optimizer='adam', loss='mse',metrics=['mae','mse','mape',coeff_determination]) 
  
    return model
  </code></pre>
  
  <p>My coeff_dermination is determined as follows:</p>
  
  <pre><code>from keras import backend as K
  
  def coeff_determination(y_true, y_pred):
      SS_res =  K.sum(K.square( y_true-y_pred )) 
      SS_tot = K.sum(K.square( y_true - K.mean(y_true) ) ) 
      return ( 1 - SS_res/(SS_tot + K.epsilon()) )
  </code></pre>
  
  <p>Mathematically, R-squared cannot be below 0 for a linear regression model because:
  Explained variance + Error variance = Total variance.</p>
  
  <p>But when I train the model above, R-squared can be at times negative.</p>
  
  <p>Why? Thanks for your thoughts/lights </p>
  
  <p><a href="https://i.stack.imgur.com/8X11J.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8X11J.png" alt="Training model"></a></p>