入力層:784画素の手書き数字画像 -> 中間層:ReLU関数 -> 出力層:softmax関数 -> 損失関数:二乗誤差
import tensorflow as tf
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.examples.tutorials.mnist import input_data
def variable_summaries(var):
""" Tensor Boardで変数のサマリーをビジュアル化する """
with tf.name_scope('summaries'):
mean = tf.reduce_mean(var)
tf.summary.scalar('mean', mean)
with tf.name_scope('stddev'):
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
tf.summary.scalar('stddev', stddev)
tf.summary.scalar('max', tf.reduce_max(var))
tf.summary.scalar('min', tf.reduce_min(var))
tf.summary.histogram('histogram', var)
mnist = input_data.read_data_sets('data/', one_hot=True)
with tf.name_scope("input"):
x = tf.placeholder(tf.float32, [None, 784], name="x-input")
y = tf.placeholder(tf.float32, [None, 10], name="y-input")
with tf.name_scope("layer1"):
with tf.name_scope("weights"):
w_1 = tf.Variable(tf.truncated_normal([784,64], stddev=0.1), name="w1") # 重み
variable_summaries(w_1)
with tf.name_scope("biases"):
b_1 = tf.Variable(tf.zeros([64]), name="b1") # バイアス
variable_summaries(b_1)
with tf.name_scope("x_matmul_w_plus_b"):
pre_act_1 = tf.matmul(x, w_1) + b_1
tf.summary.histogram("pre_activations", pre_act_1)
h_1 = tf.nn.relu(pre_act_1, name="activation") # ReLU
tf.summary.histogram("activations", h_1)
with tf.name_scope("layer2"):
with tf.name_scope("weights"):
w_2 = tf.Variable(tf.truncated_normal([64, 10], stddev=0.1), name="w2") # 重み
variable_summaries(w_2)
with tf.name_scope("biases"):
b_2 = tf.Variable(tf.zeros([10]), name="b2") # バイアス
variable_summaries(b_2)
with tf.name_scope("x_matmul_w_plus_b"):
pre_out_2 = tf.matmul(h_1, w_2) + b_2
tf.summary.histogram("pre_out", pre_out_2)
out = tf.nn.softmax(pre_out_2, name="out")
tf.summary.histogram("out", out)
with tf.name_scope("loss"):
loss = tf.reduce_mean(tf.square(y - out)) # 損失関数(2乗誤差)
tf.summary.scalar("loss", loss)
with tf.name_scope("train"):
train_step = tf.train.AdagradOptimizer(0.5).minimize(loss) # 重み(とバイアス)の調整 学習率 = 0.5
with tf.name_scope("accuracy"):
with tf.name_scope("correct_prediction"):
correct = tf.equal(tf.argmax(out, 1), tf.argmax(y, 1))
with tf.name_scope("accuracy"):
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
tf.summary.scalar("accuracy", accuracy)
with tf.Session() as sess:
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter("C:/tmp/mnist/train", sess.graph)
test_writer = tf.summary.FileWriter("C:/tmp/mnist/test")
sess.run(tf.global_variables_initializer()) # 変数初期化
test_images = mnist.test.images
test_labes = mnist.test.labels
for step in range(1000):
train_images, train_labels = mnist.train.next_batch(50)
summary, _ = sess.run([merged, train_step], feed_dict={x:train_images, y:train_labels})
#sess.run(train_step, feed_dict={x:train_images, y:train_labels})
train_writer.add_summary(summary, step)
if step % 10 == 0:
summary, acc_val = sess.run([merged, accuracy], feed_dict={x:test_images, y:test_labes})
#acc_val = sess.run(accuracy, feed_dict={x:test_images, y:test_labes})
test_writer.add_summary(summary, step)
print('Step %d: accuracy = %.2f' % (step, acc_val))
print('w1 = %s, b1 = %s, w2 = %s, b2 = %s' % (sess.run(w_1)[0][0], sess.run(b_1)[0], sess.run(w_2)[0][0], sess.run(b_2)[0]))
1.1の定義をそのまま使用。
with tf.name_scope("input"):
x = tf.placeholder(tf.float32, [None,784], name="x-input")
y = tf.placeholder(tf.float32, [None, 10], name="y-input")
img = tf.reshape(x, [-1,28,28,1])
f1 = tf.Variable(tf.truncated_normal([5,5,1,32], stddev=0.1))
conv1 = tf.nn.conv2d(img, f1, strides=[1,1,1,1], padding='SAME')
with tf.name_scope("layer1"):
with tf.name_scope("biases"):
b1 = tf.Variable(tf.constant(0.1, shape=[32]), name="b1")
variable_summaries(b1)
with tf.name_scope("convolutions"):
h_conv1 = tf.nn.relu(conv1 + b1)
tf.summary.histogram("conv1", h_conv1)
with tf.name_scope("maxpool"):
h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
tf.summary.histogram("maxpool1", h_pool1)
with tf.name_scope("layer2"):
with tf.name_scope("filters"):
f2 = tf.Variable(tf.truncated_normal([5,5,32,64], stddev=0.1), name="f2")
variable_summaries(f2)
conv2 = tf.nn.conv2d(h_pool1, f2, strides=[1,1,1,1], padding='SAME')
with tf.name_scope("biases"):
b2 = tf.Variable(tf.constant(0.1, shape=[64]), name="b2")
variable_summaries(b2)
with tf.name_scope("convolutions"):
h_conv2 = tf.nn.relu(conv2 + b2)
tf.summary.histogram("conv2", h_conv2)
with tf.name_scope("maxpool"):
h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
tf.summary.histogram("maxpool2", h_pool2)
with tf.name_scope("layer3"):
h_pool2_flat = tf.reshape(h_pool2, [-1,7*7*64])
with tf.name_scope("weight"):
w_fc1 = tf.Variable(tf.truncated_normal([7*7*64,1024], stddev=0.1), name="w1")
variable_summaries(w_fc1)
with tf.name_scope("biases"):
b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]), name="b1")
variable_summaries(b_fc1)
with tf.name_scope("x_matmul_w_plus_b"):
pre_act_1 = tf.matmul(h_pool2_flat, w_fc1) + b_fc1
tf.summary.histogram("pre_activations", pre_act_1)
h_fc1 = tf.nn.relu(pre_act_1)
tf.summary.histogram("activations", h_fc1)
with tf.name_scope("layer4"):
with tf.name_scope("weight"):
w_fc2 = tf.Variable(tf.truncated_normal([1024,10], stddev=0.1), name="w2")
variable_summaries(w_fc2)
with tf.name_scope("biases"):
b_fc2 = tf.Variable(tf.constant(0.1, shape=[10]), name="b2")
variable_summaries(b_fc2)
with tf.name_scope("x_matmul_w_plus_b"):
pre_out_2 = tf.matmul(h_fc1, w_fc2) + b_fc2
tf.summary.histogram("pre_out", pre_out_2)
out = tf.nn.softmax(pre_out_2, name="out")
tf.summary.histogram("out", out)
with tf.name_scope("loss"):
loss = tf.reduce_mean(-tf.reduce_sum(y * tf.log(out), reduction_indices=[1])) # 交差エントロピー誤差
tf.summary.scalar("loss", loss)
with tf.name_scope("train"):
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss) # 重み(とバイアス)の調整 学習率 = 0.01
with tf.name_scope("accuracy"):
with tf.name_scope("correct_prediction"):
correct = tf.equal(tf.argmax(out, 1), tf.argmax(y, 1))
with tf.name_scope("accuracy"):
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
tf.summary.scalar("accuracy", accuracy)
with tf.Session() as sess:
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter("C:/tmp/mnist2/train", sess.graph)
test_writer = tf.summary.FileWriter("C:/tmp/mnist2/test")
sess.run(tf.global_variables_initializer()) # 変数初期化
test_images = mnist.test.images
test_labes = mnist.test.labels
for step in range(1000):
train_images, train_labels = mnist.train.next_batch(50)
summary, _ = sess.run([merged, train_step], feed_dict={x:train_images, y:train_labels})
#sess.run(train_step, feed_dict={x:train_images, y:train_labels})
train_writer.add_summary(summary, step)
if step % 10 == 0:
summary, acc_val = sess.run([merged, accuracy], feed_dict={x:test_images, y:test_labes})
#acc_val = sess.run(accuracy, feed_dict={x:test_images, y:test_labes})
test_writer.add_summary(summary, step)
print('Step %d: accuracy = %.2f' % (step, acc_val))
print('w1 = %s, b1 = %s, w2 = %s, b2 = %s' % (sess.run(w_fc1)[0][0], sess.run(b_fc1)[0], sess.run(w_fc2)[0][0], sess.run(b_fc2)[0]))