Here is a version for Tensorflow based on the code of @jrb which can be used for a custom loss:
import tensorflow as tf
import tensorflow_probability as tfp
def correlation(x,y):
mx = tf.math.reduce_mean(x)
my = tf.math.reduce_mean(y)
xm, ym = x - mx, y - my
r_num = tf.math.reduce_mean(xm * ym)
r_den = tfp.stats.stddev(xm) * tfp.stats.stddev(ym)
return r_num / (r_den + tf.constant(0.00001, dtype=x.dtype))