def test_draw_once(self): llp = LiveLossPlot(True, 1, True, True) llp.batch_plt = MagicMock() llp.plt = MagicMock() state = {tb.BATCH: 1, tb.METRICS: {'test': 1}} llp.on_end_epoch(state) llp.on_end_epoch(state) llp.on_end(state) self.assertTrue(llp.plt.draw.call_count == 1) self.assertTrue(llp.batch_plt.draw.call_count == 1)
def test_not_on_batch(self): llp = LiveLossPlot(False, 10, False, False) llp.batch_plt = MagicMock() llp.plt = MagicMock() state = {tb.BATCH: 1, tb.METRICS: {'test': 1}} llp.on_step_training(state) llp.on_step_training(state) self.assertTrue(llp.batch_plt.update.call_count == 0)
def test_on_epoch(self): llp = LiveLossPlot(False, 10, True, False) llp.batch_plt = MagicMock() llp.plt = MagicMock() state = {torchbearer.BATCH: 1, torchbearer.METRICS: {'test': 1}} llp.on_end_epoch(state) llp.on_end_epoch(state) self.assertTrue(llp.batch_plt.update.call_count == 0) self.assertTrue(llp.plt.update.call_count == 2)
# load data trainset = SVHN(".", split='train', download=True, transform=transform) testset = SVHN(".", split='test', download=True, transform=transform) valset = SVHN(".", split='extra', download=True, transform=transform) # create data loaders trainloader = DataLoader(trainset, batch_size=128, shuffle=True) testloader = DataLoader(testset, batch_size=128, shuffle=True) valloader = DataLoader(valset, batch_size=128, shuffle=True) # build the model model = SimpleCNN() # define the loss function and the optimiser loss_function = nn.CrossEntropyLoss() live_loss_plot = LiveLossPlot(draw_once=True) optimiser = optim.Adam(model.parameters(), lr=0.001) scheduler = StepLR(step_size=10, gamma=0.5) device = "cuda:0" if torch.cuda.is_available() else "cpu" trial = Trial(model, optimiser, loss_function, callbacks=[scheduler, live_loss_plot], metrics=['loss', 'accuracy']).to(device) trial.with_generators(trainloader, val_generator=valloader, test_generator=testloader) history = trial.run(verbose=1, epochs=30)# results = trial.evaluate(data_key=torchbearer.TEST_DATA) print(results) # build the model model = SimpleCNN() # define the loss function and the optimiser
# install livelossplot !pip install livelossplot # utilize the GPU if one exists, otherwise use the CPU device = "cuda:0" if torch.cuda.is_available() else "cpu" # initialize control network control_model = NetworkControl(784, 1200, 10) # define the loss function loss_function = nn.CrossEntropyLoss() # define the optimiser, learning rate and momentum optimiser = optim.SGD(model.parameters(), lr=0.1, momentum=0.5) # create a live loss plot of accuracy and loss after each training epoch plot = LiveLossPlot() # train the network using model, optimiser and loss function for 2 epochs trial = torchbearer.Trial(control_model, optimiser, loss_function, callbacks=[plot], metrics=['loss', 'accuracy']).to(device) trial.with_generators(trainloader, test_generator=testloader) trial.run(epochs=2) # save the trained model torch.save(model.state_dict(),"control.pth") # print the accuracy and loss of control network on the MNIST test data results = trial.evaluate(data_key=torchbearer.TEST_DATA) print("Test Loss:\t", results.get("test_loss")) print("Test Accuracy:\t", results.get("test_acc")) from scipy.ndimage import gaussian_filter
def test_on_batch_steps(self): llp = LiveLossPlot(True, 2, False, False) llp.batch_plt = MagicMock() llp.plt = MagicMock() state = {tb.BATCH: 1, tb.METRICS: {'test': 1}} llp.on_step_training(state) state = {tb.BATCH: 2, tb.METRICS: {'test': 1}} llp.on_step_training(state) state = {tb.BATCH: 3, tb.METRICS: {'test': 1}} llp.on_step_training(state) state = {tb.BATCH: 4, tb.METRICS: {'test': 1}} llp.on_step_training(state) self.assertTrue(llp.batch_plt.draw.call_count == 2) self.assertTrue(llp.plt.draw.call_count == 0)
def test_on_start(self, llp_mock): llp = LiveLossPlot(True, 1, True, False) llp.on_start({}) self.assertTrue(llp_mock.call_count == 2)
out = self.fc1(out) out = F.relu(out) out = self.fc2(out) return out #reset the data loaders trainloader = DataLoader(train_data, batch_size=128, shuffle=True) testloader = DataLoader(test_data, batch_size=128, shuffle=True) valloader = DataLoader(val_data, batch_size=128, shuffle=True) # build the model model = SimpleCNN() # define the loss function and the optimiser loss_function = nn.MSELoss() optimiser = optim.Adam(model.parameters()) device = "cuda:0" if torch.cuda.is_available() else "cpu" trial = Trial(model, optimiser, loss_function, metrics=['loss', 'accuracy'], callbacks=[LiveLossPlot()]).to(device) trial.with_generators(trainloader, valloader, test_generator=testloader) trial.run(epochs=100) results_train = trial.evaluate(data_key=torchbearer.TRAIN_DATA) results_test = trial.evaluate(data_key=torchbearer.TEST_DATA) print(results_train) print(results_test)