【问题标题】:Neural Network with tensorflow recall over 100具有 tensorflow 召回率超过 100 的神经网络
【发布时间】:2020-08-20 02:54:55
【问题描述】:

我正在尝试获取已创建模型的所有指标:

def build_rnn_gru_model(tokenizer):
    model = tf.keras.Sequential([
        tf.keras.layers.Embedding(len(tokenizer.word_index) + 1, 64,input_length=863),
        tf.keras.layers.GRU(64, activation='relu', return_sequences=True),
        tf.keras.layers.Dense(1, activation='sigmoid')
    ])
    model.summary()
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy',f1,precision, recall])
    return model

我还使用了How to get accuracy, F1, precision and recall, for a keras model? 中高度赞成的答案中建议的指标定义,但结果是一样的:

def recall(y_true, y_pred):
    true_positives = K.sum(K.round(y_pred) * y_true)
    possible_positives = K.sum(y_true)
    return true_positives / (possible_positives + K.epsilon())


def precision(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    return precision


def f1(y_true, y_pred):
    precision_ = precision(y_true, y_pred)
    recall_ = recall(y_true, y_pred)
    return 2*((precision_*recall_)/(precision_+recall_+K.epsilon()))

在评估带有 LSTM 或没有循环层的模型时,一切看起来都还不错,但使用 GRU 时,recal 值非常高:

 199/1180 [====>.........................] - ETA: 4:45 - loss: 0.3988 - accuracy: 0.8230 - f1: 1.6155 - precision: 0.8195 - recall: 468.6583

谁能给我一个提示有什么问题?

【问题讨论】:

    标签: python tensorflow machine-learning keras precision-recall


    【解决方案1】:

    对于 TF 2,我建议您使用预定义的 metrics,在您的情况下为 tf.keras.metrics.Recall

    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=[tf.keras.metrics.Recall(), ...])
    

    我建议在您的 GRU 层中设置 return_sequences=False,因为我认为您正在执行二进制分类任务

    【讨论】:

    • 我使用 tensorflow==2.2.0rc3 并使用 tf.keras.metrics.Recall() 让我得到很多错误,例如 assert_is_compatible_with raise ValueError("Shapes %s and %s are incompatible" % (self, other)) ValueError: Shapes (None, 863) and (None, 1) are incompatible
    • @Mithrand1r 确实,这很可能是问题所在;通常,当使用预定义函数并收到此类错误/警告时,您应该怀疑自己的实际预测,而不是转储函数并尝试“手动”完成工作。
    猜你喜欢
    • 2017-02-26
    • 2019-08-13
    • 2018-05-08
    • 2015-07-20
    • 2018-11-05
    • 2018-04-21
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    相关资源
    最近更新 更多