Google Colab KeyError:“ COLAB_TPU_ADDR”

时间:2018-11-04 15:43:01

标签: deep-learning google-colaboratory google-cloud-tpu

我正在尝试使用TPU选项在Google Colab上运行一个简单的MNIST分类器。使用Keras创建模型后,我尝试通过以下方式将其转换为TPU:

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())

我得到的错误是:

---------------------------------------------------------------------------
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'

似乎我需要更改TPU地址,但是正在谷歌搜索并且还没有找到任何东西。感谢一些帮助,谢谢!

1 个答案:

答案 0 :(得分:4)

您需要使用“编辑”->“笔记本设置”菜单中可用的笔记本设置来更改后端以包括TPU。

enter image description here