@vmirly1 Thank you. How can I save model after each epoch?
here is the snippet which I put the save at the end.

criterion = nn.NLLLoss()
#optimizer = optim.Adam(model.parameters(), lr=0.00001, betas=(0.9, 0.999), eps=1e-08)  # 1e-3
optimizer = optim.Adam(model.parameters(), lr=0.00001)  # 1e-3



def train_valid_model():
    
    num_epochs=150
                      
    since = time.time()
    out_loss = open("history_loss_exp5.txt", "w")
    out_acc = open("history_acc_exp5.txt", "w")

    losses=[]
    ACCes =[]
    #losses = {}

       
    for epoch in range(num_epochs):  # loop over the dataset multiple times
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
        print('-' * 30)
        
        # Each epoch has a training and validation phase
        for phase in ['train', 'valid']:
            if phase == 'train':
                
                model.train()  # Set model to training mode
            else:
                model.eval()   # Set model to evaluate mode
        
            train_loss = 0.0
            total_train = 0
            correct_train = 0

            #iterate over data
            for t_image, mask, image_paths, target_paths in dataLoaders[phase]:
                
                 
                # get the inputs
                t_image = t_image.to(device)
                mask = mask.to(device)
                                
                 # zeroes the gradient buffers of all parameters
                optimizer.zero_grad()
                
                # forward
                # track history if only in train
                with torch.set_grad_enabled(phase == 'train'):
                    outputs = model(t_image) 
                    _, predicted = torch.max(outputs.data, 1)
                    loss = criterion(outputs, mask) # calculate the loss
            
                    # backward + optimize only if in training phase
                    if phase == 'train':
                        loss.backward() # back propagation
                        optimizer.step() # update gradients                        
            
                # accuracy
                train_loss += loss.item()
                total_train += mask.nelement()  # number of pixel in the batch
                correct_train += predicted.eq(mask.data).sum().item() # sum all precited pixel values
                
            epoch_loss = train_loss / len(dataLoaders[phase].dataset)
            #losses[phase] = epoch_loss
            losses.append(epoch_loss)
                            
            epoch_acc = 100 * correct_train / total_train
            ACCes.append(epoch_acc)
                                             
            print('{} Loss: {:.4f} {} Acc: {:.4f}'.format(phase, epoch_loss, phase, epoch_acc))     

            out_loss.write('{} {} Loss: {:.4f}\n'.format(epoch, phase, epoch_loss))
            out_acc.write('{} {} ACC: {:.4f}\n'.format(epoch, phase, epoch_acc))

            #numpy.savetxt('loss.csv', (losses), "%.4f", header= 'loss', comments='', delimiter = ",")
            #numpy.savetxt('ACC.csv', (ACCes), "%.4f", header= 'accuracy', comments='', delimiter = ",")                    
            numpy.savetxt('loss_acc_exp5.csv', numpy.c_[losses, ACCes], fmt=['%.4f', 'd'], header= "loss, acc", comments='', delimiter = ",")                    
        
            
            
    print()
    time_elapsed = time.time() - since
    
    print('Training complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))
    
    torch.save(model.state_dict(), 'train_valid_exp4.pth')