# LSTM good test/validation performance but poor on unseen data for binary classification

I have 30k sequences of 8 letters that needs to be classified in X or Y depending on the relative position of letters in the sequence.

The features are converted to numbers via a dict mapping and duplicates are removed.

I trained the following LSTM model

lstmbi = Sequential()
recurrent_regularizer=regularizers.l2(0.00001),
bias_regularizer=regularizers.l2(0.00001))))
lstmbi.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])

checkpoint_name = '/content/drive/My Drive/protease_RNN/tmp_weights.hdf5'
checkpoint = ModelCheckpoint(checkpoint_name, monitor='val_accuracy', verbose = 1, save_best_only = True, mode ='max', patience=10)
callbacks_list = [checkpoint]

history= lstmbi.fit(X_train, y_train,
validation_data=(X_val, y_val),
epochs=100,
batch_size=128,
callbacks=callbacks_list,
verbose=1)


And I monitored losses and accuracy per epoch for validation to check for over/under fitting

After I calculated performance on the test set

Accuracy: 0.871053
Precision: 0.802035
Recall: 0.674184
F1 score: 0.732573
ROC AUC: 0.896444


and everything looks kind of okay. But then I used this network to predict a set of 100k+ sequences where there are 145 that are ground positive (not used for training) and the rest is unlabelled and only 13 of this 145 are recovered.

I checked via PCA if this dataset overlaps with the training data to see if feature distribution is the same and it does.

What can be the source of the discrepancy between the two performances?

• Seems like the definition of overfitting: the model does well on the training data, but not as well on new data. stats.stackexchange.com/questions/tagged/overfitting+definition Is there a reason you don't think that this is overfitting? – Sycorax Sep 12 at 23:26
• There is independent test (X_test, y_test) which the model has never seen and performance is okay there (overfitting would be bad validation/test performance and good training performance). Seems a generalization issue but I'm not so expert in RNN models – D.A. Sep 13 at 0:29
• A problem with how overfitting is framed in classrooms is that students leave with the impression that overfitting only ever happens if the out-of-sample performance is terrible. Cleary that's undesirable, but by anchoring the overfitting discussion in terms of the worst-case scenario, it skips over the observation that overfitting can be a matter of degrees. After all, how likely is it that you'll get training and testing data to have exactly the same statistics? Unless these statistics are exactly the same, one will be larger... – Sycorax Sep 13 at 0:35
• Yeah might be a bit too dogmatic in that, I double checked data distribution to make sure that I'm not comparing apple (in data used for test/training/val) and oranges (in new data) and the PCA does not show any separation or cluster between this two datasets. Not so sure if I should just add more regularization and dropout – D.A. Sep 13 at 0:45
• The purpose of test and validation data is to simulate what happens when the model encounters new data, so the distinction you're drawing is spurious. The overall distributions might be the same, but that's unrelated to whether the model has overfit to the training data. If increasing regularization (dropout, L1/L2 penalties) shrinks the gap between training and test sets, then this is consistent with overfitting as an explanation for the discrepancy. – Sycorax Sep 13 at 0:54