Search code examples
pythonperformancekerasautoencodermse

How to speedup loss calculation, which is done one by one


I am building an autoencoder, and to validate the progress of the model - I am calculating MSE and MAE after each epoch, for each entry in the validation set:

for validation_sample in x_pred:
   prediction = autoencoder.predict(validation_sample)
   current_validation_mae.append(keras.backend.eval(keras.losses.mean_absolute_error(validation_sample, prediction)))
   current_validation_mse.append(keras.backend.eval(keras.losses.mean_squared_error(validation_sample, prediction)))

After that I take that array a get the real MAE/MSE by dividing it by num samples in validation.

My data is gene expression data. 1 sample has 17000 features and 1 datapoint for each feature. 5000 samples in total.

Performance when using validation of 10 samples (on a super computer):

Prediction created in: 0.019748687744140625 seconds.

MAE took: 1.1507933139801025 seconds.

MSE took: 1.1251187324523926 seconds.

What can be improved?


Solution

  • It turned out that .predit() indeed returns same shape as input, so there was no need to loop it:

            predictions = autoencoder.predict(x_pred)
            current_validation_mae = np.mean(keras.backend.eval(keras.losses.mean_absolute_error(x_pred, predictions)))
            current_validation_mse = np.mean(keras.backend.eval(keras.losses.mean_squared_error(x_pred, predictions)))
    

    Loss is also calculated on the whole batch.