zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python工具方法 7 keras默认loss库中添加rmse

Python方法工具 添加 默认 Keras Loss 库中
2023-09-14 09:15:04 时间

在keras的loss库中添加rmse方法有三大作用:

1、可以避免在各个py文件中重新定义rmse损失方法

2、在模型导入时,避免了需要传入特定的函数

3、使用默认的rmse方法在计算3维、4维更高维度时不会返回nan

主要原因:

作者在使用自定义的rmse方法时发现无法对三维数据计算loss,返回值为nan;后来使用keras的mse方法,复制出来改成rmse的方法也无法使用。最后在keras的loss库中添加rmse方法,成功使模型能正常使用rmse方法。

操作步骤:

1、打开keras库下的losse.py文件,并创建root_mean_squared_error方法,最终效果如图1所示

def root_mean_squared_error(y_true, y_pred):
    if not K.is_tensor(y_pred):
        y_pred = K.constant(y_pred)
    y_true = K.cast(y_true, y_pred.dtype)
    return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))