Search code examples
deep-learninggoogle-colaboratorygoogle-cloud-tpu

Google Colab KeyError: 'COLAB_TPU_ADDR'


I'm trying to run a simple MNIST classifier on Google Colab using the TPU option. After creating the model using Keras, I am trying to convert it into TPU by:

import tensorflow as tf
import os

tpu_model = tf.contrib.tpu.keras_to_tpu_model(
    model,
    strategy=tf.contrib.tpu.TPUDistributionStrategy(
        tf.contrib.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
    )
)
tpu_model.compile(
    optimizer='rmsprop',
    loss='categorical_crossentropy',
    metrics=['accuracy']
)


print(model.summary())

And the error I'm getting is:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-63c528142aab> in <module>()
      5     model,
      6     strategy=tf.contrib.tpu.TPUDistributionStrategy(
----> 7         tf.contrib.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
      8     )
      9 )

/usr/lib/python3.6/os.py in __getitem__(self, key)
    667         except KeyError:
    668             # raise KeyError with the original key value
--> 669             raise KeyError(key) from None
    670         return self.decodevalue(value)
    671 

KeyError: 'COLAB_TPU_ADDR'

It looks like I need to change the TPU address but been googling and haven't found anything yet. Appreciate some help, thanks!


Solution

  • You'll need to change the backend to include a TPU using the notebook settings available in the Edit -> Notebook settings menu.

    enter image description here