Neural Network


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

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


Out[31]:
datetime.date(2017, 2, 28)

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

In [33]:
tf.__version__


Out[33]:
'1.0.0'

In [34]:
np.__version__


Out[34]:
'1.12.0'

Activation Functions

Q1. Apply relu, elu, and softplus to x.


In [3]:
_x = np.linspace(-10., 10., 1000)
x = tf.convert_to_tensor(_x)

relu = ...
elu = ...
softplus = ...

with tf.Session() as sess:
    _relu, _elu, _softplus = sess.run([relu, elu, softplus])
    plt.plot(_x, _relu, label='relu')
    plt.plot(_x, _elu, label='elu')
    plt.plot(_x, _softplus, label='softplus')
    plt.legend(bbox_to_anchor=(0.5, 1.0))
    plt.show()


Q2. Apply sigmoid and tanh to x.


In [28]:
_x = np.linspace(-10., 10., 1000)
x = tf.convert_to_tensor(_x)

sigmoid = ...
tanh = ...

with tf.Session() as sess:
    _sigmoid, _tanh = sess.run([sigmoid, tanh])
    plt.plot(_x, _sigmoid, label='sigmoid')
    plt.plot(_x, _tanh, label='tanh')
    plt.legend(bbox_to_anchor=(0.5, 1.0))
    plt.grid()
    plt.show()


Q3. Apply softmax to x.


In [5]:
_x = np.array([[1, 2, 4, 8], [2, 4, 6, 8]], dtype=np.float32)
x = tf.convert_to_tensor(_x)
out = ...
with tf.Session() as sess:
    _out = sess.run(out)
    print(_out)    
    assert np.allclose(np.sum(_out, axis=-1), 1)


[[  8.92509008e-04   2.42609112e-03   1.79265216e-02   9.78754938e-01]
 [  2.14400887e-03   1.58422012e-02   1.17058903e-01   8.64954829e-01]]

Q4. Apply dropout with keep_prob=.5 to x.


In [6]:
_x = np.array([[1, 2, 4, 8], [2, 4, 6, 8]], dtype=np.float32)
print("_x =\n" , _x)
x = tf.convert_to_tensor(_x)
out = ...
with tf.Session() as sess:
    _out = sess.run(out)
    print("_out =\n", _out)


_x =
 [[ 1.  2.  4.  8.]
 [ 2.  4.  6.  8.]]
_out =
 [[  2.   4.   8.  16.]
 [  4.   0.   0.  16.]]

Fully Connected

Q5. Apply a fully connected layer to x with 2 outputs and then an sigmoid function.


In [3]:
x = tf.random_normal([8, 10])


[[ 0.77683932  0.37980947]
 [ 0.27589312  0.8126893 ]
 [ 0.36492586  0.35784346]
 [ 0.88072854  0.71423841]
 [ 0.01438356  0.47667173]
 [ 0.63429254  0.45833236]
 [ 0.36451232  0.83774436]
 [ 0.14801691  0.61023575]]

Convolution

Q6. Apply 2 kernels of width-height (2, 2), stride 1, and same padding to x.


In [7]:
tf.reset_default_graph()

In [8]:
x = tf.random_uniform(shape=(2, 3, 3, 3), dtype=tf.float32)
filter = tf.get_variable("filter", shape=..., dtype=tf.float32, 
                    initializer=tf.random_uniform_initializer())
out = ...
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    _out = sess.run(out)
    print(_out.shape)


(2, 3, 3, 2)

Q7. Apply 3 kernels of width-height (2, 2), stride 1, dilation_rate 2 and valid padding to x.


In [9]:
tf.reset_default_graph()

In [10]:
x = tf.random_uniform(shape=(4, 10, 10, 3), dtype=tf.float32)
filter = tf.get_variable("filter", shape=..., dtype=tf.float32, 
                    initializer=tf.random_uniform_initializer())
out = ...
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    _out = sess.run(out)
    print(_out.shape)


(4, 8, 8, 2)

Q8. Apply 4 kernels of width-height (3, 3), stride 2, and same padding to x.


In [11]:
tf.reset_default_graph()

In [12]:
x = tf.random_uniform(shape=(4, 10, 10, 5), dtype=tf.float32)
filter = tf.get_variable("filter", shape=..., dtype=tf.float32, 
                    initializer=tf.random_uniform_initializer())
out = ...
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    _out = sess.run(out)
    print(_out.shape)


(4, 5, 5, 4)

