def _4e(): net = hw.DigitsConvNet() train, val = utils.torch_digits() n_epochs = 30 optimizer = optim.SGD(net.parameters(), lr=0.005) loss_func = nn.CrossEntropyLoss() train_el, val_el = hw.fit_and_validate(net, optimizer, loss_func, train, val, n_epochs, batch_size=16) _4_plotter(train_el, val_el, 'SGD, lr = 0.005, batch = 16, epochs = 30')
def _4d(): net = hw.DigitsConvNet() train, val = utils.torch_digits() n_epochs = 30 optimizer = optim.SGD(net.parameters(), lr=0.005) scheduler = optim.lr_scheduler.ExponentialLR(optimizer, 0.95) loss_func = nn.CrossEntropyLoss() train_el, val_el = hw.fit_and_validate(net, optimizer, loss_func, train, val, n_epochs, sch=scheduler) torch.save(net.cpu().state_dict(), './conv.pb') _4_plotter(train_el, val_el, 'SGD, lr = 0.005, gamma = 0.95, batch = 1, epochs = 30')
def _4f(): net = hw.DigitsConvNet() train, val = utils.torch_digits() n_epochs = 30 optimizer = optim.SGD(net.parameters(), lr=0.005) scheduler = optim.lr_scheduler.ExponentialLR(optimizer, 0.95) loss_func = nn.CrossEntropyLoss() train_el, val_el = hw.fit_and_validate(net, optimizer, loss_func, train, val, n_epochs, batch_size=16, sch=scheduler) digits, labels, val_X, val_Y = utils.torch_digits(mode=True) intermediate = net.intermediate(digits) utils.plot_PCA(intermediate.cpu().detach().numpy(), labels.cpu().detach().numpy())
def _4g(): net = hw.DigitsConvNet() train, val = utils.torch_digits() n_epochs = 30 optimizer = optim.SGD(net.parameters(), lr=0.005) scheduler = optim.lr_scheduler.ExponentialLR(optimizer, 0.95) loss_func = nn.CrossEntropyLoss() train_el, val_el = hw.fit_and_validate(net, optimizer, loss_func, train, val, n_epochs, batch_size=16, sch=scheduler) digits, labels, val_X, val_Y = utils.torch_digits(mode=True) labels = labels.cpu().detach().numpy() val_Y = val_Y.cpu().detach().numpy() intermediate_train = net.intermediate(digits).cpu().detach().numpy() intermediate_val = net.intermediate(val_X).cpu().detach().numpy() knn = KNeighborsClassifier(n_neighbors=5, algorithm='kd_tree') knn.fit(intermediate_train, labels) accuracy = knn.score(intermediate_val, val_Y) print(accuracy)