示例#1
0
import util_mnist_reader


def relu(z):
    return np.maximum(z, 0)


if __name__ == '__main__':
    print("Part 1: Neural Network")
    fashion_path = "C:\\Users\peter\Documents\Peter\\7-College\\7-Fall-2019\CSE474\Projects\project2\data\\fashion"

    n0 = 785
    n1 = 392
    n2 = 10

    x_train, y_train = util_mnist_reader.load_mnist(fashion_path, kind='train')
    x_test, y_test = util_mnist_reader.load_mnist(fashion_path, kind='t10k')

    ns = x_train.shape[0]  # number of samples in input

    x_train = (x_train.astype("float32") / 255.0).reshape((60000, 784))
    x_test = (x_test.astype("float32") / 255.0).reshape((10000, 784))

    # Create an array for loss calculation with 1 for correct label, 0 otherwise
    y = np.zeros((ns, 10))
    for i in range(0, ns):
        y[i][int(y_train[i])] = 1
    y = y.T

    ones_arr = np.ones((x_train.shape[0], 1))
    a0 = np.hstack((x_train, ones_arr)).T
示例#2
0
#Read Fashion MNIST dataset

import util_mnist_reader
X_train, Y_train = util_mnist_reader.load_mnist('../data/fashion',
                                                kind='train')
X_test, Y_test = util_mnist_reader.load_mnist('../data/fashion', kind='t10k')

# Your code goes here . . .
#One layer Neural network
#-------------------------------------------------------------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import sklearn.metrics as metrics
from sklearn.model_selection import train_test_split

X_test, X_val, Y_test, Y_val = train_test_split(X_test,
                                                Y_test,
                                                test_size=0.5,
                                                random_state=51)

X_train = X_train / 255.0
X_test = X_test / 255.0

y_train = tf.keras.utils.to_categorical(Y_train, 10)
y_test = tf.keras.utils.to_categorical(Y_test, 10)
y_val = tf.keras.utils.to_categorical(Y_val, 10)

#Hyperparameters
m = 60000
input_nodes = 784

# function to print shape
def print_shape(matrix=None, name=None):
    if type(matrix) is list:
        for m in range(len(matrix)):
            print("Shape of {}:        \t".format(name[m]), matrix[m].shape)
    else:
        print("Shape of {}:        \t".format(name), matrix.shape)

    print(
        "------------------------------------------------------------------------"
    )


X_train, Y_train = mnist_reader.load_mnist('data/fashion', kind='train')
X_test, Y_test = mnist_reader.load_mnist('data/fashion', kind='t10k')

X_train, X_valid = X_train[5000:], X_train[:5000]
Y_train, Y_valid = Y_train[5000:], Y_train[:5000]

print("Y_train", Y_train.shape)
print("Y_valid", Y_valid.shape)
print("Y_test", Y_test.shape)

# normalize the data
X_train = X_train / 255
X_valid = X_valid / 255
X_test = X_test / 255
#one-hot encode target column
Y_train = to_categorical(Y_train)
def function_part_1():
    #loading the data
    X_train, y_train = mnist_reader.load_mnist('data/fashion', kind='train')
    X_test, y_test = mnist_reader.load_mnist('data/fashion', kind='t10k')

    #spliting the test data into test data and val data
    X_test, X_val, y_test, y_val = train_test_split(X_test,
                                                    y_test,
                                                    test_size=0.5,
                                                    shuffle=False)

    #normalize the data
    X_train = X_train / 255
    X_test = X_test / 255
    X_val = X_val / 255

    digits = 10  # no of classes
    examples = y_train.shape[0]

    y_train = y_train.reshape(1, examples)

    Y_new = np.eye(digits)[y_train.astype('int32')]
    y_train = Y_new.T.reshape(digits, examples)

    examples_val = y_val.shape[0]
    y_val = y_val.reshape(1, examples_val)

    Y_val_new = np.eye(digits)[y_val.astype('int32')]
    y_val = Y_val_new.T.reshape(digits, examples_val)

    m = 60000
    n_x = X_train.shape[1]  # 784

    n_h = 64  # no_of_nodes
    learning_rate = 1

    W1 = np.random.randn(n_h, n_x)
    b1 = np.zeros((n_h, 1))
    W2 = np.random.randn(10, n_h)
    b2 = np.zeros((10, 1))

    X = X_train
    Y = y_train

    loss_list = []
    loss_val_list = []

    for i in range(1500):

        #training dataset

        Z1 = np.matmul(W1, X.T) + b1  # (64, 60000)
        A1 = sigmoid(Z1)
        Z2 = np.matmul(W2, A1) + b2
        A2 = softmax_function(Z2)

        cost = calc_loss(Y, A2)
        loss_list.append(cost)

        #validation dataset

        Z1_val = np.matmul(W1, X_val.T) + b1  # (64, 60000)
        A1_val = sigmoid(Z1_val)
        Z2_val = np.matmul(W2, A1_val) + b2
        A2_val = softmax_function(Z2_val)

        cost_val = calc_loss(y_val, A2_val)
        loss_val_list.append(cost_val)

        dZ2 = A2 - Y
        dW2 = (1. / m) * np.matmul(dZ2, A1.T)
        db2 = (1. / m) * np.sum(dZ2, axis=1, keepdims=True)

        dA1 = np.matmul(W2.T, dZ2)
        dZ1 = dA1 * sigmoid(Z1) * (1 - sigmoid(Z1))
        dW1 = (1. / m) * np.matmul(dZ1, X)
        db1 = (1. / m) * np.sum(dZ1, axis=1, keepdims=True)

        W2 = W2 - learning_rate * dW2
        b2 = b2 - learning_rate * db2
        W1 = W1 - learning_rate * dW1
        b1 = b1 - learning_rate * db1

        if i % 100 == 0:
            print("Epoch", i, "cost: ", cost)

    print("Final cost:", cost)

    #plotting the data
    #%matplotlib inline
    get_ipython().run_line_magic('matplotlib', 'inline')
    plt.plot(loss_list)
    plt.plot(loss_val_list)
    plt.xlabel("Epochs")
    plt.ylabel("Loss")
    plt.title('Loss versus Epoch graph for Single layer neural network')
    plt.legend(["Training set", "Validation set"])

    plt.show()

    Z1 = np.matmul(W1, X_test.T) + b1
    A1 = sigmoid(Z1)
    Z2 = np.matmul(W2, A1) + b2
    A2 = softmax_function(Z2)

    predictions = np.argmax(A2, axis=0)
    # labels = np.argmax(y_test, axis=0)
    print(confusion_matrix(predictions, y_test))
    print(classification_report(predictions, y_test))
def function_part2():

    #data loading
    X_train, y_train = mnist_reader.load_mnist('data/fashion', kind='train')
    X_test, y_test = mnist_reader.load_mnist('data/fashion', kind='t10k')
    X_test, X_val, y_test, y_val = train_test_split(X_test,
                                                    y_test,
                                                    test_size=0.5,
                                                    shuffle=False)

    Y = y_test

    #convert data to one_hot representation
    y_train = tf.keras.utils.to_categorical(y_train)
    y_test = tf.keras.utils.to_categorical(y_test)
    y_val = tf.keras.utils.to_categorical(y_val)

    class_names = [
        'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal',
        'Shirt', 'Sneaker', 'Bag', 'Ankle boot'
    ]

    #reshaping the dataset
    X_train = X_train.reshape(60000, 28, 28)
    X_val = X_val.reshape(5000, 28, 28)
    X_test = X_test.reshape(5000, 28, 28)

    #normalizing the datset to get it in a range of 0 and 1
    X_train = X_train / 255.0
    X_test = X_test / 255.0
    X_val = X_val / 255.0

    #defining the model
    model = keras.Sequential([
        keras.layers.Flatten(input_shape=(28, 28)),
        keras.layers.Dense(198, activation='sigmoid'),
        keras.layers.Dense(88, activation='relu'),
        keras.layers.Dense(10, activation='softmax')
    ])

    #Model configuration
    model.compile(optimizer='SGD',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    train_loss = model.fit(X_train,
                           y_train,
                           validation_data=(X_val, y_val),
                           epochs=80)

    train_loss_list = train_loss.history['loss']
    val_loss_list = train_loss.history['val_loss']
    accuracy_list = train_loss.history['accuracy']
    val_accuracy_list = train_loss.history['val_accuracy']

    #plotting the loss versus accuracy graph
    get_ipython().run_line_magic('matplotlib', 'inline')
    plt.plot(train_loss_list)
    plt.plot(val_loss_list)
    plt.xlabel("Epochs")
    plt.ylabel("Loss")
    plt.title('Loss versus Epoch graph for Multi layer neural network')
    plt.legend(["Training set", "Validation set"])

    plt.show()

    test_loss, test_acc = model.evaluate(X_test, y_test, verbose=1)

    print('\nTest accuracy:', test_acc)

    predictions = model.predict(X_test)
    np_predictions = np.argmax(predictions, axis=1)
    print(confusion_matrix(np_predictions, Y))
    print(classification_report(np_predictions, Y))
def function_part3():
    #load data
    X_train, y_train = mnist_reader.load_mnist('data/fashion', kind='train')
    X_test, y_test = mnist_reader.load_mnist('data/fashion', kind='t10k')
    X_test, X_val, y_test, y_val = train_test_split(X_test,
                                                    y_test,
                                                    test_size=0.5,
                                                    shuffle=False)

    Y = y_test

    #reshape data
    X_train = X_train.reshape((X_train.shape[0], 28, 28, 1))
    X_test = X_test.reshape((X_test.shape[0], 28, 28, 1))
    X_val = X_val.reshape((X_val.shape[0], 28, 28, 1))

    y_train = to_categorical(y_train)
    y_test = to_categorical(y_test)
    y_val = to_categorical(y_val)

    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    X_val = X_val.astype('float32')

    # normalize to range 0-1
    X_train = X_train / 255.0
    X_test = X_test / 255.0
    X_val = X_val / 255.0

    #defining the model
    model = Sequential()
    model.add(
        Conv2D(32, (3, 3),
               activation='relu',
               kernel_initializer='he_uniform',
               input_shape=(28, 28, 1)))
    model.add(MaxPooling2D((2, 2)))
    model.add(Flatten())
    model.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))
    model.add(Dropout(0, 2))
    model.add(Dense(10, activation='softmax'))

    #Model configuration
    opt = SGD(lr=0.01, momentum=0.9)
    model.compile(optimizer=opt,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    #Model training
    nepochs = 5
    history = model.fit(X_train,
                        y_train,
                        epochs=nepochs,
                        batch_size=36,
                        validation_data=(X_val, y_val))

    # evaluate model
    test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=2)

    #plotting the loss versus accuracy graph
    get_ipython().run_line_magic('matplotlib', 'inline')
    plt.xlabel("Epochs")
    plt.ylabel("Loss")
    plt.title('Loss versus Epoch graph for convolutional neural network')
    plt.legend(["Training set", "Validation set"])
    plt.plot(range(nepochs), history.history['loss'], 'y', range(nepochs),
             history.history['val_loss'], 'g')

    plt.show()

    predictions = model.predict(X_test)
    np_predictions = np.argmax(predictions, axis=1)
    print(confusion_matrix(np_predictions, Y))
    print(classification_report(np_predictions, Y))