Pytorch Early stopping,

username: hesham_gabr
cooked: <p>import numpy as np</p>
<p>def train(n_epochs, loaders, model, optimizer, criterion, use_cuda, save_path):</p>
<pre><code>"""returns trained model"""

# initialize tracker for minimum validation loss

valid_loss_min = np.Inf 

n_epochs_stop = 5

epochs_no_improve = 0



# early_stopping = EarlyStopping(patience=patience, verbose=True) # early stopping patience; how long to wait after last time validation loss improved.

for epoch in range(1, n_epochs+1):

    # initialize variables to monitor training and validation loss

    train_loss = 0.0

    valid_loss = 0.0

    

    ###################

    # train the model #

    ###################

    model.train()

    for batch_idx, (data, target) in enumerate(loaders['train']):

       # move to GPU

        if use_cuda:

            data, target = data.cuda(), target.cuda()

        ## find the loss and update the model -- accordingly

        ## record the average training loss, using something like

        ## train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))

        

        optimizer.zero_grad()

        output = model(data)

        loss = criterion(output,target)

        

        #l1 regularization

        l1_lambda = 0.01

        l1_norm = sum(p.abs().sum() for p in model.parameters())

        loss = loss + l1_lambda * l1_norm

        loss.backward()

        optimizer.step()            

        train_loss += loss.item()*data.size(0)

    ######################    

    # validate the model #

    ######################

    model.eval()

    for batch_idx, (data, target) in enumerate(loaders['valid']):

        # move to GPU

        if use_cuda:

            data, target = data.cuda(), target.cuda()

        ## update the average validation loss

        

        output = model(data)

        loss = criterion(output,target)

        

        valid_loss += loss.item()*data.size(0)

    

    train_loss = train_loss/len(loaders['train'].dataset)

    valid_loss = valid_loss/len(loaders['valid'].dataset)

    

    # print training/validation statistics 

    print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(

        epoch, 

        train_loss,

        valid_loss

        ))

    

    ## TODO: save the model if validation loss has decreased

    if valid_loss &lt;= valid_loss_min:

        print('Validation loss decreased ({:.6f} --&gt; {:.6f}).  Saving model ...'.format(

        valid_loss_min,

        valid_loss))

        torch.save(model.state_dict(),save_path)

        valid_loss_min = valid_loss  

    if valid_loss &lt; valid_loss_min:

    # Save the model

      #torch.save(model)

      epochs_no_improve = 0

      valid_loss_min = valid_loss



    else:

      epochs_no_improve += 1
</code></pre>
<h1>
<a name="check-early-stopping-condition" class="anchor" href="#check-early-stopping-condition"></a>Check early stopping condition</h1>
<pre><code>    if epochs_no_improve == n_epochs_stop:

      print('Early stopping!' )

      early_stop = True

      break

    else:

      continue

    break

    if early_stop:

      print('stoppend')

      break

return model
</code></pre>
<p>‘’’</p>
<p>input 3 channel input to whole models</p>
<p>‘’’</p>
<h1>
<a name="train-the-model" class="anchor" href="#train-the-model"></a># train the model</h1>
<p>model_scratch = train(100, loaders_scratch, model_scratch, optimizer_scratch,</p>
<pre><code>                  criterion_scratch, use_cuda, 'model_scratch.pt')
</code></pre>
<h1>
<a name="load-the-model-that-got-the-best-validation-accuracy" class="anchor" href="#load-the-model-that-got-the-best-validation-accuracy"></a>load the model that got the best validation accuracy</h1>
<p>model_scratch.load_state_dict(torch.load(‘model_scratch.pt’))</p>
<p>the output stops after 5 epochs, I don’t need that<br>
i need if after 5 epocs the validation loss is not decreased, then stop</p>