Search code examples
python-3.xkerasmetricspearson-correlation

pearsons linear coefficient keras


I have tried to implement pearsons linear coefficient as a metric in Keras however because of placeholders I cannot compile my model using this metric.

def CC(y_true, y_pred):

y_true = K.clip(y_true, K.epsilon(), 1)
y_pred = K.clip(y_pred, K.epsilon(), 1)
n_y_true=y_true/(K.sum(y_true)+K.epsilon())
n_y_pred=y_pred/(K.sum(y_pred)+K.epsilon())
y_true_average=K.mean(y_true)
y_pred_average=K.mean(y_pred)
print((K.map_fn(lambda x: x-y_pred_average,n_y_pred)).shape[0])
if not(K.map_fn(lambda x: x-y_pred_average,n_y_pred)).shape[0]==None:
    return (K.sum(K.dot((K.map_fn(lambda x: x-y_pred_average,n_y_pred)),(K.map_fn(lambda x: x-y_true_average,n_y_true))))/(K.count_params(n_y_true)-1))/(K.dot(K.std(n_y_pred),K.std(n_y_true)))
else:
    return 0

I tried using K.dot instead of * however the same error remains. During compile I get the error unsupported operand type(s) for *: 'NoneType' and 'NoneType. And I cannot figure out how to solve it. It happens because I want to elementwise multiply two tensors but the batchsize in the shape is not difined during compile and represented as a ? in the shape of (?,224,224,3). Is there a way to set this or work around it?


Solution

  • final function:

    def CC(y_true, y_pred):
    
    
        #normalise
        n_y_true = (y_true - K.mean(y_true[:])) / K.std(y_true[:])
        n_y_pred = (y_pred - K.mean(y_pred[:])) / K.std(y_pred[:])  
    
        top=K.sum((n_y_true[:]-K.mean(n_y_true[:]))*(n_y_pred[:]-K.mean(n_y_pred[:])),axis=[-1,-2])
        bottom=K.sqrt(K.sum(K.pow((n_y_true[:]-K.mean(n_y_true[:])),2),axis=[-1,-2])*K.sum(K.pow(n_y_pred[:]-K.mean(n_y_pred[:]),2),axis=[-1,-2]))
    
        result=top/bottom
    
    
        return K.mean(result)