【问题标题】:RuntimeError:Mixing different tf.distribute.Strategy objectsRuntimeError:混合不同的 tf.distribute.Strategy 对象
【发布时间】:2020-01-28 08:33:04
【问题描述】:

您好!我在使用TPU编译模型时遇到了一些问题,部分代码如下:

resolver = tf.contrib.cluster_resolver.TPUClusterResolver(TF_MASTER)

tf.contrib.distribute.initialize_tpu_system(resolver)

strategy = tf.contrib.distribute.TPUStrategy(resolver)

with strategy.scope():

  model = create_model()

  model.compile(optimizer=tf.keras.optimizers.Adadelta(),loss='categorical_crossentropy',metrics='accuracy'])

我得到了 RuntimeError:enter image description here

你能帮帮我吗?

【问题讨论】:

    标签: google-colaboratory tpu google-cloud-tpu


    【解决方案1】:

    我通过各种乱七八糟的尝试解决了我的问题。您可以重新启动程序或注释代码:

    resolver = tf.contrib.cluster_resolver.TPUClusterResolver
    tf.contrib.distribute.initialize_tpu_system(resolver)
    strategy = tf.contrib.distribute.TPUStrategy(resolver)
    
    with strategy.scope():
      model = create_model()
      model.compile()
    

    避免问题

    【讨论】:

      【解决方案2】:

      同样的问题。 TensorFlow 的默认版本似乎是 1.x。我将代码更改为:(注释 3 行并添加其他行)

      try:
        # %tensorflow_version only exists in Colab.
        %tensorflow_version 2.x
      except Exception:
        pass
      
      # resolver = tf.contrib.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR'])
      # tf.contrib.distribute.initialize_tpu_system(resolver)
      # strategy = tf.contrib.distribute.TPUStrategy(resolver)
      resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
      tf.config.experimental_connect_to_cluster(resolver)
      tf.tpu.experimental.initialize_tpu_system(resolver)
      strategy = tf.distribute.experimental.TPUStrategy(resolver)
      

      解决了。

      【讨论】:

      • @double-beep 我终于找到了关键并改进了我的答案。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多