自定义指标访问 X 输入数据

Custom metric access X input data

我想为拼写更正模型编写一个自定义指标,以正确计算以前不正确的替换字母。并且应该计算错误地替换了以前正确的字母。

这就是我需要访问 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 每批的数据 只有 用于损失函数期间的估计并且你有自己的自定义损失函数,你为什么不通过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中的数据即可。