Keras로 학습하기

Python으로 시작하는 TensorFlow

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

학습과 평가 개요

  1. 데이터 로드 및 정제
  2. 모델 정의
  3. 모델 학습 및 검증
  4. 모델 평가
Python으로 시작하는 TensorFlow

모델 학습 방법

# Import tensorflow
import tensorflow as tf

# Define a sequential model
model = tf.keras.Sequential()
# Define the hidden layer
model.add(tf.keras.layers.Dense(16, activation='relu', input_shape=(784,)))
# Define the output layer
model.add(tf.keras.layers.Dense(4, activation='softmax'))
Python으로 시작하는 TensorFlow

모델 학습 방법

# Compile model
model.compile('adam', loss='categorical_crossentropy')
# Train model
model.fit(image_features, image_labels)
Python으로 시작하는 TensorFlow

fit() 연산

  • 필수 인자
    • features
    • labels
  • 선택 인자 다수
    • batch_size
    • epochs
    • validation_split
Python으로 시작하는 TensorFlow

배치 크기와 에포크

데이터셋이 배치로 나뉘고, 그 배치들의 조합이 한 에포크가 됨을 보여주는 다이어그램입니다.

Python으로 시작하는 TensorFlow

검증 수행

데이터셋이 학습용과 검증용으로 분할되는 모습을 보여줍니다.

Python으로 시작하는 TensorFlow

검증 수행

# Train model with validation split
model.fit(features, labels, epochs=10, validation_split=0.20)
Python으로 시작하는 TensorFlow

검증 수행

10 에포크 동안의 학습 및 검증 결과를 보여줍니다.

Python으로 시작하는 TensorFlow

평가지표 변경

# Recomile the model with the accuracy metric
model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train model with validation split
model.fit(features, labels, epochs=10, validation_split=0.20)
Python으로 시작하는 TensorFlow

평가지표 변경

10 에포크 동안의 학습 및 검증 결과를 보여줍니다.

Python으로 시작하는 TensorFlow

evaluation() 연산

데이터셋이 학습용, 검증용, 테스트용으로 분할되는 모습을 보여줍니다.

# Evaluate the test set
model.evaluate(test)
Python으로 시작하는 TensorFlow

Vamos praticar!

Python으로 시작하는 TensorFlow

Preparing Video For Download...