Exemple #1
0
def main():
    # getting the subset dataset from MNIST
    # binary classification for digits 2 and 3
    train_data, train_label, test_data, test_label = mnist(ntrain=6000,
                                                           ntest=1000,
                                                           digit_range=[2, 4])

    n_in, m = train_data.shape
    n_fin = 1
    n_h = 500
    net_dims = [n_in, n_h, n_fin]
    # initialize learning rate and num_iterations
    learning_rate = 1e-2
    num_iterations = 300

    costs, parameters = two_layer_network(train_data,
                                          train_label,
                                          net_dims,
                                          num_iterations=num_iterations,
                                          learning_rate=learning_rate)

    # compute the accuracy for training set and testing set
    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)

    trAcc = np.mean(train_Pred == train_label) * 100
    teAcc = np.mean(test_Pred == test_label) * 100
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))
    plt_cost(costs, num_iterations)
Exemple #2
0
def main():
    # getting the subset dataset from MNIST
    # binary classification for digits 2 and 3
    # for train, from the first 1000 samples, we get those samples which have label 2,4
    # for train, from the first 6000 samples, we get those samples which have label 2,4
    train_data, train_label, test_data, test_label = \
                mnist(ntrain=6000,ntest=1000,digit_range=[2,4])

    #the labels are converted to 0 and 1 values (instead of the actual numbers)
    
    n_in, m = train_data.shape
    n_fin = 1
    n_h = 500
    net_dims = [n_in, n_h, n_fin]
    
    # initialize learning rate and num_iterations
    learning_rate = 0.1
    num_iterations = 1000

    costs, parameters = two_layer_network(train_data, train_label, net_dims, \
            num_iterations=num_iterations, learning_rate=learning_rate)
    
    # compute the accuracy for training set and testing set
    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)

    trAcc = 100 - (np.sum((np.abs(train_Pred - train_label)))/train_Pred.shape[1])*100
    teAcc = 100 - (np.sum((np.abs(test_Pred - test_label)))/train_Pred.shape[1])*100
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))
Exemple #3
0
def main():
    # getting the subset dataset from MNIST
    # binary classification for digits 2 and 3
    train_data, train_label, test_data, test_label = \
                mnist(ntrain=6000,ntest=1000,digit_range=[2,4])

    n_in, m = train_data.shape
    n_fin = 1
    n_h = 500
    net_dims = [n_in, n_h, n_fin]
    # initialize learning rate and num_iterations
    learning_rate = 0.1
    num_iterations = 1000


    costs, parameters = two_layer_network(train_data, train_label, net_dims, \
            num_iterations=num_iterations, learning_rate=learning_rate)

    # compute the accuracy for training set and testing set
    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)
    m1 = train_data.shape[1]
    m2 = test_data.shape[1]
    trAcc = 100 - (np.sum(np.absolute(train_label - train_Pred)) / m1) * 100
    teAcc = 100 - (np.sum(np.absolute(test_label - test_Pred)) / m2) * 100
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))
    # CODE HERE TO PLOT costs vs iterations
    points = np.arange(0, 100)
    plt.plot(points, costs)
    plt.savefig("Error vs iterations")
def main():

    # getting the subset dataset from MNIST
    train_data, train_label, test_data, test_label = mnist(ntrain=6000,
                                                           ntest=1000,
                                                           digit_range=[0, 10])
    # initialize learning rate and num_iterations
    init_layer = train_data.shape[1]
    net_dims = [init_layer, 500, 10]
    costs, parameters = multi_layer_network(train_data, train_label, net_dims,
                                            10, 0.001)

    # compute the accuracy for training set and testing set
    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)

    trAcc = np.sum(train_Pred == train_label) / train_Pred.shape[1] * 100
    teAcc = np.sum(test_Pred == test_label) / test_Pred.shape[1] * 100
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))
    ### CODE HERE to plot costs
    plt.plot(costs)
    plt.xlabel('iteration')
    plt.ylabel('error')
    plt.savefig('error_plot_multiLayer_mp_2.png')
Exemple #5
0
def main():
    # getting the subset dataset from MNIST
    # binary classification for digits 2 and 3
    train_data, train_label, test_data, test_label = \
                mnist(ntrain=6000,ntest=1000,digit_range=[2,4])

    n_in, m = train_data.shape
    n_fin = 1
    n_h = 500
    net_dims = [n_in, n_h, n_fin]
    # initialize learning rate and num_iterations
    learning_rate = 0.1
    num_iterations = 1000

    costs, parameters = two_layer_network(train_data, train_label, net_dims, \
            num_iterations=num_iterations, learning_rate=learning_rate)
    
    # compute the accuracy for training set and testing set
    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)

    trAcc = np.sum(train_Pred == train_label)/float(len(train_Pred[0]))*100
    teAcc = np.sum(test_Pred == test_label)/float(len(test_Pred[0]))*100
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))
    # CODE HERE TO PLOT costs vs iterations

    plt.plot(range(1, len(costs) + 1), costs, label='Cost')
    plt.xlabel('Iterations')
    plt.ylabel('Cost')
    plt.title('Mini Project2:1.1 Two Layer Binary Network-Cost vs Iterations')
    plt.legend()
    plt.show()
def main():
    train_data, train_label, test_data, test_label = mnist(ntrain=50, ntest=10, digit_range=[0, 10])
    (m, H_in, W_in, Ch_in) = train_data.shape
    n_fin = 10
    n_h = 100

    net_dims = [(m, H_in, W_in, Ch_in), n_h, n_fin]

    learning_rate = 0.2
    num_iterations = 100

    costs, parameters = conv_net(train_data, train_label, net_dims, num_iterations=num_iterations, learning_rate=learning_rate)

    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)

    trAcc = np.sum(train_Pred == train_label) / float(train_label.shape[1]) * 100
    teAcc = np.sum(test_Pred == test_label) / float(test_label.shape[1]) * 100
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))

    plt.plot(range(1, len(costs) + 1), costs, label='Cost')
    plt.xlabel('Iterations')
    plt.ylabel('Cost')
    plt.title('Mini Project3:1.2 Convolutional neural network with Max Pooling (Two-layer)-Cost vs Iterations')
    plt.legend()
    plt.show()
def main():
    '''
    Trains a multilayer network for MNIST digit classification (all 10 digits)
    To create a network with 1 hidden layer of dimensions 800
    Run the progam as:
        python convPoolNeuralNetwork.py "[800]"
    The network will have the dimensions [3125,800,10]

    input size of digit images: 784 (28pix x 28pix = 784)
    after convolution with 3 x 3 filter: (28 - 3 + 1)^2 = 26 * 26 = 676
    after 2x2 pooling: (26 - 2 + 1)^2 = 625
    with 5 filters: 676 x 5 = 3125
    10 is the number of digits

    To create a network with 2 hidden layers of dimensions 800 and 500
    Run the progam as:
        python deepMultiClassNetwork_starter.py "[3125,800,500]"
    The network will have the dimensions [3125,800,500,10]
    '''
    net_dims = ast.literal_eval(sys.argv[1])
    net_dims.insert(0, 3125)
    net_dims.append(10)  # Adding the digits layer with dimensionality = 10
    print("Network dimensions are:" + str(net_dims))

    # getting the subset dataset from MNIST
    train_data, train_label, test_data, test_label = \
        mnist(ntrain=800, ntest=200, digit_range=[0, 10])
    # initialize learning rate and num_iterations
    learning_rate = 0.8
    num_iterations = 200
    decay_rate = 0.005

    costs, parameters, convParameters = multi_layer_network(
        train_data,
        train_label,
        net_dims,
        num_iterations=num_iterations,
        learning_rate=learning_rate,
        decay_rate=decay_rate)

    # compute the accuracy for training set and testing set
    train_Pred = classify(train_data, parameters, convParameters)
    test_Pred = classify(test_data, parameters, convParameters)

    trAcc = 100 * np.mean((train_Pred == train_label).astype(int))
    teAcc = 100 * np.mean((test_Pred == test_label).astype(int))
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))

    ### CODE HERE to plot costs
    iterations = range(0, num_iterations, 10)
    plt.plot(iterations, costs)
    net_dims.insert(0, 3380)
    plt.title("CNN with Max Pooling & Dropout: " + str(net_dims) +
              "\nTraining accuracy:{0:0.3f}% Testing accuracy:{1:0.3f}%".
              format(trAcc, teAcc))
    plt.xlabel("Iteration")
    plt.ylabel("Cost")
    plt.show()
