Search code examples
google-colaboratorytpugoogle-cloud-tpu

RuntimeError:Mixing different tf.distribute.Strategy objects


Hello!I have encountered some problems when compiling the model using TPU.Some part of codes as follows:

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'])

And I got RuntimeError:enter image description here

Can you help me?


Solution

  • I solved my problem by various chaos trying.You can restart your program or comment the code:

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

    to avoid the problem