Tensorflow 显存占用情况
在tensorflow中,如果不设置显存占用方式,那么默认会占用全部显存。这里将Tensorflow的显存使用方式设置为按需分配,观测其真实占用得而GPU显存资源情况,代码如下:
# 代码参考 Tensorflow P219页
# 获取所有GPU设备列表
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
# 设置 GPU 显存占用为按需分配,增长式
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
# 异常处理
print(e)