def main():
    '''
    Trains a sparse autoencoder for MNIST digit data
    '''
    net_dims = [784, 200, 784]
    print("Network dimensions are:" + str(net_dims))

    # getting the subset dataset from MNIST
    train_data, train_label, test_data, test_label = \
        mnist(ntrain=1000, ntest=200, digit_range=[0, 10])
    train_label = train_data
    test_label = test_data
    # initialize learning rate and num_iterations

    num_iterations = 400
    decay_rate = 0
    weight_decay = 0.001
    ps = [0.01, 0.1, 0.5, 0.8]
    p = ps[1]
    learning_rate = 0.3

    costs, parameters = train_sparse_autoencoder(train_data, train_label, net_dims, num_iterations=num_iterations,
        learning_rate=learning_rate, decay_rate=decay_rate, weight_decay=weight_decay, p=p)

    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)

    # plt.imshow(np.reshape(train_label[:, 10], [28, 28]), cmap='gray')
    # plt.show()
    # plt.imshow(np.reshape(train_Pred[:, 10], [28, 28]), cmap='gray')
    # plt.show()

    # compute the accuracy for training set and testing set
    trAcc = 100 * (1 - np.mean(np.abs(train_Pred - train_label)))
    teAcc = 100 * (1 - np.mean(np.abs(test_Pred - test_label)))

    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))

    ### CODE HERE to plot costs
    iterations = range(0, num_iterations, 10)
    plt.plot(iterations, costs)
    plt.title("Sparse Autoencoder: " + str(net_dims) + " (p = " + str(p) +
        ")\nTraining accuracy:{0:0.3f}% Testing accuracy:{1:0.3f}%".format(trAcc, teAcc))
    plt.xlabel("Iteration")
    plt.ylabel("Cost")
    plt.show()

    W1 = parameters["W1"]
    tmp = np.reshape(W1[0:100, :], [100, 28, 28])
    for i in range(100):
        plt.subplot(10, 10, i + 1)
        plt.axis('off')
        plt.imshow(tmp[i], cmap='gray')
    plt.subplots_adjust(left=0.16, bottom=0.05, right=0.84, top=0.95, wspace=0.05, hspace=0.05)
    plt.suptitle("100 rows of W1 in 28*28 images" + " (p = " + str(p) + ")")
    plt.show()
def main():
    '''
    Trains a multilayer network for MNIST digit classification (all 10 digits)
    To create a network with 1 hidden layer of dimensions 800
    Run the progam as:
        python deepMultiClassNetwork_starter.py "[784,800]"
    The network will have the dimensions [784,800,10]
    784 is the input size of digit images (28pix x 28pix = 784)
    10 is the number of digits

    To create a network with 2 hidden layers of dimensions 800 and 500
    Run the progam as:
        python deepMultiClassNetwork_starter.py "[784,800,500]"
    The network will have the dimensions [784,800,500,10]
    784 is the input size of digit images (28pix x 28pix = 784)
    10 is the number of digits
    '''
    net_dims = ast.literal_eval(sys.argv[1])
    net_dims.append(10)  # Adding the digits layer with dimensionality = 10
    print("Network dimensions are:" + str(net_dims))

    # getting the subset dataset from MNIST
    train_data, train_label, test_data, test_label = \
            mnist(ntrain=6000,ntest=1000,digit_range=[0,10])
    # initialize learning rate and num_iterations
    learning_rate = 0.1
    num_iterations = 1000

    costs, parameters = multi_layer_network(train_data, train_label, net_dims, \
            num_iterations=num_iterations, learning_rate=learning_rate,decay_rate=0.001)

    # compute the accuracy for training set and testing set
    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)

    trAcc = np.sum(train_Pred == train_label) / float(
        train_label.shape[1]) * 100
    teAcc = np.sum(test_Pred == test_label) / float(test_label.shape[1]) * 100
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))

    ### CODE HERE to plot costs
    plt.plot(range(1, len(costs) + 1), costs, label='Cost')
    plt.xlabel('Iterations')
    plt.ylabel('Cost')
    plt.title(
        'Mini Project2:1.2 Multi-layer neural network for multi-class classifier(Three-layer)-Cost vs Iterations'
    )
    plt.legend()
    plt.show()
