【问题标题】:Custom metric access X input data自定义指标访问 X 输入数据
【发布时间】:2019-09-14 11:45:30
【问题描述】:

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

这就是我需要访问 x_input 数据的原因。不幸的是,默认情况下只能访问 y_true 和 y_pred。是否有解决方法来获得匹配的 x_input?

是:

def custom_metric(y_true, y_pred):

通缉:

def custom_metric(x_input, y_true, y_pred):

【问题讨论】:

    标签: tensorflow keras metrics


    【解决方案1】:
    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 中的数据即可。

    【讨论】:

    • 这不是我想要的。假设我用 10,000 个样本和 128 个批量进行训练。然后,据我了解 y_true 和 y_pred 的形状大小为例如(128, 28, 28)。 x_input 的形状大小为 (10,000, 28, 28) 与您的变体并且不对应于我需要的部分。
    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多