如何在谷歌colab中使用TPU

Ami*_*mir 11 keras tensorflow google-colaboratory google-cloud-tpu

Google colab在运行时加速器中引入了TPU.我找到了一个例子,如何在官方Tensorflow github中使用TPU .但这个例子并不适用于Google-colaboratory.它停留在以下行:

tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)
Run Code Online (Sandbox Code Playgroud)

当我在colab上打印可用设备时,它会返回[]TPU加速器.有谁知道如何在colab上使用TPU?

在此输入图像描述

Bob*_*ith 13

以下是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)))
Run Code Online (Sandbox Code Playgroud)

(与GPU不同,使用TPU需要与TPU工作人员明确连接.因此,您需要调整训练和推理定义以观察加速.)