首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TensorFlow1.15,估计器input_fn的内在逻辑?还是MirroredStrategy的内在逻辑?

TensorFlow1.15,估计器input_fn的内在逻辑?还是MirroredStrategy的内在逻辑?
EN

Stack Overflow用户
提问于 2020-06-11 11:48:18
回答 1查看 58关注 0票数 2

我在4个GPU的机器上预训练BERT,而不是1个GPU。

对于每个训练步骤,我想知道input_fn是给1个GPU 1批还是给4个GPU 1批。

mirrow策略代码:

代码语言:javascript
复制
    distribution = tf.contrib.distribute.MirroredStrategy(
        devices=["device:GPU:%d" % i for i in range(FLAGS.n_gpus)],
        cross_tower_ops=tf.distribute.HierarchicalCopyAllReduce())

    run_config = RunConfig(
        train_distribute=distribution,
        log_step_count_steps=log_every_n_steps,
        model_dir=FLAGS.output_dir,
        save_checkpoints_steps=FLAGS.save_checkpoints_steps)

    model_fn = model_fn_builder(
        bert_config=bert_config,
        init_checkpoint=FLAGS.init_checkpoint,
        learning_rate=FLAGS.learning_rate,
        num_train_steps=FLAGS.num_train_steps,
        num_warmup_steps=FLAGS.num_warmup_steps,
        use_tpu=FLAGS.use_tpu,
        use_one_hot_embeddings=FLAGS.use_tpu)

    estimator = Estimator(
        model_fn=model_fn,
        params={},
        config=run_config)

input_fn代码:

代码语言:javascript
复制
  def input_fn(params):
        batch_size = FLAGS.train_batch_size

        name_to_features = {
            "input_ids":
                tf.FixedLenFeature([max_seq_length], tf.int64),
            "input_mask":
                tf.FixedLenFeature([max_seq_length], tf.int64),
            "segment_ids":
                tf.FixedLenFeature([max_seq_length], tf.int64),
            "masked_lm_positions":
                tf.FixedLenFeature([max_predictions_per_seq], tf.int64),
            "masked_lm_ids":
                tf.FixedLenFeature([max_predictions_per_seq], tf.int64),
            "masked_lm_weights":
                tf.FixedLenFeature([max_predictions_per_seq], tf.float32),
            "next_sentence_labels":
                tf.FixedLenFeature([1], tf.int64),
        }

        if is_training:
            d = tf.data.Dataset.from_tensor_slices(tf.constant(input_files))
            d = d.repeat()
            d = d.shuffle(buffer_size=len(input_files))

            cycle_length = min(num_cpu_threads, len(input_files))

            d = d.apply(
                tf.contrib.data.parallel_interleave(
                    tf.data.TFRecordDataset,
                    sloppy=is_training,
                    cycle_length=cycle_length))
            d = d.shuffle(buffer_size=100)
        else:
            d = tf.data.TFRecordDataset(input_files)
            d = d.repeat()

        d = d.apply(
            tf.contrib.data.map_and_batch(
                lambda record: _decode_record(record, name_to_features),
                batch_size=batch_size,
                num_parallel_batches=num_cpu_threads,
                drop_remainder=True))
        d = d.prefetch(10)
        return d

其他代码:

代码语言:javascript
复制
estimator.train(input_fn, max_steps=FLAGS.num_train_steps)

如果input_fn提供1个GPU 1批,则train_batch_size应为max_batchsize_per_gpu。

如果input_fn给出4个GPU 1批,那么train_batch_size应该是max_batchsize_per_gpu*4。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-18 09:39:35

根据BERT-GPU的说法。

对于1个GPU,input_fn返回1批。

batch_size适用于1个GPU。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62316736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档