我试图使用TPU选项在Google上运行一个简单的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地址,但一直在谷歌上搜索,还没有找到任何东西。感谢你的帮助,谢谢!
发布于 2022-06-09 17:16:25
很多时候在谷歌的Colab TPU是不能的,你不会得到一个TPU。而Colab会抛出这个错误。您将需要找到一个时间,当它更有可能得到科拉布TPU,如清晨或深夜。
发布于 2022-08-08 20:26:11
使用下一个代码:
import tensorflow as tf
print("Tensorflow version " + tf.__version__)
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection
print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
except ValueError:
raise BaseException('ERROR: Not connected to a TPU runtime; please see the previous cell in this notebook for instructions!')https://stackoverflow.com/questions/53142474
复制相似问题