<jupyter_start><jupyter_text>GPU计算到目前为止，我们一直在使用CPU计算。对复杂的神经网络和大规模的数据来说，使用CPU来计算可能不够高效。在本节中，我们将介绍如何使用单块NVIDIA GPU来计算。首先，需要确保已经安装好了至少一块NVIDIA GPU。然后，下载CUDA并按照提示设置好相应的路径 注意：需要tensorflow-gpu<jupyter_code>import tensorflow as tf
import numpy as np
print(tf.__version__)

gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print("可用的GPU：",gpus,"\n可用的CPU：", cpus)<jupyter_output>2.0.0
可用的GPU： [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')] 
可用的CPU： [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]<jupyter_text>check available device<jupyter_code>from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())<jupyter_output>[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 15592483132577835191
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 3063309926
locality {
  bus_id: 1
  links {
  }
}
incarnation: 3859243074925251015
physical_device_desc: "device: 0, name: GeForce GTX 1650, pci bus id: 0000:01:00.0, compute capability: 7.5"
]<jupyter_text>specify device使用tf.device()来指定特定设备(GPU/CPU)<jupyter_code>with tf.device('GPU:0'):
    a = tf.constant([1,2,3],dtype=tf.float32)
    b = tf.random.uniform((3,))
    print(tf.exp(a + b) * 2)<jupyter_output>tf.Tensor([12.172885 19.682476 53.18001 ], shape=(3,), dtype=float32)
{'fraction_non_alphanumeric': 0.14285714285714285, 'fraction_numerical': 0.08163265306122448, 'mean_word_length': 8.875, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '379321', 'license': 'Apache-2.0', 'n_tokens_mistral': 711, 'n_tokens_neox': 654, 'n_words': 78, 'path': 'code/chapter04_DL-computation/4.6_use-GPU.ipynb', 'repo_name': 'laubob/D2DL', 'size': '1421'}