Q9. Apply 4 times of kernels of width-height (3, 3), stride 2, and same padding to x, depth-wise.


In [13]:
tf.reset_default_graph()

In [14]:
x = tf.random_uniform(shape=(4, 10, 10, 5), dtype=tf.float32)
filter = tf.get_variable("filter", shape=..., dtype=tf.float32, 
                    initializer=tf.random_uniform_initializer())
out = ...
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    _out = sess.run(out)
    print(_out.shape)


(4, 5, 5, 20)

Q10. Apply 5 kernels of height 3, stride 2, and valid padding to x.


In [15]:
tf.reset_default_graph()

In [16]:
x = tf.random_uniform(shape=(4, 10, 5), dtype=tf.float32)
filter = tf.get_variable("filter", shape=..., dtype=tf.float32, 
                    initializer=tf.random_uniform_initializer())
out = ...
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    _out = sess.run(out)
    print(_out.shape)


(4, 4, 5)

Q11. Apply conv2d transpose with 5 kernels of width-height (3, 3), stride 2, and same padding to x.


In [17]:
tf.reset_default_graph()

In [18]:
x = tf.random_uniform(shape=(4, 5, 5, 4), dtype=tf.float32)
filter = tf.get_variable("filter", shape=..., dtype=tf.float32, 
                    initializer=tf.random_uniform_initializer())
shp = x.get_shape().as_list()
output_shape = ...
out = ...
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    _out = sess.run(out)
    print(_out.shape)


(4, 10, 10, 5)

Q12. Apply conv2d transpose with 5 kernels of width-height (3, 3), stride 2, and valid padding to x.


In [19]:
tf.reset_default_graph()

In [2]:
x = tf.random_uniform(shape=(4, 5, 5, 4), dtype=tf.float32)
filter = tf.get_variable("filter", shape=(3, 3, 5, 4), dtype=tf.float32, 
                    initializer=tf.random_uniform_initializer())
shp = x.get_shape().as_list()
output_shape = ...
out = ...
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    _out = sess.run(out)
    print(_out.shape)


(4, 11, 11, 5)

Q13. Apply max pooling and average pooling of window size 2, stride 1, and valid padding to x.


In [35]:
_x = np.zeros((1, 3, 3, 3), dtype=np.float32)
_x[0, :, :, 0] = np.arange(1, 10, dtype=np.float32).reshape(3, 3)
_x[0, :, :, 1] = np.arange(10, 19, dtype=np.float32).reshape(3, 3)
_x[0, :, :, 2] = np.arange(19, 28, dtype=np.float32).reshape(3, 3)
print("1st channel of x =\n", _x[:, :, :, 0])
print("\n2nd channel of x =\n", _x[:, :, :, 1])
print("\n3rd channel of x =\n", _x[:, :, :, 2])
x = tf.constant(_x)

maxpool = ...
avgpool = ...
with tf.Session() as sess:
    _maxpool, _avgpool = sess.run([maxpool, avgpool])
    print("\n1st channel of max pooling =\n", _maxpool[:, :, :, 0])
    print("\n2nd channel of max pooling =\n", _maxpool[:, :, :, 1])
    print("\n3rd channel of max pooling =\n", _maxpool[:, :, :, 2])
    print("\n1st channel of avg pooling =\n", _avgpool[:, :, :, 0])
    print("\n2nd channel of avg pooling =\n", _avgpool[:, :, :, 1])
    print("\n3rd channel of avg pooling =\n", _avgpool[:, :, :, 2])


1st channel of x =
 [[[ 1.  2.  3.]
  [ 4.  5.  6.]
  [ 7.  8.  9.]]]

2nd channel of x =
 [[[ 10.  11.  12.]
  [ 13.  14.  15.]
  [ 16.  17.  18.]]]

3rd channel of x =
 [[[ 19.  20.  21.]
  [ 22.  23.  24.]
  [ 25.  26.  27.]]]

1st channel of max pooling =
 [[[ 5.  6.]
  [ 8.  9.]]]

2nd channel of max pooling =
 [[[ 14.  15.]
  [ 17.  18.]]]

3rd channel of max pooling =
 [[[ 23.  24.]
  [ 26.  27.]]]

1st channel of avg pooling =
 [[[ 3.  4.]
  [ 6.  7.]]]

2nd channel of avg pooling =
 [[[ 12.  13.]
  [ 15.  16.]]]

3rd channel of avg pooling =
 [[[ 21.  22.]
  [ 24.  25.]]]

In [ ]: