3章 TensorFlow を使ったニューラルネットワークの実装

3.3 TensorFlowのインストール

本では Ubuntu Linux と MacOS のインストール方法が述べられているが、 自分は Windows を使うので conda 上の 「Deep Learning のための python + Visualization 環境」 のやり方でインストールした deep-graph または gpu-graph 環境を用いた。

公式の情報はこちら

In [1]:
import tensorflow as tf
deep_learning = tf.constant("Deep Learning")
session = tf.Session()
r1 = session.run(deep_learning)
print(r1)

a = tf.constant(2)
b = tf.constant(3)
multiply = tf.multiply(a, b)
r2 = session.run(multiply)
print(r2)
b'Deep Learning'
6

3.4 TensorFlow の Variable の生成と操作

TensorFlow の Variable とは、テンソルをメモリ上に格納したバッファのようなもの。 通常のテンソルは、グラフが実行されるときにインスタンス化され、実行が終わると直ちに破棄される。 Variable はテンソルであるが、複数回のグラフの実行にまたがってメモリ上に存在することができる。

  • グラフが最初に利用される時点で、Variable は明示的に初期化されていなければならない。
  • Gradient Descent によって Variable を何度も更新することで、モデルにおける最適のパラメータを発見できる。
  • Variable に保持された値を保存し、後で必要になったときに読み込むことができる。

tf.Variable を呼び出すと、次の3つの操作が計算グラフに追加される。

  • Variable の初期化に使うテンソルを生成する操作
  • Variable の使用に先立って、Variable に初期値のテンソルを割り当てる tf.assign 操作
  • Variable の現在の値を保持する操作

Variable を利用する際には tf.assign メソッドが実行されている必要がある。

  • tf.global_variables_initializer() 計算グラフ中のすべてのtf.assignが呼び出される。
  • tf.initialize_variables([var1, var2, ...]) 指定したVariableのtf.assignだけが呼び出される。

In [2]:
# shape=[300, 200]
# the standard deviation of the normal distribution = 0.5
# trainable (default)

weights = tf.Variable(
    tf.random_normal([300, 200], stddev=0.5),
    name="weights"
)

# not trainable
weights = tf.Variable(
    tf.random_normal([300, 200], stddev=0.5),
    name="weights",
    trainable=False
)
In [3]:
# How to initialize Variable

shape = [300, 200]

tf.zeros(shape, dtype=tf.float32, name=None)
tf.ones(shape, dtype=tf.float32, name=None)
tf.random_normal(
    shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None
    )
tf.truncated_normal(
    shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None
    )
tf.random_uniform(
    shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None
    )
Out[3]:
<tf.Tensor 'random_uniform:0' shape=(300, 200) dtype=float32>

3.5 TensorFlow での操作

カテゴリー
要素ごとの算術演算 Add, Sub, Mul, Div, Exp, Log, Greater, Less, Equal, ...
配列の操作 Concat, Slice, Split, Constant, Rank, Shape, Shuffle, ...
行列の操作 MatMul, MatrixInverse, MatrixDeterminant, ...
内部状態を持った操作 Variable, Assign, AssignAdd, ...
NNのlayer SoftMax, Sigmoid, ReLU, Convolution2D, MaxPool, ...
チェックポイント Save, Restore
キューと同期 Enqueue, Dequeue, MutexAcquire, MutexRelease, ...
制御フロー Merge, Switch, Enter, Leave, NextIteration

3.6 プレースホルダのテンソル

Variable は一度しか初期化されない。それに対して、グラフの実行のたびに値が設定されるのが PlaceHolder である。Session.run(), Tensor.eval(), Operation.run() のオプションの feed_dict で値を与える。

https://www.tensorflow.org/api_docs/python/tf/placeholder

In [4]:
x = tf.placeholder(tf.float32, name="x", shape=[None, 784])
W = tf.Variable(tf.random_uniform([784, 10], -1, 1), name="W")
multiply = tf.matmul(x, W)

3.7 TensorFlow でのセッション

session は初期状態の計算グラフを作成する役割を果たす。

https://www.tensorflow.org/api_docs/python/tf/Session

In [5]:
# session.py

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

mnist = input_data.read_data_sets("data", one_hot=True)
minibatch_x, minibatch_y = mnist.train.next_batch(32)

x = tf.placeholder(tf.float32, name="x", shape=[None, 784])
W = tf.Variable(tf.random_uniform([784, 10], -1, 1), name="W")
b = tf.Variable(tf.zeros([10]), name="biases")

output = tf.matmul(x, W) + b

