Part A - Tensorflow and Low Level API
(i) Two Layer Classifier
In [0]:
import tensorflow as tf
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
In [2]:
import numpy as np
train_dress_images = train_images[train_labels == 3]
train_bag_images = train_images[train_labels == 8]
train_dress_bag_images = np.append(train_dress_images, train_bag_images, axis=0)
train_dress_labels = np.zeros((len(train_dress_images),), dtype=float)
train_bag_labels = np.ones((len(train_bag_images),), dtype=float)
train_dress_bag_labels = np.append(train_dress_labels, train_bag_labels, axis=0)
test_dress_images = test_images[test_labels == 3]
test_bag_images = test_images[test_labels == 8]
test_dress_bag_images = np.append(test_dress_images, test_bag_images, axis=0)
test_dress_labels = np.zeros((len(test_dress_images),), dtype=float)
test_bag_labels = np.ones((len(test_bag_images),), dtype=float)
test_dress_bag_labels = np.append(test_dress_labels, test_bag_labels, axis=0)
print(len(train_images))
print(len(train_dress_bag_images))
print(len(train_dress_images))
print(train_dress_bag_labels)
# Reshape training dataset so that the features are flattened
train_dress_bag_images = train_dress_bag_images.reshape(
train_dress_bag_images.shape[0], -1).astype('float32')
test_dress_bag_images = test_dress_bag_images.reshape(
test_dress_bag_images.shape[0], -1).astype('float32')
train_dress_bag_images = train_dress_bag_images.T
test_dress_bag_images = test_dress_bag_images.T
train_dress_bag_labels = np.reshape(train_dress_bag_labels, (-1,1)).T
test_dress_bag_labels = np.reshape(test_dress_bag_labels, (-1,1)).T
print(test_dress_bag_labels.shape)
In [0]:
num_Iterations= 1000
display_step= 100
# Model Params
n_inputs = 784
n_hidden = 100
n_outputs=1
learning_rate= 0.01
n_epochs = 40
#training label data
x = tf.placeholder(tf.float32, [n_inputs,None])
y_ = tf.placeholder(tf.float32, [1,None])
In [4]:
# Weight Bias for each layer
W1 = tf.get_variable("W1", [n_hidden, n_inputs], initializer =
tf.glorot_uniform_initializer(seed=1))
B1 = tf.get_variable("b1", [n_hidden, 1], initializer = tf.zeros_initializer())
In [0]:
W2 = tf.get_variable("W2", [n_outputs, n_hidden],
initializer = tf.glorot_uniform_initializer(seed=1))
B2 = tf.get_variable("b2", [n_outputs, 1],
initializer = tf.zeros_initializer())
In [0]:
A1 = tf.add(tf.matmul(W1, x), B1)
H1 = tf.nn.relu(A1)
W2_T = tf.transpose(W2)
y_pred = tf.add(tf.matmul(W2, H1),B2)
y_pred_sigmoid = tf.sigmoid(y_pred)
x_entropy = tf.nn.sigmoid_cross_entropy_with_logits(logits=y_pred, labels=y_)
loss = tf.reduce_mean(x_entropy)
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
predictions = tf.round(y_pred_sigmoid)
predictions_correct = tf.cast(tf.equal(predictions, y_), tf.float32)
accuracy = tf.reduce_mean(predictions_correct)
In [7]:
print(train_dress_bag_images.dtype)
In [8]:
#Start training
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(n_epochs):
_, currentLoss, acc = sess.run([train_step, loss, accuracy],
feed_dict={x: train_dress_bag_images,
y_:train_dress_bag_labels})
print (currentLoss, " ", acc)
print ("Final Validation Accuracy ", sess.run(accuracy,
feed_dict={
x: test_dress_bag_images,
y_: test_dress_bag_labels}))
(ii) Multi Class Classification
In [9]:
X_train = train_images.reshape(train_images.shape[0], -1).astype('float32')
X_test = test_images.reshape(test_images.shape[0], -1).astype('float32')
# Normalize training data
X_train = X_train / 255.0
X_test = X_test / 255.0
X_train = X_train.T
X_test = X_test.T
# Convert labels to one-hot-encoded
number_of_classes = 10
Y_train = tf.keras.utils.to_categorical(train_labels, number_of_classes)
Y_test = tf.keras.utils.to_categorical(test_labels, number_of_classes)
# Transpose train labels so that dimensions
# are number of classes * number of instances
Y_train = Y_train.T
Y_test = Y_test.T
print ("Data extracted and reshaped: ")
print (X_train.shape, Y_train.shape, X_test.shape, Y_test.shape)
print (X_train.dtype, Y_train.dtype, X_test.dtype, Y_test.dtype)
In [50]:
import time
# Model Params
n_inputs = 784
n_layer_1 = 300
n_layer_2 = 100
n_outputs=10
learning_rate= 0.01
n_epochs = 40
error_values= list()
acc_values= list()
epoch_times= list()
tf.reset_default_graph()
with tf.name_scope("multi_class"):
#training label data
x_ = tf.placeholder(tf.float32, [n_inputs,None], name="x_")
y = tf.placeholder(tf.float32, [10,None], name="y")
WL1 = tf.get_variable("WL1", [n_layer_1, n_inputs],initializer =
tf.glorot_uniform_initializer(seed=1) )
bL1 = tf.get_variable("bL1", [n_layer_1, 1], initializer =
tf.zeros_initializer())
WL2 = tf.get_variable("WL2", [n_layer_2, n_layer_1],initializer =
tf.glorot_uniform_initializer(seed=1) )
bL2 = tf.get_variable("bL2", [n_layer_2, 1], initializer =
tf.zeros_initializer())
WL3 = tf.get_variable("WL3", [n_outputs, n_layer_2],initializer =
tf.glorot_uniform_initializer(seed=1) )
bL3 = tf.get_variable("bL3", [n_outputs, 1], initializer =
tf.zeros_initializer())
A1 = tf.add(tf.matmul(WL1, x_), bL1)
H1 = tf.nn.relu(A1)
A2 = tf.add(tf.matmul(WL2, H1), bL2)
H2 = tf.nn.relu(A2)
A3 = tf.add(tf.matmul(WL3, H2), bL3)
logits = tf.transpose(A3)
labels = tf.transpose(y)
error = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,
labels=labels)
loss = tf.reduce_mean(error)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
correct_prediction = tf.equal(tf.argmax(A3), tf.argmax(y))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
with tf.Session() as sess1:
sess1.run(tf.global_variables_initializer())
for epoch in range(n_epochs):
start_time = time.time()
_, currentLoss, acc = sess1.run([optimizer, loss, accuracy],
feed_dict={x_:X_train, y:Y_train})
epoch_times.append(time.time() - start_time)
print (currentLoss, " ", acc)
error_values.append(currentLoss)
acc_values.append(acc)
print ("Final Validation Accuracy ", sess1.run(accuracy,
feed_dict={x_:X_test,
y:Y_test}))
In [51]:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("whitegrid")
plt.title('CEE and Accuracy')
plt.plot(error_values, label="Cross Entropy Error")
plt.plot(acc_values, label="Accuracy")
plt.xlabel('Epoch')
plt.legend()
plt.show()
plt.title('Epoch Times')
plt.plot(epoch_times)
plt.ylabel('Completion Time')
plt.xlabel('Epoch')
plt.show()
In [74]:
import random as rand
# Model Params
n_inputs = 784
n_layer_1 = 300
n_layer_2 = 100
n_outputs=10
learning_rate= 0.01
n_epochs = 40
error_values= list()
acc_values= list()
epoch_times= list()
mini_batch_size = 128
tf.reset_default_graph()
def create_batches(mini_batch_size, x, y):
x_T = x.T
y_T = y.T
indices = list(range(x_T.shape[1]))
# I dont know if I should shuffle the indicies or not, I dont think each batch
# should be the same regardless of the epoch. Each epoch should split the
# training data into seperate batches
rand.shuffle(indices)
splits = list()
splits = np.array_split(indices, mini_batch_size)
X_batch = list()
Y_batch = list()
for split in splits:
X_batch.append(x_T[split].T)
Y_batch.append(y_T[split].T)
return X_batch, Y_batch
with tf.name_scope("mini_batch"):
#training label data
x_ = tf.placeholder(tf.float32, [n_inputs,None], name="x_")
y = tf.placeholder(tf.float32, [10,None], name="y")
WL1 = tf.get_variable("WL1", [n_layer_1, n_inputs],initializer =
tf.glorot_uniform_initializer(seed=1) )
bL1 = tf.get_variable("bL1", [n_layer_1, 1], initializer =
tf.zeros_initializer())
WL2 = tf.get_variable("WL2", [n_layer_2, n_layer_1],initializer =
tf.glorot_uniform_initializer(seed=1) )
bL2 = tf.get_variable("bL2", [n_layer_2, 1], initializer =
tf.zeros_initializer())
WL3 = tf.get_variable("WL3", [n_outputs, n_layer_2],initializer =
tf.glorot_uniform_initializer(seed=1) )
bL3 = tf.get_variable("bL3", [n_outputs, 1], initializer =
tf.zeros_initializer())
A1 = tf.add(tf.matmul(WL1, x_), bL1)
H1 = tf.nn.relu(A1)
A2 = tf.add(tf.matmul(WL2, H1), bL2)
H2 = tf.nn.relu(A2)
A3 = tf.add(tf.matmul(WL3, H2), bL3)
logits = tf.transpose(A3)
labels = tf.transpose(y)
error = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,
labels=labels)
loss = tf.reduce_mean(error)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
correct_prediction = tf.equal(tf.argmax(A3), tf.argmax(y))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
with tf.Session() as sess1:
sess1.run(tf.global_variables_initializer())
for epoch in range(n_epochs):
start_time = time.time()
X_batches, y_batches = create_batches(mini_batch_size, X_train, Y_train)
epoch_avg_currentLoss = 0
epoch_avg_acc = 0
for i in range(len(X_batches)):
_, currentLoss, acc = sess1.run([optimizer, loss, accuracy],feed_dict={x_:X_batches[i], y:y_batches[i]})
error_values.append(currentLoss)
acc_values.append(acc)
epoch_times.append(time.time() - start_time)
# I know this adds to the run time but its just so I can compare results
# to the non minibatch version
# currentLoss, acc = sess1.run([loss, accuracy],feed_dict={x_:X_train, y:Y_train})
print(currentLoss, " ", acc, " " ,epoch_times[-1])
print ("Final Validation Accuracy ", sess1.run(accuracy,
feed_dict={x_:X_test,
y:Y_test}))
In [66]:
import matplotlib.pyplot as plt
plt.title('CEE and Accuracy')
plt.plot(error_values, label="Cross Entropy Error")
plt.plot(acc_values, label="Accuracy")
plt.xlabel('Session Run')
plt.legend()
plt.show()
plt.title('Epoch Times')
plt.plot(epoch_times)
plt.ylabel('Completion Time')
plt.xlabel('Epoch')
plt.show()
Part B
In [54]:
from google.colab import drive
drive.mount('/content/gdrive')
In [56]:
!unzip "/content/gdrive/My Drive/Colab Notebooks/data.zip"
In [60]:
import h5py
def loadData():
with h5py.File('data.h5','r') as hf:
print('List of arrays in this file: \n', hf.keys())
allTrain = hf.get('trainData')
allTest = hf.get('testData')
npTrain = np.array(allTrain)
npTest = np.array(allTest)
print('Shape of the array dataset_1: \n', npTrain.shape)
print('Shape of the array dataset_2: \n', npTest.shape)
return npTrain[:,:-1], npTrain[:, -1], npTest[:,:-1], npTest[:, -1]
In [81]:
trainX, trainY, testX, testY = loadData()
In [88]:
# Normalize training data
X_train = trainX / 255.0
X_test = testX / 255.0
number_of_classes = 10
Y_train = trainY
Y_test = testY
print ("Data extracted and reshaped: ")
print (X_train.shape, Y_train.shape, X_test.shape, Y_test.shape)
print (X_train.dtype, Y_train.dtype, X_test.dtype, Y_test.dtype)
In [95]:
# Model Params
n_inputs = 784
n_epochs = 10
n_layer_1 = 200
n_softmax = 10
n_batch_size = 256
s_optimizer = 'adam'
s_loss= 'sparse_categorical_crossentropy'
a_metrics = ['accuracy']
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(n_layer_1, activation=tf.nn.relu, input_shape=(n_inputs,)),
tf.keras.layers.Dense(n_softmax, activation=tf.nn.softmax)])
model.compile(optimizer=s_optimizer, loss=s_loss,
metrics=a_metrics)
model.fit(X_train, Y_train, epochs=n_epochs, batch_size=n_batch_size)
results = model.evaluate(X_test, Y_test)
print(results)