Exemple #10
0
def main():
    '''
    Trains a multilayer network for MNIST digit classification (all 10 digits)
    To create a network with 1 hidden layer of dimensions 800
    Run the progam as:
        python deepMultiClassNetwork_starter.py "[784,800]"
    The network will have the dimensions [784,800,10]
    784 is the input size of digit images (28pix x 28pix = 784)
    10 is the number of digits

    To create a network with 2 hidden layers of dimensions 800 and 500
    Run the progam as:
        python deepMultiClassNetwork_starter.py "[784,800,500]"
    The network will have the dimensions [784,800,500,10]
    784 is the input size of digit images (28pix x 28pix = 784)
    10 is the number of digits
    '''
    net_dims = ast.literal_eval(sys.argv[1])
    net_dims.append(10)  # Adding the digits layer with dimensionality = 10
    print("Network dimensions are:" + str(net_dims))

    # getting the subset dataset from MNIST
    train_data, train_label, test_data, test_label = \
        mnist(ntrain=6000, ntest=1000, digit_range=[0, 10])
    # initialize learning rate and num_iterations
    learning_rate = 0.8
    num_iterations = 500
    decay_rate = 0.003

    costs, parameters = multi_layer_network(train_data, train_label, net_dims,
        num_iterations=num_iterations, learning_rate=learning_rate, decay_rate=decay_rate)

    # compute the accuracy for training set and testing set
    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)

    trAcc = 100 * np.mean((train_Pred == train_label).astype(int))
    teAcc = 100 * np.mean((test_Pred == test_label).astype(int))
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))

    ### CODE HERE to plot costs
    iterations = range(0, num_iterations, 10)
    plt.plot(iterations, costs)
    plt.title("MultiClass Classifier: " + str(net_dims) +
        "\nTraining accuracy:{0:0.3f}% Testing accuracy:{1:0.3f}%".format(trAcc, teAcc))
    plt.xlabel("Iteration")
    plt.ylabel("Cost")
    plt.show()
def main():
    # getting the subset dataset from MNIST
    # binary classification for digits 2 and 3
    train_data, train_label, test_data, test_label = mnist(ntrain=6000,
                                                           ntest=1000,
                                                           digit_range=[2, 4])

    print train_data.shape
    print train_label.shape
    print test_data.shape
    print test_label.shape

    n_in, m = train_data.shape
    n_fin = 1
    n_h = 500
    net_dims = [n_in, n_h, n_fin]

    # initialize learning rate and num_iterations
    learning_rate = 0.1
    num_iterations = 1000
    # X = train_data
    # X = X - (((X.sum(axis =1))/m).reshape(-1,1))
    # X = X / (((((X**2).sum(axis=1)) / m).reshape(-1, 1)).astype(float))
    # train_data = X
    # print train_data
    # train_data = preprocessing.scale(train_data)
    # print train_data
    costs, parameters = two_layer_network(train_data,
                                          train_label,
                                          net_dims,
                                          num_iterations=num_iterations,
                                          learning_rate=learning_rate)

    # compute the accuracy for training set and testing set
    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)

    trAcc = np.mean((train_Pred == train_label)) * 100
    teAcc = np.mean((test_Pred == test_label)) * 100
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))
    # CODE HERE TO PLOT costs vs iterations
    plt.plot(costs)
    plt.xlabel("iterations")
    plt.ylabel("Error")
    plt.title("Plot of train error vs  iterations")
    plt.show()
