TensorFlow 2.0 : Beginner Tutorials : Estimator :- Premade Estimator (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 10/10/2019
* 本ページは、TensorFlow org サイトの TF 2.0 – Beginner Tutorials – Estimator の以下のページを
翻訳した上で適宜、補足説明したものです:
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
- Windows PC のブラウザからご参加が可能です。スマートデバイスもご利用可能です。
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション |
E-Mail:sales-info@classcat.com ; WebSite: https://www.classcat.com/ |
Facebook: https://www.facebook.com/ClassCatJP/ |
Estimator :- Premade Estimator
このチュートリアルは Estimator を使用して TensorFlow でアイリス分類問題をどのように解くかを示します。Estimator は完全なモデルの TensorFlow の高位表現で、そしてそれは容易なスケーリングと非同期訓練のために設計されています。より詳細については Estimators を見てください。
TensorFlow 2.0 では、Keras API はこれらと同じタスクの多くを成し遂げることができて、そしてそれは学習するためにより容易な API であると見られていることに注意してください。もしあなたが新たに始めている場合には、Keras で始めることを貴方に勧めます。TensorFlow 2.0 で利用可能な高位 API についてのより多くの情報のためには、Standardizing on Keras を見てください。
大事なことを最初に
始めるために、最初に TensorFlow と必要な多くのライブラリをインポートします。
from __future__ import absolute_import, division, print_function, unicode_literals import tensorflow as tf import pandas as pd
データセット
このドキュメントのサンプル・プログラムはアイリス花をそれらの がく片 と 花弁 のサイズに基づいて 3 つの異なる種に分類するためのモデルを構築してテストします。
アイリス・データセットを使用してモデルを訓練します。アイリス・データセットは 4 つの特徴と 1 つの ラベル を含みます。4つの特徴は個々のアイリス花の次の植物学的特性を識別します :
- がく片長さ
- がく片幅
- 花弁長さ
- 花弁幅
これらの情報に基づいて、データをパースするための 2,3 の役立つ定数を定義できます :
CSV_COLUMN_NAMES = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species'] SPECIES = ['Setosa', 'Versicolor', 'Virginica']
次に、Keras と Pandas を使用してアイリス・データセットをダウンロードしてパースします。訓練とテストのための別個のデータセットを保持していることに注意してください。
train_path = tf.keras.utils.get_file( "iris_training.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv") test_path = tf.keras.utils.get_file( "iris_test.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv") train = pd.read_csv(train_path, names=CSV_COLUMN_NAMES, header=0) test = pd.read_csv(test_path, names=CSV_COLUMN_NAMES, header=0)
4 つの float 特徴カラムと 1 つの int32 ラベルを持つことを見るためのデータを調べることができます。
train.head()
がく片長さ | がく片幅 | 花弁長さ | 花弁幅 | 種 | |
---|---|---|---|---|---|
0 | 6.4 | 2.8 | 5.6 | 2.2 | 2 |
1 | 5.0 | 2.3 | 3.3 | 1.0 | 1 |
2 | 4.9 | 2.5 | 4.5 | 1.7 | 2 |
3 | 4.9 | 3.1 | 1.5 | 0.1 | 0 |
4 | 5.7 | 3.8 | 1.7 | 0.3 | 0 |
データセットの各々のために、ラベルを分離します、これをモデルが予測するために訓練されます。
train_y = train.pop('Species') test_y = test.pop('Species') # The label column has now been removed from the features. train.head()
がく片長さ | がく片幅 | 花弁長さ | 花弁幅 | |
---|---|---|---|---|
0 | 6.4 | 2.8 | 5.6 | 2.2 |
1 | 5.0 | 2.3 | 3.3 | 1.0 |
2 | 4.9 | 2.5 | 4.5 | 1.7 |
3 | 4.9 | 3.1 | 1.5 | 0.1 |
4 | 5.7 | 3.8 | 1.7 | 0.3 |
Estimator によるプログラミング概要
セットアップされたデータを持つ今、TensorFlow Estimator を使用してモデルを定義できます。Estimator は tf.estimator.Estimator から派生する任意のクラスです。TensorFlow は一般的な ML アルゴリズムを実装する tf.estimator のコレクション (例えば、LinearRegressor) を提供します。それらを越えて、貴方自身の カスタム Estimator を書いても良いです。単に始めるときには pre-made Estimator を使用することを勧めます。
pre-made Estimator に基づく TensorFlow プログラムを書くには、次のタスクを遂行しなければなりません :
- 一つまたはそれ以上の入力関数を作成する。
- モデルの特徴カラムを定義する。
- 特等カラムと様々なハイパーパラメータを指定して、Estimator をインスタンス化する。
- データのソースとして適切な入力関数を渡して、Estimator オブジェクト上で一つまたはそれ以上のメソッドを呼び出します。
それらのタスクがアイリス分類のためにどのように実装されるかを見ましょう。
入力関数を作成する
訓練、評価そして予測のためにデータを供給する入力関数を作成しなければなりません。
入力関数は次の 2-要素タプルを出力する tf.data.Dataset オブジェクトを返す関数です :
入力関数の形式を単に実演するために、ここに単純な実装があります :
def input_evaluation_set(): features = {'SepalLength': np.array([6.4, 5.0]), 'SepalWidth': np.array([2.8, 2.3]), 'PetalLength': np.array([5.6, 3.3]), 'PetalWidth': np.array([2.2, 1.0])} labels = np.array([2, 1]) return features, labels
貴方の入力関数は特徴辞書とラベルリストを貴方の好きな方法で生成するかもしれません。けれども、TensorFlow の Dataset API を使用することを勧めます、これは総ての種類のデータをパースできます。
Dataset API は貴方のために多くの一般的なケースを処理できます。例えば、Datset API を使用して、ファイルの巨大なコレクションから並列にレコードを容易に読み込むことができてそれらを単一のストリームに合流できます。
このサンプルで物事を単純に保持するためにデータを pandas でロードし、そしてこの in-メモリデータから入力パイプラインを構築していきます :
def input_fn(features, labels, training=True, batch_size=256): """An input function for training or evaluating""" # Convert the inputs to a Dataset. dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels)) # Shuffle and repeat if you are in training mode. if training: dataset = dataset.shuffle(1000).repeat() return dataset.batch(batch_size)
特徴カラムを定義する
特徴カラムはオブジェクトで、モデルが特徴辞書からの生入力データをどのように使用するべきかを記述します。Estimator モデルを構築するとき、モデルに使用することを望む特徴の各々を記述する特徴カラムのリストをそれに渡します。tf.feature_column モジュールはデータをモデルに表わすために多くのオプションを提供します。
アイリスのためには、4 生特徴は数値ですので、 4 つの特徴の各々を 32-bit 浮動小数点値として表わすように Estimator モデルに伝えるために特徴カラムのリストを構築します。従って、特徴カラムを作成するコードは :
# Feature columns describe how to use the input. my_feature_columns = [] for key in train.keys(): my_feature_columns.append(tf.feature_column.numeric_column(key=key))
特徴カラムはここで見せているものようりも遥かにより洗練されている可能性があります。このガイド で特徴カラムについて更に読むことができます。
モデルに生特徴をどのように表わすことを望むかの記述を持つ今、estimator を構築できます。
estimator をインスタンス化する
アイリス問題は古典的な分類問題です。幸い、TensorFlow は幾つかの pre-made 分類器 Estimator を提供しています、次を含みます :
- tf.estimator.DNNClassifier マルチクラス分類を遂行する深層モデルのため。
- tf.estimator.DNNLinearCombinedClassifier 広範な& 深層モデルのため。
- tf.estimator.LinearClassifier 線形モデルに基づく分類器のため。
このアイリス問題のためには、tf.estimator.DNNClassifier が最善の選択のようです。ここにこの Estimator をどのようにインスタンス化したかがあります :
# Build a DNN with 2 hidden layers with 30 and 10 hidden nodes each. classifier = tf.estimator.DNNClassifier( feature_columns=my_feature_columns, # Two hidden layers of 10 nodes each. hidden_units=[30, 10], # The model must choose between 3 classes. n_classes=3)
INFO:tensorflow:Using default config. WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpy5w5zoj8 INFO:tensorflow:Using config: {'_service': None, '_device_fn': None, '_train_distribute': None, '_evaluation_master': '', '_master': '', '_tf_random_seed': None, '_session_creation_timeout_secs': 7200, '_global_id_in_cluster': 0, '_task_id': 0, '_num_ps_replicas': 0, '_is_chief': True, '_protocol': None, '_keep_checkpoint_max': 5, '_experimental_max_worker_delay_secs': None, '_eval_distribute': None, '_task_type': 'worker', '_save_summary_steps': 100, '_num_worker_replicas': 1, '_experimental_distribute': None, '_keep_checkpoint_every_n_hours': 10000, '_cluster_spec':, '_model_dir': '/tmp/tmpy5w5zoj8', '_session_config': allow_soft_placement: true graph_options { rewrite_options { meta_optimizer_iterations: ONE } } , '_log_step_count_steps': 100, '_save_checkpoints_secs': 600, '_save_checkpoints_steps': None}
訓練、評価そして予測する
Estimator オブジェクトを持つ今、以下を行なうためにメソッドを呼び出せます :
- モデルを訓練する。
- 訓練されたモデルを評価する。
- 予測を行なうため訓練されたモデルを使用する。
モデルを訓練する
Estimator の train メソッドを次のように呼び出してモデルを訓練します :
# Train the Model. classifier.train( input_fn=lambda: input_fn(train, train_y, training=True), steps=5000)
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.5/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version. Instructions for updating: If using Keras pass *_constraint arguments to layers. WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.5/site-packages/tensorflow_core/python/training/training_util.py:236: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version. Instructions for updating: Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts. INFO:tensorflow:Calling model_fn. WARNING:tensorflow:Layer dnn is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because it's dtype defaults to floatx. If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2. To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor. WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.5/site-packages/tensorflow_core/python/keras/optimizer_v2/adagrad.py:108: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor INFO:tensorflow:Done calling model_fn. INFO:tensorflow:Create CheckpointSaverHook. INFO:tensorflow:Graph was finalized. INFO:tensorflow:Running local_init_op. INFO:tensorflow:Done running local_init_op. INFO:tensorflow:Saving checkpoints for 0 into /tmp/tmpy5w5zoj8/model.ckpt. INFO:tensorflow:loss = 1.2197917, step = 0 INFO:tensorflow:global_step/sec: 238.94 INFO:tensorflow:loss = 1.0917794, step = 100 (0.420 sec) INFO:tensorflow:global_step/sec: 301.312 INFO:tensorflow:loss = 1.0716088, step = 200 (0.332 sec) INFO:tensorflow:global_step/sec: 303.674 INFO:tensorflow:loss = 1.0544529, step = 300 (0.329 sec) INFO:tensorflow:global_step/sec: 302.426 INFO:tensorflow:loss = 1.0382756, step = 400 (0.331 sec) INFO:tensorflow:global_step/sec: 302.738 INFO:tensorflow:loss = 1.027103, step = 500 (0.331 sec) INFO:tensorflow:global_step/sec: 304.599 INFO:tensorflow:loss = 1.0103141, step = 600 (0.328 sec) INFO:tensorflow:global_step/sec: 303.949 INFO:tensorflow:loss = 0.9966562, step = 700 (0.329 sec) INFO:tensorflow:global_step/sec: 303.033 INFO:tensorflow:loss = 0.9852488, step = 800 (0.330 sec) INFO:tensorflow:global_step/sec: 302.643 INFO:tensorflow:loss = 0.96979976, step = 900 (0.330 sec) INFO:tensorflow:global_step/sec: 304.923 INFO:tensorflow:loss = 0.9661863, step = 1000 (0.328 sec) INFO:tensorflow:global_step/sec: 323.831 INFO:tensorflow:loss = 0.9523607, step = 1100 (0.309 sec) INFO:tensorflow:global_step/sec: 330.137 INFO:tensorflow:loss = 0.935673, step = 1200 (0.303 sec) INFO:tensorflow:global_step/sec: 328.748 INFO:tensorflow:loss = 0.929138, step = 1300 (0.304 sec) INFO:tensorflow:global_step/sec: 325.541 INFO:tensorflow:loss = 0.9089911, step = 1400 (0.307 sec) INFO:tensorflow:global_step/sec: 324.871 INFO:tensorflow:loss = 0.9001589, step = 1500 (0.308 sec) INFO:tensorflow:global_step/sec: 327.313 INFO:tensorflow:loss = 0.8927353, step = 1600 (0.305 sec) INFO:tensorflow:global_step/sec: 329.062 INFO:tensorflow:loss = 0.88943803, step = 1700 (0.304 sec) INFO:tensorflow:global_step/sec: 328.659 INFO:tensorflow:loss = 0.8707663, step = 1800 (0.304 sec) INFO:tensorflow:global_step/sec: 314.002 INFO:tensorflow:loss = 0.86156833, step = 1900 (0.319 sec) INFO:tensorflow:global_step/sec: 300.244 INFO:tensorflow:loss = 0.852653, step = 2000 (0.333 sec) INFO:tensorflow:global_step/sec: 299.23 INFO:tensorflow:loss = 0.83402675, step = 2100 (0.334 sec) INFO:tensorflow:global_step/sec: 304.725 INFO:tensorflow:loss = 0.82657015, step = 2200 (0.328 sec) INFO:tensorflow:global_step/sec: 302.436 INFO:tensorflow:loss = 0.8211421, step = 2300 (0.330 sec) INFO:tensorflow:global_step/sec: 301.456 INFO:tensorflow:loss = 0.8120619, step = 2400 (0.332 sec) INFO:tensorflow:global_step/sec: 307.489 INFO:tensorflow:loss = 0.798753, step = 2500 (0.325 sec) INFO:tensorflow:global_step/sec: 309.389 INFO:tensorflow:loss = 0.7921459, step = 2600 (0.323 sec) INFO:tensorflow:global_step/sec: 309.822 INFO:tensorflow:loss = 0.7766729, step = 2700 (0.323 sec) INFO:tensorflow:global_step/sec: 314.836 INFO:tensorflow:loss = 0.76089776, step = 2800 (0.318 sec) INFO:tensorflow:global_step/sec: 310.84 INFO:tensorflow:loss = 0.7654529, step = 2900 (0.322 sec) INFO:tensorflow:global_step/sec: 315.348 INFO:tensorflow:loss = 0.7533946, step = 3000 (0.317 sec) INFO:tensorflow:global_step/sec: 313.578 INFO:tensorflow:loss = 0.7357384, step = 3100 (0.319 sec) INFO:tensorflow:global_step/sec: 312.985 INFO:tensorflow:loss = 0.73263234, step = 3200 (0.319 sec) INFO:tensorflow:global_step/sec: 311.498 INFO:tensorflow:loss = 0.72836924, step = 3300 (0.321 sec) INFO:tensorflow:global_step/sec: 312.958 INFO:tensorflow:loss = 0.7124317, step = 3400 (0.319 sec) INFO:tensorflow:global_step/sec: 312.767 INFO:tensorflow:loss = 0.70199186, step = 3500 (0.320 sec) INFO:tensorflow:global_step/sec: 336.802 INFO:tensorflow:loss = 0.6960018, step = 3600 (0.297 sec) INFO:tensorflow:global_step/sec: 332.888 INFO:tensorflow:loss = 0.6923122, step = 3700 (0.300 sec) INFO:tensorflow:global_step/sec: 327.027 INFO:tensorflow:loss = 0.67529535, step = 3800 (0.306 sec) INFO:tensorflow:global_step/sec: 322.62 INFO:tensorflow:loss = 0.678157, step = 3900 (0.310 sec) INFO:tensorflow:global_step/sec: 322.949 INFO:tensorflow:loss = 0.6730201, step = 4000 (0.310 sec) INFO:tensorflow:global_step/sec: 335.008 INFO:tensorflow:loss = 0.65053713, step = 4100 (0.298 sec) INFO:tensorflow:global_step/sec: 332.504 INFO:tensorflow:loss = 0.64781785, step = 4200 (0.301 sec) INFO:tensorflow:global_step/sec: 335.872 INFO:tensorflow:loss = 0.64207447, step = 4300 (0.298 sec) INFO:tensorflow:global_step/sec: 335.598 INFO:tensorflow:loss = 0.6398175, step = 4400 (0.298 sec) INFO:tensorflow:global_step/sec: 334.703 INFO:tensorflow:loss = 0.6255224, step = 4500 (0.299 sec) INFO:tensorflow:global_step/sec: 337.713 INFO:tensorflow:loss = 0.62776095, step = 4600 (0.296 sec) INFO:tensorflow:global_step/sec: 340.161 INFO:tensorflow:loss = 0.6233723, step = 4700 (0.294 sec) INFO:tensorflow:global_step/sec: 335.719 INFO:tensorflow:loss = 0.6083272, step = 4800 (0.298 sec) INFO:tensorflow:global_step/sec: 332.322 INFO:tensorflow:loss = 0.5927398, step = 4900 (0.301 sec) INFO:tensorflow:Saving checkpoints for 5000 into /tmp/tmpy5w5zoj8/model.ckpt. INFO:tensorflow:Loss for final step: 0.59226876. <tensorflow_estimator.python.estimator.canned.dnn.DNNClassifierV2 at 0x7ff1da8fd438>
引数を取らない入力関数を提供する一方で、Estimator により想定される引数を捕捉するために貴方の input_fn 呼び出しを lambda でラップしていることに注意してください。steps 引数は訓練ステップの数の後訓練を停止することをメソッドに伝えます。
訓練されたモデルを評価する
モデルが訓練された今、そのパフォーマンス上の何某かの統計情報を得られます。次のコードブロックは訓練されたモデルの精度をテストデータ上で評価します :
eval_result = classifier.evaluate( input_fn=lambda: input_fn(test, test_y, training=False)) print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))
INFO:tensorflow:Calling model_fn. WARNING:tensorflow:Layer dnn is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because it's dtype defaults to floatx. If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2. To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor. INFO:tensorflow:Done calling model_fn. INFO:tensorflow:Starting evaluation at 2019-10-01T01:28:45Z INFO:tensorflow:Graph was finalized. INFO:tensorflow:Restoring parameters from /tmp/tmpy5w5zoj8/model.ckpt-5000 INFO:tensorflow:Running local_init_op. INFO:tensorflow:Done running local_init_op. INFO:tensorflow:Finished evaluation at 2019-10-01-01:28:46 INFO:tensorflow:Saving dict for global step 5000: accuracy = 0.56666666, average_loss = 0.6702302, global_step = 5000, loss = 0.6702302 INFO:tensorflow:Saving 'checkpoint_path' summary for global step 5000: /tmp/tmpy5w5zoj8/model.ckpt-5000 Test set accuracy: 0.567
train メソッドへの呼び出しとは違い、evaluate には steps 引数は渡しませんでした。eval のための input_fn はデータの単一 エポック を生成するだけです。
eval_result 辞書はまた average_loss (サンプル毎の平均損失)、損失 (ミニバッチ毎の平均損失) そして estimator の global_step (それが受けた訓練反復の数) の値を含みます。
訓練されたモデルから予測 (推論) を行なう
今は良い評価結果を生成する訓練されたモデルを持ちます。今は幾つかのラベル付けされていない測定に基づいてアイリス花の種を予測するために訓練されたモデルを使用できます。訓練と評価と同様に、単一の関数呼び出しを使用して予測を行ないます :
# Generate predictions from the model expected = ['Setosa', 'Versicolor', 'Virginica'] predict_x = { 'SepalLength': [5.1, 5.9, 6.9], 'SepalWidth': [3.3, 3.0, 3.1], 'PetalLength': [1.7, 4.2, 5.4], 'PetalWidth': [0.5, 1.5, 2.1], } def input_fn(features, batch_size=256): """An input function for prediction.""" # Convert the inputs to a Dataset without labels. return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size) predictions = classifier.predict( input_fn=lambda: input_fn(predict_x))
predict メソッドは Python iterable を返し、各サンプルのための予測の辞書を生成します。次のコードは 2, 3 の予測とそれらの確率をプリントします :
for pred_dict, expec in zip(predictions, expected): class_id = pred_dict['class_ids'][0] probability = pred_dict['probabilities'][class_id] print('Prediction is "{}" ({:.1f}%), expected "{}"'.format( SPECIES[class_id], 100 * probability, expec))
INFO:tensorflow:Calling model_fn. INFO:tensorflow:Done calling model_fn. INFO:tensorflow:Graph was finalized. INFO:tensorflow:Restoring parameters from /tmp/tmpy5w5zoj8/model.ckpt-5000 INFO:tensorflow:Running local_init_op. INFO:tensorflow:Done running local_init_op. Prediction is "Setosa" (73.0%), expected "Setosa" Prediction is "Virginica" (42.6%), expected "Versicolor" Prediction is "Virginica" (49.0%), expected "Virginica"
以上