{"id":320,"date":"2026-07-04T23:07:08","date_gmt":"2026-07-04T15:07:08","guid":{"rendered":"https:\/\/www.lixiaodong.com\/?p=320"},"modified":"2026-07-04T23:07:08","modified_gmt":"2026-07-04T15:07:08","slug":"%e5%9c%a8gpu%e4%ba%91%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8a%e8%bf%9b%e8%a1%8c%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%e6%a8%a1%e5%9e%8b%e8%ae%ad%e7%bb%83","status":"publish","type":"post","link":"https:\/\/www.lixiaodong.com\/?p=320","title":{"rendered":"\u5728GPU\u4e91\u670d\u52a1\u5668\u4e0a\u8fdb\u884c\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u8bad\u7ec3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u672c\u5730\u7535\u8111\u7684\u914d\u7f6e\u8fbe\u4e0d\u5230\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u8bad\u7ec3\u7684\u8981\u6c42\uff0cGPU\u4e91\u670d\u52a1\u5668\u6309\u4f7f\u7528\u65f6\u95f4\u8ba1\u8d39\uff0c\u914d\u7f6e\u7075\u6d3b\uff0c\u4e3a\u6211\u4eec\u8bad\u7ec3\u590d\u6742\u6a21\u578b\u63d0\u4f9b\u4e86\u4e00\u4e2a\u89e3\u51b3\u529e\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4ee5\u300aDive into Deep Learning\u300b\uff08\u52a8\u624b\u5b66\u6df1\u5ea6\u5b66\u4e60\uff09\u7b2c2\u7248 \u7b2c7.1\u5c0f\u8282 \u300a\u6df1\u5ea6\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08AlexNet\uff09\u300b \u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5982\u4f55\u5728GPU\u4e91\u670d\u52a1\u5668\u4e0a\u8bad\u7ec3\u6a21\u578b\uff0c\u5e76\u53c2\u8003\u7b2c5.5\u5c0f\u8282\u300a\u8bfb\u5199\u6587\u4ef6\u300b\uff0c\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u53c2\u6570\u4fdd\u5b58\u5230\u6587\u4ef6\uff0c\u4f9b\u4ee5\u540e\u4f7f\u7528\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u51c6\u5907GPU\u4e91\u670d\u52a1\u5668<br>\u9009\u62e9\u4e91\u670d\u52a1\u5668\u786c\u4ef6\u914d\u7f6e\uff0c\u6839\u636e\u5177\u4f53\u4efb\u52a1\u9009\u62e9CPU\uff0c\u5185\u5b58\u5927\u5c0f\uff0c\u786c\u76d8\u5bb9\u91cf\uff0c\u663e\u5361\u79cd\u7c7b\u548c\u663e\u5b58\u5927\u5c0f<br>\u64cd\u4f5c\u7cfb\u7edf\u9009\u62e9ubuntu 24.04\uff08\u4e00\u822c\u63d0\u4f9b\u7684\u90fd\u662flinux\uff0c\u56e0\u4e3a\u514d\u8d39\u3002\u90091\u4e2a\u81ea\u5df1\u719f\u6089\u7684\u53d1\u884c\u7248\uff09<br>\u8fd0\u884cnvidia-smi\uff0c\u786e\u8ba4\u5df2\u7ecf\u5b89\u88c5\u4e86cuda\u3002<\/li>\n\n\n\n<li>\u5b89\u88c5\u73af\u5883\u548c\u4f9d\u8d56<br>\u4e0b\u8f7d\u5e76\u5b89\u88c5miniconda<br>wget <a href=\"https:\/\/repo.anaconda.com\/miniconda\/Miniconda3-latest-Linux-x86_64.sh\">https:\/\/repo.anaconda.com\/miniconda\/Miniconda3-latest-Linux-x86_64.sh<\/a><br>bash Miniconda3-latest-Linux-x86_64.sh<br><br>conda\u547d\u4ee4\u5df2\u7ecf\u52a0\u5165PATH\uff0c\u4e3a\u4e86\u4f7f\u5176\u751f\u6548\uff0c\u6267\u884c\uff1a<br>source ~\/.bashrc<br><br>\u540c\u610ftos<br>conda tos accept &#8211;override-channels &#8211;channel <a href=\"https:\/\/repo.anaconda.com\/pkgs\/main\">https:\/\/repo.anaconda.com\/pkgs\/main<\/a><br>conda tos accept &#8211;override-channels &#8211;channel <a href=\"https:\/\/repo.anaconda.com\/pkgs\/r\">https:\/\/repo.anaconda.com\/pkgs\/r<\/a><br><br><br>\u521b\u5efa\u73af\u5883\u5e76\u6fc0\u6d3b<br>conda create \u2013n py39 python=3.9 -y<br>\u6ce8\u610f\uff1a\u6709\u65f6\u5019\u4f1a\u62a5\u9519\u3002\u590d\u5236\u7c98\u8d34\u4ee5\u4e0a\u547d\u4ee4\u540e\uff0c-\u548c=\uff0c\u8fd9\u4e9b\u7b26\u53f7\u9700\u8981\u5728\u547d\u4ee4\u884c\u5220\u9664\u540e\u91cd\u65b0\u6253\u5b57\uff01<br><br>conda activate py39<br><br>\u5b89\u88c5\u4f9d\u8d56<br>conda install pytorch==1.12.0 torchvision==0.13.0 cudatoolkit=11.3 -c pytorch<br>pip install d2l==0.17.6<br><br><br>conda install -c conda-forge mkl=2024.0.0 -y<br>\u5426\u5219\u4f1a\u62a5\u9519\uff1a<br>ImportError: \/root\/miniconda3\/envs\/py39\/lib\/python3.9\/site-packages\/torch\/lib\/libtorch_cpu.so: undefined symbol: iJIT_NotifyEvent<br><br><br>pip install numpy==1.26.4<br>pip uninstall numpy==1.26.4<br>pip install numpy==1.21.5<br><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 3. \u65b0\u5efapython\u6587\u4ef6\u5e76\u6267\u884c<br>\u65b0\u5efa\u4e00\u4e2a\u76ee\u5f55\u5e76\u8fdb\u5165<br>mkdir pytorch_train &amp;&amp; cd $_<br>vim alexnet.py\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 7.1 AlexNet<br><br>import matplotlib<br># Patch the missing private method to match the standard public .get method<br>if not hasattr(matplotlib.rcParams, '_get'):<br>    matplotlib.rcParams._get = matplotlib.rcParams.get<br><br>import torch<br>from torch import nn<br>from d2l import torch as d2l<br>import matplotlib.pyplot as plt<br><br>net = nn.Sequential(<br>    # \u8fd9\u91cc\u4f7f\u7528\u4e00\u4e2a11*11\u7684\u66f4\u5927\u7a97\u53e3\u6765\u6355\u6349\u5bf9\u8c61\u3002<br>    # \u540c\u65f6\uff0c\u6b65\u5e45\u4e3a4\uff0c\u4ee5\u51cf\u5c11\u8f93\u51fa\u7684\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u3002<br>    # \u53e6\u5916\uff0c\u8f93\u51fa\u901a\u9053\u7684\u6570\u76ee\u8fdc\u5927\u4e8eLeNet<br>    nn.Conv2d(1, 96, kernel_size=11, stride=4, padding=1), nn.ReLU(),<br>    nn.MaxPool2d(kernel_size=3, stride=2),<br>    # \u51cf\u5c0f\u5377\u79ef\u7a97\u53e3\uff0c\u4f7f\u7528\u586b\u5145\u4e3a2\u6765\u4f7f\u5f97\u8f93\u5165\u4e0e\u8f93\u51fa\u7684\u9ad8\u548c\u5bbd\u4e00\u81f4\uff0c\u4e14\u589e\u5927\u8f93\u51fa\u901a\u9053\u6570<br>    nn.Conv2d(96, 256, kernel_size=5, padding=2), nn.ReLU(),<br>    nn.MaxPool2d(kernel_size=3, stride=2),<br>    # \u4f7f\u7528\u4e09\u4e2a\u8fde\u7eed\u7684\u5377\u79ef\u5c42\u548c\u8f83\u5c0f\u7684\u5377\u79ef\u7a97\u53e3\u3002<br>    # \u9664\u4e86\u6700\u540e\u7684\u5377\u79ef\u5c42\uff0c\u8f93\u51fa\u901a\u9053\u7684\u6570\u91cf\u8fdb\u4e00\u6b65\u589e\u52a0\u3002<br>    # \u5728\u524d\u4e24\u4e2a\u5377\u79ef\u5c42\u4e4b\u540e\uff0c\u6c47\u805a\u5c42\u4e0d\u7528\u4e8e\u51cf\u5c11\u8f93\u5165\u7684\u9ad8\u5ea6\u548c\u5bbd\u5ea6<br>    nn.Conv2d(256, 384, kernel_size=3, padding=1), nn.ReLU(),<br>    nn.Conv2d(384, 384, kernel_size=3, padding=1), nn.ReLU(),<br>    nn.Conv2d(384, 256, kernel_size=3, padding=1), nn.ReLU(),<br>    nn.MaxPool2d(kernel_size=3, stride=2),<br>    nn.Flatten(),<br>    # \u8fd9\u91cc\uff0c\u5168\u8fde\u63a5\u5c42\u7684\u8f93\u51fa\u6570\u91cf\u662fLeNet\u4e2d\u7684\u597d\u51e0\u500d\u3002\u4f7f\u7528dropout\u5c42\u6765\u51cf\u8f7b\u8fc7\u62df\u5408<br>    nn.Linear(6400, 4096), nn.ReLU(),<br>    nn.Dropout(p=0.5),<br>    nn.Linear(4096, 4096), nn.ReLU(),<br>    nn.Dropout(p=0.5),<br>    # \u6700\u540e\u662f\u8f93\u51fa\u5c42\u3002\u7531\u4e8e\u8fd9\u91cc\u4f7f\u7528Fashion-MNIST\uff0c\u6240\u4ee5\u7528\u7c7b\u522b\u6570\u4e3a10\uff0c\u800c\u975e\u8bba\u6587\u4e2d\u76841000<br>    nn.Linear(4096, 10))<br><br><br>X = torch.randn(1, 1, 224, 224)<br>for layer in net:<br>    X=layer(X)<br>    print(layer.__class__.__name__,'output shape:\\t',X.shape)<br><br><br>batch_size = 128<br>train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)<br><br>lr, num_epochs = 0.01, 10<br>d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())<br>plt.show()<br>plt.savefig(\"alexnet_train.png\")<br># save model params<br>torch.save(net.state_dict(), \"alexnet.params\")<br><br><br><br><br><br>\u6267\u884c\u7a0b\u5e8f\uff0c\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u90fd\u5b9a\u5411\u5230\u6587\u4ef6\u5e76\u5728\u540e\u53f0\u6267\u884c\uff08\u9632\u6b62ssh\u8fde\u63a5\u610f\u5916\u65ad\u5f00\u65f6\u7a0b\u5e8f\u672a\u6267\u884c\u5b8c\u5c31\u9000\u51fa\uff09<br>nohup python alexnet.py > output.log\u00a0 2>&amp;1\u00a0 &amp;<br>\u67e5\u770b\u6267\u884c\u60c5\u51b5\uff1a<br>tail -f output.log<br><br>\u6267\u884c\u5b8c\u6bd5\uff0c\u53ef\u4ee5\u770b\u5230\u751f\u6210\u7684\u635f\u5931\u548c\u6b63\u786e\u7387\u7684\u7ed8\u56fe\u6587\u4ef6alexnet_train.png\u548c\u6a21\u578b\u53c2\u6570\u6587\u4ef6alexnet.params\u3002<br><br><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u5730\u7535\u8111\u7684\u914d\u7f6e\u8fbe\u4e0d\u5230\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u8bad\u7ec3\u7684\u8981\u6c42\uff0cGPU\u4e91\u670d\u52a1\u5668\u6309\u4f7f\u7528\u65f6\u95f4\u8ba1\u8d39\uff0c\u914d\u7f6e\u7075 &hellip; <a href=\"https:\/\/www.lixiaodong.com\/?p=320\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[],"class_list":["post-320","post","type-post","status-publish","format-standard","hentry","category-57"],"_links":{"self":[{"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/posts\/320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=320"}],"version-history":[{"count":2,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/posts\/320\/revisions"}],"predecessor-version":[{"id":322,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/posts\/320\/revisions\/322"}],"wp:attachment":[{"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}