tensorflow - 如何在 google colab 中使用 TPU

标签 tensorflow keras google-colaboratory google-cloud-tpu

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?

enter image description here

最佳答案

这是 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/

相关文章:

python - Tensorflow 在训练时两次打印相同的信息

TensorFlow 2.3 GPU - InvalidArgumentError : assertion failed: [0] [Op:Assert] name: EagerVariableNameReuse

tensorflow - tensorflow 1.0 mnist 代码出错

nlp - keras 中 CNN 和 RNN 模型的集成

tensorflow - 如何在 Tensorflow 对象检测 API 中存储最佳模型检查点,而不仅仅是最新的 5 个?

python - 试图让简单的 Keras 神经网络示例工作

python - 有没有办法通过 JavaScript 在我的网站中使用 TensorFlow 模型?

jupyter-notebook - 如何将 Jupyter Notebook 从 Local Machine 直接上传到 Google Colab?有没有办法做到这一点?

python - 当从 tensorflow 概率的分布中采样时,张量是不可散列的错误(在colab上)

python-3.x - 将图片从 Google Drive 导入到 Google Colab