keras中的自定义指标功能出错

时间:2019-04-08 22:57:28

标签: python keras

我正在Keras中定义一个自定义函数来计算R-Squared指标。我在keras后端使用了以下代码:

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

稍后将其调用到模型中时:

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

我收到以下错误:

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

1 个答案:

答案 0 :(得分:3)

问题在这里:num = num^2^是按位XOR运算符。您需要**