{"id":944830,"date":"2024-12-26T23:14:24","date_gmt":"2024-12-26T15:14:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/944830.html"},"modified":"2024-12-26T23:14:26","modified_gmt":"2024-12-26T15:14:26","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8gpu","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/944830.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528gpu"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081628\/b698b2a4-188d-41a4-a2a2-2d5c78d9739a.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528gpu\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4f7f\u7528GPU\u4e3b\u8981\u4f9d\u8d56\u4e8e\u4e00\u4e9b\u4e13\u95e8\u7684\u5e93\u548c\u6846\u67b6\uff0c\u8fd9\u4e9b\u5e93\u548c\u6846\u67b6\u80fd\u591f\u5c06\u8ba1\u7b97\u4efb\u52a1\u4eceCPU\u5378\u8f7d\u5230GPU\uff0c\u4ee5\u52a0\u901f\u8ba1\u7b97\u8fc7\u7a0b\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528CUDA\u3001\u5229\u7528\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff08\u5982TensorFlow\u3001PyTorch\uff09\u3001\u4f7f\u7528Numba\u52a0\u901f\u8ba1\u7b97\u3001\u501f\u52a9CuPy\u5e93\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u7279\u70b9\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u901a\u8fc7\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u6765\u5b9e\u73b0GPU\u52a0\u901f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\u2014\u2014TensorFlow\u6765\u8fdb\u884cGPU\u52a0\u901f\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528TensorFlow\u8fdb\u884cGPU\u52a0\u901f\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86\u652f\u6301\u7684NVIDIA GPU\u9a71\u52a8\u7a0b\u5e8f\u548cCUDA Toolkit\u3002TensorFlow\u81ea\u52a8\u68c0\u6d4b\u5e76\u5229\u7528\u53ef\u7528\u7684GPU\u8d44\u6e90\u8fdb\u884c\u8ba1\u7b97\uff0c\u8fd9\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u65b9\u4fbf\u5730\u5229\u7528GPU\u5f3a\u5927\u7684\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u3002\u5728TensorFlow\u4e2d\uff0c\u6570\u636e\u548c\u6a21\u578b\u7684\u64cd\u4f5c\u4f1a\u9ed8\u8ba4\u4f7f\u7528GPU\u6765\u6267\u884c\uff0c\u53ea\u8981GPU\u662f\u53ef\u7528\u7684\u3002\u6b64\u5916\uff0cTensorFlow\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u914d\u7f6e\u9009\u9879\uff0c\u8ba9\u7528\u6237\u53ef\u4ee5\u63a7\u5236\u5185\u5b58\u5206\u914d\u548c\u8bbe\u5907\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528CUDA\u8fdb\u884cGPU\u52a0\u901f<\/h3>\n<\/p>\n<p><p>CUDA\u662fNVIDIA\u63a8\u51fa\u7684\u4e00\u79cd\u5e76\u884c\u8ba1\u7b97\u5e73\u53f0\u548c\u7f16\u7a0b\u6a21\u578b\uff0c\u5b83\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u4f7f\u7528C\u3001C++\u3001Fortran\u8bed\u8a00\u7f16\u5199\u7a0b\u5e8f\u6765\u5229\u7528GPU\u8fdb\u884c\u901a\u7528\u8ba1\u7b97\u3002Python\u4e2d\u53ef\u4ee5\u901a\u8fc7PyCUDA\u5e93\u6765\u4f7f\u7528CUDA\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u8bbe\u7f6eCUDA\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528CUDA\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5NVIDIA\u7684CUDA Toolkit\u548c\u76f8\u5e94\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002\u53ef\u4ee5\u4eceNVIDIA\u7684\u5b98\u7f51\u4e0a\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8fd8\u9700\u8981\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u786e\u4fdd\u7cfb\u7edf\u80fd\u591f\u627e\u5230CUDA\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528PyCUDA\u5728Python\u4e2d\u8fdb\u884cGPU\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>PyCUDA\u662f\u4e00\u4e2aPython\u5e93\uff0c\u5141\u8bb8\u7528\u6237\u5728Python\u4e2d\u7f16\u5199CUDA\u7a0b\u5e8f\u3002\u4f7f\u7528PyCUDA\u9700\u8981\u7f16\u5199CUDA\u5185\u6838\uff0c\u5e76\u901a\u8fc7Python\u63a5\u53e3\u6765\u8c03\u7528\u8fd9\u4e9b\u5185\u6838\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pycuda.autoinit<\/p>\n<p>import pycuda.driver as cuda<\/p>\n<p>from pycuda.compiler import SourceModule<\/p>\n<p>mod = SourceModule(&quot;&quot;&quot;<\/p>\n<p>__global__ void multiply_them(float *dest, float *a, float *b) {<\/p>\n<p>    const int i = threadIdx.x;<\/p>\n<p>    dest[i] = a[i] * b[i];<\/p>\n<p>}<\/p>\n<p>&quot;&quot;&quot;)<\/p>\n<p>multiply_them = mod.get_function(&quot;multiply_them&quot;)<\/p>\n<p>import numpy as np<\/p>\n<p>a = np.random.randn(400).astype(np.float32)<\/p>\n<p>b = np.random.randn(400).astype(np.float32)<\/p>\n<p>dest = np.zeros_like(a)<\/p>\n<p>multiply_them(<\/p>\n<p>    cuda.Out(dest), cuda.In(a), cuda.In(b),<\/p>\n<p>    block=(400, 1, 1), grid=(1, 1))<\/p>\n<p>print(dest - a*b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u8fdb\u884cGPU\u52a0\u901f<\/h3>\n<\/p>\n<p><p>\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u5982TensorFlow\u548cPyTorch\u90fd\u63d0\u4f9b\u4e86\u76f4\u63a5\u5728GPU\u4e0a\u8fdb\u884c\u8ba1\u7b97\u7684\u80fd\u529b\u3002\u8fd9\u4e9b\u6846\u67b6\u81ea\u52a8\u7ba1\u7406\u6570\u636e\u4f20\u8f93\u548c\u8ba1\u7b97\u8bbe\u5907\u7684\u9009\u62e9\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u4e13\u6ce8\u4e8e\u9ad8\u5c42\u6b21\u7684\u6a21\u578b\u8bbe\u8ba1\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528TensorFlow<\/h4>\n<\/p>\n<p><p>TensorFlow\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u5b83\u5bf9GPU\u652f\u6301\u826f\u597d\u3002\u53ea\u9700\u5b89\u88c5GPU\u7248\u672c\u7684TensorFlow\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u81ea\u52a8\u68c0\u6d4b\u5e76\u5229\u7528GPU\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tensorflow-gpu<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4f7f\u7528TensorFlow\u65f6\uff0c\u53ea\u9700\u5728\u4ee3\u7801\u4e2d\u5b9a\u4e49\u6a21\u578b\u548c\u6570\u636e\uff0cTensorFlow\u4f1a\u81ea\u52a8\u5229\u7528\u53ef\u7528\u7684GPU\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<h2><strong>\u68c0\u67e5GPU\u8bbe\u5907<\/strong><\/h2>\n<p>print(&quot;Num GPUs Av<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lable: &quot;, len(tf.config.list_physical_devices(&#39;GPU&#39;)))<\/p>\n<h2><strong>\u6784\u5efa\u7b80\u5355\u7684\u795e\u7ecf\u7f51\u7edc<\/strong><\/h2>\n<p>model = tf.keras.models.Sequential([<\/p>\n<p>    tf.keras.layers.Dense(128, activation=&#39;relu&#39;, input_shape=(784,)),<\/p>\n<p>    tf.keras.layers.Dropout(0.2),<\/p>\n<p>    tf.keras.layers.Dense(10, activation=&#39;softmax&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;,<\/p>\n<p>              loss=&#39;sparse_categorical_crossentropy&#39;,<\/p>\n<p>              metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<h2><strong>x_train, y_train \u662f\u8bad\u7ec3\u6570\u636e\u548c\u6807\u7b7e<\/strong><\/h2>\n<h2><strong>model.fit(x_train, y_train, epochs=5)<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528PyTorch<\/h4>\n<\/p>\n<p><p>PyTorch\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u4e5f\u652f\u6301GPU\u52a0\u901f\u3002\u4f7f\u7528PyTorch\u65f6\uff0c\u9700\u8981\u5c06\u6a21\u578b\u548c\u6570\u636e\u663e\u5f0f\u5730\u79fb\u52a8\u5230GPU\u8bbe\u5907\u4e0a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install torch<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728PyTorch\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>.to(&#39;cuda&#39;)<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u548c\u6a21\u578b\u79fb\u52a8\u5230GPU\u4e0a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<h2><strong>\u68c0\u67e5CUDA\u662f\u5426\u53ef\u7528<\/strong><\/h2>\n<p>device = torch.device(&quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;)<\/p>\n<p>print(&quot;Using device:&quot;, device)<\/p>\n<h2><strong>\u5b9a\u4e49\u6a21\u578b<\/strong><\/h2>\n<p>model = torch.nn.Sequential(<\/p>\n<p>    torch.nn.Linear(784, 128),<\/p>\n<p>    torch.nn.ReLU(),<\/p>\n<p>    torch.nn.Dropout(0.2),<\/p>\n<p>    torch.nn.Linear(128, 10),<\/p>\n<p>    torch.nn.Softmax(dim=1)<\/p>\n<p>).to(device)<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = torch.randn(64, 784).to(device)<\/p>\n<h2><strong>\u524d\u5411\u4f20\u64ad<\/strong><\/h2>\n<p>output = model(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Numba\u8fdb\u884cGPU\u52a0\u901f<\/h3>\n<\/p>\n<p><p>Numba\u662f\u4e00\u4e2a\u7528\u4e8e\u52a0\u901fPython\u4ee3\u7801\u7684\u7f16\u8bd1\u5668\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5c06Python\u51fd\u6570\u7f16\u8bd1\u4e3a\u673a\u5668\u7801\uff0c\u5e76\u652f\u6301GPU\u52a0\u901f\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Numba<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numba<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Numba\u8fdb\u884cGPU\u52a0\u901f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Numba\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>@cuda.jit<\/code>\u88c5\u9970\u5668\u5c06\u51fd\u6570\u7f16\u8bd1\u4e3aCUDA\u5185\u6838\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from numba import cuda<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49CUDA\u5185\u6838<\/strong><\/h2>\n<p>@cuda.jit<\/p>\n<p>def vector_add(a, b, c):<\/p>\n<p>    i = cuda.grid(1)<\/p>\n<p>    if i &lt; a.size:<\/p>\n<p>        c[i] = a[i] + b[i]<\/p>\n<h2><strong>\u521b\u5efa\u8f93\u5165\u6570\u636e<\/strong><\/h2>\n<p>a = np.random.randn(1000000).astype(np.float32)<\/p>\n<p>b = np.random.randn(1000000).astype(np.float32)<\/p>\n<p>c = np.zeros_like(a)<\/p>\n<h2><strong>\u5c06\u6570\u636e\u4f20\u8f93\u5230GPU<\/strong><\/h2>\n<p>d_a = cuda.to_device(a)<\/p>\n<p>d_b = cuda.to_device(b)<\/p>\n<p>d_c = cuda.to_device(c)<\/p>\n<h2><strong>\u6267\u884cCUDA\u5185\u6838<\/strong><\/h2>\n<p>threads_per_block = 256<\/p>\n<p>blocks_per_grid = (a.size + (threads_per_block - 1)) \/\/ threads_per_block<\/p>\n<p>vector_add[blocks_per_grid, threads_per_block](d_a, d_b, d_c)<\/p>\n<h2><strong>\u5c06\u7ed3\u679c\u4f20\u56deCPU<\/strong><\/h2>\n<p>c = d_c.copy_to_host()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528CuPy\u8fdb\u884cGPU\u52a0\u901f<\/h3>\n<\/p>\n<p><p>CuPy\u662f\u4e00\u4e2aNumpy\u517c\u5bb9\u7684\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5229\u7528NVIDIA GPU\u8fdb\u884c\u52a0\u901f\u8ba1\u7b97\u3002\u5b83\u7684API\u8bbe\u8ba1\u4e0eNumpy\u76f8\u4f3c\uff0c\u4f7f\u5f97\u4eceNumpy\u8fc1\u79fb\u5230CuPy\u5341\u5206\u5bb9\u6613\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5CuPy<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cupy-cuda11x  # \u9009\u62e9\u9002\u5408\u7684CUDA\u7248\u672c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528CuPy\u8fdb\u884c\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>CuPy\u7684\u4f7f\u7528\u65b9\u5f0f\u4e0eNumpy\u7c7b\u4f3c\uff0c\u53ea\u9700\u5c06Numpy\u6570\u7ec4\u66ff\u6362\u4e3aCuPy\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cupy as cp<\/p>\n<h2><strong>\u521b\u5efaCuPy\u6570\u7ec4<\/strong><\/h2>\n<p>x = cp.arange(6).reshape(2, 3).astype(&#39;f&#39;)<\/p>\n<p>y = cp.arange(6).reshape(2, 3).astype(&#39;f&#39;)<\/p>\n<h2><strong>\u6267\u884c\u52a0\u6cd5\u8fd0\u7b97<\/strong><\/h2>\n<p>z = x + y<\/p>\n<h2><strong>\u5c06\u7ed3\u679c\u4f20\u56deCPU<\/strong><\/h2>\n<p>z_cpu = cp.asnumpy(z)<\/p>\n<p>print(z_cpu)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0cPython\u7a0b\u5e8f\u53ef\u4ee5\u5145\u5206\u5229\u7528GPU\u7684\u8ba1\u7b97\u80fd\u529b\uff0c\u5927\u5e45\u63d0\u5347\u8ba1\u7b97\u6548\u7387\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u7528\u6237\u7684\u9700\u6c42\uff0c\u6bd4\u5982\u6df1\u5ea6\u5b66\u4e60\u4efb\u52a1\u63a8\u8350\u4f7f\u7528TensorFlow\u6216PyTorch\uff0c\u800c\u79d1\u5b66\u8ba1\u7b97\u4efb\u52a1\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Numba\u6216CuPy\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5GPU\u662f\u5426\u53ef\u7528\uff1f<\/strong><br \/>\u8981\u68c0\u67e5GPU\u662f\u5426\u53ef\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bf8\u5982TensorFlow\u6216PyTorch\u7b49\u6df1\u5ea6\u5b66\u4e60\u5e93\u3002\u5bf9\u4e8eTensorFlow\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tf.config.list_physical_devices(&#39;GPU&#39;)<\/code>\u6765\u67e5\u770b\u53ef\u7528\u7684GPU\u8bbe\u5907\uff1b\u5bf9\u4e8ePyTorch\uff0c\u53ef\u4ee5\u4f7f\u7528<code>torch.cuda.is_available()<\/code>\u6765\u786e\u8ba4CUDA\u662f\u5426\u53ef\u7528\u3002\u786e\u4fdd\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u4e86\u5bf9\u5e94\u7684CUDA\u548ccuDNN\u7248\u672c\uff0c\u4ee5\u4fbfPython\u80fd\u591f\u8bc6\u522b\u548c\u4f7f\u7528GPU\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5b89\u88c5\u652f\u6301GPU\u7684\u5e93\uff1f<\/strong><br \/>\u5b89\u88c5\u652f\u6301GPU\u7684\u5e93\u901a\u5e38\u6d89\u53ca\u5230\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u3002\u4ee5TensorFlow\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pip install tensorflow<\/code>\u6765\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684TensorFlow\uff0c\u5b83\u4f1a\u81ea\u52a8\u68c0\u6d4b\u5e76\u4f7f\u7528\u53ef\u7528\u7684GPU\u3002\u5bf9\u4e8ePyTorch\uff0c\u53ef\u4ee5\u8bbf\u95ee\u5176\u5b98\u65b9\u7f51\u7ad9\uff0c\u9009\u62e9\u4e0eCUDA\u7248\u672c\u76f8\u5bf9\u5e94\u7684\u5b89\u88c5\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002\u786e\u4fdd\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9075\u5faa\u5b98\u65b9\u6587\u6863\u4e2d\u7684\u6307\u5bfc\uff0c\u4ee5\u907f\u514d\u51fa\u73b0\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4ee3\u7801\u4e2d\u6307\u5b9a\u4f7f\u7528GPU\u8fdb\u884c\u8ba1\u7b97\uff1f<\/strong><br \/>\u5728TensorFlow\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u6a21\u578b\u6216\u5f20\u91cf\u53d1\u9001\u5230GPU\u6765\u4f7f\u7528GPU\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>with tf.device(&#39;\/GPU:0&#39;)<\/code>\u6765\u6307\u5b9a\u5728\u7b2c\u4e00\u4e2aGPU\u4e0a\u8fd0\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002\u5728PyTorch\u4e2d\uff0c\u4f7f\u7528<code>.to(&#39;cuda&#39;)<\/code>\u6216<code>.cuda()<\/code>\u65b9\u6cd5\u5c06\u6a21\u578b\u6216\u6570\u636e\u8f6c\u79fb\u5230GPU\u3002\u4f8b\u5982\uff0c<code>model.to(&#39;cuda&#39;)<\/code>\u5c06\u6a21\u578b\u79fb\u52a8\u5230GPU\u8bbe\u5907\u4e0a\u3002\u786e\u4fdd\u5728\u6267\u884c\u8ba1\u7b97\u65f6\uff0c\u6240\u6709\u76f8\u5173\u6570\u636e\u548c\u6a21\u578b\u90fd\u5df2\u8f6c\u79fb\u5230GPU\u4e0a\uff0c\u4ee5\u5145\u5206\u5229\u7528\u5176\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528GPU\u4e3b\u8981\u4f9d\u8d56\u4e8e\u4e00\u4e9b\u4e13\u95e8\u7684\u5e93\u548c\u6846\u67b6\uff0c\u8fd9\u4e9b\u5e93\u548c\u6846\u67b6\u80fd\u591f\u5c06\u8ba1\u7b97\u4efb\u52a1\u4eceCPU\u5378\u8f7d\u5230GPU\uff0c\u4ee5\u52a0\u901f [&hellip;]","protected":false},"author":3,"featured_media":944832,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/944830"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=944830"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/944830\/revisions"}],"predecessor-version":[{"id":944834,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/944830\/revisions\/944834"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/944832"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=944830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=944830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=944830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}