Google colab 在运行时加速器中引入了 TPU。我找到了一个例子,How to use TPU in Official Tensorflow github .但是这个例子在 google-colaboratory 上不起作用。它停留在以下行:
tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)
当我print available devices在 colab 上它返回
[]
用于 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 工作线程的显式连接。因此,您需要调整训练和推理定义以观察加速情况。)
关于tensorflow - 如何在 google colab 中使用 TPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52530578/