f'| Validation: {valid_acc :.2f}%')
        train_acc_list.append(train_acc.item())
        valid_acc_list.append(valid_acc.item())

    elapsed = (time.time() - start_time)/60
    print(f'Time elapsed: {elapsed:.2f} min')

elapsed = (time.time() - start_time)/60
print(f'Total Training Time: {elapsed:.2f} min')

test_acc = compute_accuracy(model, test_loader, device=device)
print(f'Test accuracy {test_acc :.2f}%')

# ######### MAKE PLOTS ######
plot_training_loss(minibatch_loss_list=minibatch_loss_list,
                   num_epochs=SETTINGS['num epochs'],
                   iter_per_epoch=len(train_loader),
                   results_dir=args.results_path)
plot_accuracy(train_acc_list=train_acc_list,
              valid_acc_list=valid_acc_list,
              results_dir=args.results_path)

results_dict = {'train accuracies': train_acc_list,
                'validation accuracies': valid_acc_list,
                'test accuracy': test_acc.item(),
                'settings': SETTINGS}

results_path = os.path.join(args.results_path, 'results_dict.yaml')
with open(results_path, 'w') as file:
    yaml.dump(results_dict, file)
예제 #2
0
                    print('Current best acc: ', best_val)
                    print("VALUES: ", values)
                print('Final training loss: ', stats['loss_history'][-1])
                print('Final validation loss: ', stats['loss_val_history'][-1])
                print('Used values: hiddensize ', hs, 'learning rate: ', lr,
                      'reg: ', r)
                print('Final validation accuracy: ',
                      stats['val_acc_history'][-1])

    print('Best Accuracy: ', best_val)
    print('Best values: \nHidden_size: ', values[0], '\nLearning Rate: ',
          values[1], '\nReg: ', values[2])
    net = TwoLayerNeuralNetwork(input_size, 300, num_classes)
    # Generate best nets
    # 55.1% Accuracy mit hiddensize: 300 learning rate:  0.001 reg:  0.5
    stats = net.train(X_train,
                      y_train,
                      X_val,
                      y_val,
                      num_iters=num_iters,
                      batch_size=batch_size,
                      learning_rate=values[1],
                      learning_rate_decay=learning_rate_decay,
                      reg=values[2],
                      verbose=True)
    final_acc = (net.predict(X_val) == y_val).mean()
    print('Final Accuracy reached: ', final_acc)
    helper.plot_net_weights(net)
    helper.plot_accuracy(stats)
    helper.plot_loss(stats)
