Estimators APIでモデルを学習する

Introduction to TensorFlow in Python

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
Introduction to TensorFlow in Python

モデル定義と学習手順

  1. 特徴量カラムを定義
  2. データを読み込み・変換
  3. 推定器を定義
  4. 学習を実行
Introduction to TensorFlow in Python

特徴量カラムの定義

# 標準エイリアスでtensorflowをインポート
import tensorflow as tf

# 数値特徴量カラムを定義
size = tf.feature_column.numeric_column("size")
# 語彙リストを用いたカテゴリ特徴量カラムを定義
rooms = tf.feature_column.categorical_column_with_vocabulary_list("rooms",\
["1", "2", "3", "4", "5"])
Introduction to TensorFlow in Python

特徴量カラムの定義

# 特徴量カラムのリストを作成
features_list = [size, rooms]
# 行列特徴量カラムを定義
features_list = [tf.feature_column.numeric_column('image', shape=(784,))]
Introduction to TensorFlow in Python

データの読み込みと変換

# 入力データ関数を定義
def input_fn():
    # 特徴量ディクショナリを定義
    features = {"size": [1340, 1690, 2720], "rooms": [1, 3, 4]}
    # ラベルを定義
    labels = [221900, 538000, 180000]
    return features, labels
Introduction to TensorFlow in Python

回帰推定器の定義と学習

# 深層ニューラルネットの回帰を定義
model0 = tf.estimator.DNNRegressor(feature_columns=feature_list,\
    hidden_units=[10, 6, 6, 3])

# 回帰モデルを学習
model0.train(input_fn, steps=20)
Introduction to TensorFlow in Python

深層ニューラルネットの定義と学習

# 深層ニューラルネットの分類器を定義
model1 = tf.estimator.DNNClassifier(feature_columns=feature_list,\ 
   hidden_units=[32, 16, 8], n_classes=4)

# 分類器を学習
model1.train(input_fn, steps=20)
Introduction to TensorFlow in Python

練習に進みましょう!

Introduction to TensorFlow in Python

Preparing Video For Download...