tensorflow - 自定义指标访问 X 输入数据
问题描述
我想为拼写校正模型编写一个自定义指标,该模型计算正确替换的以前不正确的字母。它应该被错误地计算为以前正确的替换字母。
这就是为什么我需要访问 x_input 数据。不幸的是,默认情况下只能访问 y_true 和 y_pred。是否有解决方法来获得匹配的 x_input?
是:
def custom_metric(y_true, y_pred):
通缉:
def custom_metric(x_input, y_true, y_pred):
解决方案
def custom_loss(x_input):
def loss_fn(y_true, y_pred):
# Use your x_input here directly
return #Your loss value
return loss_fn
model = # Define your model
model.compile(loss=custom_loss(x_input))
# Values of y_true and y_pred will be passed implicitly by Keras
请记住,x_input
在训练模型时,所有批次的输入都将具有相同的值。
编辑:
由于您只x_input
需要每批的数据在损失函数期间进行估计,并且您拥有自己的自定义损失函数,为什么不传递as 标签。像这样的东西:x_input
model.fit(x=x_input, y=x_input)
model.compile(loss=custom_loss())
def custom_loss(y_true, y_pred):
# y_true corresponds to x_input data
如果您需要 x_input 并且需要传递一些其他数据,您可以这样做:
model.fit(x=x_input, y=[x_input, other_data])
model.compile(loss=custom_loss())
你现在只需要解耦数据y_true
。
推荐阅读
- javascript - 我怎样才能使这个按钮文本允许您输入图片但在上传图片时消失并在您不选择照片时返回?
- c# - BindingExtension 如何知道其有效的绑定上下文?
- reactjs - Firebase UI 回调中的 signInSuccessWithAuthResult 返回值
- javascript - 除了警报弹出框外,如何在 javascript 的客户端显示 onsubmit 验证消息?
- visual-c++ - 在 NumericUpDown 控件上单击并按住按钮:如何停止 ctrl 加速
- java - 在 Android 中更改 PreferenceFragmentCompat 对话框背景颜色
- git - 从 GIT 中提取仅更改了文件和文件夹结构
- c++ - 带有 SFINAE / 隐藏的模板转换运算符
- botframework - 主动消息,收到错误 401“此请求的授权已被拒绝”
- parsing - 是否可以将更改的 JSON 键与更大记录类型中的 aeson 与 sum 类型数据构造函数相匹配?