首页 文章

如何在谷歌colab中使用TPU

提问于
浏览
8

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

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

当我在colab上它返回 [] 用于TPU加速器 . 有谁知道如何在colab上使用TPU?

enter image description here

1 回答

  • 11

    这是一个特定于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工作人员明确连接 . 因此,您需要调整训练和推理定义以观察加速 . )

相关问题