예제 #3
0
def main():
    '''
    Main block

    Steps:
    1. Pull data (MNIST)
    2. Initialise network
    3. Train network
    4. Save weights
    '''

    DATA = download_mnist.load_mnist()
    validation = []
    for key in ['fold-{f}'.format(f=f) for f in range(4)]:
        validation += DATA[key]
    validation = np.array(validation)

    epochs = 8
    initial_lr = 8e-3
    final_lr = 8e-6

    if args.question in ["1", "2", "5"]:
        model = network.MLP([784, 1000, 500, 250, 10])

        train_losses,val_losses,test_losses,\
        train_accuracies,val_accuracies,test_accuracies\
        =model.fit(np.array(DATA['train']),validation,np.array(DATA['fold-4']),\
        epochs=epochs,\
        initial_lr=initial_lr,\
        final_lr=final_lr)
        print(val_losses, test_losses)

        helper.plot_loss([train_losses, val_losses, test_losses],
                         epochs=epochs,
                         name="sigmoid_loss")
        helper.plot_accuracy(
            [train_accuracies, val_accuracies, test_accuracies],
            epochs=epochs,
            name="sigmoid_accuracy")
        run_stats(model, DATA, tag="sigmoid")

    elif args.question == "3":
        epochs = 4
        initial_lr = 8e-1
        final_lr = 8e-6
        variance = 0.00001

        model= network.MLP([784,1000,500,250,10],activation="relu",\
        variance=variance)

        train_losses,val_losses,test_losses,\
        train_accuracies,val_accuracies,test_accuracies\
        =model.fit(np.array(DATA['train']),validation,np.array(DATA['fold-4']),\
        epochs=epochs,\
        initial_lr=initial_lr,\
        final_lr=final_lr)
        print(val_losses, test_losses)

        helper.plot_loss([train_losses, val_losses, test_losses],
                         epochs=epochs,
                         name="relu_loss")
        helper.plot_accuracy(
            [train_accuracies, val_accuracies, test_accuracies],
            epochs=epochs,
            name="sigmoid_accuracy")
        run_stats(model, DATA, tag="relu")

    elif args.question == "4":
        train_data = noise_addition(DATA['train'], sigma=1e-3)

        model = network.MLP([784, 1000, 500, 250, 10])

        train_losses,val_losses,test_losses,\
        train_accuracies,val_accuracies,test_accuracies\
        =model.fit(np.array(train_data),validation,np.array(DATA['fold-4']),\
        l2=0.1,\
        l1=0.01,\
        epochs=epochs,\
        initial_lr=initial_lr,\
        final_lr=final_lr)

        helper.plot_loss([train_losses, val_losses, test_losses],
                         epochs=epochs,
                         name="sigmoid_regularised_loss")
        helper.plot_accuracy(
            [train_accuracies, val_accuracies, test_accuracies],
            epochs=epochs,
            name="sigmoid_regularised_accuracy")
        run_stats(model, DATA, tag="sigmoid_regularised")

    elif args.question == "6":
        epochs = 10
        initial_lr = 8e-4
        final_lr = 8e-6
        variance = 0.001

        model = network.MLP([64, 32, 10])
        train_data = preprocess(DATA['train'])
        val_data = np.array(preprocess(validation))
        test_data = np.array(preprocess(DATA['fold-4']))
        print(val_data.shape)

        train_losses,val_losses,test_losses,\
        train_accuracies,val_accuracies,test_accuracies\
        =model.fit(train_data,val_data,test_data,\
        epochs=epochs,\
        l2=0.1,\
        l1=0.01,\
        initial_lr=initial_lr,\
        final_lr=final_lr)
        print(val_losses, test_losses)

        DATA_HOG_fold = {
            'fold-{f}'.format(f=f): preprocess(DATA['fold-{f}'.format(f=f)])
            for f in range(4)
        }

        helper.plot_loss([train_losses, val_losses, test_losses],
                         epochs=epochs,
                         name="sigmoid_HOG_loss")
        helper.plot_accuracy(
            [train_accuracies, val_accuracies, test_accuracies],
            epochs=epochs,
            name="sigmoid_HOG_accuracy")
        run_stats(model, DATA_HOG_fold, tag="sigmoid")

    elif args.question == "7":
        train_data = np.array(preprocess(DATA['train']))
        val_data = np.array(preprocess(validation))
        test_data = np.array(preprocess(DATA['fold-4']))

        svc = svm.SVC(kernel='linear')
        labels = np.array([
            np.where(train_data[:, 1][x] == 1)[0][0]
            for x in range(len(train_data[:, 1]))
        ])
        labels = np.array(labels).reshape((len(labels), 1))

        train_data = np.concatenate(train_data[:, 0], axis=1)
        svc.fit(train_data, labels)

        y_true = np.array([
            np.where(test_data[:, 1][x] == 1)[0][0]
            for x in range(len(test_data[:, 1]))
        ])
        test_data = np.vstack(test_data[:, 0])
        y_pred = svc.predict(test_data)

        print(sklearn.metrics.accuracy_score(y_true, y_pred))
        print(sklearn.metrics.classification_report(y_true, y_pred))

    else:
        print("Invalid question {}".format(args.question))
        minibatch_loss_list.append(loss.item())
        if not batch_idx % 50:
            print(f'Epoch: {epoch+1:03d}/{SETTINGS["num epochs"]:03d} '
                  f'| Batch {batch_idx:04d}/{len(train_loader):04d} '
                  f'| Loss: {loss:.4f}')

    model.eval()
    with torch.no_grad():  # save memory during inference
        train_acc = compute_accuracy(model, train_loader, device=device)
        valid_acc = compute_accuracy(model, valid_loader, device=device)
        print(f'Epoch: {epoch+1:03d}/{SETTINGS["num epochs"]:03d} '
              f'| Train: {train_acc :.2f}% '
              f'| Validation: {valid_acc :.2f}%')
        train_acc_list.append(train_acc.item())
        valid_acc_list.append(valid_acc.item())

    elapsed = (time.time() - start_time) / 60
    print(f'Time elapsed: {elapsed:.2f} min')

elapsed = (time.time() - start_time) / 60
print(f'Total Training Time: {elapsed:.2f} min')

test_acc = compute_accuracy(model, test_loader, device=device)
print(f'Test accuracy {test_acc :.2f}%')

# ######### MAKE PLOTS ######
plot_training_loss(minibatch_loss_list=minibatch_loss_list,
                   num_epochs=SETTINGS['num epochs'],
                   iter_per_epoch=len(train_loader))
plot_accuracy(train_acc_list=train_acc_list, valid_acc_list=valid_acc_list)