kdba
kdba

Reputation: 433

Error with custom metrics function in keras

I am defining a custom function in Keras to calculate the R-Squared metric. I used the following code with the keras backend:

import keras.backend as K
def Rsqured(y_true,y_pred):

    y_true = K.batch_flatten(y_true)
    y_pred = K.batch_flatten(y_pred)

    y_tr_mean = K.mean(y_true)
    y_pr_mean = K.mean(y_pred)

    num = K.sum((y_true-y_tr_mean) * (y_pred-y_pr_mean))
    num = num^2

    denom = K.sum((y_true-y_tr_mean)*(y_true-y_tr_mean)) * K.sum((y_pred- 
    y_pr_mean)*(y_pred-y_pr_mean))

    return num

/denom

Later when I call it into my model:

model.compile(optimizer='adam',loss='mean_squared_error', metrics=[Rsqured])

I get the following error:

Input 'x' of 'LogicalOr' Op has type int64 that does not match expected type of bool.

Upvotes: 0

Views: 65

Answers (1)

gmds
gmds

Reputation: 19885

The issue is here: num = num^2. ^ is the bitwise XOR operator. You want **.

Upvotes: 2

Related Questions