Exemple #12
0
def main():
    '''
    Trains a multilayer network for MNIST digit classification (all 10 digits)
    To create a network with 1 hidden layer of dimensions 800
    Run the progam as:
        python deepMultiClassNetwork_starter.py "[784,800]"
    The network will have the dimensions [784,800,10]
    784 is the input size of digit images (28pix x 28pix = 784)
    10 is the number of digits

    To create a network with 2 hidden layers of dimensions 800 and 500
    Run the progam as:
        python deepMultiClassNetwork_starter.py "[784,800,500]"
    The network will have the dimensions [784,800,500,10]
    784 is the input size of digit images (28pix x 28pix = 784)
    10 is the number of digits
    '''
    net_dims = ast.literal_eval(sys.argv[1])
    net_dims.append(10)  # Adding the digits layer with dimensionality = 10
    print("Network dimensions are:" + str(net_dims))

    # getting the subset dataset from MNIST
    train_data, train_label, test_data, test_label = \
            mnist(ntrain=6000,ntest=1000,digit_range=[0,10])
    # initialize learning rate and num_iterations
    learning_rate = 0.2
    num_iterations = 500

    costs, parameters = multi_layer_network(train_data, train_label, net_dims, \
            num_iterations=num_iterations, learning_rate= learning_rate)

    # compute the accuracy for training set and testing set
    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)

    trAcc = (1 - np.count_nonzero(train_Pred - train_label) /
             float(train_Pred.shape[1])) * 100
    teAcc = (1 - np.count_nonzero(test_Pred - test_label) /
             float(test_Pred.shape[1])) * 100
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))

    X = range(0, 500, 10)
    plt.plot(X, costs)
    plt.xlabel("Iterations")
    plt.ylabel("Cost")
    plt.show()
Exemple #13
0
def main():
    

    # getting the subset dataset from MNIST
    train_data, train_label, test_data, test_label = \
            mnist(ntrain=30,ntest=10,digit_range=[0,10])
    # initialize learning rate and num_iterations

    
    num_iterations = 6
    
    
    train_data = train_data.reshape(train_data.shape[1], 28,28,1)
    train_data = train_data.astype(np.float32)
    train_data = np.multiply(train_data, 1.0 / 255.0)

    test_data = test_data.reshape(test_data.shape[1], 28,28,1)
    test_data = test_data.astype(np.float32)
    test_data = np.multiply(test_data, 1.0 / 255.0)
    
    
    
    # compute the accuracy for training set and testing set
    W,b=initialize_parameters(filter_size=3,filters=5)
    parameters = cnn(train_data, train_label,W,b,1,1,num_iterations)
    train_error=parameters[1]
    W=parameters[0][0]
    b=parameters[0][1]
    j=sorted(list(map(lambda x: x*-0.001,train_error)))[::-1]
    train_Pred = classify(train_data,W,b,1,1)
    test_Pred = classify(test_data,W,b,1,1)
    #trAcc = (np.abs(train_Pred - train_label) < tolerance ).all().mean()
    #teAcc = (np.abs(test_Pred - test_label) < tolerance ).all().mean()
    trAcc = np.abs(100-(np.mean(np.abs(train_Pred - train_data)))/np.sum(train_data))
    teAcc = np.abs(100-(np.mean(np.abs(test_Pred - test_data)))/np.sum(test_data))
    print("Accuracy for training set is {0:0.3f}".format(trAcc))
    print("Accuracy for testing set is {0:0.3f}".format(teAcc))
    
    
    plt.xlabel("iteration")
    plt.ylabel("error")
    plt.title("error vs iteration")
    plt.plot(range(0,num_iterations),train_error)
    plt.yticks(train_error, j[0:3], rotation='horizontal')
    plt.show()
Exemple #14
0
def main():
   
    net_dims = ast.literal_eval( sys.argv[1] )
    net_dims.append(10) # Adding the digits layer with dimensionality = 10
    print("Network dimensions are:" + str(net_dims))

    
    train_data, train_label, test_data, test_label = \
            mnist(ntrain=6000,ntest=1000,digit_range=[0,10])
    
    learning_rate = 0.2
    num_iterations = 500
    costs, parameters = multi_layer_network(train_data, train_label, net_dims, \
            num_iterations=num_iterations, learning_rate=learning_rate)
    
    

    trAcc = None
    teAcc = None
