TensorFlow 2 MNIST Examples

 

Remarks

이 글은 https://www.tensorflow.org/tutorials/quickstart/beginner?hl=ko를 참고하여 작성되었습니다.


1. For beginners

# 0. Import libraries
import tensorflow as tf

# 1. Load dataset
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

# 2. Modeling
model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

# 3. Select loss function, optimizer
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 4. Train and test
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test,  y_test, verbose=2)
Train on 60000 samples
Epoch 1/5
60000/60000 [==============================] - 3s 45us/sample - loss: 0.2976 - accuracy: 0.9139
Epoch 2/5
60000/60000 [==============================] - 3s 42us/sample - loss: 0.1443 - accuracy: 0.9565
Epoch 3/5
60000/60000 [==============================] - 3s 42us/sample - loss: 0.1087 - accuracy: 0.9668
Epoch 4/5
60000/60000 [==============================] - 3s 42us/sample - loss: 0.0859 - accuracy: 0.9728
Epoch 5/5
60000/60000 [==============================] - 3s 42us/sample - loss: 0.0761 - accuracy: 0.9761
10000/1 - 0s - loss: 0.0387 - accuracy: 0.9761
[0.07635148925301619, 0.9761]

2. For experts

# 0. Import libraries
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras import Model

# 1. Load dataset
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

# 2. Add channel dimension (x: [height, width, channel])
x_train = x_train[..., tf.newaxis]
x_test = x_test[..., tf.newaxis]

# 3. Shuffle dataset and generate batch
train_ds = tf.data.Dataset.from_tensor_slices(
    (x_train, y_train)).shuffle(10000).batch(32)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)

# 4. Modeling using subclass API
class MyModel(Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.conv1 = Conv2D(32, 3, activation='relu')
    self.flatten = Flatten()
    self.d1 = Dense(128, activation='relu')
    self.d2 = Dense(10, activation='softmax')
  def call(self, x):
    x = self.conv1(x)
    x = self.flatten(x)
    x = self.d1(x)
    return self.d2(x)
model = MyModel()

# 5. Select loss function and optimizer
loss_object = tf.keras.losses.SparseCategoricalCrossentropy()
optimizer = tf.keras.optimizers.Adam()

# 6. Select metrics
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')

# 7. Train function
@tf.function
def train_step(images, labels):
  with tf.GradientTape() as tape:
    predictions = model(images)
    loss = loss_object(labels, predictions)
  gradients = tape.gradient(loss, model.trainable_variables)
  optimizer.apply_gradients(zip(gradients, model.trainable_variables))
  train_loss(loss)
  train_accuracy(labels, predictions)

# 8. Test function
@tf.function
def test_step(images, labels):
  predictions = model(images)
  t_loss = loss_object(labels, predictions)
  test_loss(t_loss)
  test_accuracy(labels, predictions)

# 9. Train and test
EPOCHS = 5
for epoch in range(EPOCHS):
  for images, labels in train_ds:
    train_step(images, labels)
  for test_images, test_labels in test_ds:
    test_step(test_images, test_labels)
  print('에포크: {}, 손실: {}, 정확도: {}, 테스트 손실: {}, 테스트 정확도: {}'.format(
    epoch+1,
    train_loss.result(),
    train_accuracy.result()*100,
    test_loss.result(),
    test_accuracy.result()*100))
에포크: 1, 손실: 0.1350245475769043, 정확도: 95.97332763671875, 테스트 손실: 0.06761721521615982, 테스트 정확도: 97.72999572753906
에포크: 2, 손실: 0.0883970707654953, 정확도: 97.3308334350586, 테스트 손실: 0.06164100021123886, 테스트 정확도: 97.90999603271484
에포크: 3, 손실: 0.06630987673997879, 정확도: 97.96833038330078, 테스트 손실: 0.05878804996609688, 테스트 정확도: 98.0433349609375
에포크: 4, 손실: 0.05323632061481476, 정확도: 98.36708068847656, 테스트 손실: 0.0587901771068573, 테스트 정확도: 98.12750244140625
에포크: 5, 손실: 0.044575780630111694, 정확도: 98.62133026123047, 테스트 손실: 0.059710338711738586, 테스트 정확도: 98.1719970703125