title: Keras: Optimal epoch selection
body: <p>I'm trying to write some logic that selects the best epoch to run a neural network in Keras. My code saves the training loss and the test loss for a set number of epochs and then picks the best fitting epoch according to some logic. The code looks like this:</p>
  
  <pre><code>ini_epochs = 100
  
  df_train_loss = DataFrame(data=history.history['loss'], columns=['Train_loss']); 
  df_test_loss = DataFrame(data=history.history['val_loss'], columns=['Test_loss']);
  df_loss = concat([df_train_loss,df_test_loss], axis=1)
  
  Min_loss = max(df_loss['Test_loss'])
  for i in range(ini_epochs):
      Test_loss = df_loss['Test_loss'][i];
      Train_loss = df_loss['Train_loss'][i]; 
      if Test_loss &gt;  Train_loss and Test_loss &lt; Min_loss:
          Min_loss = Test_loss;
  </code></pre>
  
  <p>The idea behind the logic is this; to get the best model, the epoch selected should select the model with the lowest loss value, but it must be above the training loss value to avoid overfitting.</p>
  
  <p>In general, this epoch selection method works OK. However, if the test loss value is below the train loss from the start, then this method picks an epoch of zero (see below). 
  <a href="https://i.stack.imgur.com/egpKU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/egpKU.png" alt="enter image description here"></a></p>
  
  <p>Now I could add another <code>if</code> statement assessing whether the difference between the test and train losses are positive or negative, and then write logic for each case, but what happens if the difference starts positive and then ends up negative. I get confused and haven't been able to write effective code.</p>
  
  <p>So, my questions are:</p>
  
  <p>1) Can you show me how you what code you would write to  to account for the situation show in the graph (and for the case where the test and train loss curves cross). I'd say the strategy would be to take the value that with the minimum difference. </p>
  
  <p>2) There is a good chance that I'm going about this the wrong way. I know Keras has a callbacks feature but I don't like the idea of using the <code>save_best_only</code> feature because it can save overfitted models. Any advice on a more efficient epoch selection method would be great. </p>