def main():

    # getting the subset dataset from MNIST
    train_data, train_label, test_data, test_label = \
            mnist(ntrain=100,ntest=10,digit_range=[0,10])
    # initialize learning rate and num_iterations

    num_iterations = 4

    train_data = train_data.reshape(train_data.shape[1], 28, 28, 1)
    train_data = train_data.astype(np.float32)
    train_data = np.multiply(train_data, 1.0 / 255.0)

    test_data = test_data.reshape(test_data.shape[1], 28, 28, 1)
    test_data = test_data.astype(np.float32)
    test_data = np.multiply(test_data, 1.0 / 255.0)

    # compute the accuracy for training set and testing set
    W, b = initialize_parameters(filter_size=3, filters=5)
    parameters, train_error = cnn(train_data, train_label, W, b, 1, 1,
                                  num_iterations)
    W = parameters[0]
    b = parameters[1]
    train_Pred = classify(train_data, W, b, 1, 1)
    test_Pred = classify(test_data, W, b, 1, 1)

    trAcc = np.abs(100 - (np.mean(np.abs(train_Pred - train_data))) /
                   np.sum(train_data))
    teAcc = np.abs(100 - (np.mean(np.abs(test_Pred - test_data))) /
                   np.sum(test_data))
    print("Accuracy for training set is {0:0.3f}".format(trAcc))
    print("Accuracy for testing set is {0:0.3f}".format(teAcc))

    plt.xlabel("iteration")
    plt.ylabel("error")
    plt.title("error vs iteration")
    plt.plot(range(0, num_iterations), train_error)
    plt.show()
Exemple #16
0
import numpy as np
import matplotlib.pyplot as plt
from load_dataset import mnist


def one_hot(y, n):
    y_one_hot = np.zeros((y.shape[1], n))
    for i in range(y.shape[1]):
        y_one_hot[i, int(y[0, i])] = 1
    return y_one_hot


# Loading the data
trainX, trainY, testX, testY = mnist(ntrain=60000,
                                     ntest=10000,
                                     onehot=False,
                                     subset=True,
                                     digit_range=[0, 10],
                                     shuffle=True)
trainX = trainX.T
testX = testX.T

n_input = 28 * 28
n_classes = 10
batch_size = 128
n_epoch = 200
learning_rate = 0.01
'''
As we have multiple classes we first need to perform one hot encoding of our labels
'''
train_Y_one_hot = one_hot(trainY, n_classes)
test_Y_one_hot = one_hot(testY, n_classes)
Exemple #17
0
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from load_dataset import mnist

learning_rate = 0.1
iterations = 400
sparsity = [0.01, 0.1, 0.5, 0.8]
lambd = 0.0001
beta = 3.0

num_of_hidden_units = 200
input_size = 784

input_x, _, _, _ = mnist(ntrain=60000,
                         ntest=10,
                         digit_range=[0, 10],
                         shuffle=True)

fig_n = 1
for r in sparsity:
    print("For sparsity parameter : " + str(r))

    X = tf.placeholder(tf.float32, [input_size, None])
    rho = tf.placeholder(tf.float32)

    weights = dict()
    biases = dict()

    weights["en"] = tf.Variable(
        tf.random_normal([num_of_hidden_units, input_size], stddev=0.01)
    )  #tf.Variable(tf.truncated_normal([num_of_hidden_units, input_size], stddev=0.001))
np.random.seed(0)

learning_rate = 0.1
iterations = 400
lambd = 0.0001
beta = 3.0

input_x_labeled = []
input_y_labeled = []
input_x_unlabeled = []
input_y_unlabeled = []

for i in range(1, 11):
    temp_data, temp_label, _, _ = mnist(ntrain=60000,
                                        ntest=1,
                                        digit_range=[i - 1, i])
    input_x_labeled.extend(temp_data.T[0:100])
    input_y_labeled.extend(temp_label.T[0:100])
    input_x_unlabeled.extend(temp_data.T[100:])
    input_y_unlabeled.extend(temp_label.T[100:])

input_x_labeled, input_y_labeled = shuffle(input_x_labeled,
                                           input_y_labeled,
                                           random_state=10)
input_x_unlabeled, input_y_unlabeled = shuffle(input_x_unlabeled,
                                               input_y_unlabeled,
                                               random_state=10)

