Skip to content

Instantly share code, notes, and snippets.

@benman1
Created November 5, 2019 13:24
Show Gist options
  • Save benman1/597ad29662805e9963f52e853623b90f to your computer and use it in GitHub Desktop.
Save benman1/597ad29662805e9963f52e853623b90f to your computer and use it in GitHub Desktop.
for regression - calculate the correlation coefficient between predictions and true values
def pearson(x, y):
'''Metric for correlation coefficient.
Use as follows:
model.compile(loss='mse', optimizer='adam', metrics=[pearson])
'''
mx = K.mean(x)
my = K.mean(y)
xm, ym = x-mx, y-my
r_num = K.sum(tf.multiply(xm,ym))
r_den = K.sqrt(tf.multiply(K.sum(K.square(xm)), K.sum(K.square(ym))))
r = tf.clip_by_value(r_num / (r_den + K.epsilon()), -1., 1.)
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment