5

您好!我在使用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

你能帮助我吗?

4

2 回答 2

2

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

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

避免问题

于 2019-10-11T12:45:00.410 回答
1

同样的问题。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)

它解决了。

于 2020-02-12T18:10:02.997 回答