Estimators API로 모델 학습

Python으로 시작하는 TensorFlow

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Estimators API란?

  • 상위 수준 하위 모듈
  • 유연성 낮음
  • 모범 사례 준수 유도
  • 배포 더 빠름
  • 사전 구성 모델 다수 제공

이 이미지는 Estimators API가 하위 수준 API와 어떻게 연결되는지 보여주는 도식입니다.

1 Image taken from https://www.tensorflow.org/guide/premade_estimators
Python으로 시작하는 TensorFlow

모델 정의와 학습 절차

  1. 피처 컬럼 정의
  2. 데이터 로드 및 변환
  3. 추정기 정의
  4. 학습 실행
Python으로 시작하는 TensorFlow

피처 컬럼 정의

# Import tensorflow under its standard alias
import tensorflow as tf

# Define a numeric feature column
size = tf.feature_column.numeric_column("size")
# Define a categorical feature column
rooms = tf.feature_column.categorical_column_with_vocabulary_list("rooms",\
["1", "2", "3", "4", "5"])
Python으로 시작하는 TensorFlow

피처 컬럼 정의

# Create feature column list
features_list = [size, rooms]
# Define a matrix feature column
features_list = [tf.feature_column.numeric_column('image', shape=(784,))]
Python으로 시작하는 TensorFlow

데이터 로딩 및 변환

# Define input data function
def input_fn():
    # Define feature dictionary
    features = {"size": [1340, 1690, 2720], "rooms": [1, 3, 4]}
    # Define labels
    labels = [221900, 538000, 180000]
    return features, labels
Python으로 시작하는 TensorFlow

회귀 추정기 정의 및 학습

# Define a deep neural network regression
model0 = tf.estimator.DNNRegressor(feature_columns=feature_list,\
    hidden_units=[10, 6, 6, 3])

# Train the regression model
model0.train(input_fn, steps=20)
Python으로 시작하는 TensorFlow

심층 신경망 정의 및 학습

# Define a deep neural network classifier
model1 = tf.estimator.DNNClassifier(feature_columns=feature_list,\ 
   hidden_units=[32, 16, 8], n_classes=4)

# Train the classifier
model1.train(input_fn, steps=20)
Python으로 시작하는 TensorFlow

연습해 봅시다!

Python으로 시작하는 TensorFlow

Preparing Video For Download...