Created
November 5, 2019 13:24
-
-
Save benman1/597ad29662805e9963f52e853623b90f to your computer and use it in GitHub Desktop.
for regression - calculate the correlation coefficient between predictions and true values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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