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

このガイドでは、単一ワーカーのマルチ GPU ワークフローを TensorFlow 1 から TensorFlow 2 に移行する方法を実演します。

1 台のマシンのマルチ GPU で同期トレーニングを実行するには、

セットアップ

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

import tensorflow as tf import tensorflow.compat.v1 as tf1
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.Estimator を使った単一ワーカーでの分散トレーニング

この例では、TensorFlow 1 での単一ワーカーのマルチ GPU トレーニングの標準ワークフローを示します。tf.estimator.Estimatorconfig パラメータを使用して、分散ストラテジー (tf.distribute.MirroredStrategy)を設定する必要があります。

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(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) strategy = tf1.distribute.MirroredStrategy() config = tf1.estimator.RunConfig( train_distribute=strategy, eval_distribute=strategy) estimator = tf1.estimator.Estimator(model_fn=_model_fn, config=config) train_spec = tf1.estimator.TrainSpec(input_fn=_input_fn) eval_spec = tf1.estimator.EvalSpec(input_fn=_eval_input_fn) tf1.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

TensorFlow 2: Keras を使った単一ワーカーでのトレーニング

TensorFlow 2 に移行する場合は、tf.distribute.MirroredStrategy で Keras API を使用できます。

モデル構築に tf.keras API を使用し、トレーニングに Keras Model.fit を使用する場合、主な違いは、tf.estimator.Estimatorconfig を定義する代わりに Keras モデル、オプティマイザ、および指標をStrategy.scope のコンテキストでインスタンス化することです。

カスタムトレーニングループを使用する必要がある場合は、カスタムトレーニングループで tf.distribute.Strategy を使用するガイドを確認してください。

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)
strategy = tf.distribute.MirroredStrategy() with strategy.scope(): model = tf.keras.models.Sequential([tf.keras.layers.Dense(1)]) optimizer = tf.keras.optimizers.Adagrad(learning_rate=0.05) model.compile(optimizer=optimizer, loss='mse') model.fit(dataset) model.evaluate(eval_dataset, return_dict=True)

次のステップ

TensorFlow 2 の tf.distribute.MirroredStrategy を使った分散トレーニングの詳細については、次のドキュメントを参照してください。