Tensorflow 設置GPU按需分配 memory


import tensorflow as tf

def Set_GPU_Memory_Growth():
	gpus = tf.config.experimental.list_physical_devices('GPU')
	if gpus:
		try:
			# 設置 GPU 顯存佔用爲按需分配
			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:
			# 異常處理
			print(e)
	else :
		print('No GPU')

# 放在建立模型實例之前
Set_GPU_Memory_Growth()

參考地址:https://github.com/dragen1860/TensorFlow-2.x-Tutorials

發佈了226 篇原創文章 · 獲贊 250 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章