0

The part of the code that needs help is indicated with ###, the code below is appending a dict called results into history = [] and returning history.

def epoch_end(result,epoch):
      print("epoch: {}, last_lr {},  Epoch_loss:{}, Epoch_accuracy {}, train_loss {}" .format(epoch, result["lrs"][-1], result["Loss"], result["Accuracy"], result["train loss"] ))
    
def fit(epochs,train_set,val_dl,model,lr): #model trainer
   optimizer = torch.optim.Adam(model.parameters(), lr, weight_decay = 0.001) #defining the optimizer

   scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer, lr*2, epochs = epochs,steps_per_epoch = len(train_set))# learning rate scheduler

   def get_lr():
    for param_group in optimizer.param_groups:  #getting the learning rates of e
      return param_group["lr"]

   history = []      
   


   for epoch in range(epochs):
      model.train()
      train_loss = []
      lrs = []
      for batch in train_set:
        loss = training_step(batch, model)
        train_loss.append(loss)
        loss.backward()
        nn.utils.clip_grad_norm_(model.parameters(), 0.1)
        optimizer.step()
        optimizer.zero_grad()
        lrs.append(get_lr())
        scheduler.step()
      #validation 
      results = validation_combine_loss(val_dl,model)
      results["lrs"] = lrs
      results["train loss"] = torch.stack(train_loss).mean().item()
      epoch_end(results,epoch) ###
      history.append(results) ###
   return history

fit(20,train_set,val_set,model,0.01)  #training model 

The output will be

epoch: 0, last_lr 0.0010401048569784015,  Epoch_loss:1.3724077939987183, Epoch_accuracy 0.5091797113418579, train_loss 1.513434886932373
epoch: 1, last_lr 0.002795876957832664,  Epoch_loss:1.785091757774353, Epoch_accuracy 0.3656249940395355, train_loss 1.467275619506836
epoch: 2, last_lr 0.0051964283175401065,  Epoch_loss:2.189635992050171, Epoch_accuracy 0.3041015565395355, train_loss 1.5236047506332397
epoch: 3, last_lr 0.0075979375927655805,  Epoch_loss:1.843380331993103, Epoch_accuracy 0.36347657442092896, train_loss 1.593713641166687
epoch: 4, last_lr 0.009356326529838265,  Epoch_loss:3.570767641067505, Epoch_accuracy 0.150390625, train_loss 1.6375536918640137
epoch: 5, last_lr 0.01,  Epoch_loss:2.7340424060821533, Epoch_accuracy 0.142578125, train_loss 1.6638548374176025
epoch: 6, last_lr 0.009874640062350875,  Epoch_loss:1.8366981744766235, Epoch_accuracy 0.31230467557907104, train_loss 1.6672502756118774
epoch: 7, last_lr 0.009504846320134737,  Epoch_loss:2.4218339920043945, Epoch_accuracy 0.21992187201976776, train_loss 1.6705607175827026
epoch: 8, last_lr 0.0089091617757105,  Epoch_loss:1.7438850402832031, Epoch_accuracy 0.3316406309604645, train_loss 1.6584593057632446

However when I try to append epoch_end(results, epoch) to history instead of appending the dict results to history the outputs are completely different.

for epoch in range(epochs):
      model.train()
      train_loss = []
      lrs = []
      for batch in train_set:
        loss = training_step(batch, model)
        train_loss.append(loss)
        loss.backward()
        nn.utils.clip_grad_norm_(model.parameters(), 0.1)
        optimizer.step()
        optimizer.zero_grad()
        lrs.append(get_lr())
        scheduler.step()
      #validation 
      results = validation_combine_loss(val_dl,model)
      results["lrs"] = lrs
      results["train loss"] = torch.stack(train_loss).mean().item()
      epoch_end(results,epoch) ###
      string_formatting = epoch_end(results,epoch) ###
      history.append(string_formatting) ###
   return history

The output will be, comparing the two outputs, this output seems to have duplicates, is there a reason for this?

epoch: 0, last_lr 0.002080209713956803,  Epoch_loss:1.5075801610946655, Epoch_accuracy 0.43378907442092896, train_loss 1.4565353393554688
epoch: 0, last_lr 0.002080209713956803,  Epoch_loss:1.5075801610946655, Epoch_accuracy 0.43378907442092896, train_loss 1.4565353393554688
epoch: 1, last_lr 0.005591753915665328,  Epoch_loss:8.766005516052246, Epoch_accuracy 0.09824218600988388, train_loss 1.5436667203903198
epoch: 1, last_lr 0.005591753915665328,  Epoch_loss:8.766005516052246, Epoch_accuracy 0.09824218600988388, train_loss 1.5436667203903198
epoch: 2, last_lr 0.010392856635080213,  Epoch_loss:15.781758308410645, Epoch_accuracy 0.12871094048023224, train_loss 1.6487261056900024
epoch: 2, last_lr 0.010392856635080213,  Epoch_loss:15.781758308410645, Epoch_accuracy 0.12871094048023224, train_loss 1.6487261056900024
epoch: 3, last_lr 0.015195875185531161,  Epoch_loss:4.121547698974609, Epoch_accuracy 0.15214844048023224, train_loss 1.7063096761703491
epoch: 3, last_lr 0.015195875185531161,  Epoch_loss:4.121547698974609, Epoch_accuracy 0.15214844048023224, train_loss 1.7063096761703491
epoch: 4, last_lr 0.01871265305967653,  Epoch_loss:2.113895893096924, Epoch_accuracy 0.21621093153953552, train_loss 1.776288390159607
epoch: 4, last_lr 0.01871265305967653,  Epoch_loss:2.113895893096924, Epoch_accuracy 0.21621093153953552, train_loss 1.776288390159607
XtrollerX
  • 5
  • 2

0 Answers0