【问题标题】:How do masked values affect the metrics in Keras?掩码值如何影响 Keras 中的指标?
【发布时间】:2018-08-07 14:01:20
【问题描述】:

如果我查看keras metric,我会发现y_truey_predict 的值在每个时期结束时与categorical_accuracy 进行比较:

def categorical_accuracy(y_true, y_pred):
    return K.cast(K.equal(K.argmax(y_true, axis=-1),
                          K.argmax(y_pred, axis=-1)),
                  K.floatx())

如何处理掩码值?如果我理解正确,屏蔽会禁止屏蔽值影响训练,但它仍然会产生对屏蔽值的预测。因此,在我看来,它确实会影响指标。

关于它如何影响指标的更多解释:

在填充/屏蔽过程中,我将y_true 中的填充/屏蔽值设置为未使用的类,例如类0。 如果现在argmax() 正在单热编码的y_true 中寻找最大值,则它只会返回 0,因为总(屏蔽)行是相同的。 我没有 0 类,因为它是我的掩码值/类,因此 y_predy_true 肯定具有不同的值,从而降低了准确性。

Keras 指标中是否已经考虑到这一点并且我监督了它? 否则,我将不得不创建一个自定义指标或回调,创建与categorical_accuracy 类似的指标,并在比较之前消除y_predy_true 中的所有屏蔽值。

【问题讨论】:

    标签: python tensorflow keras metrics


    【解决方案1】:

    也许最好的答案是来自Keras.metrics

    度量函数类似于损失函数,只是评估度量的结果在训练模型时不使用

    训练只受实现掩码的损失函数的影响。 然而,您显示的结果与实际结果不符,可能会导致误导性结论。

    由于在训练过程中不使用度量​​,回调函数可以解决这个问题。

    类似的东西(基于 Andrew Ng)。我在这里搜索 0,因为对于我的掩码目标,所有 one-hot-encoded 目标都是 0(未激活类)。

    import numpy as np
    from keras.callbacks import Callback
    from sklearn.metrics import accuracy_score
    
    class categorical_accuracy_no_mask(Callback):
    
       def on_train_begin(self, logs={}):
           self.val_acc = []
    
       def on_epoch_end(self, epoch, logs={}):
           val_predict = (np.asarray(self.model.predict(self.model.validation_data[0]))).round()
           val_targ = self.model.validation_data[1] 
           indx = np.where(~val_targ.any(axis=2))[0] #find where all targets are zero. That are the masked once as we masked the target with 0 and the data with 666
           y_true_nomask = numpy.delete(val_targe, indx, axis=0)
           y_pred_nomask = numpy.delete(val_predict, indx, axis=0)
    
           _val_accuracy = accuracy_score(y_true_nomask, y_pred_nomask)
           self.val_acc.append(_val_accuracy)
    
           print “ — val_accuracy : %f ” %( _val_accuracy )
           return
    

    当然,现在你还可以添加精确召回等。

    【讨论】:

      猜你喜欢
      • 2020-07-23
      • 2015-09-29
      • 2020-06-16
      • 2023-02-10
      • 2017-12-03
      • 2017-05-07
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      相关资源
      最近更新 更多