tensorflow tf.is_gpu_available() (判断GPU是否可用)
生活随笔
收集整理的這篇文章主要介紹了
tensorflow tf.is_gpu_available() (判断GPU是否可用)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
@tf_export("test.is_gpu_available")
def is_gpu_available(cuda_only=False, min_cuda_compute_capability=None):"""Returns whether TensorFlow can access a GPU.返回TensorFlow是否可以訪問GPU。Args:cuda_only: limit the search to CUDA gpus. 將搜索限制在CUDA GPU中。min_cuda_compute_capability: a (major,minor) pair that indicates the minimumCUDA compute capability required, or None if no requirement.一個(主要,次要)對,指示所需的最低CUDA計算能力;如果沒有要求,則為無。Returns:True iff a gpu device of the requested kind is available.如果請求的類型的gpu設備可用,則為true。"""def compute_capability_from_device_desc(device_desc):# TODO(jingyue): The device description generator has to be in sync with# this file. Another option is to put compute capability in# DeviceAttributes, but I avoided that to keep DeviceAttributes# target-independent. Reconsider this option when we have more things like# this to keep in sync.# LINT.IfChange# TODO(jingyue):設備描述生成器必須與此文件同步。 # 另一個選擇是將計算功能放到DeviceAttributes中,但是我避免了使DeviceAttributes與目標無關的問題。 # 當我們有更多類似的東西保持同步時,請重新考慮此選項。 LINT.IfChangematch = re.search(r"compute capability: (\d+)\.(\d+)", device_desc)# LINT.ThenChange(//tensorflow/core/\# common_runtime/gpu/gpu_device.cc)if not match:return 0, 0return int(match.group(1)), int(match.group(2))try:for local_device in device_lib.list_local_devices():if local_device.device_type == "GPU":if (min_cuda_compute_capability is None orcompute_capability_from_device_desc(local_device.physical_device_desc) >=min_cuda_compute_capability):return Trueif local_device.device_type == "SYCL" and not cuda_only:return Truereturn Falseexcept errors_impl.NotFoundError as e:if not all([x in str(e) for x in ["CUDA", "not find"]]):raise eelse:logging.error(str(e))return False
總結
以上是生活随笔為你收集整理的tensorflow tf.is_gpu_available() (判断GPU是否可用)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 计时器 timeit 报错
- 下一篇: tensorflow tf.data.D