Tensorflow Neural Network Lab: Notmnist
Tensorflow Neural Network Lab: Notmnist
In this lab, you'll use all the tools you learned from Introduction to TensorFlow to label images of English
letters! The data you are using, notMNIST, consists of images of a letter from A to J in different fonts.
The above images are a few examples of the data you'll be training on. After training the network, you will
compare your prediction model against test data. Your goal, by the end of this lab, is to make predictions
against that test set with at least an 80% accuracy. Let's jump in!
To start this lab, you first need to import all the necessary modules. Run the code below. If it runs
successfully, it will print "All modules imported".
import os
import pickle
import numpy as np
The notMNIST dataset is too large for many computers to handle. It contains 500,000 images for just
training. You'll be using a subset of this data, 15,000 images for each label (A-J).
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 1/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
"""
"""
if not os.path.isfile(file):
urlretrieve(url, file)
print('Download Finished')
download('https://s3.amazonaws.com/udacity-sdc/notMNIST_train.zip',
'notMNIST_train.zip')
download('https://s3.amazonaws.com/udacity-sdc/notMNIST_test.zip',
'notMNIST_test.zip')
# Wait until you see that all files have been downloaded.
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 2/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
"""
"""
features = []
labels = []
# Progress Bar
if not filename.endswith('/'):
image = Image.open(image_file)
image.load()
label = os.path.split(filename)[1][0]
features.append(feature)
labels.append(label)
docker_size_limit = 150000
# Set flags for feature engineering. This will prevent you from ski
pping an important step.
is_features_normal = False
is_labels_encod = False
# Wait until you see that all features and labels have been uncompre
ssed.
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 3/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
Problem 1
The first problem involves normalizing the features for your training and test data.
Implement Min-Max scaling in the normalize_grayscale() function to a range of a=0.1 and b=0.9.
After scaling, the values of the pixels in the input data should range from 0.1 to 0.9.
Since the raw notMNIST image data is in grayscale, the current values range from a min of 0 to a max of
255.
Min-Max Scaling:
If you're having trouble solving problem 1, you can view the solution here.
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 4/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
def normalize_grayscale(image_data):
"""
"""
# Test Cases
np.testing.assert_array_almost_equal(
normalize_grayscale(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
255])),
decimal=3)
np.testing.assert_array_almost_equal(
0.896862745098, 0.9])
if not is_features_normal:
train_features = normalize_grayscale(train_features)
test_features = normalize_grayscale(test_features)
is_features_normal = True
print('Tests Passed!')
encoder.fit(train_labels)
train_labels = encoder.transform(train_labels)
test_labels = encoder.transform(test_labels)
train_labels = train_labels.astype(np.float32)
test_labels = test_labels.astype(np.float32)
is_labels_encod = True
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 5/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
train_features,
train_labels,
test_size=0.05,
random_state=832289)
pickle_file = 'notMNIST.pickle'
if not os.path.isfile(pickle_file):
try:
pickle.dump(
'train_dataset': train_features,
'train_labels': train_labels,
'valid_dataset': valid_features,
'valid_labels': valid_labels,
'test_dataset': test_features,
'test_labels': test_labels,
},
pfile, pickle.HIGHEST_PROTOCOL)
except Exception as e:
raise
Checkpoint
All your progress is now saved to the pickle file. If you need to leave and comeback to this lab, you no longer
have to start from the beginning. Just run the code block below and it will load all the data and modules
required to proceed.
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 6/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
import pickle
import math
import numpy as np
import tensorflow as tf
pickle_file = 'notMNIST.pickle'
pickle_data = pickle.load(f)
train_features = pickle_data['train_dataset']
train_labels = pickle_data['train_labels']
valid_features = pickle_data['valid_dataset']
valid_labels = pickle_data['valid_labels']
test_features = pickle_data['test_dataset']
test_labels = pickle_data['test_labels']
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 7/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
Problem 2
Now it's time to build a simple neural network using TensorFlow. Here, your network will be just an input layer
and an output layer.
For the input here the images have been flattened into a vector of features. Then, we're
trying to predict the image digit so there are 10 output units, one for each label. Of course, feel free to add
hidden layers if you want, but this notebook is built to guide you through a single layer network.
For the neural network to train on your data, you need the following float32 tensors:
features
Placeholder tensor for feature data
(train_features/valid_features/test_features)
labels
Placeholder tensor for label data (train_labels/valid_labels/test_labels)
weights
Variable Tensor with random numbers from a truncated normal distribution.
See `tf.truncated_normal()` documentationfor help.
biases
Variable Tensor with all zeros.
See `tf.zeros()` documentation for help.
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 8/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
If you're having trouble solving problem 2, review "TensorFlow Linear Function" section of the class. If that
doesn't help, the solution for this problem is available here.
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 9/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
features_count = 784
labels_count = 10
# features =
# labels =
# weights =
# biases =
#Test Cases
# Linear Function WX + b
prediction = tf.nn.softmax(logits)
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 10/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
# Cross entropy
# Training loss
loss = tf.reduce_mean(cross_entropy)
# Test Cases
session.run(init)
session.run(loss, feed_dict=train_feed_dict)
session.run(loss, feed_dict=valid_feed_dict)
session.run(loss, feed_dict=test_feed_dict)
biases_data = session.run(biases)
print('Tests Passed!')
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 11/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
Problem 3
Below are 2 parameter configurations for training the neural network. In each configuration, one of the
parameters has multiple options. For each configuration, choose the option that gives the best acccuracy.
Parameter configurations:
Configuration 1
Epochs: 1
Learning Rate:
0.8
0.5
0.1
0.05
0.01
Configuration 2
Epochs:
1
2
3
4
5
Learning Rate: 0.2
The code will print out a Loss and Accuracy graph, so you can see how well the neural network performed.
If you're having trouble solving problem 3, you can view the solution here.
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 12/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
batch_size = 128
# epochs =
# learning_rate =
# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimiz
e(loss)
validation_accuracy = 0.0
log_batch_step = 50
batches = []
loss_batch = []
train_acc_batch = []
valid_acc_batch = []
session.run(init)
batch_count = int(math.ceil(len(train_features)/batch_size))
# Progress bar
batch_features = train_features[batch_start:batch_start
+ batch_size]
batch_labels = train_labels[batch_start:batch_start + ba
tch_size]
_, l = session.run(
[optimizer, loss],
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 13/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
# Log batches
batches.append(log_batch_step + previous_batch)
loss_batch.append(l)
train_acc_batch.append(training_accuracy)
valid_acc_batch.append(validation_accuracy)
loss_plot = plt.subplot(211)
loss_plot.set_title('Loss')
loss_plot.set_xlim([batches[0], batches[-1]])
acc_plot = plt.subplot(212)
acc_plot.set_title('Accuracy')
acc_plot.set_ylim([0, 1.0])
acc_plot.set_xlim([batches[0], batches[-1]])
acc_plot.legend(loc=4)
plt.tight_layout()
plt.show()
Test
You're going to test your model against your hold out dataset/testing data. This will give you a good indicator
of how well the model will do in the real world. You should have a test accuracy of at least 80%.
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 14/15
10/26/21, 9:45 AM machine-learning/intro_to_tensorflow.ipynb at master · vnikov/machine-learning
test_accuracy = 0.0
session.run(init)
batch_count = int(math.ceil(len(train_features)/batch_size))
# Progress bar
batch_features = train_features[batch_start:batch_start
+ batch_size]
batch_labels = train_labels[batch_start:batch_start + ba
tch_size]
# Run optimizer
Multiple layers
Good job! You built a one layer TensorFlow network! However, you might want to build more than one layer.
This is deep learning after all! In the next section, you will start to satisfy your need for more layers.
https://github.com/vnikov/machine-learning/blob/master/projects/intro-to-tensorflow/intro_to_tensorflow.ipynb 15/15