首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有Tensorflow v1的TPU

带有Tensorflow v1的TPU
EN

Stack Overflow用户
提问于 2020-07-03 00:30:04
回答 1查看 109关注 0票数 2

请任何人能给我的代码运行TPU与Tensorflow V1?我正在尝试这段代码,但它只适用于Tensorflow 2.0:

代码语言:javascript
复制
 try:
    # TPU detection. No parameters necessary if TPU_NAME environment variable is
    # set: this is always the case on Kaggle.
    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
    print('Running on TPU ', tpu.master())
 except ValueError:
    tpu = None

 if tpu:
    tf.config.experimental_connect_to_cluster(tpu)
    tf.tpu.experimental.initialize_tpu_system(tpu)
    strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
    # Default distribution strategy in Tensorflow. Works on CPU and single GPU.
    strategy = tf.distribute.get_strategy()

 print("REPLICAS: ", strategy.num_replicas_in_sync)
EN

回答 1

Stack Overflow用户

发布于 2020-07-05 17:54:59

Tensorflow V1.0 TPU或GPU检测代码:

代码语言:javascript
复制
# Detect hardware
try:
  tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection
except ValueError:
  tpu = None
  gpus = tf.config.experimental.list_logical_devices("GPU")
    
# Select appropriate distribution strategy
if tpu:
  tf.tpu.experimental.initialize_tpu_system(tpu)
# Going back and forth between TPU and host is expensive. Better to run 128 batches on the TPU before reporting back.
  strategy = tf.distribute.experimental.TPUStrategy(tpu, steps_per_run=128) 
  print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])  
elif len(gpus) > 1:
  strategy = tf.distribute.MirroredStrategy([gpu.name for gpu in gpus])
  print('Running on multiple GPUs ', [gpu.name for gpu in gpus])
elif len(gpus) == 1:
  strategy = tf.distribute.get_strategy() # default strategy that works on CPU and single GPU
  print('Running on single GPU ', gpus[0].name)
else:
  strategy = tf.distribute.get_strategy() # default strategy that works on CPU and single GPU
  print('Running on CPU')
print("Number of accelerators: ", strategy.num_replicas_in_sync)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62700996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档