首页 > 解决方案 > Keras,混洗和非混洗测试数据的不同精度和召回率

问题描述

我正在编写一个模型,使用 keras 进行基于某些值的二进制分类,在我的测试/训练集中,我将正负示例分开,为了训练,我将它们混在一起。在测试集上,我希望我不需要打乱数据,因为顺序应该没有区别。虽然没有混洗测试数据的模型的精度确实会降低,但召回率和精度仍然很低。另一方面,当我对测试数据进行混洗时,精度保持不变,但召回率和精度具有更高的值。我在下面留下了精确度和召回率图表,所以请检查它们。

所以我的第一个问题是,为什么混洗数据和非混洗数据在精度和召回值方面存在差异?

第二个问题我应该信任哪个分数,或者我应该以不同的方式衡量召回率和准确率?

代码如下:

top = 34000
toptop=35000

x_neg = full_x_neg[:43000]
y_neg = np.zeros(len(x_neg))
x_pos = full_x_pos[:34000]
y_pos = np.ones(len(x_pos))

x_test = np.asarray(full_x_neg[43000:45000] + full_x_pos[top:toptop])
y_test = np.asarray(np.concatenate((np.zeros(len(full_x_neg[43000:45000])), np.ones(len(full_x_pos[top:toptop])))))
x_test = x_test.reshape((len(x_test), 10, 12))
x_test, y_test = unison_shuffled_copies(x_test, y_test) #shuffling test

x, y = unison_shuffled_copies(x, y)
x = x.reshape((len(x), 10, 12))
batch_size = 64
print('Build model...')
model = Sequential()
model.add(LSTM(128, dropout=0.2,input_shape=(10, 12)))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
          optimizer='adam',
          metrics=[precision, recall, fscore])
print('Training...')
history = model.fit(x, y,validation_data=(x_test, y_test),
      batch_size=batch_size,
      epochs=15)
score, prec, rec, fscore = model.evaluate(x_test, y_test, batch_size=batch_size)

召回功能:

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

精密功能:

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

使用打乱的测试数据进行召回

混洗测试数据的精度

召回没有 suffling 测试数据

无需提供测试数据的精度

标签: pythontensorflowmachine-learningkeras

解决方案


推荐阅读