tensorflow 常见代码段

tf2 allow gpu memory growth

否则将占用全部内存

import tensorflow as tf
# Config
gpus = tf.config.experimental.list_physical_devices('GPU')
print("GPU Available: ", gpus)
if gpus:
  try:
    # Currently, memory growth needs to be the same across GPUs
    for gpu in gpus:
        tf.config.experimental.set_memory_growth(gpu, True)
    logical_gpus = tf.config.experimental.list_logical_devices('GPU')
    print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
  except RuntimeError as e:
    # Memory growth must be set before GPUs have been initialized
    print(e)

你可能感兴趣的:(tensorflow 常见代码段)