【问题标题】:Training with Global Batch Size on a TPU (tensorflow)在 TPU 上使用全局批量大小进行训练(张量流)
【发布时间】:2020-10-29 15:12:51
【问题描述】:

我最近在 Google Colab 上启动了一个神经网络项目,我发现我可以使用 TPU。我一直在研究如何使用它,发现了 tensorflow 的TPUStrategy(我使用的是 tensorflow 2.2.0),并且能够成功定义模型并在 TPU 上运行训练步骤。

但是,我不确定这意味着什么。可能是因为我没有仔细阅读 Google 的 TPU 指南,但我的意思是我不知道在训练步骤中到底发生了什么。

该指南要求您定义一个GLOBAL_BATCH_SIZE,每个 TPU 内核采用的批大小由per_replica_batch_size = GLOBAL_BATCH_SIZE / strategy.num_replicas_in_sync 给出,这意味着每个 TPU 的批大小小于您开始时的批大小。在 Colab 上,strategy.num_replicas_in_sync = 8,这意味着如果我从 64 的GLOBAL_BATCH_SIZE 开始,per_replica_batch_size 是 8。

现在,我不明白的是,当我计算一个训练步骤时,优化器是否会在大小为 per_replica_batch_size 的批次上计算 8 个不同的步骤,将模型的权重更新 8 个不同时间,或者它只是并行化计算以这种方式训练步骤,最后只计算一个批量大小GLOBAL_BATCH_SIZE 的优化器步骤。谢谢。

【问题讨论】:

    标签: tensorflow neural-network tensorflow2.0 tpu batchsize


    【解决方案1】:

    这是一个很好的问题,并且与 Distribution Strategy 更相关。

    看完这个Tensorflow Documentation,TPU Strategy Documentation和这个Synchronous and Asynchronous Training的解释后,

    我可以这么说

    > the optimizer computes 8 different steps on batches of size
    > per_replica_batch_size, updating the weights of the model 8 different
    > times
    

    Tensorflow Documentation 的以下解释应该澄清:

    > So, how should the loss be calculated when using a
    > tf.distribute.Strategy?
    > 
    > For an example, let's say you have 4 GPU's and a batch size of 64. One
    > batch of input is distributed across the replicas (4 GPUs), each
    > replica getting an input of size 16.
    > 
    > The model on each replica does a forward pass with its respective
    > input and calculates the loss. Now, instead of dividing the loss by
    > the number of examples in its respective input (BATCH_SIZE_PER_REPLICA
    > = 16), the loss should be divided by the GLOBAL_BATCH_SIZE (64).
    

    在下面提供其他链接的解释(以防它们将来不起作用):

    TPU Strategy documentation 状态:

    > In terms of distributed training architecture, `TPUStrategy` is the
    > same `MirroredStrategy` - it implements `synchronous` distributed
    > training. `TPUs` provide their own implementation of efficient
    > `all-reduce` and other collective operations across multiple `TPU`
    > cores, which are used in `TPUStrategy`.
    

    Synchronous and Asynchronous Training的解释如下:

    > `Synchronous vs asynchronous training`: These are two common ways of
    > `distributing training` with `data parallelism`. In `sync training`, all
    > `workers` train over different slices of input data in `sync`, and
    > **`aggregating gradients`** at each step. In `async` training, all workers are
    > independently training over the input data and updating variables
    > `asynchronously`. Typically sync training is supported via all-reduce
    > and `async` through parameter server architecture.
    

    你也可以通过这个MPI Tutorial详细了解All_Reduce的概念。

    下面的屏幕截图显示了 All_Reduce 的工作原理:

    【讨论】:

    • 感谢您的回答。我正在进行同步训练,所以梯度是由 All_Reduce 聚合的,对吧?但这是否意味着它只是用总梯度更新权重一次,而不是 8 次?或者这是否意味着权重使用相同的梯度更新了 8 次?
    • 对于有 8 个副本和 per_replica_batch_size = 8 的示例,我认为优化器应该只更新 1 次权重,并且在收到所有副本的梯度之后。如果更新权重 8 次,那么与单台设备训练(1 个副本)相比会有不同的结果。
    猜你喜欢
    • 2020-09-04
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2021-06-17
    • 2018-11-08
    • 2018-03-12
    相关资源
    最近更新 更多