input_x_labeled = np.array(input_x_labeled).T
input_y_labeled = np.array(input_y_labeled).T
Exemple #19
0
def main():
    '''
    Trains a sparse autoencoder for MNIST digit data
    '''
    net_dims = [784, 200, 784]
    print("Network dimensions are:" + str(net_dims))

    # getting the subset dataset from MNIST
    train_data, train_label, test_data, test_label = \
        mnist(ntrain=1000, ntest=200, digit_range=[0, 10])
    train_label = train_label.astype(int)
    test_label = test_label.astype(int)
    # initialize learning rate and num_iterations

    num_iterations = 400
    decay_rate = 0
    weight_decay = 0.001
    ps = [0.01, 0.1, 0.5, 0.8]
    p = ps[1]
    learning_rate = 0.3

    # train sparse autoencoder
    print("Training sparse autoencoder...")
    costs, parameters = train_sparse_autoencoder(train_data,
                                                 train_data,
                                                 net_dims,
                                                 num_iterations=num_iterations,
                                                 learning_rate=learning_rate,
                                                 decay_rate=decay_rate,
                                                 weight_decay=weight_decay,
                                                 p=p)

    train_Pred = classify(train_data, parameters)
    test_Pred = classify(test_data, parameters)

    # compute the accuracy for training set and testing set
    trAcc = 100 * (1 - np.mean(np.abs(train_Pred - train_data)))
    teAcc = 100 * (1 - np.mean(np.abs(test_Pred - test_data)))

    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))

    # train classifier with W1 an b1 from autoencoder
    net_dims = [200, 10]
    learning_rate = 0.8
    decay_rate = 0.003
    print("Semi-supervised learning...")
    costs, parameters = semi_supervised_learning(train_data,
                                                 train_label,
                                                 net_dims,
                                                 parameters,
                                                 num_iterations=num_iterations,
                                                 learning_rate=learning_rate,
                                                 decay_rate=decay_rate)

    train_Pred = softmax_classify(train_data, parameters)
    test_Pred = softmax_classify(test_data, parameters)
    trAcc = 100 * np.mean((train_Pred == train_label).astype(int))
    teAcc = 100 * np.mean((test_Pred == test_label).astype(int))
    net_dims = [784, 200, 10]

    ### CODE HERE to plot costs
    iterations = range(0, num_iterations, 10)
    plt.plot(iterations, costs)
    plt.title("Sparse autoencoder network: " + str(net_dims) + " (p = " +
              str(p) +
              ")\nTraining accuracy:{0:0.3f}% Testing accuracy:{1:0.3f}%".
              format(trAcc, teAcc))
    plt.xlabel("Iteration")
    plt.ylabel("Cost")
    plt.show()

    # train softmax classfier
    net_dims = [784, 200, 10]
    learning_rate = 0.8
    print("Training softmax classfier...")
    costs, parameters = multi_layer_network(train_data,
                                            train_label,
                                            net_dims,
                                            num_iterations=num_iterations,
                                            learning_rate=learning_rate,
                                            decay_rate=decay_rate)

    train_Pred = softmax_classify(train_data, parameters)
    test_Pred = softmax_classify(test_data, parameters)
    trAcc = 100 * np.mean((train_Pred == train_label).astype(int))
    teAcc = 100 * np.mean((test_Pred == test_label).astype(int))
    print("Accuracy for training set is {0:0.3f} %".format(trAcc))
    print("Accuracy for testing set is {0:0.3f} %".format(teAcc))

    ### CODE HERE to plot costs
    iterations = range(0, num_iterations, 10)
    plt.plot(iterations, costs)
    plt.title("Regular neuron network: " + str(net_dims) +
              ")\nTraining accuracy:{0:0.3f}% Testing accuracy:{1:0.3f}%".
              format(trAcc, teAcc))
    plt.xlabel("Iteration")
    plt.ylabel("Cost")
    plt.show()
