Example #1
0
def train():
    """
    Performs training and evaluation of MLP model.
    """

    ### DO NOT CHANGE SEEDS!
    # Set the random seeds for reproducibility
    np.random.seed(42)

    ## Prepare all functions
    # Get number of units in each hidden layer specified in the string such as 100,100
    if FLAGS.dnn_hidden_units:
        dnn_hidden_units = FLAGS.dnn_hidden_units.split(",")
        dnn_hidden_units = [
            int(dnn_hidden_unit_) for dnn_hidden_unit_ in dnn_hidden_units
        ]
    else:
        dnn_hidden_units = []

    ########################
    # PUT YOUR CODE HERE  #
    #######################

    # set up the data
    cifar10 = cifar10_utils.get_cifar10(FLAGS.data_dir)
    test_images, test_labels = cifar10['test'].images, cifar10['test'].labels
    test_vectors = reshape_images(test_images)

    # set up the model
    mlp_model = MLP(3072, dnn_hidden_units, 10)
    loss_module = CrossEntropyModule()

    accuracies = []
    losses = []
    for i in range(FLAGS.max_steps):
        images, labels = cifar10['train'].next_batch(FLAGS.batch_size)
        image_vectors = reshape_images(images)

        # forward pass
        model_pred = mlp_model.forward(image_vectors)

        # backward pass
        loss = loss_module.forward(model_pred, labels)
        loss_grad = loss_module.backward(model_pred, labels)
        mlp_model.backward(loss_grad)

        # update all weights and biases
        mlp_model.update(FLAGS.learning_rate)

        # evaluate the model on the data set every eval_freq steps
        if i % FLAGS.eval_freq == 0:
            test_pred = mlp_model.forward(test_vectors)
            test_accuracy = accuracy(test_pred, test_labels)
            accuracies.append(test_accuracy)
            losses.append(loss)

    plot_curve(accuracies, 'Accuracy')
    plot_curve(losses, 'Loss')
Example #2
0
def train():
    """
    Performs training and evaluation of MLP model.
    """

    ### DO NOT CHANGE SEEDS!
    # Set the random seeds for reproducibility
    np.random.seed(42)

    ## Prepare all functions
    # Get number of units in each hidden layer specified in the string such as 100,100
    if FLAGS.dnn_hidden_units:
        dnn_hidden_units = FLAGS.dnn_hidden_units.split(",")
        dnn_hidden_units = [
            int(dnn_hidden_unit_) for dnn_hidden_unit_ in dnn_hidden_units
        ]
    else:
        dnn_hidden_units = []

    data = cifar10_utils.get_cifar10(data_dir=FLAGS.data_dir)
    train = data['train']
    test = data['test']
    n_inputs = train.images[0].flatten().shape[0]
    n_classes = train.labels[0].shape[0]

    mlp = MLP(n_inputs, dnn_hidden_units, n_classes)
    loss_mod = CrossEntropyModule()

    loss_history = []
    acc_history = []
    for step in range(FLAGS.max_steps):  #FLAGS.max_steps
        x, y = train.next_batch(FLAGS.batch_size)
        x = x.reshape(x.shape[0], n_inputs)
        out = mlp.forward(x)
        loss = loss_mod.forward(out, y)
        loss_history.append(loss)
        dout = loss_mod.backward(out, y)
        mlp.backward(dout)
        mlp.update(FLAGS.learning_rate)
        if step == 0 or (step + 1) % FLAGS.eval_freq == 0:
            x, y = test.images, test.labels
            x = x.reshape(x.shape[0], n_inputs)
            test_out = mlp.forward(x)
            acc = accuracy(test_out, y)
            print('Accuracy:', acc)
            acc_history.append(acc)
    print('Final loss:', loss_history[-1])
    print('Final acc:', acc_history[-1])
    print(len(acc_history))

    plt.plot(loss_history)
    plt.step(range(0, FLAGS.max_steps + 1, FLAGS.eval_freq), acc_history)
    plt.legend(['loss', 'accuracy'])
    plt.show()