<jupyter_start><jupyter_text>MNIST softmax with `tf.summary`* MNIST data를 가지고 softmax classifier를 만들어보자.  * [참고 소스: mnist_softmax.py in verion 1.4](https://github.com/tensorflow/tensorflow/blob/r1.4/tensorflow/examples/tutorials/mnist/mnist_softmax.py)  * `tf.summary`를 이용하여 트레이닝 상황을 모니터링해보자.  Import modules<jupyter_code>"""A very simple MNIST classifier.
See extensive documentation at
https://www.tensorflow.org/get_started/mnist/beginners in version 1.4
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import time
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from IPython.display import clear_output

import tensorflow as tf

sess_config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
os.environ["CUDA_VISIBLE_DEVICES"]="0"<jupyter_output>/home/lab4all/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters<jupyter_text>Import data<jupyter_code># Load training and eval data from tf.keras
(train_data, train_labels), (test_data, test_labels) = \
    tf.keras.datasets.mnist.load_data()

train_data = train_data / 255.
train_data = train_data.reshape(-1, 784)
train_labels = np.asarray(train_labels, dtype=np.int32)

test_data = test_data / 255.
test_data = test_data.reshape(-1, 784)
test_labels = np.asarray(test_labels, dtype=np.int32)<jupyter_output><empty_output><jupyter_text>Show the MNIST<jupyter_code>index = 1234
print("label = {}".format(train_labels[index]))
plt.imshow(train_data[index].reshape(28, 28))
plt.colorbar()
#plt.gca().grid(False)
plt.show()<jupyter_output>label = 3<jupyter_text>Set up dataset with `tf.data` input pipeline `tf.data.Dataset` and Transformation<jupyter_code>tf.set_random_seed(219)
batch_size = 32
max_epochs = 1

# for train
train_dataset = tf.data.Dataset.from_tensor_slices((train_data, train_labels))
train_dataset = train_dataset.shuffle(buffer_size = 10000)
train_dataset = train_dataset.repeat(count = max_epochs)
train_dataset = train_dataset.batch(batch_size = batch_size)
print(train_dataset)

# for test
test_dataset = tf.data.Dataset.from_tensor_slices((test_data, test_labels))
test_dataset = test_dataset.batch(batch_size = len(test_data))
print(test_dataset)<jupyter_output><BatchDataset shapes: ((?, 784), (?,)), types: (tf.float64, tf.int32)>
<BatchDataset shapes: ((?, 784), (?,)), types: (tf.float64, tf.int32)><jupyter_text>Define Iterator<jupyter_code># tf.data.Iterator.from_string_handle의 output_shapes는 default = None이지만 꼭 값을 넣는 게 좋음
handle = tf.placeholder(tf.string, shape=[])
iterator = tf.data.Iterator.from_string_handle(handle,
                                               train_dataset.output_types,
                                               train_dataset.output_shapes)
x, y = iterator.get_next()
x = tf.cast(x, dtype = tf.float32)
y = tf.cast(y, dtype = tf.int32)<jupyter_output><empty_output><jupyter_text>Create the model<jupyter_code>#x = tf.placeholder(tf.float32, [None, 784])
W = tf.get_variable(name='weights', shape=[784, 10], initializer=tf.zeros_initializer)
b = tf.get_variable(name='bias', shape=[10], initializer=tf.zeros_initializer)
y_pred = tf.matmul(x, W) + b

# for summary images
x_image = tf.reshape(x, [-1, 28, 28, 1])<jupyter_output><empty_output><jupyter_text>Define loss and optimizer<jupyter_code>y_one_hot = tf.one_hot(y, depth=10)
cross_entropy = tf.losses.softmax_cross_entropy(onehot_labels=y_one_hot,
                                                logits=y_pred)
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)<jupyter_output><empty_output><jupyter_text>Assign `tf.summary.FileWriter`<jupyter_code>graph_location = 'graphs/04.mnist.summary'
print('Saving graph to: %s' % graph_location)
train_writer = tf.summary.FileWriter(graph_location)
train_writer.add_graph(tf.get_default_graph())<jupyter_output>Saving graph to: graphs/04.mnist.summary<jupyter_text>`tf.summary`<jupyter_code>with tf.name_scope('summaries'):
  tf.summary.scalar('loss/cross_entropy', cross_entropy)
  tf.summary.image('images', x_image)
  for var in tf.trainable_variables():
    tf.summary.histogram(var.op.name, var)
  # merge all summaries
  summary_op = tf.summary.merge_all()<jupyter_output><empty_output><jupyter_text>`tf.Session()` and train<jupyter_code>sess = tf.Session(config=sess_config)
sess.run(tf.global_variables_initializer())

# train_iterator
train_iterator = train_dataset.make_one_shot_iterator()
train_handle = sess.run(train_iterator.string_handle())

# Train
step = 1
loss_history = []

start_time = time.time()
while True:
  try:
    _, loss = sess.run([train_step, cross_entropy],
                       feed_dict={handle: train_handle})
    loss_history.append(loss)

    if step % 100 == 0:
      clear_output(wait=True)
      epochs = batch_size * step / float(len(train_data))
      print("epochs: {:.2f}, step: {}, loss: {}".format(epochs, step, loss))

      # summary
      summary_str = sess.run(summary_op, feed_dict={handle: train_handle})
      train_writer.add_summary(summary_str, global_step=step)

    step += 1

  except tf.errors.OutOfRangeError:
    print("End of dataset")  # ==> "End of dataset"
    break
    
print("training done!")
print("Elapsed time: {}".format(time.time() - start_time))<jupyter_output>epochs: 0.96, step: 1800, loss: 0.18183940649032593
End of dataset
training done!
Elapsed time: 2.3740274906158447<jupyter_text>Plot the loss funtion<jupyter_code>plt.plot(loss_history, label='loss')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Test trained model* test accuracy: 0.9166 for 1 epochs<jupyter_code># test_iterator
test_iterator = test_dataset.make_one_shot_iterator()
test_handle = sess.run(test_iterator.string_handle())
correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.cast(y, tf.int64))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("test accuracy:", sess.run(accuracy, feed_dict={handle: test_handle}))<jupyter_output>test accuracy: 0.9166<jupyter_text>Plot test set<jupyter_code>np.random.seed(219)
test_batch_size = 16
batch_index = np.random.choice(len(test_data), size=test_batch_size, replace=False)

batch_xs = test_data[batch_index]
batch_ys = test_labels[batch_index]
y_pred_ = sess.run(y_pred, feed_dict={x: batch_xs})

fig = plt.figure(figsize=(16, 10))
for i, (px, py) in enumerate(zip(batch_xs, y_pred_)):
  p = fig.add_subplot(4, 8, i+1)
  if np.argmax(py) == batch_ys[i]:
    p.set_title("y_pred: {}".format(np.argmax(py)), color='blue')
  else:
    p.set_title("y_pred: {}".format(np.argmax(py)), color='red')
  p.imshow(px.reshape(28, 28))
  p.axis('off')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1310763888888889, 'fraction_numerical': 0.02589699074074074, 'mean_word_length': 5.68568665377176, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 50, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '185115', 'license': 'Apache-2.0', 'n_tokens_mistral': 2675, 'n_tokens_neox': 2550, 'n_words': 439, 'path': 'tf.version.1/02.regression/04.mnist.summary.ipynb', 'repo_name': 'jinhwanhan/tensorflow.tutorials', 'size': '6912'}