Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/ja/guide/migrate/metrics_optimizers.ipynb
25118 views
Kernel: Python 3
#@title Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.

TF1 では、tf.metrics はすべての指標関数の API 名前空間です。各指標は、labelprediction を入力パラメータとして取り、対応する指標テンソルを結果として返す関数です。TF2 では、tf.keras.metrics にすべての指標関数とオブジェクトが含まれています。Metric オブジェクトを tf.keras.Model および tf.keras.layers.layer で使用して、指標値を計算できます。

セットアップ

いくつかの必要な TensorFlow インポートから始めましょう。

import tensorflow as tf import tensorflow.compat.v1 as tf1

デモ用にいくつかの簡単なデータを準備します。

features = [[1., 1.5], [2., 2.5], [3., 3.5]] labels = [0, 0, 1] eval_features = [[4., 4.5], [5., 5.5], [6., 6.5]] eval_labels = [0, 1, 1]

TF1: Estimator を使用した tf.compat.v1.metrics

TF1 では、指標は eval_metric_ops として EstimatorSpec に追加でき、演算は tf.metrics で定義されたすべての指標関数を介して生成されます。例に従って、tf.metrics.accuracy の使用方法を確認できます。

def _input_fn(): return tf1.data.Dataset.from_tensor_slices((features, labels)).batch(1) def _eval_input_fn(): return tf1.data.Dataset.from_tensor_slices( (eval_features, eval_labels)).batch(1) def _model_fn(features, labels, mode): logits = tf1.layers.Dense(2)(features) predictions = tf.math.argmax(input=logits, axis=1) loss = tf1.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits) optimizer = tf1.train.AdagradOptimizer(0.05) train_op = optimizer.minimize(loss, global_step=tf1.train.get_global_step()) accuracy = tf1.metrics.accuracy(labels=labels, predictions=predictions) return tf1.estimator.EstimatorSpec(mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops={'accuracy': accuracy}) estimator = tf1.estimator.Estimator(model_fn=_model_fn) estimator.train(_input_fn)
estimator.evaluate(_eval_input_fn)

また、指標は tf.estimator.add_metrics() を介してエスティメータに直接追加できます。

def mean_squared_error(labels, predictions): labels = tf.cast(labels, predictions.dtype) return {"mean_squared_error": tf1.metrics.mean_squared_error(labels=labels, predictions=predictions)} estimator = tf1.estimator.add_metrics(estimator, mean_squared_error) estimator.evaluate(_eval_input_fn)

TF2: tf.keras.Model を使用した Keras メトリクス API

TF2 では、tf.keras.metrics にすべての指標クラスと関数が含まれています。これらは OOP スタイルで設計されており、他の tf.keras API と密接に統合されています。すべての指標は tf.keras.metrics 名前空間で見つけることができ、通常は tf.compat.v1.metricstf.keras.metrics の間に直接マッピングがあります。

次の例では、指標が model.compile() メソッドに追加されています。ユーザーは、ラベルと予測テンソルを指定せずに、指標インスタンスを作成するだけで済みます。Keras モデルは、モデルの出力とラベルを指標オブジェクトにルーティングします。

dataset = tf.data.Dataset.from_tensor_slices((features, labels)).batch(1) eval_dataset = tf.data.Dataset.from_tensor_slices( (eval_features, eval_labels)).batch(1) inputs = tf.keras.Input((2,)) logits = tf.keras.layers.Dense(2)(inputs) predictions = tf.math.argmax(input=logits, axis=1) model = tf.keras.models.Model(inputs, predictions) optimizer = tf.keras.optimizers.Adagrad(learning_rate=0.05) model.compile(optimizer, loss='mse', metrics=[tf.keras.metrics.Accuracy()])
model.evaluate(eval_dataset, return_dict=True)

Eager execution を有効にすると、tf.keras.metrics.Metric インスタンスを直接使用して、numpy データまたは Eager テンソルを評価できます。tf.keras.metrics.Metric オブジェクトはステートフルコンテナーです。指標値は metric.update_state(y_true, y_pred) で更新でき、結果は metrics.result() で取得できます。

accuracy = tf.keras.metrics.Accuracy() accuracy.update_state(y_true=[0, 0, 1, 1], y_pred=[0, 0, 0, 1]) accuracy.result().numpy()
accuracy.update_state(y_true=[0, 0, 1, 1], y_pred=[0, 0, 0, 0]) accuracy.update_state(y_true=[0, 0, 1, 1], y_pred=[1, 1, 0, 0]) accuracy.result().numpy()

tf.keras.metrics.Metric の詳細については、tf.keras.metrics.Metric の API ドキュメントと移行ガイドを参照してください。

TF1.x オプティマイザの Keras オプティマイザへの移行

Adam オプティマイザ勾配降下オプティマイザなどの tf.compat.v1.train 内のオプティマイザは、tf.keras.optimizers 内に同等のものをもちます。

以下の表は、これらのレガシーオプティマイザを Keras の同等のものに変換する方法をまとめたものです。追加の手順(デフォルトの学習率の更新など)が必要でない限り、TF1.x バージョンを TF2 バージョンに直接置き換えることができます。

オプティマイザを変換すると、古いチェックポイントの互換性が失われる可能性があることに注意してください。

TF1.x TF2 追加の手順
`tf.v1.train.GradientDescentOptimizer` `tf.keras.optimizers.SGD` なし
`tf.v1.train.MomentumOptimizer` `tf.keras.optimizers.SGD` `momentum` 引数を含む
`tf.v1.train.AdamOptimizer` `tf.keras.optimizers.Adam` `beta1` および `beta2` 引数の名前を `beta_1` および `beta_2` に変更する
`tf.v1.train.RMSPropOptimizer` `tf.keras.optimizers.RMSprop` `decay` 引数の名前を `rho` に変更する
`tf.v1.train.AdadeltaOptimizer` `tf.keras.optimizers.Adadelta` なし
`tf.v1.train.AdagradOptimizer` `tf.keras.optimizers.Adagrad` なし
`tf.v1.train.FtrlOptimizer` `tf.keras.optimizers.Ftrl` `accum_name` および `linear_name` 引数を削除する
`tf.contrib.AdamaxOptimizer` `tf.keras.optimizers.Adamax` `beta1` および `beta2` 引数の名前を `beta_1` および `beta_2` に変更する
`tf.contrib.Nadam` `tf.keras.optimizers.Nadam` `beta1` および `beta2` 引数の名前を `beta_1` および `beta_2` に変更する

注意: TF2 では、すべてのイプシロン(数値安定定数)のデフォルトが 1e-8 ではなく 1e-7 になりました。ほとんどの場合、この違いは無視できます。