[tensorflow2.x 기초 - 4] MNIST data를 활용해 optimization, loss_function, training 구현 기본 (keras)

2020. 9. 1. 16:56DL in Python/Tensorflow2.x 기초

 

 

 

 

 

Optimization & Training (Beginner)

 
  • tf와 layers 패키지 불러오기
In [1]:
import tensorflow as tf
from tensorflow.keras import layers

from tensorflow.keras import datasets
 

학습 과정 돌아보기

 

Data -> (Model -> logit -> loss -> Optm : 반복) => result

 

Prepare MNIST Datset

In [2]:
(train_x, train_y), (test_x, test_y) = datasets.mnist.load_data()
 

Build Model

In [3]:
inputs = layers.Input((28, 28, 1))
net = layers.Conv2D(32, (3, 3), padding='SAME')(inputs)
net = layers.Activation('relu')(net)
net = layers.Conv2D(32, (3, 3), padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Conv2D(64, (3, 3), padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.Conv2D(64, (3, 3), padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.25)(net)

net = layers.Flatten()(net)
net = layers.Dense(512)(net)
net = layers.Activation('relu')(net)
net = layers.Dropout(0.5)(net)
net = layers.Dense(10)(net)  # num_classes
net = layers.Activation('softmax')(net)

model = tf.keras.Model(inputs=inputs, outputs=net, name='Basic_CNN')
 

Optimization

모델을 학습하기 전 설정

 
  • Loss Function
  • Optimization
  • Metrics
 

Loss Function

Loss Function 방법 확인

 

Categorical vs Binary

In [4]:
# binary
loss = 'binary_crossentropy'

# more than two categories
loss = 'categorical_crossentropy'
 

sparse_categorical_crossentropy vs categorical_crossentropy

In [5]:
# 사용할 loss function, one hot encoding이 필요하지 않음
loss_function = tf.keras.losses.sparse_categorical_crossentropy
In [6]:
tf.keras.losses.categorical_crossentropy
Out[6]:
<function tensorflow.python.keras.losses.categorical_crossentropy(y_true, y_pred, from_logits=False, label_smoothing=0)>
In [7]:
tf.keras.losses.binary_crossentropy
Out[7]:
<function tensorflow.python.keras.losses.binary_crossentropy(y_true, y_pred, from_logits=False, label_smoothing=0)>
 

Metrics

모델을 평가하는 방법

 

accuracy를 이름으로 넣는 방법

In [8]:
metrics = ['accuracy']
 

tf.keras.metrics.

In [26]:
tf.keras.metrics.Accuracy()
tf.keras.metrics.Precision()
tf.keras.metrics.Recall()

metrics = tf.keras.metrics.SparseCategoricalAccuracy()
 

Compile

Optimizer 적용

 
  • 'sgd'
  • 'rmsprop'
  • 'adam'
In [27]:
optm = tf.keras.optimizers.Adam()
 
  • tf.keras.optimizers.SGD()
  • tf.keras.optimizers.RMSprop()
  • tf.keras.optimizers.Adam()
In [28]:
model.compile(optimizer = optm,
              loss = loss_function,
              metrics = metrics)
In [ ]:
 
 

Prepare Dataset

학습에 사용할 데이터셋 준비

 

shape 확인

In [12]:
train_x.shape, train_y.shape
Out[12]:
((60000, 28, 28), (60000,))
In [13]:
test_x.shape, test_y.shape
Out[13]:
((10000, 28, 28), (10000,))
 

차원 수 늘리기

In [14]:
import numpy as np
In [15]:
np.expand_dims(train_x, -1).shape
Out[15]:
(60000, 28, 28, 1)
In [16]:
tf.expand_dims(train_x, -1).shape
Out[16]:
TensorShape([60000, 28, 28, 1])
In [17]:
train_x = train_x[..., tf.newaxis]
test_x = test_x[..., tf.newaxis]
In [18]:
#train_y = train_y[..., tf.newaxis]
#test_y = test_y[..., tf.newaxis]
 

차원 수 잘 늘었는지 확인

In [19]:
train_x.shape, test_x.shape
Out[19]:
((60000, 28, 28, 1), (10000, 28, 28, 1))
 

Rescaling

In [20]:
np.min(train_x), np.max(train_x)
Out[20]:
(0, 255)
In [21]:
train_x = train_x / 255.
In [22]:
test_x = test_x / 255
In [23]:
np.min(train_x), np.max(train_x)
Out[23]:
(0.0, 1.0)
 

Training

본격적으로 학습 들어가기

 

학습용 Hyperparameter 설정

  • num_epochs : 반복횟수, 데이터를 한 번 보면 epoch - 1
  • batch_size : 한 번에 들어가는 데이터의 수
In [24]:
num_epochs = 1
batch_size = 32
 
  • model.fit
In [29]:
model.fit(train_x,train_y,
          batch_size = batch_size,
          shuffle = True,
          epochs = num_epochs)
 
1875/1875 [==============================] - 154s 82ms/step - loss: 0.1473 - sparse_categorical_accuracy: 0.9536
Out[29]:
<tensorflow.python.keras.callbacks.History at 0x1c63b289488>
 

Check History

학습 과정(History) 결과 확인

In [ ]: