In [1]:
import sys; print('Python \t\t{0[0]}.{0[1]}'.format(sys.version_info))
import tensorflow as tf; print('Tensorflow \t{}'.format(tf.__version__))
import keras; print('Keras \t\t{}'.format(keras.__version__))
In [2]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("../mnist-data/", one_hot=True)
In [4]:
mnist.train.images.shape
Out[4]:
In [5]:
plt.figure(figsize=(15,5))
for i in list(range(10)):
plt.subplot(1, 10, i+1)
pixels = mnist.test.images[i+100]
pixels = pixels.reshape((28, 28))
plt.imshow(pixels, cmap='gray_r')
plt.show()
In [6]:
import tensorflow as tf
import tensorlayer as tl
In [7]:
# Set parameters
training_iteration = 10
batch_size = 250
display_step = 5
FLAGS = None
In [8]:
# TF graph input
x = tf.placeholder('float', [None, 784]) # mnist data image of shape 28*28=784
y = tf.placeholder('float', [None,10]) # 0-9 digits recognition => 10 classes
In [9]:
network = tl.layers.InputLayer(x, name='input_layer')
network = tl.layers.DenseLayer(network, n_units=512,act = tf.nn.relu, name='relu1')
network = tl.layers.DropoutLayer(network, keep=0.75, name='drop1')
network = tl.layers.DenseLayer(network, n_units=512,act = tf.nn.relu, name='relu2')
network = tl.layers.DropoutLayer(network, keep=0.75, name='drop2')
network = tl.layers.DenseLayer(network, n_units=10,act = tl.activation.identity, name='output_layer')
# unscaled output (log probabilities)
y_hat = network.outputs
# scaled output
y_out = tf.nn.softmax(y_hat)
In [10]:
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=y_hat))
In [11]:
train_params = network.all_params
with tf.name_scope("train") as scope:
# Gradient descent
optimizer = tf.train.AdamOptimizer()
learn = optimizer.minimize(cost, var_list=train_params)
In [12]:
# Initializing the variables
init = tf.global_variables_initializer()
# Merge all summaries into a single operator
merged_summary_op = tf.summary.merge_all()
In [13]:
# Launch the graph
sess = tf.InteractiveSession()
sess.run(init)
In [14]:
# Change this to a location on your computer
summary_writer = tf.summary.FileWriter('./tensorboard/tf', graph=sess.graph)
In [15]:
# Test the model, Calculate accuracy
prediction = tf.equal(tf.argmax(y_hat, 1), tf.argmax(y, 1))
acc = tf.reduce_mean(tf.cast(prediction, tf.float32))
In [16]:
# train the network
tl.utils.fit(sess, network, learn, cost, mnist.train.images, mnist.train.labels, x, y,
acc=acc, batch_size=batch_size, n_epoch=training_iteration, print_freq=display_step,
X_val=mnist.test.images, y_val=mnist.test.labels, eval_train=False)
In [17]:
# evaluation
tl.utils.test(sess, network, acc, mnist.test.images, mnist.test.labels, x, y, batch_size=None)
In [18]:
dp_dict = tl.utils.dict_to_one( network.all_drop )
dp_dict
Out[18]:
In [19]:
# test item #100 is a "six"
pixels = mnist.test.images[100]
feed_dict = {x:[pixels]}
feed_dict.update(dp_dict)
result = sess.run(y_out, feed_dict=feed_dict)
dict(zip(range(10), result[0]))
Out[19]:
In [20]:
def test_render(pixels, result, truth):
#pixels, result and truth are np vectors
plt.figure(figsize=(10,5))
plt.subplot(1, 2, 1)
pixels = pixels.reshape((28, 28))
plt.imshow(pixels, cmap='gray_r')
plt.subplot(1, 2, 2)
#index, witdh
ind = np.arange(len(result))
width = 0.4
plt.barh(ind,result, width, color='orange', edgecolor='k', hatch="/")
plt.barh(ind+width,truth,width, color='g', edgecolor='k')
plt.yticks(ind+width, range(10))
plt.margins(y=0)
plt.show()
In [21]:
import random
i = random.randint(0,mnist.test.images.shape[0])
pixels = mnist.test.images[i]
truth = mnist.test.labels[i]
feed_dict = {x:[pixels]}
feed_dict.update(dp_dict)
result = sess.run(y_out, feed_dict=feed_dict)[0]
test_render(pixels, result, truth)
In [22]:
### What went wrong?
pixels = mnist.test.images
truth = mnist.test.labels
feed_dict = {x:pixels}
feed_dict.update(dp_dict)
result = sess.run(y_out, feed_dict=feed_dict)
In [23]:
acc = result.argmax(axis=1) == truth.argmax(axis=1)
incorrect = np.argwhere(acc==False).flatten()
print("Incorrect predictions: {}".format(len(incorrect)))
In [24]:
plt.figure(figsize=(20,5))
plt_idx = 1
for i in list(incorrect[:16]):
plt.subplot(1, 16, plt_idx)
pixels = mnist.test.images[i]
pixels = pixels.reshape((28, 28))
plt.imshow(pixels, cmap='gray_r')
plt_idx += 1
plt.show()
In [25]:
i = random.choice(list(incorrect))
pixels = mnist.test.images[i]
truth = mnist.test.labels[i]
feed_dict = {x:[pixels]}
feed_dict.update(dp_dict)
result = sess.run(y_out, feed_dict=feed_dict)[0]
test_render(pixels, result, truth)
In [ ]:
# Close the Session when we're done.
sess.close()
In [ ]: