In [1]:
import tensorflow as tf

what is tensor?

tensor is a multidimensional array!


In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

What is the MNIST dataset?

The above two lines downloaded the mnist dataset. It is the base of handwritten digits.

What are we doing with it?

every image is 28x28. So it is 784 points. So, every image can be represented as a point in 784d array.

we will also use one-hot vectors to signal, what is the number of image.

In [1]: def one_hot(k, n):
   ...:     vector = [0 for _ in range(k)]
   ...:     vector[n] = 1
   ...:     return vector
   ...: 

In [2]: one_hot(10, 3)
Out[2]: [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]

In [19]:
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))


Out[19]:
<tf.Tensor 'zeros_12:0' shape=(5,) dtype=float32>

we did describe the variables. Now, call the softmax function


In [25]:
y = tf.nn.softmax(tf.matmul(x, W) + b)
tf.nn.softmax?

What is softmax?

  • an activation function. What is an activation funciton?
  • the function that results in either 1 or 0. It decides whether or not the neuron will fire. softmax = exp(logits) / reduce_sum(exp(logits), dim) where, logits is Tensor.

basically, magic.

also, matmul is matrix multiplication


In [29]:
y_ = tf.placeholder(tf.float32, [None, 10])  # correct answers

In [32]:
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

In [34]:
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

In [40]:
for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

In [45]:
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
print(correct_prediction)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))


Tensor("Equal_5:0", shape=(?,), dtype=bool)
0.9214

In [ ]: