[Python] Pytorch : Early stopping Mechanism

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I work with Pytorch and CIFAR100 dataset, While I'm newer, I would like to incorporate the early stopping mechanism in my code,

    def train(net,trainloader,epochs,use_gpu = True):
    ...
    net.train()
    for epoch in range(epochs):
    print ("Epoch {}/{}".format(epoch+1, epochs))
    running_loss = 0.0
    running_corrects = 0
    for i, data in enumerate(trainloader, 0):
    images, labels = data[0].to(device), data[1].to(device)
    optimizer.zero_grad()
    outputs = net(images)
    _, preds = torch.max(outputs, 1)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()
    running_loss += loss.item()

    epoch_loss = running_loss/len(trainloader.dataset)
    print('Loss: {}'.format(epoch_loss))


    This is the class of early stopping:

    class EarlyStopper:
    def __init__(self, patience=1, min_delta=0):
    self.patience = patience
    self.min_delta = min_delta
    self.counter = 0
    self.min_validation_loss = float('inf')

    def early_stop(self, validation_loss):
    if validation_loss < self.min_validation_loss:
    self.min_validation_loss = validation_loss
    self.counter = 0
    elif validation_loss > (self.min_validation_loss + self.min_delta):
    self.counter += 1
    if self.counter >= self.patience:
    return True
    return False


    I don't have a validation set, I suppose that test set is same as validation set Now, I would like to know where I must make those lines:

    early_stopper = EarlyStopper(patience=3, min_delta=10)
    for epoch in np.arange(n_epochs):
    train_loss = train_one_epoch(model, train_loader)
    validation_loss = validate_one_epoch(model, validation_loader)
    if early_stopper.early_stop(validation_loss):
    break

    Continue reading...

Compartilhe esta Página