Neural Network Part2


In [1]:
from __future__ import print_function
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
from datetime import date
date.today()


Out[2]:
datetime.date(2017, 3, 22)

In [3]:
author = "kyubyong. https://github.com/Kyubyong/tensorflow-exercises"

In [4]:
tf.__version__


Out[4]:
'1.0.0'

In [5]:
np.__version__


Out[5]:
'1.12.0'

Normalization

Q1. Apply l2_normalize to x.


In [20]:
_x = np.arange(1, 11)
epsilon = 1e-12
x = tf.convert_to_tensor(_x, tf.float32)


[ 0.05096472  0.10192944  0.15289417  0.20385888  0.2548236   0.30578834
  0.35675305  0.40771776  0.45868248  0.50964719]

Q2. Calculate the mean and variance of x based on the sufficient statistics.


In [40]:
_x = np.arange(1, 11)
x = tf.convert_to_tensor(_x, tf.float32)


5.5 8.25

Q3. Calculate the mean and variance of x.


In [39]:
tf.reset_default_graph()
_x = np.arange(1, 11)
x = tf.convert_to_tensor(_x, tf.float32)


5.5 8.25

Q4. Calculate the mean and variance of x using unique_x and counts.


In [63]:
tf.reset_default_graph()
x = tf.constant([1, 1, 2, 2, 2, 3], tf.float32)

# From `x`
mean, variance = tf.nn.moments(x, [0])
with tf.Session() as sess:
    print(sess.run([mean, variance]))

# From unique elements and their counts
unique_x, _, counts = tf.unique_with_counts(x)
mean, variance = ...
with tf.Session() as sess:
    print(sess.run([mean, variance]))


[1.8333334, 0.47222227]
[1.8333334, 0.47222221]

Q5. The code below is to implement the mnist classification task. Complete it by adding batch normalization.


In [16]:
# Load data 
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=False)

# build graph
class Graph:
    def __init__(self, is_training=False):
        # Inputs and labels
        self.x = tf.placeholder(tf.float32, shape=[None, 784])
        self.y = tf.placeholder(tf.int32, shape=[None])

        # Layer 1
        w1 = tf.get_variable("w1", shape=[784, 100], initializer=tf.truncated_normal_initializer())
        output1 = tf.matmul(self.x, w1)
        output1 = tf.contrib.layers.batch_norm(...)

        #Layer 2
        w2 = tf.get_variable("w2", shape=[100, 10], initializer=tf.truncated_normal_initializer())
        logits = tf.matmul(output1, w2)
        preds = tf.to_int32(tf.arg_max(logits, dimension=1))

        # training
        loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.y, logits=logits)
        self.train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
        self.acc = tf.reduce_mean(tf.to_float(tf.equal(self.y, preds)))


# Training
tf.reset_default_graph()
g = Graph(is_training=True)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    saver = tf.train.Saver()
    for i in range(1, 10000+1):
        batch = mnist.train.next_batch(60)
        sess.run(g.train_op, {g.x: batch[0], g.y: batch[1]})
        # Evaluation
        if i % 100 == 0:
            print("training steps=", i, "Acc. =", sess.run(g.acc, {g.x: mnist.test.images, g.y: mnist.test.labels}))
    save_path = saver.save(sess, './my-model')
        
