RuntimeError:Mixing different tf.distribute.Strategy objects

RuntimeError:Mixing different tf.distribute.Strategy objects

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

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

你能帮帮我吗?

我解决了我的问题trying.You可以重新启动你的程序或评论代码:

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()

避免问题

同样的问题。似乎默认的 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)

解决了。