# TensorFlow 03: MNIST and CNN

Now you are a not a beginner 😀
Since right now, you know what is CNN, lol, that’s amazing! Let’s raise our accuracy by applying a “professional” CNN. Well, basic but still works!

It’s said to be approximately 99.2%, here’s my testing after training 1000 times (too less but still works as 96.6%!): View the code here, or:

```
# coding: utf-8

# In:

import tensorflow.examples.tutorials.mnist.input_data as input_data
import tensorflow as tf
import os

# In:

# import dataset

# In:

# --------------------------Init--------------------------
# build a softmax regression model
# imput images(x), target output classes(y_)
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])

# Init weights, bioas, (all zeros first)
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros())
init = tf.initialize_all_variables()

# define softmax function
# first multiply x and w, then add b vector. apply softmax to get probabilities
y = tf.nn.softmax(tf.matmul(x, W) + b)

# launch the model in a Session, do NOT run the initialized operation here
sess = tf.Session()

# --------------------------Trainning--------------------------

# -------Build a Multilayer Convolutional Network--------------

# init functions: weights and bias generation.
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)

def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)

# In:

# convolution and Pooling functions
def conv2d(x, W):
return tf.nn.conv2d(input=x, filter=W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],

# In:

# First Convolutional Layer
x_image = tf.reshape(x, [-1,28,28,1])
sess = tf.InteractiveSession()

print x_image
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable()
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

# In:

# Second Convolutional Layer
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable()

h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

# In:

# Densely Connected Layer

W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable()

h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

keep_prob = tf.placeholder(&quot;float&quot;)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

# In:

W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable()

y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, &quot;float&quot;))

sess.run(tf.initialize_all_variables())

# In:

# Start Training: train for 1000 times.

for i in range(1000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(session=sess, feed_dict={
x:batch, y_: batch, keep_prob: 1.0})
print &quot;step %d, training accuracy %g&quot;%(i, train_accuracy)
train_step.run(session=sess, feed_dict={x: batch, y_: batch, keep_prob: 0.5})

# In:

print &quot;test accuracy %g&quot;%accuracy.eval(session=sess,feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})

# In[ ]:

```

x is our original input, its a m * 784 2-D tensor, or a matrix.
The reshape function, change each element in x, in to a shape of 1 * 28 * 28 * 1. -1 means 1-D here. So we have a 4-D input, [filter_height * filter_width * in_channels, output_channels].
The Filter is the Weight tensor or matrix. For each patch, right-multiplies the filter matrix and the image patch vector.
Usually, strides = [1, stride, stride, 1]. strides: A list of ints. 1-D of length 4. The stride of the sliding window for each dimension of input.
Padding, “same” or ”valid”, same will give you the same shape after computing.

Returns a same shape tensor with the input (x).

tf.nn.max_pool(x, ksize=[1, 2, 2, 1],

ksize: A list of ints that has length >= 4. The size of the window for each dimension of the input tensor. Here we have a 2 * 2 patch as our window size.
strides we have also 2 * 2. ?

Liine 66-74 First C Layer
First three lines initialize input shape, weights and biases.
Line72, tf.nn.relu() gives the ReLU function, which is max(feature, 0), you can think of it as softmax or sigmoid functions, so as to map to probabilities within the range (0,1). As the input to the pooling funtion. (??)

Liine 76-85 Second C Layer
We set this layer 64 features, with 5*5 patches.
Same with the first layer, just change numbers in dimensions.

Liine 87-99 Fully (Densely) Connected Layer
Create a layer of 1024 neurone (can be any), then reshape the pooling output from the second convolution layer to fit for the standard NN. Combine with a Linear model (times with weights and adds up with biases), then use ReLU to map into probabilities.

Dropout *

To reduce overfitting. Think of a fully connected net, there might be noises if we consider all of the connections between a layer to its next. So a way to prevent additional traing is to drop some less important neuros. Train
Different from the website, “session=sess” is added in the equal() function. Or you will meet an error:

ValueError: Cannot evaluate tensor using eval(): No default session is registered. Use `with sess.as_default()` or pass an explicit session to eval(session=sess)

Run the code with patience, good luck!

Framework C: convolutional layers
S: subsampling layers