Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/ja/guide/migrate/sessionrunhook_callback.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.

TensorFlow 1 でトレーニングの動作をカスタマイズするには、tf.estimator.Estimatortf.estimator.SessionRunHook を使用します。このガイドでは、SessionRunHook から tf.keras.callbacks.Callback API を使用して TensorFlow 2 のカスタムコールバックに移行する方法を示します。これは、トレーニングのために Keras Model.fitModel.evaluate および Model.predict も)と使用できます。この方法を学習するために、トレーニング時に 1 秒あたりのサンプルを測定する SessionRunHookCallback タスクを実装します。

コールバックの例は、チェックポイントの保存 (tf.keras.callbacks.ModelCheckpoint)と TensorBoard の要約の書き込みです。Keras コールバックは、組み込みの Keras Model.fit/Model.evaluate/Model.predict API のトレーニング/評価/予測時にさまざまな時点で呼び出されるオブジェクトです。コールバックの詳細については、tf.keras.callbacks.Callback API ドキュメント、および独自のコールバックの作成組み込みメソッドを使用したトレーニングと評価コールバックの使用セクション)ガイドを参照してください。

セットアップ

まず、インポートとデモ用の単純なデータセットから始めます。

import tensorflow as tf import tensorflow.compat.v1 as tf1 import time from datetime import datetime from absl import flags
features = [[1., 1.5], [2., 2.5], [3., 3.5]] labels = [[0.3], [0.5], [0.7]] eval_features = [[4., 4.5], [5., 5.5], [6., 6.5]] eval_labels = [[0.8], [0.9], [1.]]

TensorFlow 1: tf.estimator API を使用してカスタム SessionRunHook を作成する

次の TensorFlow 1 の例は、トレーニング時に 1 秒あたりのサンプルを測定するカスタム SessionRunHook を設定する方法を示しています。フック (LoggerHook) を作成し、tf.estimator.Estimator.trainhooks パラメータに渡します。

def _input_fn(): return tf1.data.Dataset.from_tensor_slices( (features, labels)).batch(1).repeat(100) def _model_fn(features, labels, mode): logits = tf1.layers.Dense(1)(features) loss = tf1.losses.mean_squared_error(labels=labels, predictions=logits) optimizer = tf1.train.AdagradOptimizer(0.05) train_op = optimizer.minimize(loss, global_step=tf1.train.get_global_step()) return tf1.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
class LoggerHook(tf1.train.SessionRunHook): """Logs loss and runtime.""" def begin(self): self._step = -1 self._start_time = time.time() self.log_frequency = 10 def before_run(self, run_context): self._step += 1 def after_run(self, run_context, run_values): if self._step % self.log_frequency == 0: current_time = time.time() duration = current_time - self._start_time self._start_time = current_time examples_per_sec = self.log_frequency / duration print('Time:', datetime.now(), ', Step #:', self._step, ', Examples per second:', examples_per_sec) estimator = tf1.estimator.Estimator(model_fn=_model_fn) # Begin training. estimator.train(_input_fn, hooks=[LoggerHook()])

TensorFlow 2: Model.fit のカスタム Keras コールバックを作成する

TensorFlow 2 では、組み込みの Keras Model.fit(または Model.evaluate)をトレーニング/評価に使用する場合、カスタム tf.keras.callbacks.Callback を構成し、Model.fit(または Model.evaluate)の callbacks パラメータに渡します。(詳細については、独自のコールバックの作成ガイドを参照してください)。

以下の例では、さまざまな指標をログに記録するカスタム tf.keras.callbacks.Callback を記述します。これは 1 秒あたりのサンプルを測定します。これは、前の SessionRunHook のサンプルの指標と同様になるはずです。

class CustomCallback(tf.keras.callbacks.Callback): def on_train_begin(self, logs = None): self._step = -1 self._start_time = time.time() self.log_frequency = 10 def on_train_batch_begin(self, batch, logs = None): self._step += 1 def on_train_batch_end(self, batch, logs = None): if self._step % self.log_frequency == 0: current_time = time.time() duration = current_time - self._start_time self._start_time = current_time examples_per_sec = self.log_frequency / duration print('Time:', datetime.now(), ', Step #:', self._step, ', Examples per second:', examples_per_sec) callback = CustomCallback() dataset = tf.data.Dataset.from_tensor_slices( (features, labels)).batch(1).repeat(100) model = tf.keras.models.Sequential([tf.keras.layers.Dense(1)]) optimizer = tf.keras.optimizers.Adagrad(learning_rate=0.05) model.compile(optimizer, "mse") # Begin training. result = model.fit(dataset, callbacks=[callback], verbose = 0) # Provide the results of training metrics. result.history

次のステップ

コールバックの詳細については、次を参照してください。

次の移行関連のリソースも参照してください。