Exemple #20
0
def nnArc(layers, activation_function, netNum):
    '''
    Loading the data set.
    Here 
    trainX : trainging data
    trainY : training labels
    testX : testing data
    testY : testing lables
    '''
    trainX, trainY, testX, testY = mnist(ntrain=60000,
                                         ntest=10000,
                                         onehot=False,
                                         subset=True,
                                         digit_range=[0, 10],
                                         shuffle=True)  # Loading the data
    trainX = trainX.T
    testX = testX.T

    n_input = 28 * 28  #Number of input neurons
    n_classes = 10  # Number of classes.
    batch_size = 128
    n_epoch = 200  # Number of iteratios we will perform.
    learning_rate = 0.01
    '''
    As we have multiple classes we first need to perform one hot encoding of our labels
    '''
    train_Y_one_hot = one_hot(trainY, n_classes)
    test_Y_one_hot = one_hot(testY, n_classes)

    features = tf.placeholder(dtype=tf.float32,
                              shape=[None, n_input
                                     ])  # placeholder for the input features
    labels = tf.placeholder(dtype=tf.float32,
                            shape=[None,
                                   n_classes])  # placeholder for the labels
    '''
    The line below will generate a hidden layers which has features as it's input, 128 neurons and sigmoid as the 
    activation function.
    '''
    hidden_layers = list()

    for i, layer in enumerate(layers):
        hidden_layer = tf.contrib.layers.fully_connected(
            features if i == 0 else hidden_layers[i - 1],
            128,
            activation_fn=activation_function)
        hidden_layers.append(hidden_layer)
    '''
    Define your Hidden Layers Here. Your last hidden layer should have varianle name "prev_hidden_layer"
    '''
    lay = hidden_layers.pop()

    logits = tf.contrib.layers.fully_connected(
        lay, n_classes, activation_fn=None)  # Defining the final layer.

    softmax_op = tf.nn.softmax(logits)  # Calculating the softmax activation.
    preds = tf.argmax(softmax_op, axis=1)  # Computing the final predictions.
    '''
    The line below calculates the cross entropy loss for mutliclass predictions.
    '''
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits))

    correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(
        labels,
        1))  #Comparing network predictons with the actual class labels.
    accuracy = tf.reduce_mean(
        tf.cast(correct_prediction, tf.float32)
    )  # Computing the accuracy ( How many correct prediction / Total predictions to make)

    optimizer = tf.train.RMSPropOptimizer(learning_rate)
    '''
    This operations does all the important work from calculating the gradients to updating the parameters.
    '''

    train_step = optimizer.minimize(loss)

    train_losses = []
    test_losses = []

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for ii in range(n_epoch):
            for j in range(trainX.shape[1] // batch_size):
                batch_x = trainX[batch_size * j:batch_size * j + batch_size, :]
                batch_y = train_Y_one_hot[batch_size * j:batch_size * j +
                                          batch_size, :]
                sess.run(train_step,
                         feed_dict={
                             features: batch_x,
                             labels: batch_y
                         })
            if ii % 10 == 0:
                '''
                For Every 10th epoch get the training loss and tesing loss and store them.
                You do it for all the the data points in your training and testing sets, not for batches.
                '''
                train_loss = sess.run(loss,
                                      feed_dict={
                                          features: trainX,
                                          labels: train_Y_one_hot
                                      })
                train_acc = sess.run(accuracy,
                                     feed_dict={
                                         features: trainX,
                                         labels: train_Y_one_hot
                                     })
                test_loss = sess.run(loss,
                                     feed_dict={
                                         features: testX,
                                         labels: test_Y_one_hot
                                     })
                print('Epoch : {}, Training Loss : {}, Training Accuracy : {}'.
                      format(ii, train_loss, train_acc))
                train_losses.append(train_loss)
                test_losses.append(test_loss)

        test_accuracy = sess.run(
            accuracy, feed_dict={
                features: testX,
                labels: test_Y_one_hot
            }
        )  # Get the test accuracy by evaluating the accuracy tensor with test data and test labels.
    '''
    The following code generates the plot for epochs vs training loss and epoch vs testing loss.
    You will need to note the test accuracy and generate a plot for architecture vs test accuracy. 
    '''
    print('Testing accuracy : {}'.format(test_accuracy))

    X_axis = range(1, n_epoch + 1, 10)

    plt.title('NN#{} ({})'.format(netNum, activation_function.__name__))
    plt.plot(X_axis, train_losses, "-", color="blue")
    plt.plot(X_axis, test_losses, "--", color="red")
    plt.legend(["Training Loss", "Testing Loss"])

    plt.xlabel('Epochs')
    plt.ylabel('Loss')

    plt.show()

    return (test_accuracy)