How to run a Tensorflow Evaluator on multiple GPUs with parallelism data

I have a standard evaluation calculator with some model and want to run it on several GPUs instead of one. How can this be done using parallelism data?

I searched Tensorflow Docs but did not find an example; only suggestions saying that with Estimator will be easy.

Does anyone have a good example using tf.learn.Estimator? Or a link to a textbook or so?

+11
source share
5 answers

, tf.contrib.estimator.replicate_model_fn . tf.contrib.estimator.replicate_model_fn,

...
def model_fn(...):  # See 'model_fn' in 'Estimator'.
  loss = ...
  optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
  optimizer = tf.contrib.estimator.TowerOptimizer(optimizer)
  if mode == tf.estimator.ModeKeys.TRAIN:
    #  See the section below on 'EstimatorSpec.train_op'.
    return EstimatorSpec(mode=mode, loss=loss,
                         train_op=optimizer.minimize(loss))

  #  No change for 'ModeKeys.EVAL' or 'ModeKeys.PREDICT'.
  return EstimatorSpec(...)
...
classifier = tf.estimator.Estimator(
  model_fn=tf.contrib.estimator.replicate_model_fn(model_fn))

tf.contrib.estimator.TowerOptimize model_fn() tf.contrib.estimator.replicate_model_fn(). TPU squeezenet 4 . .

+7

:

 with tf.variable_scope(tf.get_variable_scope()):
  for i in xrange(FLAGS.num_gpus):
    with tf.device('/gpu:%d' % i):
      with tf.name_scope('%s_%d' % (cifar10.TOWER_NAME, i)) as scope:

: https://github.com/tensorflow/models/blob/master/tutorials/image/cifar10/cifar10_multi_gpu_train.py

+1

, , .

: https://www.youtube.com/watch?v=bRMGoPqsn20

: https://www.tensorflow.org/api_docs/python/tf/distribute/Strategy

: https://medium.com/tensorflow/multi-gpu-training-with-estimators-tf-keras-and-tf-data-ba584c3134db

NUM_GPUS = 8
dist_strategy = tf.contrib.distribute.MirroredStrategy(num_gpus=NUM_GPUS)
config = tf.estimator.RunConfig(train_distribute=dist_strategy)
estimator = tf.estimator.Estimator(model_fn,model_dir,config=config)
+1

You can find an example of using tf.distribute.MirroredStrategyand tf.estimator.train_and_evaluate here .

0
source

Source: https://habr.com/ru/post/1689064/


All Articles