# Inference
tf.reset_default_graph()
g2 = Graph(is_training=False)
with tf.Session() as sess:
    saver = tf.train.Saver()
    saver.restore(sess, save_path)
    hits = 0
    for i in range(100):
        hits += sess.run(g2.acc, {g2.x: [mnist.test.images[i]], g2.y: [mnist.test.labels[i]]})
    print(hits)


Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
training steps= 100 Acc. = 0.7739
training steps= 200 Acc. = 0.8215
training steps= 300 Acc. = 0.8529
training steps= 400 Acc. = 0.8649
training steps= 500 Acc. = 0.879
training steps= 600 Acc. = 0.8804
training steps= 700 Acc. = 0.8874
training steps= 800 Acc. = 0.8981
training steps= 900 Acc. = 0.8952
training steps= 1000 Acc. = 0.8998
training steps= 1100 Acc. = 0.9033
training steps= 1200 Acc. = 0.9087
training steps= 1300 Acc. = 0.9101
training steps= 1400 Acc. = 0.9084
training steps= 1500 Acc. = 0.9161
training steps= 1600 Acc. = 0.9165
training steps= 1700 Acc. = 0.9168
training steps= 1800 Acc. = 0.9222
training steps= 1900 Acc. = 0.9212
training steps= 2000 Acc. = 0.9248
training steps= 2100 Acc. = 0.9272
training steps= 2200 Acc. = 0.9258
training steps= 2300 Acc. = 0.9274
training steps= 2400 Acc. = 0.9286
training steps= 2500 Acc. = 0.9315
training steps= 2600 Acc. = 0.931
training steps= 2700 Acc. = 0.9275
training steps= 2800 Acc. = 0.9344
training steps= 2900 Acc. = 0.9304
training steps= 3000 Acc. = 0.9306
training steps= 3100 Acc. = 0.9316
training steps= 3200 Acc. = 0.934
training steps= 3300 Acc. = 0.9311
training steps= 3400 Acc. = 0.9404
training steps= 3500 Acc. = 0.935
training steps= 3600 Acc. = 0.9396
training steps= 3700 Acc. = 0.9389
training steps= 3800 Acc. = 0.9377
training steps= 3900 Acc. = 0.9357
training steps= 4000 Acc. = 0.9432
training steps= 4100 Acc. = 0.9407
training steps= 4200 Acc. = 0.9412
training steps= 4300 Acc. = 0.9409
training steps= 4400 Acc. = 0.9399
training steps= 4500 Acc. = 0.9446
training steps= 4600 Acc. = 0.9456
training steps= 4700 Acc. = 0.9424
training steps= 4800 Acc. = 0.9408
training steps= 4900 Acc. = 0.943
training steps= 5000 Acc. = 0.9458
training steps= 5100 Acc. = 0.9443
training steps= 5200 Acc. = 0.9425
training steps= 5300 Acc. = 0.9454
training steps= 5400 Acc. = 0.946
training steps= 5500 Acc. = 0.9456
training steps= 5600 Acc. = 0.943
training steps= 5700 Acc. = 0.9452
training steps= 5800 Acc. = 0.9494
training steps= 5900 Acc. = 0.9472
training steps= 6000 Acc. = 0.9448
training steps= 6100 Acc. = 0.9481
training steps= 6200 Acc. = 0.9468
training steps= 6300 Acc. = 0.949
training steps= 6400 Acc. = 0.9492
training steps= 6500 Acc. = 0.9503
training steps= 6600 Acc. = 0.9494
training steps= 6700 Acc. = 0.9509
training steps= 6800 Acc. = 0.9502
training steps= 6900 Acc. = 0.9531
training steps= 7000 Acc. = 0.9488
training steps= 7100 Acc. = 0.9528
training steps= 7200 Acc. = 0.9526
training steps= 7300 Acc. = 0.9511
training steps= 7400 Acc. = 0.9549
training steps= 7500 Acc. = 0.9529
training steps= 7600 Acc. = 0.9524
training steps= 7700 Acc. = 0.9501
training steps= 7800 Acc. = 0.9546
training steps= 7900 Acc. = 0.9536
training steps= 8000 Acc. = 0.9559
training steps= 8100 Acc. = 0.9534
training steps= 8200 Acc. = 0.954
training steps= 8300 Acc. = 0.9543
training steps= 8400 Acc. = 0.9538
training steps= 8500 Acc. = 0.9555
training steps= 8600 Acc. = 0.9504
training steps= 8700 Acc. = 0.9545
training steps= 8800 Acc. = 0.9537
training steps= 8900 Acc. = 0.957
training steps= 9000 Acc. = 0.9555
training steps= 9100 Acc. = 0.9559
training steps= 9200 Acc. = 0.9538
training steps= 9300 Acc. = 0.9557
training steps= 9400 Acc. = 0.9527
training steps= 9500 Acc. = 0.956
training steps= 9600 Acc. = 0.9563
training steps= 9700 Acc. = 0.9575
training steps= 9800 Acc. = 0.9574
training steps= 9900 Acc. = 0.9572
training steps= 10000 Acc. = 0.9571
99.0

Losses

Q06. Compute half the L2 norm of x without the sqrt.


In [89]:
tf.reset_default_graph()
x = tf.constant([1, 1, 2, 2, 2, 3], tf.float32)


11.5
11.5

Classification

Q7. Compute softmax cross entropy between logits and labels. Note that the rank of them is not the same.


In [108]:
tf.reset_default_graph()
logits = tf.random_normal(shape=[2, 5, 10])
labels = tf.convert_to_tensor(np.random.randint(0, 10, size=[2, 5]), tf.int32)
output = tf.nn....
with tf.Session() as sess:
    print(sess.run(output))


[[ 3.20745516  2.13898396  3.07336521  2.74353743  3.27086067]
 [ 3.30273581  2.07290959  3.74236178  2.16736674  2.34212399]]

Q8. Compute softmax cross entropy between logits and labels.


In [110]:
logits = tf.random_normal(shape=[2, 5, 10])
labels = tf.convert_to_tensor(np.random.randint(0, 10, size=[2, 5]), tf.int32)
labels = tf.one_hot(labels, depth=10)

output = tf.nn....
with tf.Session() as sess:
    print(sess.run(output))


[[ 4.10439205  3.70805836  3.14382291  2.34843588  3.06786823]
 [ 2.46797109  1.83231926  1.75755191  4.17633867  4.40706158]]

Embeddings

Q9. Map tensor x to the embedding.


In [113]:
tf.reset_default_graph()
x = tf.constant([0, 2, 1, 3, 4], tf.int32)
embedding = tf.constant([0, 0.1, 0.2, 0.3, 0.4], tf.float32)
output = tf.nn....
with tf.Session() as sess:
    print(sess.run(output))


[ 0.          0.2         0.1         0.30000001  0.40000001]