<jupyter_start><jupyter_text>`tf.train.Saver`* code `02_mnist_cnn_with_slim.ipynb`를 바탕으로 모델 save 및 load (restore)를 해보자.* [`tf.contrib.slim`](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim) 참고  Import modules<jupyter_code>"""A very simple MNIST classifier.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import time
import numpy as np

import tensorflow as tf

slim = tf.contrib.slim

sess_config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))

np.random.seed(219)
tf.set_random_seed(219)<jupyter_output><empty_output><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_labels = np.asarray(train_labels, dtype=np.int32)

test_data = test_data / 255.
test_labels = np.asarray(test_labels, dtype=np.int32)

# for fast learning with small dataset
N = 200
train_data = train_data[:N]
train_labels = train_labels[:N]<jupyter_output><empty_output><jupyter_text>Show the MNIST<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

index = 0
print("label = {}".format(train_labels[index]))
plt.imshow(train_data[index].reshape(28, 28), cmap='gray')
plt.show()<jupyter_output><empty_output><jupyter_text>Set up dataset with `tf.data` create input pipeline with `tf.data.Dataset`<jupyter_code>batch_size = 32

# 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.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.shuffle(buffer_size = 10000)
test_dataset = test_dataset.batch(batch_size = len(test_data))
print(test_dataset)<jupyter_output><empty_output><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>def cnn_model_fn(x):
  """Model function for CNN.
  Args:
    x: input images
    mode: boolean whether trainig mode or test mode
    
  Returns:
    logits: unnormalized score funtion
  """
  # Input Layer
  # Reshape X to 4-D tensor: [batch_size, width, height, channels]
  # MNIST images are 28x28 pixels, and have one color channel
  with tf.name_scope('reshape'):
    x_image = tf.reshape(x, [-1, 28, 28, 1])

  # Convolutional Layer #1
  # Input Tensor Shape: [batch_size, 28, 28, 1]
  # Output Tensor Shape: [batch_size, 28, 28, 32]
  # 여기를 직접 채워 넣으시면 됩니다.
  conv1 = slim.conv2d(x_image, 32, [5, 5], scope='conv1')
                      

  # Pooling Layer #1
  # Input Tensor Shape: [batch_size, 28, 28, 32]
  # Output Tensor Shape: [batch_size, 14, 14, 32]
  # 여기를 직접 채워 넣으시면 됩니다.
  pool1 = slim.max_pool2d(conv1, [2, 2], scope='pool1')
  
  # Convolutional Layer #2
  # Input Tensor Shape: [batch_size, 14, 14, 32]
  # Output Tensor Shape: [batch_size, 14, 14, 64]
  # 여기를 직접 채워 넣으시면 됩니다.
  conv2 = slim.conv2d(pool1, 64, [5, 5], scope='conv2')

  # Pooling Layer #2
  # Second max pooling layer with a 2x2 filter and stride of 2
  # Input Tensor Shape: [batch_size, 14, 14, 64]
  # Output Tensor Shape: [batch_size, 7, 7, 64]
  # 여기를 직접 채워 넣으시면 됩니다.
  pool2 = slim.max_pool2d(conv2, [2, 2], scope='pool2')

  # Flatten tensor into a batch of vectors
  # Input Tensor Shape: [batch_size, 7, 7, 64]
  # Output Tensor Shape: [batch_size, 7 * 7 * 64]
  # 여기를 직접 채워 넣으시면 됩니다.
  pool2_flat = slim.flatten(pool2, scope='flatten')
  
  # Fully connected Layer
  # Input Tensor Shape: [batch_size, 7 * 7 * 64]
  # Output Tensor Shape: [batch_size, 1024]
  # 여기를 직접 채워 넣으시면 됩니다.
  fc1 = slim.fully_connected(pool2_flat, 1024, scope='fc1')

  # Add dropout operation; 0.6 probability that element will be kept
  is_training = tf.placeholder(tf.bool)
  # 여기를 직접 채워 넣으시면 됩니다.
  fc1_drop = slim.dropout(fc1, keep_prob=0.6, is_training=is_training, scope='dropout')

  # Logits layer
  # Input Tensor Shape: [batch_size, 1024]
  # Output Tensor Shape: [batch_size, 10]
  # 여기를 직접 채워 넣으시면 됩니다.
  logits = slim.fully_connected(fc1_drop, 10, activation_fn=None, scope='logits')
  
  return logits, is_training, x_image<jupyter_output><empty_output><jupyter_text>Build the model<jupyter_code>logits, is_training, x_image = cnn_model_fn(x)<jupyter_output><empty_output><jupyter_text>Define loss and optimizer<jupyter_code># 여기를 직접 채워 넣으시면 됩니다.
cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=y, logits=logits)

train_op = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)<jupyter_output><empty_output><jupyter_text>Assign `tf.summary.FileWriter`<jupyter_code>train_dir = 'graphs/train/'
print('Saving graph to: %s' % train_dir)
train_writer = tf.summary.FileWriter(train_dir)
train_writer.add_graph(tf.get_default_graph())<jupyter_output><empty_output><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.train.Saver````pythontf.train.Saver.save(sess, save_path, global_step=None...)tf.train.Saver.restore(sess, save_path)```<jupyter_code># Create a saver object
saver = tf.train.Saver()<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_initializable_iterator()
train_handle = sess.run(train_iterator.string_handle())

# Train
max_epochs = 6
step = 0
for epochs in range(max_epochs):
  # 여기를 직접 채워 넣으시면 됩니다.
  sess.run(train_iterator.initializer)

  start_time = time.time()
  while True:
    try:
      # 여기를 직접 채워 넣으시면 됩니다.
      _, loss = sess.run([train_op, cross_entropy],
                         feed_dict={handle: train_handle,
                                    is_training: True})
      if step % 10 == 0:
        print("step: {}, loss: {}".format(step, loss))
        
        # summary
        summary_str = sess.run(summary_op,
                               feed_dict={handle: train_handle,
                                          is_training: False})
        train_writer.add_summary(summary_str, global_step=step)
        
      step += 1

    except tf.errors.OutOfRangeError:
      print("  End of dataset")  # ==> "End of dataset"
      break

  # Save a model per every one epoch in periodically
  if epochs % 2 == 0:
    print("    Save model at {} epochs".format(epochs))
    saver.save(sess, train_dir + 'model.ckpt', global_step=step)
    
  print("  Epochs: {} Elapsed time: {}".format(epochs, time.time() - start_time))
  print("\n")

train_writer.close()
print("training done!")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1189710610932476, 'fraction_numerical': 0.024383708467309754, 'mean_word_length': 4.205718270571827, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 50, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '369688', 'license': 'Apache-2.0', 'n_tokens_mistral': 2979, 'n_tokens_neox': 2901, 'n_words': 596, 'path': 'week03/03_tf.train.Saver.save.ipynb', 'repo_name': 'modulabs/modu-tensorflow', 'size': '7464'}