loss.backward() # calculate the gradient and store in .grad attribute. optimizer.step() running_loss += loss.item() * trainx.shape[0] running_corrects += torch.sum( preds.cpu().squeeze() == trainy.squeeze()) #print("train_size: " + str(train_size)) lr_schedulerr.step() # test it epoch_loss = running_loss / train_size epoch_acc = running_corrects.double() / train_size print("Training loss: {:.2f}; Accuracy: {:.2f}.".format( epoch_loss, epoch_acc.item())) #print("Training " + str(epoch) + ": loss: " + str(epoch_loss) + "," + "Accuracy: " + str(epoch_acc.item()) + ".") state = { 'net': net.state_dict(), 'optimizer': optimizer.state_dict(), 'epoch': epoch, 'loss': epoch_loss } savepath = model_path + 'checkpoint' + str(epoch) + '.pth' #torch.save(state, savepath) running_loss = 0.0 running_corrects = 0 if epoch % 1 == 0: net.eval() # print("Validating...") with torch.no_grad(): for _, (val_x, val_y) in enumerate(val_loader): if isinstance(net, timm.models.visformer.Visformer): val_x = torch.unsqueeze(val_x, dim=1)
], device=device, ) # Model training for a specified number of epochs. `y` is None as it is already supplied # in the dataset. clf.fit(train_set, y=None, epochs=n_epochs) clf.load_params(checkpoint=cp) # Load the model with the lowest valid_loss import os os.remove('./params.pt') # Delete parameters file ###################################################################### # Save the feature extractor model # ------------ torch.save(FE_model.state_dict(), './model.pth') ###################################################################### # Plot Results # ------------ # ###################################################################### # Now we use the history stored by Skorch throughout training to plot # accuracy and loss curves. # import matplotlib.pyplot as plt from matplotlib.lines import Line2D import pandas as pd