如何在 google colab 中使用 TPU

How use TPU in google colab

Google colab 在运行时加速器中引入了 TPU。我找到了一个示例,如何在 Official Tensorflow github 中使用 TPU。但该示例不适用于 google-colaboratory。它停留在以下行:

tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)

当我 在 colab 上 return [] 用于 TPU 加速器。有人知道如何在 colab 上使用 TPU 吗?

这是一个特定于 Colab 的 TPU 示例: https://colab.research.google.com/github/tensorflow/tpu/blob/master/tools/colab/shakespeare_with_tpu_and_keras.ipynb

关键线是连接到 TPU 本身的线:

# This address identifies the TPU we'll use when configuring TensorFlow.
TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR']

...

tpu_model = tf.contrib.tpu.keras_to_tpu_model(
training_model,
strategy=tf.contrib.tpu.TPUDistributionStrategy(
    tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKER)))

(与 GPU 不同,使用 TPU 需要与 TPU worker 建立显式连接。因此,您需要调整训练和推理定义才能观察到加速。)