【问题标题】:TPU with Tensorflow v1TPU 与 TensorFlow v1
【发布时间】:2020-07-02 16:30:04
【问题描述】:

请任何人都可以给我代码以使用 Tensorflow V1 运行 TPU 吗? 我正在尝试这段代码,但它仅适用于 Tensorflow 2.0:

 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)

【问题讨论】:

    标签: tensorflow tpu


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 2019-11-06
      • 2021-10-02
      • 2020-01-06
      • 2021-10-05
      • 2019-03-24
      • 1970-01-01
      • 2022-07-31
      • 1970-01-01
      相关资源
      最近更新 更多