이 포스팅은 Tensorflow 2.0 시리즈 2 편 중 1 번째 글 입니다.

  • Part 1 - This Post
  • Part 2 - 02: Expert Start Code
▼ 목록 보기

목차

▼ 내리기

Tensorflow 2.0 Tutorials의 Beginner Start Code를 정리한다.

Code

!pip install tensorflow-gpu==2.0.0-rc1
import tensorflow as tf

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

# 간단하게 만들 때는 이런 방법도 나쁘지는 않다.
# 다만 나중에 관리가 짜증나겠지?
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')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=["accuracy"])

model.fit(x_train, y_train, epochs=5)

model.evaluate(x_test, y_test, verbose=2)

Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0355 - accuracy: 0.9879
Epoch 2/5
1875/1875 [==============================] - 5s 3ms/step - loss: 0.0323 - accuracy: 0.9886
Epoch 3/5
1875/1875 [==============================] - 5s 2ms/step - loss: 0.0322 - accuracy: 0.9890
Epoch 4/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0298 - accuracy: 0.9897
Epoch 5/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0289 - accuracy: 0.9901
313/313 - 1s - loss: 0.0831 - accuracy: 0.9791
[0.08314403146505356, 0.9790999889373779]

Reference

Tensorflow 2.0 tutorials