python - 使用外部参数实现自定义 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 倍的训练时间。
有人有实现这种技术的聪明方法吗?无论是在度量标准还是其他方法中?
解决方案
答案是使用包装函数:
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
推荐阅读
- solr - 单个查询中的 apache solr 最大值和最小值
- javascript - JavaScript - 以特定方式反转数组
- javascript - 使用nodejs gridjs,multer上传带有图片url的图片
- nginx - 我应该如何使用 Nginx 进行以下操作?
- logstash - 从日志路径解析的字段未添加到 logstash
- python - (pymysql.err.OperationalError) (2013, '查询期间丢失与 MySQL 服务器的连接)
- r-markdown - 在同一个 Bookdown 项目中有一个 beamer_presentation 和一个 pdf_book
- node.js - 猫鼬。是否可以在一个操作中删除(如果存在)并插入(如果不存在)?
- laravel - 如何从连接中的一组记录中获取列的总和?
- vba - 日期条件的 FindFirst 语法错误