Python tensorflow召回率超过100%的神经网络

Python tensorflow召回率超过100%的神经网络,python,tensorflow,machine-learning,keras,precision-recall,Python,Tensorflow,Machine Learning,Keras,Precision Recall,我正在尝试获取所创建模型的所有指标: 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),

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

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
我还使用了中投票率较高的答案中建议的度量定义,但结果是相同的:

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

谁能给我一个提示吗?

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

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

我建议在GRU层中设置
return\u sequences=False
,因为我认为您正在使用TF 2执行二进制分类任务

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

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

我建议在您的GRU层中设置
return\u sequences=False
,因为我认为您正在执行一项二进制分类任务

我使用
tensorflow==2.2.0rc3
并使用
tf.keras.metrics.Recall()
couse让我获得类似于
assert\u与raise ValueError\u兼容的错误(“形状%s和%s不兼容”%(自身,其他))值错误:形状(无,863)和(无,1)不兼容
@Mithrand1r事实上,这可能是问题所在;一般来说,当使用预定义函数并收到此类错误/警告时,您应该怀疑您的实际预测,而不是转储函数并尝试“手动”完成此工作“.I使用
tensorflow==2.2.0rc3
并使用
tf.keras.metrics.Recall()
可以让我得到tonz的错误,比如
assert\u与raise ValueError兼容(“形状%s和%s不兼容”%(self,other))ValueError:Shapes(None,863)和(None,1)不兼容
@mitrand1r确实,这就是问题所在;一般来说,当使用预定义函数并获得此类错误/警告时,您应该怀疑您的实际预测,而不是转储函数并尝试“手动”完成工作。