【问题标题】:How to check if TPU is available in Google Colab?如何检查 TPU 在 Google Colab 中是否可用?
【发布时间】:2020-10-23 22:53:03
【问题描述】:

我正在尝试根据 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?

【问题讨论】:

    标签: tensorflow google-colaboratory tensorflow2.0 tpu


    【解决方案1】:

    以下代码有效:

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

    【讨论】:

      猜你喜欢
      • 2021-02-17
      • 2019-12-06
      • 2020-12-16
      • 2021-07-04
      • 2020-02-02
      • 1970-01-01
      • 2021-02-20
      • 2020-10-30
      • 2020-02-10
      相关资源
      最近更新 更多