gpt4 book ai didi

tensorflow - 如何检查 TPU 在 Google Colab 中是否可用?

转载 作者:行者123 更新时间:2023-12-05 01:11:45 24 4
gpt4 key购买 nike

我正在尝试根据 TPU 的可用性选择分配策略。

我的代码如下:

import tensorflow as tf
if tf.config.list_physical_devices('tpu'):
resolver = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(resolver)
tf.tpu.experimental.initialize_tpu_system(resolver)
print("All devices: ", tf.config.list_logical_devices('TPU'))
strategy = tf.distribute.experimental.TPUStrategy(resolver)
else: # use default strategy
strategy = tf.distribute.get_strategy()

但它不起作用。

如何识别 TPU?

最佳答案

以下代码有效:

import tensorflow as tf
try:
resolver = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(resolver)
tf.tpu.experimental.initialize_tpu_system(resolver)
print("All devices: ", tf.config.list_logical_devices('TPU'))
strategy = tf.distribute.experimental.TPUStrategy(resolver)
except ValueError:
strategy = tf.distribute.get_strategy()

关于tensorflow - 如何检查 TPU 在 Google Colab 中是否可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62711603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com