【问题标题】:Why Tensorflow's MirroredStrategy and OneDevicestrategy does not work on colab?为什么 TensorFlow 的 MirroredStrategy 和 OneDevicestrategy 在 colab 上不起作用?
【发布时间】:2020-01-08 12:50:33
【问题描述】:

我正在尝试了解使用 MirroredStrategy 及其其他变体的 Tensorflow 分布式训练。我已经在 colab 上有一个简单的 MNIST 脚本,但需要在多个 GPU 上进行测试。运行和实现代码给我一个错误,因为“ValueError:编译中的分发参数在 TF 2.0 中不可用,请在分发策略范围内创建模型。”我也尝试过 OneDeviceStrategy,但它也不起作用。我想使用各种分布式训练方法比较时间复杂度和准确性。这里有一个 Screenshot of the errorLink to the code on Colab

【问题讨论】:

    标签: tensorflow machine-learning deep-learning artificial-intelligence distributed-computing


    【解决方案1】:

    问题本质上是您使用分布策略范围的方式。在 TF 2.0 中,您不会将分发策略传递给 compile 方法。相反,您需要构建模型并在分发策略范围内对其进行编译。请注意,对 model.fit(...) 的调用不应在分发策略范围内。例如,这是您的 colab 代码的修订版本,应该可以解决您的问题:

    ...
    
    strategy = tf.distribute.MirroredStrategy()
    
    with strategy.scope():
      input_img = layers.Input(shape=IMG_SIZE)
      model = layers.Conv2D(32, (3, 3), padding='same')(input_img)
      ... # model definition
      output_img = layers.Activation('softmax')(model)
    
      model = models.Model(input_img, output_img)
    
      model.compile(optimizers.Adam(lr=0.0001), loss='categorical_crossentropy', metrics=["accuracy"])
    
    ...
    
    history = model.fit(...)
    

    请参阅Distributed training with TensorFlow 指南的Using tf.distribute.Strategy with Keras 部分了解更多信息。

    【讨论】:

      猜你喜欢
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 2022-06-21
      • 2019-09-28
      • 2019-05-07
      • 2017-12-06
      • 2022-11-02
      • 1970-01-01
      相关资源
      最近更新 更多