Assignment 2.4.1 Multiclass Classification
Assignment 2.4.1 Multiclass Classification
1 Multiclass Classification
# ATTENTION: Please do not alter any of the provided code in the exercise. Only add your own code where
indicated
# ATTENTION: Please do not add or remove any cells in the exercise. The grader will check specific cells
based on the cell position.
# ATTENTION: Please use the provided epoch values when training.
import csv
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from os import getcwd
In [12]:
def get_data(filename):
# You will need to write code that will read the file passed
# into this function. The first line contains the column headers
# so you should ignore it
# Each successive line contians 785 comma separated values between 0 and 255
# The first value is the label
# The rest are the pixel values for that picture
# The function will return 2 np.array types. One with all the labels
# One with all the images
#
# Tips:
# If you read a full line (as 'row') then row[0] has the label
# and row[1:785] has the 784 pixel values
# Take a look at np.array_split to turn the 784 pixels into 28x28
# You are reading in strings, but need the values to be floats
# Check out np.array().astype for a conversion
with open(filename) as training_file:
# Your code starts here
first_line = True
temp_labels = []
temp_images = []
csv_reader = csv.reader(training_file, delimiter = ',')
for row in csv_reader:
if first_line:
first_line = False
else:
temp_labels.append(row[0])
temp_images.append(np.array_split(row[1:],28))
labels = np.array(temp_labels).astype('float')
images = np.array(temp_images).astype('float')
path_sign_mnist_train = f"{getcwd()}/../tmp2/sign_mnist_train.csv"
path_sign_mnist_test = f"{getcwd()}/../tmp2/sign_mnist_test.csv"
training_images, training_labels = get_data(path_sign_mnist_train)
testing_images, testing_labels = get_data(path_sign_mnist_test)
# Keep these
print(training_images.shape)
print(training_labels.shape)
print(testing_images.shape)
print(testing_labels.shape)
# In this section you will have to add another dimension to the data
# So, for example, if your array is (10000, 28, 28)
# You will need to make it (10000, 28, 28, 1)
# Hint: np.expand_dims
validation_generator = validation_datagen.flow(testing_images,
testing_labels,
batch_size = 20)
# Keep These
print(training_images.shape)
print(testing_images.shape)
# Compile Model.
model.compile(loss = 'sparse_categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])# Your Code
Here)
# Plot the chart for accuracy and loss on both training and validation
%matplotlib inline
import matplotlib.pyplot as plt
acc = history.history['accuracy']# Your Code Here
val_acc = history.history['val_accuracy'] # Your Code Here
loss = history.history['loss'] # Your Code Here
val_loss = history.history['val_loss']# Your Code Here
epochs = range(len(acc))
plt.show()
Submission Instructions
In [ ]:
%%javascript
<!-- Save the notebook -->
IPython.notebook.save_checkpoint();
In [ ]:
%%javascript
IPython.notebook.session.delete();
window.onbeforeunload = null
setTimeout(function() { window.close(); }, 1000);