init_op = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init_op)
feed_dict = {x : minibatch_x}
sess.run(output, feed_dict=feed_dict)
WARNING: Logging before flag parsing goes to stderr.
W0822 09:59:49.908180 11828 deprecation.py:323] From <ipython-input-5-be8b68b8381e>:6: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
W0822 09:59:49.908180 11828 deprecation.py:323] From D:\sys\Anaconda3\envs\gpu-graph\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
W0822 09:59:49.911148 11828 deprecation.py:323] From D:\sys\Anaconda3\envs\gpu-graph\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting data\train-images-idx3-ubyte.gz
W0822 09:59:50.541388 11828 deprecation.py:323] From D:\sys\Anaconda3\envs\gpu-graph\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
W0822 09:59:50.570414 11828 deprecation.py:323] From D:\sys\Anaconda3\envs\gpu-graph\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
W0822 09:59:50.735927 11828 deprecation.py:323] From D:\sys\Anaconda3\envs\gpu-graph\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
Extracting data\train-labels-idx1-ubyte.gz
Extracting data\t10k-images-idx3-ubyte.gz
Extracting data\t10k-labels-idx1-ubyte.gz
W0822 09:59:51.122924 11828 deprecation.py:323] From D:\sys\Anaconda3\envs\gpu-graph\lib\site-packages\tensorflow\python\util\tf_should_use.py:193: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
Out[5]:
array([[ 3.07297707e-01,  2.12898684e+00,  2.15093970e+00,
         2.72409248e+00,  2.02692962e+00, -2.97545695e+00,
         9.18175507e+00,  7.79365444e+00, -2.37528229e+00,
         7.19871092e+00],
       [ 1.66653228e+00, -7.28324056e-02,  1.09871101e+00,
         2.06557512e+00, -7.35610962e+00,  1.43941879e-01,
         1.21507454e+01,  4.68716621e+00,  7.46682501e+00,
         4.32856178e+00],
       [ 2.79686856e+00,  5.54188156e+00, -4.20636833e-01,
         8.48845720e-01, -3.96041155e+00,  8.47752810e-01,
         1.32612076e+01,  1.42815566e+00,  1.39339352e+00,
         7.58010864e+00],
       [ 4.96979475e+00,  1.15509367e+00,  1.31926739e+00,
         1.20149307e+01,  9.01111317e+00,  8.03406715e-01,
         7.18732595e+00,  5.85313654e+00, -4.85519648e+00,
         5.51384354e+00],
       [ 3.23182917e+00,  1.52423306e+01,  1.02857990e+01,
         6.07715702e+00,  8.90856934e+00,  3.75592232e-01,
         2.77842569e+00,  1.22090759e+01,  3.12774634e+00,
         1.01518097e+01],
       [ 5.56962395e+00,  3.59874916e+00,  1.39271069e+00,
         8.89954758e+00, -8.37058830e+00,  3.59749651e+00,
         8.67726326e+00,  6.65310001e+00, -1.53348899e+00,
         8.44609547e+00],
       [ 1.00824270e+01,  1.77582741e+00, -4.44570875e+00,
         8.02924156e+00,  7.48022842e+00, -1.38138270e+00,
         3.44161010e+00,  8.17632961e+00, -1.27403772e+00,
        -5.33966112e+00],
       [ 5.69356060e+00,  1.12908602e-01,  8.58191681e+00,
         5.56873083e+00, -9.40891743e-01, -1.61957073e+00,
         4.28891993e+00,  1.04166374e+01, -1.02185953e+00,
         1.06447086e+01],
       [ 8.08805466e+00,  1.61228180e-01,  7.99864197e+00,
         2.18362832e+00,  1.42449732e+01,  3.27777147e+00,
         1.21757336e+01,  6.67744350e+00, -2.59357095e+00,
         7.74960613e+00],
       [ 4.19095802e+00,  9.15550995e+00,  2.90194821e+00,
        -1.40758085e+00,  8.69159126e+00, -5.23703098e+00,
         1.07460709e+01,  6.85014677e+00, -7.55459595e+00,
         5.41463995e+00],
       [ 1.81478262e+00,  3.51504636e+00,  1.79434693e+00,
         8.11958313e+00,  1.32059097e-01, -4.06736994e+00,
        -1.79864335e+00,  7.43675280e+00,  5.28252697e+00,
         8.89715004e+00],
       [-2.92168760e+00, -9.86902833e-01,  6.21522141e+00,
         5.80177975e+00, -3.34435463e-01,  8.06844234e-03,
         1.12442017e+01,  6.09990025e+00, -4.60190821e+00,
         4.51750422e+00],
       [ 4.55481529e+00, -8.66565704e-02,  2.94132519e+00,
         1.12205877e+01,  1.32992649e+00, -7.66694260e+00,
         7.51166105e+00,  8.62253189e+00, -4.93522406e+00,
         4.82953167e+00],
       [ 4.69435978e+00,  1.08806610e+01, -2.95091319e+00,
         9.79522896e+00,  1.50136948e-01, -3.02916193e+00,
         1.43240786e+01,  5.79319000e+00,  2.22648048e+00,
         1.77819614e+01],
       [-1.58751631e+00,  1.16945744e-01,  1.57217512e+01,
         7.22359610e+00,  3.14162683e+00,  1.95708132e+00,
         5.18780613e+00, -3.32508326e-01, -1.07611299e-01,
         6.96816444e+00],
       [ 1.05559998e+01,  8.95738888e+00, -1.50290251e+00,
         4.80139065e+00,  3.96134424e+00, -2.17245197e+00,
         1.94209042e+01,  9.19804096e+00,  3.89516020e+00,
         4.84423828e+00],
       [ 1.26068811e+01,  4.86016369e+00,  3.05459619e+00,
        -3.04468584e+00,  6.43466711e+00,  4.60065842e-01,
         1.54136028e+01,  2.62889528e+00,  2.38344908e+00,
         8.12982559e+00],
       [ 2.87285948e+00,  5.54695559e+00,  1.39809465e+00,
         4.97482777e+00, -2.93606377e+00,  1.31104612e+00,
         8.39674950e+00,  8.79363441e+00, -5.65588474e-02,
         5.17025852e+00],
       [ 9.02176189e+00,  3.14056396e+00, -9.32643652e-01,
         2.34562778e+00, -2.01168776e+00,  2.47299528e+00,
         7.82424927e-01,  8.89600945e+00, -2.01739120e+00,
         2.71129179e+00],
       [ 4.43592024e+00,  4.53191233e+00,  1.87311053e+00,
         6.95913315e+00,  8.63540363e+00, -1.48183823e+00,
         1.26947451e+00,  1.23858070e+01,  6.10204220e-01,
         6.13304806e+00],
       [ 2.96086144e+00,  7.18411684e-01, -6.96005058e+00,
         3.16264892e+00, -7.12957430e+00,  3.37920475e+00,
         5.77892399e+00, -4.19538677e-01,  6.27131796e+00,
         2.56903005e+00],
       [-3.28231645e+00,  2.30265737e+00,  1.50551069e+00,
         3.09854841e+00, -1.70940161e-01,  2.13255882e-01,
         1.15423346e+01,  1.29384890e+01, -5.24901247e+00,
         3.11646986e+00],
       [ 5.07374334e+00,  5.68999720e+00,  1.87491941e+00,
         1.02867675e+00,  2.53149033e+00, -1.44024944e+00,
         9.70729828e+00,  5.08202267e+00,  4.59882927e+00,
         5.31275511e+00],
       [ 8.78514826e-01, -5.92130327e+00,  2.94700432e+00,
         1.61451304e+00, -6.70919657e+00, -2.70839262e+00,
         7.99837971e+00,  3.82196021e+00,  2.12036681e+00,
         5.96122551e+00],
       [ 1.10969305e+01,  7.92234612e+00,  9.24248409e+00,
         1.02876396e+01,  1.67509212e+01,  8.79198265e+00,
         1.59384747e+01,  1.36997719e+01, -1.41112041e+00,
         1.00365667e+01],
       [ 1.01019554e+01,  1.91130352e+00, -3.65221739e-01,
         7.06894398e+00,  7.60911655e+00,  4.92074823e+00,
         7.95589972e+00,  5.04328966e-01,  4.80956841e+00,
         5.73950005e+00],
       [ 8.80724669e-01,  4.38782978e+00,  9.38898802e-01,
         8.16920280e+00, -3.29743743e+00, -6.61366844e+00,
         3.25786877e+00,  5.62209988e+00,  9.21698380e+00,
         9.25290680e+00],
       [ 3.95739889e+00,  6.20005131e+00,  3.96663499e+00,
         2.49567151e+00,  2.61197352e+00, -3.98114264e-01,
         1.29795494e+01, -3.86043787e-01,  5.42931128e+00,
         5.49443960e+00],
       [ 8.50076103e+00,  7.03443861e+00, -6.69067287e+00,
         7.15107250e+00,  3.28731203e+00, -1.90096855e-01,
         8.71439743e+00,  1.07262316e+01,  3.55190492e+00,
         1.13707933e+01],
       [ 1.21072369e+01,  8.91240501e+00,  3.62326837e+00,
         8.71779633e+00,  8.81261730e+00,  8.37757492e+00,
         1.63835239e+01,  6.45947456e+00,  2.12029934e+00,
         1.16985226e+00],
       [ 2.11297083e+00,  1.07289190e+01, -4.97177982e+00,
         1.29305477e+01,  4.06337929e+00, -2.19316292e+00,
         5.84520102e+00,  8.43266106e+00,  2.18481731e+00,
         8.88282967e+00],
       [ 1.29534540e+01,  2.41881514e+00,  3.89282942e+00,
         6.17118359e+00,  1.16552839e+01,  2.57499123e+00,
         1.50248680e+01,  2.55808616e+00,  5.08395433e+00,
         2.88491678e+00]], dtype=float32)

3.8 Variable のスコープと共有

In [6]:
# scope1.py

import tensorflow as tf

def my_network(input):
    W_1 = tf.Variable(tf.random_uniform([784, 100], -1, 1), name="W_1")
    b_1 = tf.Variable(tf.zeros([100]), name="biases_1")
    output_1 = tf.matmul(input, W_1) + b_1

    W_2 = tf.Variable(tf.random_uniform([100, 50], -1, 1), name="W_2")
    b_2 = tf.Variable(tf.zeros([50]), name="biases_2")
    output_2 = tf.matmul(output_1, W_2) + b_2

    W_3 = tf.Variable(tf.random_uniform([50, 10], -1, 1),name="W_3")
    b_3 = tf.Variable(tf.zeros([10]), name="biases_3")
    output_3 = tf.matmul(output_2, W_3) + b_3

    # printing names
    print("Printing names of weight parameters")
    print(W_1.name, W_2.name, W_3.name)
    print("Printing names of bias parameters")
    print(b_1.name, b_2.name, b_3.name)

    return output_3


i_1 = tf.placeholder(tf.float32, [1000, 784], name="i_1")
my_network(i_1)

i_2 = tf.placeholder(tf.float32, [1000, 784], name="i_2")
my_network(i_2)
Printing names of weight parameters
W_1_1:0 W_2:0 W_3:0
Printing names of bias parameters
biases_1:0 biases_2:0 biases_3:0
Printing names of weight parameters
W_1_2:0 W_2_1:0 W_3_1:0
Printing names of bias parameters
biases_1_1:0 biases_2_1:0 biases_3_1:0
Out[6]:
<tf.Tensor 'add_6:0' shape=(1000, 10) dtype=float32>

scope1.py では、 my_network() を呼び出すたびに、異なる変数が用意されてしまうことがわかる。 たとえば "W_1" という変数は、最初の呼び出しでは W_1_1:0 , 2番目の呼び出しでは W_1_2:0 という別の変数になっていることがわかる。

以下に示す scope2.py では、関数を呼び出す度に同じ変数が使われる。

https://www.tensorflow.org/api_docs/python/tf/get_variable
https://www.tensorflow.org/api_docs/python/tf/variable_scope

In [7]:
# scope2.py

import tensorflow as tf

def layer(input, weight_shape, bias_shape):
    weight_init = tf.random_uniform_initializer(minval=-1, maxval=1)
    bias_init = tf.constant_initializer(value=0)
    W = tf.get_variable("W", weight_shape, initializer=weight_init)
    b = tf.get_variable("b", bias_shape, initializer=bias_init)
    return tf.matmul(input, W) + b


def my_network(input):
    with tf.variable_scope("layer_1"):
        output_1 = layer(input, [784, 100], [100])

    with tf.variable_scope("layer_2"):
        output_2 = layer(output_1, [100, 50], [50])

    with tf.variable_scope("layer_3"):
        output_3 = layer(output_2, [50, 10], [10])

    return output_3
In [8]:
i_1 = tf.placeholder(tf.float32, [1000, 784], name="i_1")
my_network(i_1)

i_2 = tf.placeholder(tf.float32, [1000, 784], name="i_2")
# my_network(i_2)
# ValueError: Over-sharing: Variable layer_1/W already exists...

tf.variable_scope() を宣言することで、Variableの名前は "layer_1/W" のように、名前空間がついたものになる。

tf.get_variable() は Variable が既にインスタンス化されているかどうかをチェックする。 デフォルトでは共有は無効化されているので、2度インスタンス化しようとするとエラーとなる。 共有を有効化するには、scope.reuse_variables() を呼び出す。

In [9]:
with tf.variable_scope("shared_variables") as scope:
    i_1 = tf.placeholder(tf.float32, [1000, 784], name="i_1")
    my_network(i_1)
    scope.reuse_variables()
    i_2 = tf.placeholder(tf.float32, [1000, 784], name="i_2")
    my_network(i_2)

3.9 CPU と GPU 上でのモデルの管理

with tf.device() を用いて特定のデバイスを選択できる。 指定されたデバイスが利用できない場合はエラーとなる。 使えるデバイスを使って計算を進める場合は Session のオプションとして allow_soft_placement フラグを指定する。

説明
cpu:0 CPU を表す
gpu:0 1つ目の GPU を表す
gpu:1 2つ目の GPU を表す
In [15]:
import tensorflow as tf


with tf.device("/gpu:2"):
    a = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2], name="a")
    b = tf.constant([1.0, 2.0], shape=[2, 1], name="b")
    c = tf.matmul(a, b)

sess = tf.Session(
    config=tf.ConfigProto(
        allow_soft_placement=True,
        log_device_placement=True
    )
)

ans = sess.run(c)

print(ans)
[[ 5.]
 [11.]]

プログラムでは、 $$ \begin{pmatrix} 1 & 2 \\ 3 & 4 \\ \end{pmatrix} \begin{pmatrix} 1 \\ 2 \\ \end{pmatrix} = \begin{pmatrix} 1 \times 1 + 2 \times 2 \\ 3 \times 1 + 4 \times 2 \\ \end{pmatrix} = \begin{pmatrix} 1 \times 1 + 2 \times 2 \\ 3 \times 1 + 4 \times 2 \\ \end{pmatrix} = \begin{pmatrix} 5 \\ 11 \end{pmatrix} $$ を計算している。

In [14]:
# device.py

c = []

for d in ["/gpu:0", "/gpu:1"]:
    with tf.device(d):
        a = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2], name="a")
        b = tf.constant([1.0, 2.0], shape=[2, 1], name="b")
        c.append(tf.matmul(a, b))

with tf.device("/cpu:0"):
    sum = tf.add_n(c)

sess = tf.Session(
    config=tf.ConfigProto(
        allow_soft_placement=True,
        log_device_placement=True
    )
)
ans = sess.run(sum)

print(ans)
[[10.]
 [22.]]

上の例では、同じ計算をしてできた行列 $ \begin{pmatrix} 5 \\ 11 \\ \end{pmatrix} $ を リストに append して $ c = \begin{pmatrix} \begin{pmatrix} 5 \\ 11 \\ \end{pmatrix} & \begin{pmatrix} 5 \\ 11 \\ \end{pmatrix} \end{pmatrix} $ となり、tf.add_n(c) を呼び出すので各要素が加算されて $ \begin{pmatrix} 10 \\ 22 \end{pmatrix} $ となる。

3.10 ロジスティック回帰のモデルを記述する

ロジスティック回帰 (logistic regression) は、入力をクラス分け (classify) する仕組みで、各クラスに属する確率が求まる。

$$ P(y=i|x) = softmax_i (Wx+b) = \frac{e^{W_i x + b_i}}{\sum_{j} e^{W_j x + b_j}} $$

まず、隠れ層 (hidden layer) を持たないニューラルネットワークを考える。 (だんだん改良していく)

https://www.tensorflow.org/api_docs/python/tf/math/reduce_sum
https://www.tensorflow.org/api_docs/python/tf/nn/softmax
https://www.tensorflow.org/api_docs/python/tf/math/reduce_mean
https://www.tensorflow.org/api_docs/python/tf/train/GradientDescentOptimizer
https://www.tensorflow.org/api_docs/python/tf/summary
https://www.tensorflow.org/api_docs/python/tf/summary/histogram
https://www.tensorflow.org/api_docs/python/tf/train

In [16]:
# logistic_regression.py

import tensorflow as tf
import time, shutil, os
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("data", one_hot=True)

# Parameters
learning_rate = 0.01
training_epochs = 100
batch_size = 100
display_step = 1


def inference(x):
    init = tf.constant_initializer(value=0)
    W = tf.get_variable("W", [784, 10], initializer=init)
    b = tf.get_variable("b", [10], initializer=init)
    output = tf.nn.softmax(tf.matmul(x, W) + b)
    tf.summary.histogram("weights", W)
    tf.summary.histogram("biases", b)
    tf.summary.histogram("output", output)
    return output


def loss(output, y):
    dot_product = y * tf.log(output)
    # Reduction along axis 0 collapses each column into a single
    # value, whereas reduction along axis 1 collapses each row 
    # into a single value. In general, reduction along axis i 
    # collapses the ith dimension of a tensor to size 1.
    xentropy = -tf.reduce_sum(dot_product, axis=1)
    loss = tf.reduce_mean(xentropy)
    return loss


def training(cost, global_step):
    tf.summary.scalar("cost", cost)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    train_op = optimizer.minimize(cost, global_step=global_step)
    return train_op


def evaluate(output, y):
    correct_prediction = tf.equal(tf.argmax(output, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar("validation error", (1.0 - accuracy))
    return accuracy
Extracting data\train-images-idx3-ubyte.gz
Extracting data\train-labels-idx1-ubyte.gz
Extracting data\t10k-images-idx3-ubyte.gz
Extracting data\t10k-labels-idx1-ubyte.gz
In [17]:
if __name__ == "__main__":
    # Remove old summaries and checkpoints
    if os.path.exists("logistic_logs"):
        shutil.rmtree("logistic_logs")

    with tf.Graph().as_default():
        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
        output = inference(x)
        cost = loss(output, y)
        global_step = tf.Variable(0, name="global_step", trainable=False)
        train_op = training(cost, global_step)
        eval_op = evaluate(output, y)
        summary_op = tf.summary.merge_all()
        saver = tf.train.Saver()
        sess = tf.Session()
        summary_writer = tf.summary.FileWriter(
            "logistic_logs",
            graph_def=sess.graph_def
        )
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        # Training cycle
        for epoch in range(training_epochs):
            avg_cost = 0.
            total_batch = int(mnist.train.num_examples/batch_size)
            # Loop over all batches
            for i in range(total_batch):
                minibatch_x, minibatch_y = mnist.train.next_batch(batch_size)
                # Fit training using batch data
                sess.run(train_op, feed_dict={x: minibatch_x, y: minibatch_y})
                # Compute average loss
                avg_cost += sess.run(
                    cost, feed_dict={x: minibatch_x, y: minibatch_y}
                ) / total_batch
            # Display logs per epoch step
            if epoch % display_step == 0:
                print("Epoch: {:04d} cost: {:.9f}".format(epoch+1, avg_cost))
                accuracy = sess.run(eval_op, feed_dict={x: mnist.validation.images, y: mnist.validation.labels})
                print("Validation Error: {}".format(1 - accuracy))
                summary_str = sess.run(summary_op, feed_dict={x: minibatch_x, y: minibatch_y})
                summary_writer.add_summary(summary_str, sess.run(global_step))
                saver.save(sess, os.path.join("logistic_logs", "model-checkpoint"), global_step=global_step)

        print("Optimization Finished!")
        accuracy = sess.run(eval_op, feed_dict={x: mnist.test.images, y: mnist.test.labels})
        print("Test Accuracy: {}".format(accuracy))
W0822 10:42:46.021615 11828 writer.py:199] Passing a `GraphDef` to the SummaryWriter is deprecated. Pass a `Graph` object instead, such as `sess.graph`.
Epoch: 0001 cost: 1.176797048
Validation Error: 0.15160000324249268
Epoch: 0002 cost: 0.662675477
Validation Error: 0.1284000277519226
Epoch: 0003 cost: 0.550757095
Validation Error: 0.12059998512268066
Epoch: 0004 cost: 0.496798432
Validation Error: 0.11379998922348022
Epoch: 0005 cost: 0.463822677
Validation Error: 0.11000001430511475
W0822 10:42:58.759492 11828 deprecation.py:323] From D:\sys\Anaconda3\envs\gpu-graph\lib\site-packages\tensorflow\python\training\saver.py:960: remove_checkpoint (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.
Instructions for updating:
Use standard file APIs to delete files with this prefix.
Epoch: 0006 cost: 0.440845016
Validation Error: 0.10579997301101685
Epoch: 0007 cost: 0.423962175
Validation Error: 0.10479998588562012
Epoch: 0008 cost: 0.410644458
Validation Error: 0.10259997844696045
Epoch: 0009 cost: 0.399916407
Validation Error: 0.10019999742507935
Epoch: 0010 cost: 0.390971053
Validation Error: 0.09939998388290405
Epoch: 0011 cost: 0.383357659
Validation Error: 0.0974000096321106
Epoch: 0012 cost: 0.376724289
Validation Error: 0.09600001573562622
Epoch: 0013 cost: 0.371043418
Validation Error: 0.09380000829696655
Epoch: 0014 cost: 0.365912697
Validation Error: 0.09280002117156982
Epoch: 0015 cost: 0.361401314
Validation Error: 0.09200000762939453
Epoch: 0016 cost: 0.357294162
Validation Error: 0.0899999737739563
Epoch: 0017 cost: 0.353541035
Validation Error: 0.0899999737739563
Epoch: 0018 cost: 0.350156383
Validation Error: 0.08980000019073486
Epoch: 0019 cost: 0.347043993
Validation Error: 0.08920001983642578
Epoch: 0020 cost: 0.344165125
Validation Error: 0.08799999952316284
Epoch: 0021 cost: 0.341465678
Validation Error: 0.08639997243881226
Epoch: 0022 cost: 0.338998255
Validation Error: 0.0867999792098999
Epoch: 0023 cost: 0.336663089
Validation Error: 0.08560001850128174
Epoch: 0024 cost: 0.334544293
Validation Error: 0.08539998531341553
Epoch: 0025 cost: 0.332475037
Validation Error: 0.08520001173019409
Epoch: 0026 cost: 0.330538430
Validation Error: 0.08539998531341553
Epoch: 0027 cost: 0.328749352
Validation Error: 0.08600002527236938
Epoch: 0028 cost: 0.327005772
Validation Error: 0.0843999981880188
Epoch: 0029 cost: 0.325393468
Validation Error: 0.08420002460479736
Epoch: 0030 cost: 0.323849694
Validation Error: 0.08380001783370972
Epoch: 0031 cost: 0.322362668
Validation Error: 0.08520001173019409
Epoch: 0032 cost: 0.320969228
Validation Error: 0.08399999141693115
Epoch: 0033 cost: 0.319601334
Validation Error: 0.08459997177124023
Epoch: 0034 cost: 0.318335391
Validation Error: 0.08279997110366821
Epoch: 0035 cost: 0.317137007
Validation Error: 0.08219999074935913
Epoch: 0036 cost: 0.315990672
Validation Error: 0.08300000429153442
Epoch: 0037 cost: 0.314814411
Validation Error: 0.08179998397827148
Epoch: 0038 cost: 0.313745127
Validation Error: 0.08219999074935913
Epoch: 0039 cost: 0.312720669
Validation Error: 0.08219999074935913
Epoch: 0040 cost: 0.311689516
Validation Error: 0.08139997720718384
Epoch: 0041 cost: 0.310697127
Validation Error: 0.0812000036239624
Epoch: 0042 cost: 0.309777532
Validation Error: 0.08020001649856567
Epoch: 0043 cost: 0.308911459
Validation Error: 0.0812000036239624
Epoch: 0044 cost: 0.308041230
Validation Error: 0.0812000036239624
Epoch: 0045 cost: 0.307169095
Validation Error: 0.08020001649856567
Epoch: 0046 cost: 0.306386721
Validation Error: 0.0788000226020813
Epoch: 0047 cost: 0.305589440
Validation Error: 0.07920002937316895
Epoch: 0048 cost: 0.304811100
Validation Error: 0.0788000226020813
Epoch: 0049 cost: 0.304108404
Validation Error: 0.07920002937316895
Epoch: 0050 cost: 0.303350700
Validation Error: 0.07840001583099365
Epoch: 0051 cost: 0.302663036
Validation Error: 0.0788000226020813
Epoch: 0052 cost: 0.301943820
Validation Error: 0.07819998264312744
Epoch: 0053 cost: 0.301337035
Validation Error: 0.07859998941421509
Epoch: 0054 cost: 0.300639340
Validation Error: 0.0777999758720398
Epoch: 0055 cost: 0.300054946
Validation Error: 0.07819998264312744
Epoch: 0056 cost: 0.299416082
Validation Error: 0.078000009059906
Epoch: 0057 cost: 0.298857720
Validation Error: 0.07719999551773071
Epoch: 0058 cost: 0.298265099
Validation Error: 0.07859998941421509
Epoch: 0059 cost: 0.297684274
Validation Error: 0.07740002870559692
Epoch: 0060 cost: 0.297087176
Validation Error: 0.07840001583099365
Epoch: 0061 cost: 0.296582665
Validation Error: 0.07740002870559692
Epoch: 0062 cost: 0.296069481
Validation Error: 0.07700002193450928
Epoch: 0063 cost: 0.295549399
Validation Error: 0.07620000839233398
Epoch: 0064 cost: 0.295061976
Validation Error: 0.07679998874664307
Epoch: 0065 cost: 0.294578046
Validation Error: 0.07660001516342163
Epoch: 0066 cost: 0.294088927
Validation Error: 0.07679998874664307
Epoch: 0067 cost: 0.293655734
Validation Error: 0.07700002193450928
Epoch: 0068 cost: 0.293115676
Validation Error: 0.07700002193450928
Epoch: 0069 cost: 0.292710676
Validation Error: 0.07580000162124634
Epoch: 0070 cost: 0.292266944
Validation Error: 0.07580000162124634
Epoch: 0071 cost: 0.291849442
Validation Error: 0.07620000839233398
Epoch: 0072 cost: 0.291409454
Validation Error: 0.07620000839233398
Epoch: 0073 cost: 0.291001606
Validation Error: 0.07639998197555542
Epoch: 0074 cost: 0.290622401
Validation Error: 0.07580000162124634
Epoch: 0075 cost: 0.290208489
Validation Error: 0.07599997520446777
Epoch: 0076 cost: 0.289766339
Validation Error: 0.07480001449584961
Epoch: 0077 cost: 0.289454902
Validation Error: 0.07480001449584961
Epoch: 0078 cost: 0.289021721
Validation Error: 0.07580000162124634
Epoch: 0079 cost: 0.288661042
Validation Error: 0.0756000280380249
Epoch: 0080 cost: 0.288346772
Validation Error: 0.07580000162124634
Epoch: 0081 cost: 0.287967052
Validation Error: 0.07580000162124634
Epoch: 0082 cost: 0.287635785
Validation Error: 0.07599997520446777
Epoch: 0083 cost: 0.287261959
Validation Error: 0.07499998807907104
Epoch: 0084 cost: 0.286927564
Validation Error: 0.07520002126693726
Epoch: 0085 cost: 0.286586735
Validation Error: 0.07520002126693726
Epoch: 0086 cost: 0.286299869
Validation Error: 0.07539999485015869
Epoch: 0087 cost: 0.285925640
Validation Error: 0.07539999485015869
Epoch: 0088 cost: 0.285634465
Validation Error: 0.07499998807907104
Epoch: 0089 cost: 0.285297557
Validation Error: 0.07480001449584961
Epoch: 0090 cost: 0.285028340
Validation Error: 0.07520002126693726
Epoch: 0091 cost: 0.284673164
Validation Error: 0.07539999485015869
Epoch: 0092 cost: 0.284402090
Validation Error: 0.07499998807907104
Epoch: 0093 cost: 0.284128709
Validation Error: 0.07480001449584961
Epoch: 0094 cost: 0.283818540
Validation Error: 0.07499998807907104
Epoch: 0095 cost: 0.283534214
Validation Error: 0.07499998807907104
Epoch: 0096 cost: 0.283261782
Validation Error: 0.07539999485015869
Epoch: 0097 cost: 0.282942454
Validation Error: 0.0756000280380249
Epoch: 0098 cost: 0.282724679
Validation Error: 0.07580000162124634
Epoch: 0099 cost: 0.282448787
Validation Error: 0.07480001449584961
Epoch: 0100 cost: 0.282174008
Validation Error: 0.07520002126693726
Optimization Finished!
Test Accuracy: 0.921500027179718

3.12 TensorBoard を使って計算グラフと学習を可視化する

https://www.tensorflow.org/guide/graph_viz

  • condaを起動してその中で tensorboard を起動する。
  •   (base) c:\Users\nitta> g:
      (base) g:\> cd マイドライブ\deeplearning\book3\ch03
      (base) g:\マイドライブ\deeplearning\book3\ch03> conda activate gpu-graph
      (gpu-graph) g:\マイドライブ\deeplearning\book3\ch03> tensorboard --logdir logistic_logs logistic_logs
    
  • ブラウザを起動して http://localhost:6006/ にアクセスする。







3.13 多階層の MNIST モデル

256 子の ReLU ニューロンからなる隠れ層を2つ持つモデルを考える。

ReLU ニューロンについては、 [He 2015] で述べられているように、ネットワーク内での重みの分散を $\frac{2}{n_{in}}$ にする。

softmax の計算を inference の中ではなく、損失の算出時に行うことに変更する。

以上の変更により、性能がかなり改善されることがわかる。

In [18]:
# multilayer_perceptron.py

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import time, shutil, os

mnist = input_data.read_data_sets("data", one_hot=True)

# Architecture
n_hidden_1 = 256
n_hidden_2 = 256

# Parameters
learning_rate = 0.01
training_epochs = 300
batch_size = 100
display_step = 1


def layer(input, weight_shape, bias_shape):
    weight_init = tf.random_normal_initializer(stddev=(2.0/weight_shape[0])**0.5)
    bias_init = tf.constant_initializer(value=0)
    W = tf.get_variable("W", weight_shape, initializer=weight_init)
    b = tf.get_variable("b", bias_shape, initializer=bias_init)
    return tf.nn.relu(tf.matmul(input, W) + b)


def inference(x):
    with tf.variable_scope("hidden_1"):
        hidden_1 = layer(x, [784, n_hidden_1], [n_hidden_1])     
    with tf.variable_scope("hidden_2"):
        hidden_2 = layer(hidden_1, [n_hidden_1, n_hidden_2], [n_hidden_2])
    with tf.variable_scope("output"):
        output = layer(hidden_2, [n_hidden_2, 10], [10])
    return output


def loss(output, y):
    xentropy = tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y)    
    loss = tf.reduce_mean(xentropy)
    return loss


def training(cost, global_step):
    tf.summary.scalar("cost", cost)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    train_op = optimizer.minimize(cost, global_step=global_step)
    return train_op


def evaluate(output, y):
    correct_prediction = tf.equal(tf.argmax(output, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar("validation", accuracy)
    return accuracy
Extracting data\train-images-idx3-ubyte.gz
Extracting data\train-labels-idx1-ubyte.gz
Extracting data\t10k-images-idx3-ubyte.gz
Extracting data\t10k-labels-idx1-ubyte.gz
In [19]:
if __name__ == "__main__":
    # Remove old summaries and checkpoints
    if os.path.exists("mlp_logs"):
        shutil.rmtree("mlp_logs")

    with tf.Graph().as_default():
        with tf.variable_scope("mlp_model"):
            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

            output = inference(x)
            cost = loss(output, y)
            global_step = tf.Variable(0, name="global_step", trainable=False)
            train_op = training(cost, global_step)
            eval_op = evaluate(output, y)
            summary_op = tf.summary.merge_all()
            saver = tf.train.Saver()
            sess = tf.Session()
            summary_writer = tf.summary.FileWriter(
                "mlp_logs",
                graph_def=sess.graph_def
            )
            init_op = tf.global_variables_initializer()
            sess.run(init_op)

            # Training cycle
            for epoch in range(training_epochs):
                avg_cost = 0.
                total_batch = int(mnist.train.num_examples/batch_size)
                # Loop over all batches
                for i in range(total_batch):
                    minibatch_x, minibatch_y = mnist.train.next_batch(batch_size)
                    # Fit training using batch data
                    sess.run(train_op, feed_dict={x: minibatch_x, y: minibatch_y})
                    # Compute average loss
                    avg_cost += sess.run(cost, feed_dict={x: minibatch_x, y: minibatch_y})/total_batch
                # Display logs per epoch step
                if epoch % display_step == 0:
                    print("Epoch: {:04d} cost: {:.9f}".format(epoch+1, avg_cost))
                    accuracy = sess.run(eval_op, feed_dict={x: mnist.validation.images, y: mnist.validation.labels})
                    print("Validation Error: {}".format(1 - accuracy))
                    summary_str = sess.run(summary_op, feed_dict={x: minibatch_x, y: minibatch_y})
                    summary_writer.add_summary(summary_str, sess.run(global_step))
                    saver.save(sess, os.path.join("mlp_logs", "model-checkpoint"), global_step=global_step)

            print("Optimization Finished!")
            accuracy = sess.run(eval_op, feed_dict={x: mnist.test.images, y: mnist.test.labels})
            print("Test Accuracy: {}".format(accuracy))
W0822 17:21:57.726389 11828 deprecation.py:323] From <ipython-input-18-77c176d4a5e3>:39: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:

Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.

See `tf.nn.softmax_cross_entropy_with_logits_v2`.

W0822 17:21:58.263858 11828 writer.py:199] Passing a `GraphDef` to the SummaryWriter is deprecated. Pass a `Graph` object instead, such as `sess.graph`.
Epoch: 0001 cost: 1.155528759
Validation Error: 0.12980002164840698
Epoch: 0002 cost: 0.420003600
Validation Error: 0.09259998798370361
Epoch: 0003 cost: 0.324155134
Validation Error: 0.08319997787475586
Epoch: 0004 cost: 0.284075277
Validation Error: 0.07239997386932373
Epoch: 0005 cost: 0.257920302
Validation Error: 0.0690000057220459
Epoch: 0006 cost: 0.238601945
Validation Error: 0.06319999694824219
Epoch: 0007 cost: 0.222525908
Validation Error: 0.059800028800964355
Epoch: 0008 cost: 0.208743082
Validation Error: 0.05699998140335083
Epoch: 0009 cost: 0.196968288
Validation Error: 0.05320000648498535
Epoch: 0010 cost: 0.186194047
Validation Error: 0.05419999361038208
Epoch: 0011 cost: 0.176901473
Validation Error: 0.0493999719619751
Epoch: 0012 cost: 0.168219248
Validation Error: 0.04780000448226929
Epoch: 0013 cost: 0.160226422
Validation Error: 0.045400023460388184
Epoch: 0014 cost: 0.153010859
Validation Error: 0.04339998960494995
Epoch: 0015 cost: 0.146306994
Validation Error: 0.04079997539520264
Epoch: 0016 cost: 0.140271676
Validation Error: 0.03979998826980591
Epoch: 0017 cost: 0.134648749
Validation Error: 0.038999974727630615
Epoch: 0018 cost: 0.129483470
Validation Error: 0.037400007247924805
Epoch: 0019 cost: 0.124079336
Validation Error: 0.036400020122528076
Epoch: 0020 cost: 0.119929243
Validation Error: 0.03519999980926514
Epoch: 0021 cost: 0.115870998
Validation Error: 0.03519999980926514
Epoch: 0022 cost: 0.111597315
Validation Error: 0.03420001268386841
Epoch: 0023 cost: 0.107701490
Validation Error: 0.033399999141693115
Epoch: 0024 cost: 0.104228117
Validation Error: 0.03320002555847168
Epoch: 0025 cost: 0.100860158
Validation Error: 0.032199978828430176
Epoch: 0026 cost: 0.097493520
Validation Error: 0.03240001201629639
Epoch: 0027 cost: 0.094657076
Validation Error: 0.030399978160858154
Epoch: 0028 cost: 0.091641084
Validation Error: 0.03140002489089966
Epoch: 0029 cost: 0.088888735
Validation Error: 0.0307999849319458
Epoch: 0030 cost: 0.086376273
Validation Error: 0.030399978160858154
Epoch: 0031 cost: 0.083749135
Validation Error: 0.029600024223327637
Epoch: 0032 cost: 0.081360804
Validation Error: 0.02920001745223999
Epoch: 0033 cost: 0.079283832
Validation Error: 0.027000010013580322
Epoch: 0034 cost: 0.077078916
Validation Error: 0.027599990367889404
Epoch: 0035 cost: 0.074949517
Validation Error: 0.02799999713897705
Epoch: 0036 cost: 0.073132153
Validation Error: 0.027000010013580322
Epoch: 0037 cost: 0.071021939
Validation Error: 0.027000010013580322
Epoch: 0038 cost: 0.069247796
Validation Error: 0.027800023555755615
Epoch: 0039 cost: 0.067405452
Validation Error: 0.024800002574920654
Epoch: 0040 cost: 0.065590148
Validation Error: 0.02499997615814209
Epoch: 0041 cost: 0.064074420
Validation Error: 0.02460002899169922
Epoch: 0042 cost: 0.062505088
Validation Error: 0.026000022888183594
Epoch: 0043 cost: 0.061010278
Validation Error: 0.025399982929229736
Epoch: 0044 cost: 0.059406908
Validation Error: 0.0252000093460083
Epoch: 0045 cost: 0.058044656
Validation Error: 0.02460002899169922
Epoch: 0046 cost: 0.056558073
Validation Error: 0.024200022220611572
Epoch: 0047 cost: 0.055206590
Validation Error: 0.02679997682571411
Epoch: 0048 cost: 0.054026436
Validation Error: 0.024200022220611572
Epoch: 0049 cost: 0.052775049
Validation Error: 0.024399995803833008
Epoch: 0050 cost: 0.051546302
Validation Error: 0.02399998903274536
Epoch: 0051 cost: 0.050473685
Validation Error: 0.022599995136260986
Epoch: 0052 cost: 0.049346155
Validation Error: 0.023000001907348633
Epoch: 0053 cost: 0.048091512
Validation Error: 0.022599995136260986
Epoch: 0054 cost: 0.047058610
Validation Error: 0.022599995136260986
Epoch: 0055 cost: 0.045955510
Validation Error: 0.022599995136260986
Epoch: 0056 cost: 0.044946699
Validation Error: 0.022800028324127197
Epoch: 0057 cost: 0.044043614
Validation Error: 0.02240002155303955
Epoch: 0058 cost: 0.043016634
Validation Error: 0.022599995136260986
Epoch: 0059 cost: 0.042110117
Validation Error: 0.02219998836517334
Epoch: 0060 cost: 0.041231925
Validation Error: 0.02240002155303955
Epoch: 0061 cost: 0.040331570
Validation Error: 0.021799981594085693
Epoch: 0062 cost: 0.039442729
Validation Error: 0.022599995136260986
Epoch: 0063 cost: 0.038699011
Validation Error: 0.021000027656555176
Epoch: 0064 cost: 0.037814990
Validation Error: 0.021399974822998047
Epoch: 0065 cost: 0.037069053
Validation Error: 0.020799994468688965
Epoch: 0066 cost: 0.036292122
Validation Error: 0.02120000123977661
Epoch: 0067 cost: 0.035538642
Validation Error: 0.022000014781951904
Epoch: 0068 cost: 0.034835604
Validation Error: 0.021600008010864258
Epoch: 0069 cost: 0.034147033
Validation Error: 0.02060002088546753
Epoch: 0070 cost: 0.033449004
Validation Error: 0.021600008010864258
Epoch: 0071 cost: 0.032758468
Validation Error: 0.021799981594085693
Epoch: 0072 cost: 0.032086724
Validation Error: 0.02120000123977661
Epoch: 0073 cost: 0.031446635
Validation Error: 0.02039998769760132
Epoch: 0074 cost: 0.030869418
Validation Error: 0.020200014114379883
Epoch: 0075 cost: 0.030183723
Validation Error: 0.020799994468688965
Epoch: 0076 cost: 0.029549940
Validation Error: 0.021600008010864258
Epoch: 0077 cost: 0.029107898
Validation Error: 0.019999980926513672
Epoch: 0078 cost: 0.028354086
Validation Error: 0.02039998769760132
Epoch: 0079 cost: 0.027941003
Validation Error: 0.021000027656555176
Epoch: 0080 cost: 0.027281640
Validation Error: 0.019800007343292236
Epoch: 0081 cost: 0.026770020
Validation Error: 0.020799994468688965
Epoch: 0082 cost: 0.026376246
Validation Error: 0.019800007343292236
Epoch: 0083 cost: 0.025794807
Validation Error: 0.019200026988983154
Epoch: 0084 cost: 0.025225831
Validation Error: 0.01940000057220459
Epoch: 0085 cost: 0.024914008
Validation Error: 0.019599974155426025
Epoch: 0086 cost: 0.024441784
Validation Error: 0.01940000057220459
Epoch: 0087 cost: 0.023895256
Validation Error: 0.018599987030029297
Epoch: 0088 cost: 0.023490204
Validation Error: 0.019599974155426025
Epoch: 0089 cost: 0.023140182
Validation Error: 0.01940000057220459
Epoch: 0090 cost: 0.022644429
Validation Error: 0.01940000057220459
Epoch: 0091 cost: 0.022291003
Validation Error: 0.019800007343292236
Epoch: 0092 cost: 0.021830115
Validation Error: 0.018999993801116943
Epoch: 0093 cost: 0.021476939
Validation Error: 0.019200026988983154
Epoch: 0094 cost: 0.021022398
Validation Error: 0.018999993801116943
Epoch: 0095 cost: 0.020624073
Validation Error: 0.01819998025894165
Epoch: 0096 cost: 0.020309624
Validation Error: 0.018800020217895508
Epoch: 0097 cost: 0.019885141
Validation Error: 0.018599987030029297
Epoch: 0098 cost: 0.019596462
Validation Error: 0.018999993801116943
Epoch: 0099 cost: 0.019252348
Validation Error: 0.018999993801116943
Epoch: 0100 cost: 0.018953344
Validation Error: 0.018599987030029297
Epoch: 0101 cost: 0.018561574
Validation Error: 0.018599987030029297
Epoch: 0102 cost: 0.018243696
Validation Error: 0.01840001344680786
Epoch: 0103 cost: 0.017940210
Validation Error: 0.018800020217895508
Epoch: 0104 cost: 0.017697294
Validation Error: 0.018800020217895508
Epoch: 0105 cost: 0.017331894
Validation Error: 0.018599987030029297
Epoch: 0106 cost: 0.016986375
Validation Error: 0.018800020217895508
Epoch: 0107 cost: 0.016773813
Validation Error: 0.018800020217895508
Epoch: 0108 cost: 0.016466196
Validation Error: 0.018000006675720215
Epoch: 0109 cost: 0.016188323
Validation Error: 0.018800020217895508
Epoch: 0110 cost: 0.015915882
Validation Error: 0.018999993801116943
Epoch: 0111 cost: 0.015667328
Validation Error: 0.018599987030029297
Epoch: 0112 cost: 0.015409318
Validation Error: 0.018800020217895508
Epoch: 0113 cost: 0.015189957
Validation Error: 0.018000006675720215
Epoch: 0114 cost: 0.014954760
Validation Error: 0.01840001344680786
Epoch: 0115 cost: 0.014696587
Validation Error: 0.018599987030029297
Epoch: 0116 cost: 0.014470426
Validation Error: 0.01840001344680786
Epoch: 0117 cost: 0.014276701
Validation Error: 0.018599987030029297
Epoch: 0118 cost: 0.014021859
Validation Error: 0.018599987030029297
Epoch: 0119 cost: 0.013764962
Validation Error: 0.018599987030029297
Epoch: 0120 cost: 0.013568181
Validation Error: 0.018999993801116943
Epoch: 0121 cost: 0.013370951
Validation Error: 0.01940000057220459
Epoch: 0122 cost: 0.013135655
Validation Error: 0.018999993801116943
Epoch: 0123 cost: 0.012975959
Validation Error: 0.018000006675720215
Epoch: 0124 cost: 0.012741968
Validation Error: 0.01759999990463257
Epoch: 0125 cost: 0.012566599
Validation Error: 0.018800020217895508
Epoch: 0126 cost: 0.012395765
Validation Error: 0.01819998025894165
Epoch: 0127 cost: 0.012215430
Validation Error: 0.018599987030029297
Epoch: 0128 cost: 0.012006933
Validation Error: 0.01759999990463257
Epoch: 0129 cost: 0.011852338
Validation Error: 0.01819998025894165
Epoch: 0130 cost: 0.011645928
Validation Error: 0.017799973487854004
Epoch: 0131 cost: 0.011500965
Validation Error: 0.01819998025894165
Epoch: 0132 cost: 0.011368062
Validation Error: 0.01840001344680786
Epoch: 0133 cost: 0.011180046
Validation Error: 0.018599987030029297
Epoch: 0134 cost: 0.011021952
Validation Error: 0.01840001344680786
Epoch: 0135 cost: 0.010884800
Validation Error: 0.018800020217895508
Epoch: 0136 cost: 0.010663491
Validation Error: 0.01819998025894165
Epoch: 0137 cost: 0.010557894
Validation Error: 0.01840001344680786
Epoch: 0138 cost: 0.010429118
Validation Error: 0.01819998025894165
Epoch: 0139 cost: 0.010301042
Validation Error: 0.019200026988983154
Epoch: 0140 cost: 0.010137712
Validation Error: 0.01840001344680786
Epoch: 0141 cost: 0.010008107
Validation Error: 0.018599987030029297
Epoch: 0142 cost: 0.009877960
Validation Error: 0.01840001344680786
Epoch: 0143 cost: 0.009712499
Validation Error: 0.01840001344680786
Epoch: 0144 cost: 0.009599588
Validation Error: 0.018599987030029297
Epoch: 0145 cost: 0.009465550
Validation Error: 0.01840001344680786
Epoch: 0146 cost: 0.009346498
Validation Error: 0.018800020217895508
Epoch: 0147 cost: 0.009236482
Validation Error: 0.018599987030029297
Epoch: 0148 cost: 0.009109201
Validation Error: 0.017400026321411133
Epoch: 0149 cost: 0.009004397
Validation Error: 0.01840001344680786
Epoch: 0150 cost: 0.008893338
Validation Error: 0.018999993801116943
Epoch: 0151 cost: 0.008799606
Validation Error: 0.018599987030029297
Epoch: 0152 cost: 0.008664483
Validation Error: 0.01840001344680786
Epoch: 0153 cost: 0.008578646
Validation Error: 0.01840001344680786
Epoch: 0154 cost: 0.008458314
Validation Error: 0.01840001344680786
Epoch: 0155 cost: 0.008329224
Validation Error: 0.018999993801116943
Epoch: 0156 cost: 0.008253439
Validation Error: 0.01819998025894165
Epoch: 0157 cost: 0.008156153
Validation Error: 0.018599987030029297
Epoch: 0158 cost: 0.008048138
Validation Error: 0.017799973487854004
Epoch: 0159 cost: 0.007967423
Validation Error: 0.01819998025894165
Epoch: 0160 cost: 0.007825943
Validation Error: 0.01819998025894165
Epoch: 0161 cost: 0.007769434
Validation Error: 0.01840001344680786
Epoch: 0162 cost: 0.007703736
Validation Error: 0.01840001344680786
Epoch: 0163 cost: 0.007598549
Validation Error: 0.019200026988983154
Epoch: 0164 cost: 0.007500803
Validation Error: 0.018599987030029297
Epoch: 0165 cost: 0.007424784
Validation Error: 0.018000006675720215
Epoch: 0166 cost: 0.007346010
Validation Error: 0.018599987030029297
Epoch: 0167 cost: 0.007256393
Validation Error: 0.018999993801116943
Epoch: 0168 cost: 0.007182349
Validation Error: 0.017799973487854004
Epoch: 0169 cost: 0.007092040
Validation Error: 0.017799973487854004
Epoch: 0170 cost: 0.007011024
Validation Error: 0.01840001344680786
Epoch: 0171 cost: 0.006927173
Validation Error: 0.01819998025894165
Epoch: 0172 cost: 0.006857768
Validation Error: 0.018800020217895508
Epoch: 0173 cost: 0.006788897
Validation Error: 0.01840001344680786
Epoch: 0174 cost: 0.006714331
Validation Error: 0.01819998025894165
Epoch: 0175 cost: 0.006643480
Validation Error: 0.01840001344680786
Epoch: 0176 cost: 0.006589544
Validation Error: 0.018599987030029297
Epoch: 0177 cost: 0.006509996
Validation Error: 0.018800020217895508
Epoch: 0178 cost: 0.006438364
Validation Error: 0.01840001344680786
Epoch: 0179 cost: 0.006364934
Validation Error: 0.018599987030029297
Epoch: 0180 cost: 0.006300597
Validation Error: 0.018800020217895508
Epoch: 0181 cost: 0.006243806
Validation Error: 0.018999993801116943
Epoch: 0182 cost: 0.006183430
Validation Error: 0.018000006675720215
Epoch: 0183 cost: 0.006111193
Validation Error: 0.017799973487854004
Epoch: 0184 cost: 0.006076603
Validation Error: 0.017799973487854004
Epoch: 0185 cost: 0.005999717
Validation Error: 0.01819998025894165
Epoch: 0186 cost: 0.005945564
Validation Error: 0.018800020217895508
Epoch: 0187 cost: 0.005874727
Validation Error: 0.018000006675720215
Epoch: 0188 cost: 0.005831263
Validation Error: 0.018000006675720215
Epoch: 0189 cost: 0.005768126
Validation Error: 0.018599987030029297
Epoch: 0190 cost: 0.005707011
Validation Error: 0.01840001344680786
Epoch: 0191 cost: 0.005669763
Validation Error: 0.01840001344680786
Epoch: 0192 cost: 0.005603307
Validation Error: 0.018800020217895508
Epoch: 0193 cost: 0.005554478
Validation Error: 0.018999993801116943
Epoch: 0194 cost: 0.005472773
Validation Error: 0.018599987030029297
Epoch: 0195 cost: 0.005460750
Validation Error: 0.01840001344680786
Epoch: 0196 cost: 0.005397154
Validation Error: 0.01840001344680786
Epoch: 0197 cost: 0.005340563
Validation Error: 0.01840001344680786
Epoch: 0198 cost: 0.005309242
Validation Error: 0.019200026988983154
Epoch: 0199 cost: 0.005250989
Validation Error: 0.018599987030029297
Epoch: 0200 cost: 0.005202763
Validation Error: 0.01840001344680786
Epoch: 0201 cost: 0.005159452
Validation Error: 0.01819998025894165
Epoch: 0202 cost: 0.005099370
Validation Error: 0.01819998025894165
Epoch: 0203 cost: 0.005062916
Validation Error: 0.018599987030029297
Epoch: 0204 cost: 0.005041177
Validation Error: 0.018599987030029297
Epoch: 0205 cost: 0.004988176
Validation Error: 0.01840001344680786
Epoch: 0206 cost: 0.004937512
Validation Error: 0.018999993801116943
Epoch: 0207 cost: 0.004894424
Validation Error: 0.01840001344680786
Epoch: 0208 cost: 0.004852831
Validation Error: 0.01819998025894165
Epoch: 0209 cost: 0.004809991
Validation Error: 0.01840001344680786
Epoch: 0210 cost: 0.004779147
Validation Error: 0.018999993801116943
Epoch: 0211 cost: 0.004726758
Validation Error: 0.018999993801116943
Epoch: 0212 cost: 0.004695122
Validation Error: 0.01840001344680786
Epoch: 0213 cost: 0.004659180
Validation Error: 0.01840001344680786
Epoch: 0214 cost: 0.004621285
Validation Error: 0.018599987030029297
Epoch: 0215 cost: 0.004580638
Validation Error: 0.01840001344680786
Epoch: 0216 cost: 0.004540453
Validation Error: 0.01819998025894165
Epoch: 0217 cost: 0.004494600
Validation Error: 0.01819998025894165
Epoch: 0218 cost: 0.004470402
Validation Error: 0.018599987030029297
Epoch: 0219 cost: 0.004436185
Validation Error: 0.018800020217895508
Epoch: 0220 cost: 0.004396487
Validation Error: 0.01840001344680786
Epoch: 0221 cost: 0.004358522
Validation Error: 0.01819998025894165
Epoch: 0222 cost: 0.004329594
Validation Error: 0.01840001344680786
Epoch: 0223 cost: 0.004301302
Validation Error: 0.01819998025894165
Epoch: 0224 cost: 0.004257170
Validation Error: 0.018999993801116943
Epoch: 0225 cost: 0.004225837
Validation Error: 0.01840001344680786
Epoch: 0226 cost: 0.004209440
Validation Error: 0.018000006675720215
Epoch: 0227 cost: 0.004165300
Validation Error: 0.018000006675720215
Epoch: 0228 cost: 0.004128867
Validation Error: 0.01840001344680786
Epoch: 0229 cost: 0.004106564
Validation Error: 0.01819998025894165
Epoch: 0230 cost: 0.004073126
Validation Error: 0.018599987030029297
Epoch: 0231 cost: 0.004036251
Validation Error: 0.01819998025894165
Epoch: 0232 cost: 0.004014350
Validation Error: 0.01840001344680786
Epoch: 0233 cost: 0.003985934
Validation Error: 0.01840001344680786
Epoch: 0234 cost: 0.003956959
Validation Error: 0.018599987030029297
Epoch: 0235 cost: 0.003927414
Validation Error: 0.01840001344680786
Epoch: 0236 cost: 0.003889599
Validation Error: 0.01840001344680786
Epoch: 0237 cost: 0.003865099
Validation Error: 0.01819998025894165
Epoch: 0238 cost: 0.003838034
Validation Error: 0.01840001344680786
Epoch: 0239 cost: 0.003807513
Validation Error: 0.01819998025894165
Epoch: 0240 cost: 0.003790173
Validation Error: 0.01840001344680786
Epoch: 0241 cost: 0.003757570
Validation Error: 0.01819998025894165
Epoch: 0242 cost: 0.003730570
Validation Error: 0.01840001344680786
Epoch: 0243 cost: 0.003709705
Validation Error: 0.018000006675720215
Epoch: 0244 cost: 0.003682990
Validation Error: 0.018000006675720215
Epoch: 0245 cost: 0.003662317
Validation Error: 0.01819998025894165
Epoch: 0246 cost: 0.003620372
Validation Error: 0.018599987030029297
Epoch: 0247 cost: 0.003605607
Validation Error: 0.01840001344680786
Epoch: 0248 cost: 0.003583322
Validation Error: 0.01819998025894165
Epoch: 0249 cost: 0.003557839
Validation Error: 0.01840001344680786
Epoch: 0250 cost: 0.003533291
Validation Error: 0.01840001344680786
Epoch: 0251 cost: 0.003502273
Validation Error: 0.01819998025894165
Epoch: 0252 cost: 0.003489226
Validation Error: 0.018000006675720215
Epoch: 0253 cost: 0.003467938
Validation Error: 0.017799973487854004
Epoch: 0254 cost: 0.003444141
Validation Error: 0.01840001344680786
Epoch: 0255 cost: 0.003419023
Validation Error: 0.018599987030029297
Epoch: 0256 cost: 0.003393180
Validation Error: 0.018599987030029297
Epoch: 0257 cost: 0.003374406
Validation Error: 0.01819998025894165
Epoch: 0258 cost: 0.003347315
Validation Error: 0.01840001344680786
Epoch: 0259 cost: 0.003334690
Validation Error: 0.018000006675720215
Epoch: 0260 cost: 0.003310092
Validation Error: 0.01840001344680786
Epoch: 0261 cost: 0.003285280
Validation Error: 0.018000006675720215
Epoch: 0262 cost: 0.003266898
Validation Error: 0.01819998025894165
Epoch: 0263 cost: 0.003249319
Validation Error: 0.018000006675720215
Epoch: 0264 cost: 0.003231086
Validation Error: 0.01819998025894165
Epoch: 0265 cost: 0.003206476
Validation Error: 0.018000006675720215
Epoch: 0266 cost: 0.003186480
Validation Error: 0.018599987030029297
Epoch: 0267 cost: 0.003164600
Validation Error: 0.01819998025894165
Epoch: 0268 cost: 0.003152315
Validation Error: 0.017799973487854004
Epoch: 0269 cost: 0.003126550
Validation Error: 0.018599987030029297
Epoch: 0270 cost: 0.003109326
Validation Error: 0.01819998025894165
Epoch: 0271 cost: 0.003090129
Validation Error: 0.01819998025894165
Epoch: 0272 cost: 0.003075481
Validation Error: 0.018999993801116943
Epoch: 0273 cost: 0.003051823
Validation Error: 0.01819998025894165
Epoch: 0274 cost: 0.003032597
Validation Error: 0.01840001344680786
Epoch: 0275 cost: 0.003014216
Validation Error: 0.01819998025894165
Epoch: 0276 cost: 0.002998491
Validation Error: 0.018000006675720215
Epoch: 0277 cost: 0.002977609
Validation Error: 0.01819998025894165
Epoch: 0278 cost: 0.002963779
Validation Error: 0.018599987030029297
Epoch: 0279 cost: 0.002945563
Validation Error: 0.018599987030029297
Epoch: 0280 cost: 0.002931028
Validation Error: 0.018000006675720215
Epoch: 0281 cost: 0.002909412
Validation Error: 0.018599987030029297
Epoch: 0282 cost: 0.002892998
Validation Error: 0.01819998025894165
Epoch: 0283 cost: 0.002874069
Validation Error: 0.018599987030029297
Epoch: 0284 cost: 0.002858964
Validation Error: 0.01819998025894165
Epoch: 0285 cost: 0.002843738
Validation Error: 0.01840001344680786
Epoch: 0286 cost: 0.002826844
Validation Error: 0.01819998025894165
Epoch: 0287 cost: 0.002813726
Validation Error: 0.018599987030029297
Epoch: 0288 cost: 0.002793939
Validation Error: 0.01819998025894165
Epoch: 0289 cost: 0.002781566
Validation Error: 0.018599987030029297
Epoch: 0290 cost: 0.002766684
Validation Error: 0.018000006675720215
Epoch: 0291 cost: 0.002744857
Validation Error: 0.017799973487854004
Epoch: 0292 cost: 0.002736198
Validation Error: 0.018599987030029297
Epoch: 0293 cost: 0.002715864
Validation Error: 0.018800020217895508
Epoch: 0294 cost: 0.002702934
Validation Error: 0.018599987030029297
Epoch: 0295 cost: 0.002692321
Validation Error: 0.018000006675720215
Epoch: 0296 cost: 0.002674153
Validation Error: 0.018000006675720215
Epoch: 0297 cost: 0.002661825
Validation Error: 0.01819998025894165
Epoch: 0298 cost: 0.002643820
Validation Error: 0.01819998025894165
Epoch: 0299 cost: 0.002625672
Validation Error: 0.018800020217895508
Epoch: 0300 cost: 0.002614849
Validation Error: 0.01840001344680786
Optimization Finished!
Test Accuracy: 0.9793000221252441
In [ ]: