Ejemplo n.º 1
0
def train(config):
    '''The config object required for training is in train_config.py'''

    config['hyperparameters']['seed'] = np.random.randint(1e5)

    if config['dataset_name'] == 'mnist':
        dataset = load_mnist(config['mnist_sample_shape'])

    elif config['dataset_name'] == 'cifar10':
        dataset = load_cifar10(flatten_input=(config['nn_type'] == 'mlp'))

    else:
        raise ValueError(err_msg)

    nn = NN(data=dataset, **config['hyperparameters'])

    start = timeit.default_timer()
    
    train_logs = nn.train_loop(eval_each_epoch=config['eval_while_training'])
    
    elapsed = round(timeit.default_timer()-start,4)
    print(f'training runtime: {elapsed} seconds')

    exp = utils.ExperimentResults()
    exp.save(train_logs, 'train_logs')
    exp.save(config, 'config')
    exp.save(nn, 'neural_network')
    exp.save(elapsed,'training_runtime')

    test_results = nn.evaluate()
    exp.save(test_results, 'test_results')
Ejemplo n.º 2
0
def mnist_net():
    mndata = MNIST("mnist", return_type="numpy")
    print("Loading images...")
    images, labels = mndata.load_training()
    features = images.T / 255
    z = np.zeros((60000, 10))
    z[np.arange(60000), labels] = 1
    Y = z.T
    nn = NN([784, 100, 30, 10])
    nn.set_hyperparameters(learning_rate=0.5)
    t = time()
    nn.initialize_parameters()
    print("Start Training...")
    nn.minimize({"features": features, "labels": Y}, 20)
    print("Finish Training.")
    print("Training time: {0} seconds".format(round(time() - t, 2)))
    print("Start Testing...")
    t = time()
    test_images, test_labels = mndata.load_testing()
    test_features = test_images.T / 255
    z = np.zeros((10000, 10))
    z[np.arange(10000), test_labels] = 1
    test_Y = z.T
    print("Testing accuracy: {}".format(
        round(nn.evaluate({
            "features": test_features,
            "labels": test_Y
        }), 4)))
    print("Testing time: {0} seconds".format(round(time() - t, 2)))
Ejemplo n.º 3
0
def train(hyperparameters, sample_shape, nn_type):

    hyperparameters['seed'] = np.random.randint(1e5)

    cifar10 = load_cifar10(flatten_input=(
        nn_type == 'mlp'))  # because mlp only processes 1D input
    nn = NN(data=cifar10, **hyperparameters)

    perform_evaluation = False
    if nn_type == 'mlp':
        perform_evaluation = True
    elif hyperparameters['n_epochs'] == 1:
        perform_evaluation = True

    train_logs = nn.train_loop(eval_each_epoch=perform_evaluation)

    exp = utils.ExperimentResults()
    exp.save(train_logs, 'train_logs')
    exp.save(hyperparameters, 'hyperparams')
    exp.save(nn, 'neural_network')

    test_results = nn.evaluate()
    exp.save(test_results, 'test_results')
Ejemplo n.º 4
0
def xor_net():
    a = np.array([[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0]])
    features = a[:, 0:2].T
    labels = a[:, 2].reshape((1, 4))
    z = np.zeros((4, 2))
    z[np.arange(4), labels] = 1
    labels = z.T
    nn = NN([2, 4, 3, 2])
    nn.set_hyperparameters(batch_size=4, learning_rate=0.75)
    t = time()
    nn.initialize_parameters()
    print("Start Training...")
    nn.minimize({"features": features, "labels": labels}, 10000)
    print("Finish Training.")
    print("Training time: {0} seconds".format(round(time() - t, 2)))
    print("Start Testing...")
    t = time()
    print("Testing accuracy: {}".format(
        round(nn.evaluate({
            "features": features,
            "labels": labels
        }), 4)))
    print("Testing time: {0} seconds".format(round(time() - t, 2)))