train = True test = True model = None if train: # Create models: model = my_models.model_2() my_models.test_model_size(model, dtype) # test model size output: optimizer = optim.Adadelta(model.parameters()) # Train model: model, loss_data = my_models.train_model(model, optimizer, train_loader, val_loader, device, dtype, epoches=2) # Save model to file: torch.save(model.state_dict(), MODEL_PATH + MODEL_NAME) # Save loss data to file: np.savetxt(LOSS_PATH + 'data_part_4_train.csv', loss_data, delimiter=',') fix_loss_data('data_part_4_train.csv') print('Training is Finished !') if test: print('Checking model Accuracy over Test Set...') if model is None: # load model:
dtype = torch.float32 # we will be using float train = False train_loops = 20 loss_data = [] if train: for tl in range(train_loops): # Create models: model = my_models.model_2() #my_models.test_model_size(model, dtype) # test model size output: optimizer = optim.Adadelta(model.parameters()) # Train model: model, current_loss_data = my_models.train_model(model, optimizer, train_loader, test_loader, device, dtype, epoches=4, print_every=5) loss_data.append(current_loss_data) # Save model to file: #torch.save(model.state_dict(), MODEL_PATH + MODEL_NAME) print('Training Loop {}/{} is Finished !'.format(tl + 1, train_loops)) print() # Add test accuracy and save data to file: loss_data = np.asarray(loss_data) loss_data = fix_loss_array(loss_data) # Saving Accuracy to file np.save(LOSS_PATH + 'data_part_2_acc.npy', loss_data)