import tensorflow as tf from tensorflow import keras from tensorflow.keras import backend as K def loss(a,b,K=K): """correlation between a and the first entry of b should be zero. Correlations are hard to optimize. So use corvariance and metric keeping properties""" if len(b.shape)>1: b=b[:,0] if len(a.shape)>1: a=a[:,0] return K.abs(K.mean((a-K.mean(a))*(b-K.mean(b)))) if __name__=='__main__': import numpy as np x=np.random.uniform(-1,1,size=(1000,2)) print(numpyloss2d(x[:,0],x[:,1],n=25))