首页 > 解决方案 > 使用外部参数实现自定义 keras 指标

问题描述

我想实现一个自定义指标来评估我的模型,使用我的训练集中未包含的值来评估预测。

我的指标的想法:

def custom_metric(y_true, y_pred, other_values):
   """
   y_true is training data, binary value
   y_pred is predicted values 
   other_values is an inputted tensor, includes values in (0, 2) """

   Use K backend to return the proper value.
   ...

可能还有其他方法可以进行相同的计算,从而有效地解决相同的问题。其中之一是在每个时期检查模型并在训练完成后运行此分析。但是,我不希望这样做,因为我在 TPU 上进行训练并将权重复制到 CPU 需要在每个 epoch 进行编译,并且需要 100 倍的训练时间。

有人有实现这种技术的聪明方法吗?无论是在度量标准还是其他方法中?

标签: pythontensorflowkeras

解决方案


答案是使用包装函数:

IE

def custom_metric(my_variable):
   # 1. conversion of my_variable to tensor
   def metric_name(y_pred, y_true):
      # 2. return tensorflow backend metric, utilizing custom tensor
   return metric_name

推荐阅读