擅长:python、mysql、java
<p>签出<a href="https://stackoverflow.com/questions/38559755/how-to-get-current-available-gpus-in-tensorflow">this answer</a>以列出可用的GPU</p>
<pre class="lang-py prettyprint-override"><code>from tensorflow.python.client import device_lib
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
</code></pre>
<p>您还可以使用CUDA来<a href="https://pytorch.org/docs/stable/cuda.html#torch.cuda.current_device" rel="nofollow noreferrer">list the current device</a>,如果需要,还可以使用<a href="https://pytorch.org/docs/stable/cuda.html#torch.cuda.set_device" rel="nofollow noreferrer">set the device</a></p>
<pre class="lang-py prettyprint-override"><code>import torch
print(torch.cuda.is_available())
print(torch.cuda.current_device())
</code></pre>