FreeChat
Open main menu
ChatGPT知识
搜索
搜索
关闭
Previous
Previous
Next
Next
GPU Linux虚拟主机GN7型安装配置文档 定稿
sockstack
/
147
/
2023-11-15 00:48:55
<p><span style="color: red; font-size: 18px">ChatGPT 可用网址,仅供交流学习使用,如对您有所帮助,请收藏并推荐给需要的朋友。</span><br><a href="https://ckai.xyz/?sockstack§ion=detail" target="__blank">https://ckai.xyz</a><br><br></p> <article class="_2rhmJa"><p> 我现在配的虚拟主机缺了颗GPU,一些使用GPU的算法无法在线演示,有点美中不足。网上搜了一圈,腾讯云现在有个推广活动,花点小钱就可以配一个实验了,比较便宜,其它一些厂的个人担负不起,所以在腾讯云上买了一个实例,试用一个月,以完成配置测试的实验。</p> <p> 完全没有用过Ubuntu,大概需要重装很多次才能搞定。进度很难估计,先用一个月看看。所以要写下每一步的详细文档,以便随时重装。我将安装tensorflow2.6、pytorch1.11.0与HanLP2.1,它们的版本不会冲突。对应的是CUDA11.2与cuDNN8.5,cuDNN8.5适配CUDA11.X(最后改回cuDNN8.1了),Python 3.9。然后会在Rstudio中通过reticulate包、tensorflow包与keras包调用它们。如果有时间,也会测试一下R语言实现的<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fcran.r-project.org%2Fweb%2Fpackages%2Ftorch%2Findex.html" target="_blank">torch包</a>,它提供了类似PyTorch的功能,直接调用<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fpytorch%2Fpytorch%2Fblob%2Fmaster%2Fdocs%2Flibtorch.rst" target="_blank">libtorch</a>。</p> <p> 腾讯云GPU计算型虚拟主机 GN7,搭载 NVIDIA T4 GPU,8核CPU+32G RAM+100G SSD+1颗T4,带宽5M,¥80/试用一个月,试用计划<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fcloud.tencent.com%2Fact%2Fpro%2Fgpu-study%3FfromSource%3Dgwzcw.6866631.6866631.6866631%26utm_medium%3Dcpc%26utm_id%3Dgwzcw.6866631.6866631.6866631%26bd_vid%3D11503804075512292365" target="_blank">GPU实验室</a>,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fcloud.tencent.com%2Fdeveloper%2Farticle%2F2073793" target="_blank">入门教程</a>。</p> <p>一、从镜像安装操作系统。</p> <p> 不同的GPU驱动版本,可选的CUDA版本不同,要选460.106.00版。</p> <p>公共镜像:Ubuntu Server 18.04.1 LTS64位</p> <p>后台自动安装GPU驱动</p> <p>GPU 驱动版本:460.106.00</p> <p>CUDA版本: 11.2.2</p> <p>cuDNN版本: 8.2.1</p> <p>用户名: ubuntu</p> <p>网址 :172.16.XX.XX(内)106.52.XX.XX(公)</p> <p> 安装完成,用SecureCRT或PuTTY连接,它的SSH服务器启用了更新的密钥交换算法,SecureCRT要升级到9.0版以上。</p> <p>1、登录机器后,先启用root账户,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fblog.csdn.net%2Fweixin_42677788%2Farticle%2Fdetails%2F120488072" target="_blank">参阅资料</a>。设置root账户密码:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash">$sudo passwd root <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>账户切换:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash">$su root #su ubuntu <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre> </div> <p>如果要允许root在SSH登录,参阅<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fwp.gxnas.com%2F10746.html" target="_blank">资料1</a>与<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fblog.csdn.net%2Faiwowuzui%2Farticle%2Fdetails%2F126930388" target="_blank">资料2</a>。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># vi /etc/ssh/sshd_config <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>找到这一段:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># Authentication: #LoginGraceTime 2m #PermitRootLogin prohibit-password #StrictModes yes #MaxAuthTries 6 #MaxSessions 10 <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>改成这样:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># Authentication: #LoginGraceTime 2m #PermitRootLogin prohibit-password PermitRootLogin yes StrictModes yes #MaxAuthTries 6 #MaxSessions 10 <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>重启SSH服务器:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># systemctl restart sshd.service <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>为了方便后面安装软件,关闭sudo命令的PATH限制,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fsuperuser.com%2Fquestions%2F927512%2Fhow-to-set-path-for-sudo-commands" target="_blank">参阅资料</a>,用wq!存盘:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># vi /etc/sudoers <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-objectivec"><code class=" language-objectivec">Defaults env_reset Defaults mail_badpass <span class="token macro property"># Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre> </div> <p>然后用 -E选项运行sudo命令可以继承当前用户的环境变量设置,这样安装软件也可以不用登录到root用户,比如后面用conda命令安装Python软件包:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> ubuntu<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span>$ sudo <span class="token operator">-</span><span class="token constant">E</span> conda list hanlp <span class="token comment"># packages in environment at /usr/local/anaconda3/envs/gpu:</span> <span class="token comment">#</span> <span class="token comment"># Name Version Build Channel</span> hanlp <span class="token number">2.1</span><span class="token number">.0</span>b42 pypi_0 pypi hanlp<span class="token operator">-</span>common <span class="token number">0.0</span><span class="token number">.18</span> pypi_0 pypi hanlp<span class="token operator">-</span>downloader <span class="token number">0.0</span><span class="token number">.25</span> pypi_0 pypi hanlp<span class="token operator">-</span>trie <span class="token number">0.0</span><span class="token number">.5</span> pypi_0 pypi <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>2、大约需要10~15分钟进行安装,可以用以下命令查看当前安装进程:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-cpp"><code class=" language-cpp">root@VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span><span class="token operator">~</span># ps aux <span class="token operator">|</span> grep <span class="token operator">-</span>i install root <span class="token number">8158</span> <span class="token number">0.0</span> <span class="token number">0.0</span> <span class="token number">13776</span> <span class="token number">1156</span> pts<span class="token operator">/</span><span class="token number">0</span> S<span class="token operator">+</span> <span class="token number">08</span><span class="token operator">:</span><span class="token number">50</span> <span class="token number">0</span><span class="token operator">:</span><span class="token number">00</span> grep <span class="token operator">--</span>color<span class="token operator">=</span><span class="token keyword">auto</span> <span class="token operator">-</span>i install <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre> </div> <p>如上面所示,里面没有nv_driver_install.sh及nv_cuda_install.sh,则表示驱动安装已经完成。</p> <p>3、验证GPU驱动安装成功。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby">root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># nvidia-smi</span> <span class="token constant">Sat</span> <span class="token constant">Oct</span> <span class="token number">29</span> <span class="token number">08</span><span class="token punctuation">:</span><span class="token number">52</span><span class="token punctuation">:</span><span class="token number">11</span> <span class="token number">2022</span> <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span> <span class="token operator">|</span> <span class="token constant">NVIDIA</span><span class="token operator">-</span><span class="token constant">SMI</span> <span class="token number">460.106</span><span class="token number">.00</span> <span class="token constant">Driver</span> <span class="token constant">Version</span><span class="token punctuation">:</span> <span class="token number">460.106</span><span class="token number">.00</span> <span class="token constant">CUDA</span> <span class="token constant">Version</span><span class="token punctuation">:</span> <span class="token number">11.2</span> <span class="token operator">|</span> <span class="token operator">|</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span> <span class="token operator">|</span> <span class="token constant">GPU</span> <span class="token constant">Name</span> <span class="token constant">Persistence</span><span class="token operator">-</span><span class="token constant">M</span><span class="token operator">|</span> <span class="token constant">Bus</span><span class="token operator">-</span><span class="token constant">Id</span> <span class="token constant">Disp</span><span class="token punctuation">.</span><span class="token constant">A</span> <span class="token operator">|</span> <span class="token constant">Volatile</span> <span class="token constant">Uncorr</span><span class="token punctuation">.</span> <span class="token constant">ECC</span> <span class="token operator">|</span> <span class="token operator">|</span> <span class="token constant">Fan</span> <span class="token constant">Temp</span> <span class="token constant">Perf</span> <span class="token constant">Pwr</span><span class="token symbol">:Usage</span><span class="token operator">/</span><span class="token constant">Cap</span><span class="token operator">|</span> <span class="token constant">Memory</span><span class="token operator">-</span><span class="token constant">Usage</span> <span class="token operator">|</span> <span class="token constant">GPU</span><span class="token operator">-</span><span class="token constant">Util</span> <span class="token constant">Compute</span> <span class="token constant">M</span><span class="token punctuation">.</span> <span class="token operator">|</span> <span class="token operator">|</span> <span class="token operator">|</span> <span class="token operator">|</span> <span class="token constant">MIG</span> <span class="token constant">M</span><span class="token punctuation">.</span> <span class="token operator">|</span> <span class="token operator">|</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">=</span><span class="token operator">+</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">=</span><span class="token operator">+</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">=</span><span class="token operator">|</span> <span class="token operator">|</span> <span class="token number">0</span> <span class="token constant">Tesla</span> <span class="token constant">T4</span> <span class="token constant">On</span> <span class="token operator">|</span> <span class="token number">00000000</span><span class="token punctuation">:</span><span class="token number">00</span><span class="token punctuation">:</span><span class="token number">08.0</span> <span class="token constant">Off</span> <span class="token operator">|</span> <span class="token number">0</span> <span class="token operator">|</span> <span class="token operator">|</span> <span class="token constant">N</span><span class="token operator">/</span><span class="token constant">A</span> <span class="token number">28</span>C <span class="token constant">P8</span> <span class="token number">8</span>W <span class="token operator">/</span> <span class="token number">70</span>W <span class="token operator">|</span> <span class="token number">0</span>MiB <span class="token operator">/</span> <span class="token number">15109</span>MiB <span class="token operator">|</span> <span class="token number">0</span><span class="token operator">%</span> <span class="token constant">Default</span> <span class="token operator">|</span> <span class="token operator">|</span> <span class="token operator">|</span> <span class="token operator">|</span> <span class="token constant">N</span><span class="token operator">/</span><span class="token constant">A</span> <span class="token operator">|</span> <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span> <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span> <span class="token operator">|</span> <span class="token constant">Processes</span><span class="token punctuation">:</span> <span class="token operator">|</span> <span class="token operator">|</span> <span class="token constant">GPU</span> <span class="token constant">GI</span> <span class="token constant">CI</span> <span class="token constant">PID</span> <span class="token constant">Type</span> <span class="token constant">Process</span> name <span class="token constant">GPU</span> <span class="token constant">Memory</span> <span class="token operator">|</span> <span class="token operator">|</span> <span class="token constant">ID</span> <span class="token constant">ID</span> <span class="token constant">Usage</span> <span class="token operator">|</span> <span class="token operator">|</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">===</span><span class="token operator">==</span><span class="token operator">|</span> <span class="token operator">|</span> <span class="token constant">No</span> running processes found <span class="token operator">|</span> <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>4、验证CUDA 安装成功。上面入门教程写的不适用于这个配置组合,/usr/local/cuda是到/usr/local/cuda-11.2的链接。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby">root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># cat /usr/local/cuda/version.txt</span> cat<span class="token punctuation">:</span> <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>cuda<span class="token operator">/</span>version<span class="token punctuation">.</span>txt<span class="token punctuation">:</span> <span class="token constant">No</span> such file <span class="token keyword">or</span> directory root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># find / -name cuda</span> <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>cuda<span class="token operator">-</span><span class="token number">11.2</span><span class="token operator">/</span>targets<span class="token operator">/</span>x86_64<span class="token operator">-</span>linux<span class="token operator">/</span>include<span class="token operator">/</span>cuda <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>cuda<span class="token operator">-</span><span class="token number">11.2</span><span class="token operator">/</span>targets<span class="token operator">/</span>x86_64<span class="token operator">-</span>linux<span class="token operator">/</span>include<span class="token operator">/</span>thrust<span class="token operator">/</span>system<span class="token operator">/</span>cuda <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>cuda root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># cd /usr/local/cuda</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>cuda<span class="token comment"># ls</span> bin <span class="token constant">DOCS</span> extras lib64 nsight<span class="token operator">-</span>compute<span class="token operator">-</span><span class="token number">2020.3</span><span class="token number">.1</span> nsight<span class="token operator">-</span>systems<span class="token operator">-</span><span class="token number">2020.4</span><span class="token number">.3</span> nvvm <span class="token constant">README</span> share targets version<span class="token punctuation">.</span>json compute<span class="token operator">-</span>sanitizer <span class="token constant">EULA</span><span class="token punctuation">.</span>txt include libnvvp nsightee_plugins nvml nvvm<span class="token operator">-</span>prev samples src tools root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>cuda<span class="token comment"># cd bin</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>cuda<span class="token operator">/</span>bin<span class="token comment"># ./nvcc -V</span> nvcc<span class="token punctuation">:</span> <span class="token constant">NVIDIA</span> <span class="token punctuation">(</span><span class="token constant">R</span><span class="token punctuation">)</span> <span class="token constant">Cuda</span> compiler driver <span class="token constant">Copyright</span> <span class="token punctuation">(</span>c<span class="token punctuation">)</span> <span class="token number">2005</span><span class="token operator">-</span><span class="token number">2021</span> <span class="token constant">NVIDIA</span> <span class="token constant">Corporation</span> <span class="token constant">Built</span> on <span class="token constant">Sun_Feb_14_21</span><span class="token punctuation">:</span><span class="token number">12</span><span class="token punctuation">:</span><span class="token number">58</span>_PST_2021 <span class="token constant">Cuda</span> compilation tools<span class="token punctuation">,</span> release <span class="token number">11.2</span><span class="token punctuation">,</span> <span class="token constant">V11</span><span class="token number">.2</span><span class="token number">.152</span> <span class="token constant">Build</span> cuda_11<span class="token punctuation">.</span><span class="token number">2.</span>r11<span class="token punctuation">.</span><span class="token number">2</span><span class="token operator">/</span>compiler<span class="token punctuation">.</span><span class="token number">29618528</span>_0 <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>5、验证cuDNN安装,上面入门教程写的同样不适用,系统从镜像安装cuDNN没有成功。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby">root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>cuda<span class="token operator">/</span>bin<span class="token comment"># cat /usr/include/cudnn_version.h | grep CUDNN_MAJOR -A 2</span> cat<span class="token punctuation">:</span> <span class="token operator">/</span>usr<span class="token operator">/</span>include<span class="token operator">/</span>cudnn_version<span class="token punctuation">.</span>h<span class="token punctuation">:</span> <span class="token constant">No</span> such file <span class="token keyword">or</span> directory <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre> </div> <p>二、手工安装cuDNN,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fblog.csdn.net%2Ftangjiahao10%2Farticle%2Fdetails%2F125227005" target="_blank">参阅资料</a>。</p> <p>cuDNN下载要登录Nvidia的网站,所以用下面的命令是不行的:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby">wget https<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>developer<span class="token punctuation">.</span>nvidia<span class="token punctuation">.</span>com<span class="token operator">/</span>compute<span class="token operator">/</span>cudnn<span class="token operator">/</span>secure<span class="token operator">/</span><span class="token number">8.5</span><span class="token number">.0</span><span class="token operator">/</span>local_installers<span class="token operator">/</span><span class="token number">11.7</span><span class="token operator">/</span>cudnn<span class="token operator">-</span>linux<span class="token operator">-</span>x86_64<span class="token operator">-</span><span class="token number">8.5</span><span class="token number">.0</span><span class="token number">.96</span>_cuda11<span class="token operator">-</span>archive<span class="token punctuation">.</span>tar<span class="token punctuation">.</span>xz <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>1、在笔记本上下载好,再用SecureFX从SSH端口传到服务器上,解压安装。Linux上验证过的CUDA与cuDNN等的匹配关系<a href="https://links.jianshu.com/go?to=https%3A%2F%2Ftensorflow.google.cn%2Finstall%2Fsource%23linux" target="_blank">参阅该资料</a>。</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 341px;"> <div class="image-container-fill" style="padding-bottom: 31.369999999999997%;"></div> <div class="image-view" data-width="1087" data-height="341"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-d154a2fc6aae162b.PNG" data-original-width="1087" data-original-height="341" data-original-format="image/png" data-original-filesize="30538" data-image-index="0" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">tensorflow-cuda-cudnn-python版本对照表</div> </div> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-css"><code class=" language-css"># tar -xvf cudnn-linux-x86_64-8.5.0.96_cuda11-archive.tar.xz # cd cudnn-linux-x86_64-8.5.0.96_cuda11-archive # cp lib/* /usr/local/cuda/lib64/ # cp include/* /usr/local/cuda/include/ # chmod a+r /usr/local/cuda/lib64/* # chmod a+r /usr/local/cuda/include/* <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>2、将CUDA目录加入全局环境变量:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># vi /etc/profile <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash">export PATH=/usr/local/cuda-11.2/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda-11.2/lib64:$LD_LIBRARY_PATH export CUDA_HOME=/usr/local/cuda-11.2 <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre> </div> <p>3、source /etc/profile使它生效,或者logout再login,验证cuDNN安装:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash">root@VM-0-14-ubuntu:/usr/local/cuda/bin# source /etc/profile root@VM-0-14-ubuntu:/usr/local/cuda/bin# echo $PATH /usr/local/cuda-11.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin root@VM-0-14-ubuntu:/usr/local/cuda/bin# nvcc -V nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2021 NVIDIA Corporation Built on Sun_Feb_14_21:12:58_PST_2021 Cuda compilation tools, release 11.2, V11.2.152 Build cuda_11.2.r11.2/compiler.29618528_0 root@VM-0-14-ubuntu:/usr/local/cuda/bin# cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2 #define CUDNN_MAJOR 8 #define CUDNN_MINOR 5 #define CUDNN_PATCHLEVEL 0 -- #define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL) #endif /* CUDNN_VERSION_H */ <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>三、安装Anaconda</p> <p>1、下载安装Anaconda,装在/usr/local/anaconda3目录。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby">$ wget https<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>mirrors<span class="token punctuation">.</span>tuna<span class="token punctuation">.</span>tsinghua<span class="token punctuation">.</span>edu<span class="token punctuation">.</span>cn<span class="token operator">/</span>anaconda<span class="token operator">/</span>archive<span class="token operator">/</span><span class="token constant">Anaconda3</span><span class="token operator">-</span><span class="token number">2022.10</span><span class="token operator">-</span><span class="token constant">Linux</span><span class="token operator">-</span>x86_64<span class="token punctuation">.</span>sh $ sudo bash <span class="token constant">Anaconda3</span><span class="token operator">-</span><span class="token number">2022.10</span><span class="token operator">-</span><span class="token constant">Linux</span><span class="token operator">-</span>x86_64<span class="token punctuation">.</span>sh <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre> </div> <p>安装完成,选择运行 conda init:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash">done installation finished. Do you wish the installer to initialize Anaconda3 by running conda init? [yes|no] [no] >>> yes modified /usr/local/anaconda3/condabin/conda modified /usr/local/anaconda3/bin/conda modified /usr/local/anaconda3/bin/conda-env no change /usr/local/anaconda3/bin/activate no change /usr/local/anaconda3/bin/deactivate no change /usr/local/anaconda3/etc/profile.d/conda.sh no change /usr/local/anaconda3/etc/fish/conf.d/conda.fish no change /usr/local/anaconda3/shell/condabin/Conda.psm1 no change /usr/local/anaconda3/shell/condabin/conda-hook.ps1 no change /usr/local/anaconda3/lib/python3.9/site-packages/xontrib/conda.xsh no change /usr/local/anaconda3/etc/profile.d/conda.csh modified /root/.bashrc ==> For changes to take effect, close and re-open your current shell. <== If you'd prefer that conda's base environment not be activated on startup, set the auto_activate_base parameter to false: conda config --set auto_activate_base false Thank you for installing Anaconda3! =========================================================================== Working with Python and Jupyter is a breeze in DataSpell. It is an IDE designed for exploratory data analysis and ML. Get better data insights with DataSpell. DataSpell for Anaconda is available at: https://www.anaconda.com/dataspell <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>编辑全局变量脚本,把设置conda环境的脚本加到最后,以便所有用户都可用。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># vi /etc/profile <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># >>> conda initialize >>> # !! Contents within this block are managed by 'conda init' !! __conda_setup="$('/usr/local/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)" if [ $? -eq 0 ]; then eval "$__conda_setup" else if [ -f "/usr/local/anaconda3/etc/profile.d/conda.sh" ]; then . "/usr/local/anaconda3/etc/profile.d/conda.sh" else export PATH="/usr/local/anaconda3/bin:$PATH" fi fi unset __conda_setup # <<< conda initialize <<< <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>运行~/.bashrc使conda base环境生效,或者logout再login。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># source ~/.bashrc <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>2、root安装tensorflow-gpu 2.6。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># conda create --name gpu python=3.9 # pip install ipykernel # python -m ipykernel install --user --name gpu # conda activate gpu # pip install tensorflow-gpu==2.6 <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>3、ubuntu用户测试安装。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> ubuntu<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span>$ conda activate gpu <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> ubuntu<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span>$ python <span class="token constant">Python</span> <span class="token number">3.9</span><span class="token number">.13</span> <span class="token punctuation">(</span>main<span class="token punctuation">,</span> <span class="token constant">Oct</span> <span class="token number">13</span> <span class="token number">2022</span><span class="token punctuation">,</span> <span class="token number">21</span><span class="token punctuation">:</span><span class="token number">15</span><span class="token punctuation">:</span><span class="token number">33</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token constant">GCC</span> <span class="token number">11.2</span><span class="token number">.0</span><span class="token punctuation">]</span> <span class="token punctuation">:</span><span class="token punctuation">:</span> <span class="token constant">Anaconda</span><span class="token punctuation">,</span> <span class="token constant">Inc</span><span class="token punctuation">.</span> on linux <span class="token constant">Type</span> <span class="token string">"help"</span><span class="token punctuation">,</span> <span class="token string">"copyright"</span><span class="token punctuation">,</span> <span class="token string">"credits"</span> <span class="token keyword">or</span> <span class="token string">"license"</span> <span class="token keyword">for</span> more information<span class="token punctuation">.</span> <span class="token operator">></span><span class="token operator">></span><span class="token operator">></span> import tensorflow as tf <span class="token operator">></span><span class="token operator">></span><span class="token operator">></span> tf<span class="token punctuation">.</span>test<span class="token punctuation">.</span>is_built_with_cuda<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token constant">True</span> <span class="token operator">></span><span class="token operator">></span><span class="token operator">></span> a <span class="token operator">=</span> tf<span class="token punctuation">.</span>constant<span class="token punctuation">(</span><span class="token number">1.</span><span class="token punctuation">)</span> <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">29</span> <span class="token number">18</span><span class="token punctuation">:</span><span class="token number">14</span><span class="token punctuation">:</span><span class="token number">29.577429</span><span class="token punctuation">:</span> <span class="token constant">I</span> tensorflow<span class="token operator">/</span>stream_executor<span class="token operator">/</span>cuda<span class="token operator">/</span>cuda_gpu_executor<span class="token punctuation">.</span>cc<span class="token punctuation">:</span><span class="token number">937</span><span class="token punctuation">]</span> successful <span class="token constant">NUMA</span> node read from <span class="token constant">SysFS</span> had negative value <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> but there must be at least one <span class="token constant">NUMA</span> node<span class="token punctuation">,</span> so returning <span class="token constant">NUMA</span> node zero <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">29</span> <span class="token number">18</span><span class="token punctuation">:</span><span class="token number">14</span><span class="token punctuation">:</span><span class="token number">29.585025</span><span class="token punctuation">:</span> <span class="token constant">I</span> tensorflow<span class="token operator">/</span>stream_executor<span class="token operator">/</span>cuda<span class="token operator">/</span>cuda_gpu_executor<span class="token punctuation">.</span>cc<span class="token punctuation">:</span><span class="token number">937</span><span class="token punctuation">]</span> successful <span class="token constant">NUMA</span> node read from <span class="token constant">SysFS</span> had negative value <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> but there must be at least one <span class="token constant">NUMA</span> node<span class="token punctuation">,</span> so returning <span class="token constant">NUMA</span> node zero <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">29</span> <span class="token number">18</span><span class="token punctuation">:</span><span class="token number">14</span><span class="token punctuation">:</span><span class="token number">29.585898</span><span class="token punctuation">:</span> <span class="token constant">I</span> tensorflow<span class="token operator">/</span>stream_executor<span class="token operator">/</span>cuda<span class="token operator">/</span>cuda_gpu_executor<span class="token punctuation">.</span>cc<span class="token punctuation">:</span><span class="token number">937</span><span class="token punctuation">]</span> successful <span class="token constant">NUMA</span> node read from <span class="token constant">SysFS</span> had negative value <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> but there must be at least one <span class="token constant">NUMA</span> node<span class="token punctuation">,</span> so returning <span class="token constant">NUMA</span> node zero <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">29</span> <span class="token number">18</span><span class="token punctuation">:</span><span class="token number">14</span><span class="token punctuation">:</span><span class="token number">29.587034</span><span class="token punctuation">:</span> <span class="token constant">I</span> tensorflow<span class="token operator">/</span>core<span class="token operator">/</span>platform<span class="token operator">/</span>cpu_feature_guard<span class="token punctuation">.</span>cc<span class="token punctuation">:</span><span class="token number">142</span><span class="token punctuation">]</span> <span class="token constant">This</span> <span class="token constant">TensorFlow</span> binary is optimized with oneAPI <span class="token constant">Deep</span> <span class="token constant">Neural</span> <span class="token constant">Network</span> <span class="token constant">Library</span> <span class="token punctuation">(</span>oneDNN<span class="token punctuation">)</span> to use the following <span class="token constant">CPU</span> instructions <span class="token keyword">in</span> performance<span class="token operator">-</span>critical operations<span class="token punctuation">:</span> <span class="token constant">AVX2</span> <span class="token constant">AVX512F</span> <span class="token constant">FMA</span> <span class="token constant">To</span> enable them <span class="token keyword">in</span> other operations<span class="token punctuation">,</span> rebuild <span class="token constant">TensorFlow</span> with the appropriate compiler flags<span class="token punctuation">.</span> <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">29</span> <span class="token number">18</span><span class="token punctuation">:</span><span class="token number">14</span><span class="token punctuation">:</span><span class="token number">29.587744</span><span class="token punctuation">:</span> <span class="token constant">I</span> tensorflow<span class="token operator">/</span>stream_executor<span class="token operator">/</span>cuda<span class="token operator">/</span>cuda_gpu_executor<span class="token punctuation">.</span>cc<span class="token punctuation">:</span><span class="token number">937</span><span class="token punctuation">]</span> successful <span class="token constant">NUMA</span> node read from <span class="token constant">SysFS</span> had negative value <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> but there must be at least one <span class="token constant">NUMA</span> node<span class="token punctuation">,</span> so returning <span class="token constant">NUMA</span> node zero <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">29</span> <span class="token number">18</span><span class="token punctuation">:</span><span class="token number">14</span><span class="token punctuation">:</span><span class="token number">29.588624</span><span class="token punctuation">:</span> <span class="token constant">I</span> tensorflow<span class="token operator">/</span>stream_executor<span class="token operator">/</span>cuda<span class="token operator">/</span>cuda_gpu_executor<span class="token punctuation">.</span>cc<span class="token punctuation">:</span><span class="token number">937</span><span class="token punctuation">]</span> successful <span class="token constant">NUMA</span> node read from <span class="token constant">SysFS</span> had negative value <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> but there must be at least one <span class="token constant">NUMA</span> node<span class="token punctuation">,</span> so returning <span class="token constant">NUMA</span> node zero <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">29</span> <span class="token number">18</span><span class="token punctuation">:</span><span class="token number">14</span><span class="token punctuation">:</span><span class="token number">29.589442</span><span class="token punctuation">:</span> <span class="token constant">I</span> tensorflow<span class="token operator">/</span>stream_executor<span class="token operator">/</span>cuda<span class="token operator">/</span>cuda_gpu_executor<span class="token punctuation">.</span>cc<span class="token punctuation">:</span><span class="token number">937</span><span class="token punctuation">]</span> successful <span class="token constant">NUMA</span> node read from <span class="token constant">SysFS</span> had negative value <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> but there must be at least one <span class="token constant">NUMA</span> node<span class="token punctuation">,</span> so returning <span class="token constant">NUMA</span> node zero <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">29</span> <span class="token number">18</span><span class="token punctuation">:</span><span class="token number">14</span><span class="token punctuation">:</span><span class="token number">30.245462</span><span class="token punctuation">:</span> <span class="token constant">I</span> tensorflow<span class="token operator">/</span>stream_executor<span class="token operator">/</span>cuda<span class="token operator">/</span>cuda_gpu_executor<span class="token punctuation">.</span>cc<span class="token punctuation">:</span><span class="token number">937</span><span class="token punctuation">]</span> successful <span class="token constant">NUMA</span> node read from <span class="token constant">SysFS</span> had negative value <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> but there must be at least one <span class="token constant">NUMA</span> node<span class="token punctuation">,</span> so returning <span class="token constant">NUMA</span> node zero <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">29</span> <span class="token number">18</span><span class="token punctuation">:</span><span class="token number">14</span><span class="token punctuation">:</span><span class="token number">30.246301</span><span class="token punctuation">:</span> <span class="token constant">I</span> tensorflow<span class="token operator">/</span>stream_executor<span class="token operator">/</span>cuda<span class="token operator">/</span>cuda_gpu_executor<span class="token punctuation">.</span>cc<span class="token punctuation">:</span><span class="token number">937</span><span class="token punctuation">]</span> successful <span class="token constant">NUMA</span> node read from <span class="token constant">SysFS</span> had negative value <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> but there must be at least one <span class="token constant">NUMA</span> node<span class="token punctuation">,</span> so returning <span class="token constant">NUMA</span> node zero <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">29</span> <span class="token number">18</span><span class="token punctuation">:</span><span class="token number">14</span><span class="token punctuation">:</span><span class="token number">30.247122</span><span class="token punctuation">:</span> <span class="token constant">I</span> tensorflow<span class="token operator">/</span>stream_executor<span class="token operator">/</span>cuda<span class="token operator">/</span>cuda_gpu_executor<span class="token punctuation">.</span>cc<span class="token punctuation">:</span><span class="token number">937</span><span class="token punctuation">]</span> successful <span class="token constant">NUMA</span> node read from <span class="token constant">SysFS</span> had negative value <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> but there must be at least one <span class="token constant">NUMA</span> node<span class="token punctuation">,</span> so returning <span class="token constant">NUMA</span> node zero <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">29</span> <span class="token number">18</span><span class="token punctuation">:</span><span class="token number">14</span><span class="token punctuation">:</span><span class="token number">30.247901</span><span class="token punctuation">:</span> <span class="token constant">I</span> tensorflow<span class="token operator">/</span>core<span class="token operator">/</span>common_runtime<span class="token operator">/</span>gpu<span class="token operator">/</span>gpu_device<span class="token punctuation">.</span>cc<span class="token punctuation">:</span><span class="token number">1510</span><span class="token punctuation">]</span> <span class="token constant">Created</span> device <span class="token operator">/</span>job<span class="token symbol">:localhost</span><span class="token operator">/</span>replica<span class="token punctuation">:</span><span class="token number">0</span><span class="token operator">/</span>task<span class="token punctuation">:</span><span class="token number">0</span><span class="token operator">/</span>device<span class="token symbol">:GPU</span><span class="token punctuation">:</span><span class="token number">0</span> with <span class="token number">13803</span> <span class="token constant">MB</span> memory<span class="token punctuation">:</span> <span class="token operator">-</span><span class="token operator">></span> device<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> name<span class="token punctuation">:</span> <span class="token constant">Tesla</span> <span class="token constant">T4</span><span class="token punctuation">,</span> pci bus id<span class="token punctuation">:</span> <span class="token number">0000</span><span class="token punctuation">:</span><span class="token number">00</span><span class="token punctuation">:</span><span class="token number">08.0</span><span class="token punctuation">,</span> compute capability<span class="token punctuation">:</span> <span class="token number">7.5</span> <span class="token operator">></span><span class="token operator">></span><span class="token operator">></span> b <span class="token operator">=</span> tf<span class="token punctuation">.</span>constant<span class="token punctuation">(</span><span class="token number">2.</span><span class="token punctuation">)</span> <span class="token operator">></span><span class="token operator">></span><span class="token operator">></span> print<span class="token punctuation">(</span>a<span class="token operator">+</span>b<span class="token punctuation">)</span> tf<span class="token punctuation">.</span><span class="token constant">Tensor</span><span class="token punctuation">(</span><span class="token number">3.0</span><span class="token punctuation">,</span> shape<span class="token operator">=</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> dtype<span class="token operator">=</span>float32<span class="token punctuation">)</span> <span class="token operator">></span><span class="token operator">></span><span class="token operator">></span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>四、配置Jupyter Notebook</p> <p> Jupyter Notebook的安装配置要简单一点,先配起它来验证GPU环境的安装,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fblog.csdn.net%2Fm0_46459047%2Farticle%2Fdetails%2F121567309" target="_blank">参阅资料</a>。</p> <p>1、安装Anaconda3时base环境已经安装了Jupyter Notebook,但上面建立的虚拟环境"gpu"里面没有安装,要安装一下,先用conda activate激活环境再装。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash">(base) root@VM-0-14-ubuntu:~# conda activate gpu (gpu) root@VM-0-14-ubuntu:~# conda list jupyter # packages in environment at /usr/local/anaconda3/envs/gpu: # # Name Version Build Channel (gpu) root@VM-0-14-ubuntu:~# conda install jupyter notebook Collecting package metadata (current_repodata.json): done Solving environment: done ## Package Plan ## environment location: /usr/local/anaconda3/envs/gpu added / updated specs: - jupyter - notebook The following packages will be downloaded: package | build ---------------------------|----------------- asttokens-2.0.5 | pyhd3eb1b0_0 20 KB ...... Proceed ([y]/n)? y Downloading and Extracting Packages soupsieve-2.3.2.post | 65 KB | ################################################################################################################################################## | 100% ...... asttokens-2.0.5 | 20 KB | ################################################################################################################################################## | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done Retrieving notices: ...working... done <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>2、为用户ubuntu配置Jupyter Notebook。</p> <p>1)产生配置文件。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-tsx"><code class=" language-tsx"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> ubuntu@<span class="token constant">VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span>$ jupyter notebook <span class="token operator">--</span>generate<span class="token operator">-</span>config Writing <span class="token keyword">default</span> config to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>ubuntu<span class="token operator">/</span><span class="token punctuation">.</span>jupyter<span class="token operator">/</span>jupyter_notebook_config<span class="token punctuation">.</span>py <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre> </div> <p>2)产生登录口令的Hash。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> ubuntu<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span>$ python <span class="token constant">Python</span> <span class="token number">3.9</span><span class="token number">.13</span> <span class="token punctuation">(</span>main<span class="token punctuation">,</span> <span class="token constant">Aug</span> <span class="token number">25</span> <span class="token number">2022</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">:</span><span class="token number">26</span><span class="token punctuation">:</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token constant">GCC</span> <span class="token number">11.2</span><span class="token number">.0</span><span class="token punctuation">]</span> <span class="token punctuation">:</span><span class="token punctuation">:</span> <span class="token constant">Anaconda</span><span class="token punctuation">,</span> <span class="token constant">Inc</span><span class="token punctuation">.</span> on linux <span class="token constant">Type</span> <span class="token string">"help"</span><span class="token punctuation">,</span> <span class="token string">"copyright"</span><span class="token punctuation">,</span> <span class="token string">"credits"</span> <span class="token keyword">or</span> <span class="token string">"license"</span> <span class="token keyword">for</span> more information<span class="token punctuation">.</span> <span class="token operator">></span><span class="token operator">></span><span class="token operator">></span> from notebook<span class="token punctuation">.</span>auth import passwd <span class="token operator">></span><span class="token operator">></span><span class="token operator">></span> passwd<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token constant">Enter</span> password<span class="token punctuation">:</span> <span class="token constant">Verify</span> password<span class="token punctuation">:</span> <span class="token string">'argon2:$argon2id$v=19$m=10240,t=10,p=xxxxxxxxxxxxxxxxxxx'</span> <span class="token operator">></span><span class="token operator">></span><span class="token operator">></span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>3、编辑配置文件,拷贝上面登录口令的Hash到配置文件。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby">$ vi <span class="token operator">~</span><span class="token operator">/</span><span class="token punctuation">.</span>jupyter<span class="token operator">/</span>jupyter_notebook_config<span class="token punctuation">.</span>py <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-php"><code class=" language-php">c<span class="token punctuation">.</span>NotebookApp<span class="token punctuation">.</span>ip<span class="token operator">=</span><span class="token single-quoted-string string">'*'</span> <span class="token shell-comment comment"># 就是设置所有ip皆可访问 </span> c<span class="token punctuation">.</span>NotebookApp<span class="token punctuation">.</span>password <span class="token operator">=</span> <span class="token single-quoted-string string">'argon2:$argon2id$v=19$m=10240,t=10,p=xxxxxxxxxxxxxxxxxxx'</span> <span class="token shell-comment comment"># 上面复制的那个sha密文' </span> c<span class="token punctuation">.</span>NotebookApp<span class="token punctuation">.</span>open_browser <span class="token operator">=</span> <span class="token boolean constant">False</span> <span class="token shell-comment comment"># 禁止自动打开浏览器 </span> c<span class="token punctuation">.</span>NotebookApp<span class="token punctuation">.</span>port <span class="token operator">=</span><span class="token number">8888</span> <span class="token shell-comment comment"># 端口</span> c<span class="token punctuation">.</span>NotebookApp<span class="token punctuation">.</span>notebook_dir <span class="token operator">=</span> <span class="token single-quoted-string string">'/home/ubuntu/jupyternotebook'</span> <span class="token shell-comment comment">#设置Notebook启动进入的目录</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>4、启动Jupyter Notebook,注意要先激活使用"gpu"环境,用的是它。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> ubuntu<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span>$ conda activate gpu <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> ubuntu<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span>$ conda list jupyter <span class="token comment"># packages in environment at /usr/local/anaconda3/envs/gpu:</span> <span class="token comment">#</span> <span class="token comment"># Name Version Build Channel</span> jupyter <span class="token number">1.0</span><span class="token number">.0</span> py39h06a4308_8 jupyter_client <span class="token number">7.3</span><span class="token number">.5</span> py39h06a4308_0 jupyter_console <span class="token number">6.4</span><span class="token number">.3</span> pyhd3eb1b0_0 jupyter_core <span class="token number">4.11</span><span class="token number">.1</span> py39h06a4308_0 jupyter_server <span class="token number">1.18</span><span class="token number">.1</span> py39h06a4308_0 jupyterlab <span class="token number">3.4</span><span class="token number">.4</span> py39h06a4308_0 jupyterlab_pygments <span class="token number">0.1</span><span class="token number">.2</span> py_0 jupyterlab_server <span class="token number">2.15</span><span class="token number">.2</span> py39h06a4308_0 jupyterlab_widgets <span class="token number">1.0</span><span class="token number">.0</span> pyhd3eb1b0_1 <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> ubuntu<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span>$ jupyter notebook <span class="token operator">&</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token number">16510</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> ubuntu<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span>$ <span class="token punctuation">[</span><span class="token constant">W</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.094</span> <span class="token constant">NotebookApp</span><span class="token punctuation">]</span> <span class="token constant">WARNING</span><span class="token punctuation">:</span> <span class="token constant">The</span> notebook server is listening on all <span class="token constant">IP</span> addresses <span class="token keyword">and</span> <span class="token keyword">not</span> using encryption<span class="token punctuation">.</span> <span class="token constant">This</span> is <span class="token keyword">not</span> recommended<span class="token punctuation">.</span> <span class="token punctuation">[</span><span class="token constant">W</span> <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">30</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.326</span> <span class="token constant">LabApp</span><span class="token punctuation">]</span> <span class="token string">'ip'</span> has moved from <span class="token constant">NotebookApp</span> to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">This</span> config will be passed to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">Be</span> sure to update your config before our <span class="token keyword">next</span> release<span class="token punctuation">.</span> <span class="token punctuation">[</span><span class="token constant">W</span> <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">30</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.326</span> <span class="token constant">LabApp</span><span class="token punctuation">]</span> <span class="token string">'password'</span> has moved from <span class="token constant">NotebookApp</span> to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">This</span> config will be passed to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">Be</span> sure to update your config before our <span class="token keyword">next</span> release<span class="token punctuation">.</span> <span class="token punctuation">[</span><span class="token constant">W</span> <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">30</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.326</span> <span class="token constant">LabApp</span><span class="token punctuation">]</span> <span class="token string">'password'</span> has moved from <span class="token constant">NotebookApp</span> to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">This</span> config will be passed to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">Be</span> sure to update your config before our <span class="token keyword">next</span> release<span class="token punctuation">.</span> <span class="token punctuation">[</span><span class="token constant">W</span> <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">30</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.326</span> <span class="token constant">LabApp</span><span class="token punctuation">]</span> <span class="token string">'port'</span> has moved from <span class="token constant">NotebookApp</span> to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">This</span> config will be passed to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">Be</span> sure to update your config before our <span class="token keyword">next</span> release<span class="token punctuation">.</span> <span class="token punctuation">[</span><span class="token constant">W</span> <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">30</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.326</span> <span class="token constant">LabApp</span><span class="token punctuation">]</span> <span class="token string">'notebook_dir'</span> has moved from <span class="token constant">NotebookApp</span> to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">This</span> config will be passed to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">Be</span> sure to update your config before our <span class="token keyword">next</span> release<span class="token punctuation">.</span> <span class="token punctuation">[</span><span class="token constant">W</span> <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">30</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.326</span> <span class="token constant">LabApp</span><span class="token punctuation">]</span> <span class="token string">'notebook_dir'</span> has moved from <span class="token constant">NotebookApp</span> to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">This</span> config will be passed to <span class="token constant">ServerApp</span><span class="token punctuation">.</span> <span class="token constant">Be</span> sure to update your config before our <span class="token keyword">next</span> release<span class="token punctuation">.</span> <span class="token punctuation">[</span><span class="token constant">I</span> <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">30</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.333</span> <span class="token constant">LabApp</span><span class="token punctuation">]</span> <span class="token constant">JupyterLab</span> extension loaded from <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>anaconda3<span class="token operator">/</span>envs<span class="token operator">/</span>gpu<span class="token operator">/</span>lib<span class="token operator">/</span>python3<span class="token punctuation">.</span><span class="token number">9</span><span class="token operator">/</span>site<span class="token operator">-</span>packages<span class="token operator">/</span>jupyterlab <span class="token punctuation">[</span><span class="token constant">I</span> <span class="token number">2022</span><span class="token operator">-</span><span class="token number">10</span><span class="token operator">-</span><span class="token number">30</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.333</span> <span class="token constant">LabApp</span><span class="token punctuation">]</span> <span class="token constant">JupyterLab</span> application directory is <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>anaconda3<span class="token operator">/</span>envs<span class="token operator">/</span>gpu<span class="token operator">/</span>share<span class="token operator">/</span>jupyter<span class="token operator">/</span>lab <span class="token punctuation">[</span><span class="token constant">I</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.337</span> <span class="token constant">NotebookApp</span><span class="token punctuation">]</span> <span class="token constant">Serving</span> notebooks from local directory<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>ubuntu<span class="token operator">/</span>jupyternotebook <span class="token punctuation">[</span><span class="token constant">I</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.337</span> <span class="token constant">NotebookApp</span><span class="token punctuation">]</span> <span class="token constant">Jupyter</span> <span class="token constant">Notebook</span> <span class="token number">6.4</span><span class="token number">.12</span> is running at<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token constant">I</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.337</span> <span class="token constant">NotebookApp</span><span class="token punctuation">]</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token constant">VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token number">8888</span><span class="token operator">/</span> <span class="token punctuation">[</span><span class="token constant">I</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">21.337</span> <span class="token constant">NotebookApp</span><span class="token punctuation">]</span> <span class="token constant">Use</span> <span class="token constant">Control</span><span class="token operator">-</span><span class="token constant">C</span> to stop this server <span class="token keyword">and</span> shut down all kernels <span class="token punctuation">(</span>twice to skip confirmation<span class="token punctuation">)</span><span class="token punctuation">.</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>5、浏览器访问,输入上面设置的密码登录,然后新建一个测试的notebook测试GPU环境的安装。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-dart"><code class=" language-dart"><span class="token keyword">import</span> tensorflow <span class="token operator">as</span> tf tf<span class="token punctuation">.</span>test<span class="token punctuation">.</span><span class="token function">is_built_with_cuda</span><span class="token punctuation">(</span><span class="token punctuation">)</span> a <span class="token operator">=</span> tf<span class="token punctuation">.</span><span class="token function">constant</span><span class="token punctuation">(</span><span class="token number">1.</span><span class="token punctuation">)</span> b <span class="token operator">=</span> tf<span class="token punctuation">.</span><span class="token function">constant</span><span class="token punctuation">(</span><span class="token number">2.</span><span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span>a<span class="token operator">+</span>b<span class="token punctuation">)</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-7c228fb43658a541.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="162808" data-image-index="1" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">JupyterNotebook测试tensorflow-gpu安装</div> </div> <br> <p>6、新建一个测试的notebook测试keras与cuDNN。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-python"><code class=" language-python"><span class="token keyword">import</span> os <span class="token keyword">import</span> tensorflow <span class="token keyword">as</span> tf <span class="token keyword">from</span> tensorflow <span class="token keyword">import</span> keras <span class="token keyword">from</span> tensorflow<span class="token punctuation">.</span>keras <span class="token keyword">import</span> layers<span class="token punctuation">,</span>optimizers<span class="token punctuation">,</span> datasets <span class="token keyword">from</span> tensorflow<span class="token punctuation">.</span>keras<span class="token punctuation">.</span>models <span class="token keyword">import</span> load_model <span class="token keyword">from</span> matplotlib <span class="token keyword">import</span> pyplot <span class="token keyword">as</span> plt <span class="token keyword">import</span> numpy <span class="token keyword">as</span> np <span class="token comment"># 一、数据集处理</span> <span class="token comment"># 构建模型</span> <span class="token punctuation">(</span>x_train_raw<span class="token punctuation">,</span> y_train_raw<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span>x_test_raw<span class="token punctuation">,</span>y_test_raw<span class="token punctuation">)</span> <span class="token operator">=</span> datasets<span class="token punctuation">.</span>mnist<span class="token punctuation">.</span>load_data<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>y_train_raw<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 5</span> <span class="token keyword">print</span><span class="token punctuation">(</span>x_train_raw<span class="token punctuation">.</span>shape<span class="token punctuation">,</span> y_train_raw<span class="token punctuation">.</span>shape<span class="token punctuation">)</span> <span class="token comment"># (60000,28,28)6万张训练集</span> <span class="token keyword">print</span><span class="token punctuation">(</span>x_test_raw<span class="token punctuation">.</span>shape<span class="token punctuation">,</span> y_test_raw<span class="token punctuation">.</span>shape<span class="token punctuation">)</span> <span class="token comment"># (10000,28,28)1万张测试集</span> num_classes <span class="token operator">=</span> <span class="token number">10</span> y_train<span class="token operator">=</span> keras<span class="token punctuation">.</span>utils<span class="token punctuation">.</span>to_categorical<span class="token punctuation">(</span>y_train_raw<span class="token punctuation">,</span>num_classes<span class="token punctuation">)</span> <span class="token comment"># 将分类标签变为独热码(onehot)</span> y_test <span class="token operator">=</span> keras<span class="token punctuation">.</span>utils<span class="token punctuation">.</span>to_categorical<span class="token punctuation">(</span>y_test_raw<span class="token punctuation">,</span>num_classes<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>y_train<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]</span> <span class="token comment"># 数据可视化,看看测试的数据</span> plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">:</span> plt<span class="token punctuation">.</span>subplot<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span> plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>x_train_raw<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span> plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 二、构建并编译全连接神经网络</span> <span class="token comment"># 编译全连接层</span> x_train <span class="token operator">=</span> x_train_raw<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">60000</span><span class="token punctuation">,</span><span class="token number">784</span><span class="token punctuation">)</span> <span class="token comment"># 将28*28的图像展开成784*1的向量</span> x_test <span class="token operator">=</span> x_test_raw<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">10000</span><span class="token punctuation">,</span><span class="token number">784</span><span class="token punctuation">)</span> <span class="token comment"># 将图像像素值归一化0~1</span> x_train<span class="token operator">=</span> x_train<span class="token punctuation">.</span>astype<span class="token punctuation">(</span><span class="token string">'float32'</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">255</span> x_test <span class="token operator">=</span> x_test<span class="token punctuation">.</span>astype<span class="token punctuation">(</span><span class="token string">'float32'</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">255</span> model <span class="token operator">=</span> keras<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token comment"># 创建模型。模型包括3个全连接层和两个RELU激活函数</span> layers<span class="token punctuation">.</span>Dense<span class="token punctuation">(</span><span class="token number">512</span><span class="token punctuation">,</span>activation<span class="token operator">=</span><span class="token string">'relu'</span><span class="token punctuation">,</span> input_dim <span class="token operator">=</span> <span class="token number">784</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment"># 降维处理</span> layers<span class="token punctuation">.</span>Dense<span class="token punctuation">(</span><span class="token number">256</span><span class="token punctuation">,</span>activation<span class="token operator">=</span><span class="token string">'relu'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> layers<span class="token punctuation">.</span>Dense<span class="token punctuation">(</span><span class="token number">124</span><span class="token punctuation">,</span>activation<span class="token operator">=</span><span class="token string">'relu'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> layers<span class="token punctuation">.</span>Dense<span class="token punctuation">(</span>num_classes<span class="token punctuation">,</span>activation<span class="token operator">=</span><span class="token string">'softmax'</span><span class="token punctuation">)</span> <span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 三、训练网络</span> Optimizer <span class="token operator">=</span> optimizers<span class="token punctuation">.</span>Adam<span class="token punctuation">(</span><span class="token number">0.001</span><span class="token punctuation">)</span> model<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span>loss<span class="token operator">=</span>keras<span class="token punctuation">.</span>losses<span class="token punctuation">.</span>categorical_crossentropy<span class="token punctuation">,</span> optimizer<span class="token operator">=</span>Optimizer<span class="token punctuation">,</span> <span class="token comment"># Adam优化器 </span> metrics<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'accuracy'</span><span class="token punctuation">]</span> <span class="token punctuation">)</span> model<span class="token punctuation">.</span>fit<span class="token punctuation">(</span>x_train<span class="token punctuation">,</span>y_train<span class="token punctuation">,</span> <span class="token comment"># 训练集数据标签</span> batch_size<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">,</span> <span class="token comment"># 批大小 </span> epochs<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token comment"># 训练的轮次</span> verbose<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment"># 输出日志</span> <span class="token comment"># 四、测试模型</span> score <span class="token operator">=</span> model<span class="token punctuation">.</span>evaluate<span class="token punctuation">(</span>x_test<span class="token punctuation">,</span>y_test<span class="token punctuation">,</span>verbose<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Test loss:'</span><span class="token punctuation">,</span> score<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 损失函数: 0.0853068439</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Test accuracy:'</span><span class="token punctuation">,</span> score<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 精确度: 0.9767</span> test_loss<span class="token punctuation">,</span>test_acc <span class="token operator">=</span> model<span class="token punctuation">.</span>evaluate<span class="token punctuation">(</span>x<span class="token operator">=</span>x_test<span class="token punctuation">,</span>y<span class="token operator">=</span>y_test<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Test Accuracy %.2f"</span><span class="token operator">%</span>test_acc<span class="token punctuation">)</span> <span class="token comment"># 精确度: 0.9 </span> <span class="token comment"># 五、保存模型</span> model<span class="token punctuation">.</span>save<span class="token punctuation">(</span><span class="token string">'./final_DNN_mode1.h5'</span><span class="token punctuation">)</span> <span class="token comment"># 保存DNN模型</span> <span class="token comment"># 六、加载保存的模型</span> new_model <span class="token operator">=</span> load_model<span class="token punctuation">(</span><span class="token string">'./final_DNN_mode1.h5'</span><span class="token punctuation">)</span> new_model<span class="token punctuation">.</span>summary<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 七、CNN 模型测试 -----------------------------------------------------------------------------------------------------</span> <span class="token comment"># 将数据扩充维度,以适应CNN模型</span> X_train<span class="token operator">=</span>x_train<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">60000</span><span class="token punctuation">,</span><span class="token number">28</span><span class="token punctuation">,</span><span class="token number">28</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span> X_test<span class="token operator">=</span>x_test<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">10000</span><span class="token punctuation">,</span><span class="token number">28</span><span class="token punctuation">,</span><span class="token number">28</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment"># 定义卷积神经网络模型</span> model<span class="token operator">=</span>keras<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token comment"># 创建网络序列</span> layers<span class="token punctuation">.</span>Conv2D<span class="token punctuation">(</span>filters<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">,</span>kernel_size <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">,</span>strides <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> padding <span class="token operator">=</span><span class="token string">'same'</span><span class="token punctuation">,</span>activation <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">,</span>input_shape <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">28</span><span class="token punctuation">,</span><span class="token number">28</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment"># 添加第一层卷积层和池化层</span> layers<span class="token punctuation">.</span>MaxPool2D<span class="token punctuation">(</span>pool_size<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>strides <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>padding <span class="token operator">=</span> <span class="token string">'valid'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment"># 添加第二层卷积层和泄化层</span> layers<span class="token punctuation">.</span>Conv2D<span class="token punctuation">(</span>filters<span class="token operator">=</span><span class="token number">64</span><span class="token punctuation">,</span> kernel_size <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">,</span> strides<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>padding<span class="token operator">=</span><span class="token string">'same'</span><span class="token punctuation">,</span> activation <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">)</span><span class="token punctuation">,</span> layers<span class="token punctuation">.</span>MaxPool2D<span class="token punctuation">(</span>pool_size<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>strides <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>padding <span class="token operator">=</span> <span class="token string">'valid'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment"># 添加dropout层 以减少过拟合</span> layers<span class="token punctuation">.</span>Dropout<span class="token punctuation">(</span><span class="token number">0.25</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment"># 随机丢弃神经元的比例 </span> layers<span class="token punctuation">.</span>Flatten<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment"># 添加两层全连接层</span> layers<span class="token punctuation">.</span>Dense<span class="token punctuation">(</span>units<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">,</span>activation <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>relu<span class="token punctuation">)</span><span class="token punctuation">,</span> layers<span class="token punctuation">.</span>Dropout<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> layers<span class="token punctuation">.</span>Dense<span class="token punctuation">(</span>units<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">,</span>activation <span class="token operator">=</span> tf<span class="token punctuation">.</span>nn<span class="token punctuation">.</span>softmax<span class="token punctuation">)</span> <span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 编译并训练模型</span> Optimizer <span class="token operator">=</span> optimizers<span class="token punctuation">.</span>Adam<span class="token punctuation">(</span><span class="token number">0.001</span><span class="token punctuation">)</span> model<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span>Optimizer<span class="token punctuation">,</span>loss<span class="token operator">=</span><span class="token string">"categorical_crossentropy"</span><span class="token punctuation">,</span>metrics<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'accuracy'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> model<span class="token punctuation">.</span>fit<span class="token punctuation">(</span>x<span class="token operator">=</span>X_train<span class="token punctuation">,</span>y<span class="token operator">=</span>y_train<span class="token punctuation">,</span>epochs<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">,</span>batch_size<span class="token operator">=</span><span class="token number">128</span><span class="token punctuation">)</span> <span class="token comment"># 轮次为5</span> <span class="token comment"># 保存CNN模型</span> model<span class="token punctuation">.</span>save<span class="token punctuation">(</span><span class="token string">'./final_CNN_model.h5'</span><span class="token punctuation">)</span> <span class="token comment"># 加载保存的模型</span> new_model <span class="token operator">=</span> load_model<span class="token punctuation">(</span><span class="token string">'./final_CNN_model.h5'</span><span class="token punctuation">)</span> <span class="token comment"># 八、测试数据进行可视化测试</span> <span class="token comment"># @matplotlib.inline</span> <span class="token keyword">def</span> <span class="token function">res_Visual</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token comment"># 参阅 https://blog.csdn.net/yiyihuazi/article/details/122323349</span> <span class="token comment"># keras 2.6删除了predict_classes()函数</span> <span class="token comment"># final_opt_a=new_model.predict_classes(X_test[0:n]) # 通过模型预测测试集</span> <span class="token comment"># 用下面的语句代替</span> predicts <span class="token operator">=</span> new_model<span class="token punctuation">.</span>predict<span class="token punctuation">(</span>X_test<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span>n<span class="token punctuation">]</span><span class="token punctuation">)</span> final_opt_a <span class="token operator">=</span> np<span class="token punctuation">.</span>argmax<span class="token punctuation">(</span>predicts<span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span> fig<span class="token punctuation">,</span> ax <span class="token operator">=</span> plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span>nrows<span class="token operator">=</span><span class="token builtin">int</span><span class="token punctuation">(</span>n<span class="token operator">/</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> ncols<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span> ax <span class="token operator">=</span> ax<span class="token punctuation">.</span>flatten<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'前{}张图片预测结果为:'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>final_opt_a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span>end<span class="token operator">=</span><span class="token string">'.'</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token punctuation">(</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'\t'</span><span class="token punctuation">)</span> <span class="token comment"># 图片可视化展示</span> img <span class="token operator">=</span> X_test<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">28</span><span class="token punctuation">,</span><span class="token number">28</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># 读取每行数据,格式为Ndarry</span> plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">"off"</span><span class="token punctuation">)</span> ax<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img<span class="token punctuation">,</span>cmap<span class="token operator">=</span><span class="token string">'Greys'</span><span class="token punctuation">,</span>interpolation<span class="token operator">=</span><span class="token string">'nearest'</span><span class="token punctuation">)</span> <span class="token comment"># 可视化</span> ax<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">"off"</span><span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'测试集前{}张图片为:'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span> res_Visual<span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>keras要降低版本到2.6.0,否则出错,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fblog.csdn.net%2Fqq_43440490%2Farticle%2Fdetails%2F125458488" target="_blank">参阅资料</a>。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-jsx"><code class=" language-jsx">ImportError<span class="token punctuation">:</span> cannot <span class="token keyword">import</span> name <span class="token string">'dtensor'</span> <span class="token keyword">from</span> <span class="token string">'tensorflow.compat.v2.experimental'</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># conda list keras</span> <span class="token comment"># packages in environment at /usr/local/anaconda3/envs/gpu:</span> <span class="token comment">#</span> <span class="token comment"># Name Version Build Channel</span> keras <span class="token number">2.10</span><span class="token number">.0</span> pypi_0 pypi keras<span class="token operator">-</span>preprocessing <span class="token number">1.1</span><span class="token number">.2</span> pypi_0 pypi <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># pip install keras==2.6</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p> 测试程序前面DNN全连接神经网络的部分通过了,后面使用cuDNN的CNN卷积神经网络部分没有通过,cuDNN8.5的版本可能过高,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Ftensorflow%2Ftensorflow%2Fissues%2F45044" target="_blank">参阅资料</a>。需要降回经过测试确认的8.1版。报错:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-css"><code class=" language-css">OP_REQUIRES failed at conv_ops.<span class="token property">cc</span><span class="token punctuation">:</span>1276 <span class="token punctuation">:</span> Not <span class="token property">found</span><span class="token punctuation">:</span> No algorithm worked! <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>7、降低cuDNN版本到8.1。笔记本上下载并用SecureFX通过SSH传到服务器上,拷贝并替换cuDNN8.5的文件。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># tar -xvf cudnn-11.2-linux-x64-v8.1.1.33.tgz # cd cuda # cp -f lib64/* /usr/local/cuda/lib64/ # cp -f include/* /usr/local/cuda/include/ # chmod a+r /usr/local/cuda/lib64/* # chmod a+r /usr/local/cuda/include/* <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p> 在全局环境变量中加入下面的设置,否则跑CNN测试时可能会报错说申请的内存过大,导致算法运行失败:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># vi /etc/profile <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-undefined"><code class=" language-undefined">TF_GPU_ALLOCATOR=cuda_malloc_async <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p> 更新动态链接库的Cache,否则链接不对,重启系统:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># ldconfig -X # reboot now <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre> </div> <p> 用ubuntu用户登录,激活"gpu"环境并启动Jupyter Notebook:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby">$ conda activate gpu $ jupyter notebook <span class="token operator">&</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre> </div> <p>8、重新运行刚才的notebook测试GPU环境的安装,通过。</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-153f298830bd13ca.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="110246" data-image-index="2" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">1、加载tensorflow识别手写数字例子数据集</div> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-a83eab0eb5a33c2a.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="186918" data-image-index="3" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">2、构建并编译DNN神经网络</div> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-46e421ea1e6c88fe.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="124233" data-image-index="4" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">3、训练网络</div> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-bf6df69caaac3ad3.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="112141" data-image-index="5" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">4、测试模型</div> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-cfc5645f409210b2.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="137679" data-image-index="6" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">5、CNN 模型测试</div> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-51725c7e1844a943.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="125099" data-image-index="7" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">6、测试数据进行可视化</div> </div> <p>五、安装Pytorch与HanLP</p> <p> 我在同一个虚拟环境"gpu"中安装Tensorflow、Pytorch与HanLP,因为要跑HanLP2.1,它同时支持两个后端。</p> <p>1、安装Pytorch。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash">(gpu) root@VM-0-14-ubuntu:~# conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=11.3 -c pytorch Collecting package metadata (current_repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Collecting package metadata (repodata.json): done Solving environment: done ## Package Plan ## environment location: /usr/local/anaconda3/envs/gpu added / updated specs: - cudatoolkit=11.3 - pytorch==1.11.0 - torchaudio==0.11.0 - torchvision==0.12.0 The following packages will be downloaded: package | build ---------------------------|----------------- cudatoolkit-11.3.1 | h2bc3f7f_2 549.3 MB ...... torchvision pytorch/linux-64::torchvision-0.12.0-py39_cu113 None Proceed ([y]/n)? y Downloading and Extracting Packages lame-3.100 | 323 KB | ################################################################################################################################################## | 100% ...... ######################################################################## | 100% Preparing transaction: done Verifying transaction: done Executing transaction: | By downloading and using the CUDA Toolkit conda packages, you accept the terms and conditions of the CUDA End User License Agreement (EULA): https://docs.nvidia.com/cuda/eula/index.html done Retrieving notices: ...working... done <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>2、安装HanLP。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-csharp"><code class=" language-csharp"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root@VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span># pip install hanlp <span class="token class-name">Looking</span> <span class="token keyword">in</span> indexes<span class="token punctuation">:</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>mirrors<span class="token punctuation">.</span>tencentyun<span class="token punctuation">.</span>com<span class="token operator">/</span>pypi<span class="token operator">/</span>simple <span class="token class-name">Collecting</span> hanlp <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token class-name">Successfully</span> built hanlp<span class="token operator">-</span>common hanlp<span class="token operator">-</span>trie hanlp<span class="token operator">-</span>downloader phrasetree <span class="token class-name">Installing</span> collected packages<span class="token punctuation">:</span> toposort<span class="token punctuation">,</span> tokenizers<span class="token punctuation">,</span> phrasetree<span class="token punctuation">,</span> tqdm<span class="token punctuation">,</span> regex<span class="token punctuation">,</span> pyyaml<span class="token punctuation">,</span> pynvml<span class="token punctuation">,</span> hanlp<span class="token operator">-</span>common<span class="token punctuation">,</span> filelock<span class="token punctuation">,</span> huggingface<span class="token operator">-</span>hub<span class="token punctuation">,</span> hanlp<span class="token operator">-</span>trie<span class="token punctuation">,</span> hanlp<span class="token operator">-</span>downloader<span class="token punctuation">,</span> transformers<span class="token punctuation">,</span> hanlp <span class="token class-name">Successfully</span> installed filelock<span class="token operator">-</span><span class="token number">3.8</span><span class="token number">.0</span> hanlp<span class="token operator">-</span><span class="token number">2.1</span><span class="token number">.0</span>b42 hanlp<span class="token operator">-</span>common<span class="token operator">-</span><span class="token number">0.0</span><span class="token number">.18</span> hanlp<span class="token operator">-</span>downloader<span class="token operator">-</span><span class="token number">0.0</span><span class="token number">.25</span> hanlp<span class="token operator">-</span>trie<span class="token operator">-</span><span class="token number">0.0</span><span class="token number">.5</span> huggingface<span class="token operator">-</span>hub<span class="token operator">-</span><span class="token number">0.10</span><span class="token number">.1</span> phrasetree<span class="token operator">-</span><span class="token number">0.0</span><span class="token number">.8</span> pynvml<span class="token operator">-</span><span class="token number">11.4</span><span class="token number">.1</span> pyyaml<span class="token operator">-</span><span class="token number">6.0</span> regex<span class="token operator">-</span><span class="token number">2022.9</span><span class="token number">.13</span> tokenizers<span class="token operator">-</span><span class="token number">0.11</span><span class="token number">.6</span> toposort<span class="token operator">-</span><span class="token number">1.5</span> tqdm<span class="token operator">-</span><span class="token number">4.64</span><span class="token number">.1</span> transformers<span class="token operator">-</span><span class="token number">4.23</span><span class="token number">.1</span> WARNING<span class="token punctuation">:</span> <span class="token class-name">Running</span> pip <span class="token keyword">as</span> the <span class="token string">'root'</span> user can result <span class="token keyword">in</span> broken permissions and conflicting behaviour with the system package manager<span class="token punctuation">.</span> <span class="token class-name">It</span> <span class="token keyword">is</span> recommended to use a <span class="token keyword">virtual</span> environment instead<span class="token punctuation">:</span> https<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>pip<span class="token punctuation">.</span>pypa<span class="token punctuation">.</span>io<span class="token operator">/</span>warnings<span class="token operator">/</span>venv <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>安装fasttext,这是HanLP一些Tensorflow预训练模型要用的:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-csharp"><code class=" language-csharp"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root@VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span># pip install fasttext <span class="token class-name">Looking</span> <span class="token keyword">in</span> indexes<span class="token punctuation">:</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>mirrors<span class="token punctuation">.</span>tencentyun<span class="token punctuation">.</span>com<span class="token operator">/</span>pypi<span class="token operator">/</span>simple <span class="token class-name">Collecting</span> fasttext <span class="token class-name">Downloading</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>mirrors<span class="token punctuation">.</span>tencentyun<span class="token punctuation">.</span>com<span class="token operator">/</span>pypi<span class="token operator">/</span>packages<span class="token operator">/</span>f8<span class="token operator">/</span><span class="token number">85</span><span class="token operator">/</span>e2b368ab6d3528827b147fdb814f8189acc981a4bc2f99ab894650e05c40<span class="token operator">/</span>fasttext<span class="token operator">-</span><span class="token number">0.9</span><span class="token number">.2</span><span class="token punctuation">.</span>tar<span class="token punctuation">.</span>gz <span class="token punctuation">(</span><span class="token number">68</span> kB<span class="token punctuation">)</span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <span class="token number">68.8</span><span class="token operator">/</span><span class="token number">68.8</span> kB <span class="token number">332.3</span> kB<span class="token operator">/</span>s eta <span class="token number">0</span><span class="token punctuation">:</span><span class="token number">00</span><span class="token punctuation">:</span><span class="token number">00</span> <span class="token class-name">Preparing</span> metadata <span class="token punctuation">(</span>setup<span class="token punctuation">.</span>py<span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> done <span class="token class-name">Collecting</span> pybind11<span class="token operator">>=</span><span class="token number">2.2</span> <span class="token class-name">Using</span> cached http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>mirrors<span class="token punctuation">.</span>tencentyun<span class="token punctuation">.</span>com<span class="token operator">/</span>pypi<span class="token operator">/</span>packages<span class="token operator">/</span><span class="token number">1</span>d<span class="token operator">/</span><span class="token number">53</span><span class="token operator">/</span>e6b27f3596278f9dd1d28ef1ddb344fd0cd5db98ef2179d69a2044e11897<span class="token operator">/</span>pybind11<span class="token operator">-</span><span class="token number">2.10</span><span class="token number">.1</span><span class="token operator">-</span>py3<span class="token operator">-</span>none<span class="token operator">-</span>any<span class="token punctuation">.</span>whl <span class="token punctuation">(</span><span class="token number">216</span> kB<span class="token punctuation">)</span> <span class="token class-name">Requirement</span> already satisfied<span class="token punctuation">:</span> setuptools<span class="token operator">>=</span><span class="token number">0.7</span><span class="token number">.0</span> <span class="token keyword">in</span> <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>anaconda3<span class="token operator">/</span>envs<span class="token operator">/</span>gpu<span class="token operator">/</span>lib<span class="token operator">/</span>python3<span class="token punctuation">.</span><span class="token number">9</span><span class="token operator">/</span>site<span class="token operator">-</span>packages <span class="token punctuation">(</span><span class="token keyword">from</span> fasttext<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token number">65.5</span><span class="token number">.0</span><span class="token punctuation">)</span> <span class="token class-name">Requirement</span> already satisfied<span class="token punctuation">:</span> numpy <span class="token keyword">in</span> <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>anaconda3<span class="token operator">/</span>envs<span class="token operator">/</span>gpu<span class="token operator">/</span>lib<span class="token operator">/</span>python3<span class="token punctuation">.</span><span class="token number">9</span><span class="token operator">/</span>site<span class="token operator">-</span>packages <span class="token punctuation">(</span><span class="token keyword">from</span> fasttext<span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token number">1.23</span><span class="token number">.3</span><span class="token punctuation">)</span> <span class="token class-name">Building</span> wheels <span class="token keyword">for</span> collected packages<span class="token punctuation">:</span> fasttext <span class="token class-name">Building</span> wheel <span class="token keyword">for</span> fasttext <span class="token punctuation">(</span>setup<span class="token punctuation">.</span>py<span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> done <span class="token class-name">Created</span> wheel <span class="token keyword">for</span> fasttext<span class="token punctuation">:</span> filename<span class="token operator">=</span>fasttext<span class="token operator">-</span><span class="token number">0.9</span><span class="token number">.2</span><span class="token operator">-</span>cp39<span class="token operator">-</span>cp39<span class="token operator">-</span>linux_x86_64<span class="token punctuation">.</span>whl size<span class="token operator">=</span><span class="token number">299146</span> sha256<span class="token operator">=</span><span class="token number">4</span>dee6f6dc5fb53404fb5cbb69c2cc3a2faef7f3af0500567ad49dc01f26d89d7 <span class="token class-name">Stored</span> <span class="token keyword">in</span> directory<span class="token punctuation">:</span> <span class="token operator">/</span>root<span class="token operator">/</span><span class="token punctuation">.</span>cache<span class="token operator">/</span>pip<span class="token operator">/</span>wheels<span class="token operator">/</span>ca<span class="token operator">/</span><span class="token number">08</span><span class="token operator">/</span>ee<span class="token operator">/</span>d0dd871c6c089c4c3971722067bd577f8827c9b4d5d6f2477a <span class="token class-name">Successfully</span> built fasttext <span class="token class-name">Installing</span> collected packages<span class="token punctuation">:</span> pybind11<span class="token punctuation">,</span> fasttext <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>3、测试PyTorch及HanLP。</p> <p> 先简单测试下,后面会继续测试。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-swift"><code class=" language-swift"><span class="token keyword">import</span> torch <span class="token function">print</span><span class="token punctuation">(</span>torch<span class="token punctuation">.</span>__version__<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span>torch<span class="token punctuation">.</span>cuda<span class="token punctuation">.</span><span class="token function">is_available</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-3ee8f3f284424de8.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="82396" data-image-index="8" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">pytorch识别出GPU</div> </div> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-go"><code class=" language-go"># 先运行Tensorflow模型再运行PyTorch模型就成功,如果前面先运行过PyTorch模型,这里就会失败。 <span class="token keyword">import</span> hanlp tokenizer <span class="token operator">=</span> hanlp<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>hanlp<span class="token punctuation">.</span>pretrained<span class="token punctuation">.</span>tok<span class="token punctuation">.</span>LARGE_ALBERT_BASE<span class="token punctuation">)</span> text <span class="token operator">=</span> <span class="token string">'NLP统计模型没有加规则,聪明人知道自己加。英文、数字、自定义词典统统都是规则。'</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token function">tokenizer</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span> # 后面的测试不受运行顺序的影响 <span class="token keyword">import</span> hanlp HanLP <span class="token operator">=</span> hanlp<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>hanlp<span class="token punctuation">.</span>pretrained<span class="token punctuation">.</span>mtl<span class="token punctuation">.</span>CLOSE_TOK_POS_NER_SRL_DEP_SDP_CON_ELECTRA_SMALL_ZH<span class="token punctuation">)</span> # 世界最大中文语料库 <span class="token function">HanLP</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'2021年HanLPv2.1为生产环境带来次世代最先进的多语种NLP技术。'</span><span class="token punctuation">,</span> <span class="token string">'阿婆主来到北京立方庭参观自然语义科技公司。'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">import</span> hanlp HanLP <span class="token operator">=</span> hanlp<span class="token punctuation">.</span><span class="token function">pipeline</span><span class="token punctuation">(</span><span class="token punctuation">)</span> \ <span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>hanlp<span class="token punctuation">.</span>utils<span class="token punctuation">.</span>rules<span class="token punctuation">.</span>split_sentence<span class="token punctuation">,</span> output_key<span class="token operator">=</span><span class="token string">'sentences'</span><span class="token punctuation">)</span> \ <span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>hanlp<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token string">'FINE_ELECTRA_SMALL_ZH'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> output_key<span class="token operator">=</span><span class="token string">'tok'</span><span class="token punctuation">)</span> \ <span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>hanlp<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token string">'CTB9_POS_ELECTRA_SMALL'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> output_key<span class="token operator">=</span><span class="token string">'pos'</span><span class="token punctuation">)</span> \ <span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>hanlp<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token string">'MSRA_NER_ELECTRA_SMALL_ZH'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> output_key<span class="token operator">=</span><span class="token string">'ner'</span><span class="token punctuation">,</span> input_key<span class="token operator">=</span><span class="token string">'tok'</span><span class="token punctuation">)</span> \ <span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>hanlp<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token string">'CTB9_DEP_ELECTRA_SMALL'</span><span class="token punctuation">,</span> conll<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> output_key<span class="token operator">=</span><span class="token string">'dep'</span><span class="token punctuation">,</span> input_key<span class="token operator">=</span><span class="token string">'tok'</span><span class="token punctuation">)</span>\ <span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>hanlp<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token string">'CTB9_CON_ELECTRA_SMALL'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> output_key<span class="token operator">=</span><span class="token string">'con'</span><span class="token punctuation">,</span> input_key<span class="token operator">=</span><span class="token string">'tok'</span><span class="token punctuation">)</span> <span class="token function">HanLP</span><span class="token punctuation">(</span><span class="token string">'2021年HanLPv2.1为生产环境带来次世代最先进的多语种NLP技术。阿婆主来到北京立方庭参观自然语义科技公司。'</span><span class="token punctuation">)</span> <span class="token function">HanLP</span><span class="token punctuation">(</span><span class="token string">'2021年HanLPv2.1为生产环境带来次世代最先进的多语种NLP技术。'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">pretty_print</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">import</span> hanlp tok <span class="token operator">=</span> hanlp<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>hanlp<span class="token punctuation">.</span>pretrained<span class="token punctuation">.</span>tok<span class="token punctuation">.</span>COARSE_ELECTRA_SMALL_ZH<span class="token punctuation">)</span> <span class="token function">tok</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'商品和服务。'</span><span class="token punctuation">,</span> <span class="token string">'阿婆主来到北京立方庭参观自然语义科技公司。'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> tok_fine <span class="token operator">=</span> hanlp<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>hanlp<span class="token punctuation">.</span>pretrained<span class="token punctuation">.</span>tok<span class="token punctuation">.</span>FINE_ELECTRA_SMALL_ZH<span class="token punctuation">)</span> <span class="token function">tok_fine</span><span class="token punctuation">(</span><span class="token string">'阿婆主来到北京立方庭参观自然语义科技公司'</span><span class="token punctuation">)</span> pos <span class="token operator">=</span> hanlp<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>hanlp<span class="token punctuation">.</span>pretrained<span class="token punctuation">.</span>pos<span class="token punctuation">.</span>CTB9_POS_ELECTRA_SMALL<span class="token punctuation">)</span> <span class="token function">pos</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">"我"</span><span class="token punctuation">,</span> <span class="token string">"的"</span><span class="token punctuation">,</span> <span class="token string">"希望"</span><span class="token punctuation">,</span> <span class="token string">"是"</span><span class="token punctuation">,</span> <span class="token string">"希望"</span><span class="token punctuation">,</span> <span class="token string">"张晚霞"</span><span class="token punctuation">,</span> <span class="token string">"的"</span><span class="token punctuation">,</span> <span class="token string">"背影"</span><span class="token punctuation">,</span> <span class="token string">"被"</span><span class="token punctuation">,</span> <span class="token string">"晚霞"</span><span class="token punctuation">,</span> <span class="token string">"映红"</span><span class="token punctuation">,</span> <span class="token string">"。"</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-f83d01b114c0fb84.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="129921" data-image-index="9" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">分词与词性标注</div> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-93308dfea49a1938.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="119564" data-image-index="10" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">管道操作</div> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-d4799d7861662ebf.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="128877" data-image-index="11" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">打印语法树</div> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-8c9d89a3337aa19c.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="127723" data-image-index="12" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">各种预训练模型模型分词</div> </div> <p>六、安装配置JupyterHub</p> <p> Linux GPU虚拟主机作为科研、开发、测试或生产环境,多用户是很自然的,Jupyter Notebook是单用户的,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fjupyterhub.readthedocs.io%2Fen%2Fstable%2Findex.html" target="_blank">JupyterHub</a>则提供了一层多用户的代理,让大家可以通过它登录系统,使用各自的Jupyter Notebook或Jupyter Lab,后者是前者的下一代版本。</p> <p></p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 525px;"> <div class="image-container-fill" style="padding-bottom: 75.0%;"></div> <div class="image-view" data-width="1024" data-height="768"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-9b941db52827e473.png" data-original-width="1024" data-original-height="768" data-original-format="image/png" data-original-filesize="126076" data-image-index="13" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">Jupyterhub统一代理各用户的Jupyterlab,从而实现多用户服务</div> </div> <br> 根据<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F47605853%2F500-error-in-jupyterhub" target="_blank">该帖子</a>,如果曾经运行过Jupyter Notebook,那么它在$HOME/.jupyter下的配置文件会与Jupyterhub要启动的用户Jupyter Lab或Jupyter Notebook Server冲突,导致服务进程不能启动,代理转发失败,这是个BUG?所以如果曾经运行过Jupyter Notebook,像前面那样,要先删除那个目录。这个问题搞了两天,几乎要崩溃,还是stackoverflow给力。<p></p> <p> <a href="https://www.jianshu.com/p/3585d51eb577" target="_blank">参阅资料1</a>,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fcloud.tencent.com%2Fdeveloper%2Farticle%2F1816547" target="_blank">参阅资料2</a>,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fzhuanlan.zhihu.com%2Fp%2F363793325" target="_blank">参阅资料3</a>,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fjupyterhub.readthedocs.io%2Fen%2Fstable%2Fquickstart.html%23installation" target="_blank">参阅资料4</a>。</p> <p>1、安装并升级node.js与npm。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># #从软件源获取最新软件列表,更新系统软件 # apt-get update # apt-get upgrade # #安装依赖 # apt install -y npm nodejs <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>升级node.js,不要安装最新的18版,兼容性有问题,会报错,<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F72921215%2Fgetting-glibc-2-28-not-found" target="_blank">参阅资料</a>,JupyterHub要求版本10以上,而Ubuntu18安装的是版本8。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash">##----- 先清除 npm cache # npm cache clean -f ##----- 安装 n 模块 # npm install -g n <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>升级node.js:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-tsx"><code class=" language-tsx">root@<span class="token constant">VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span># n <span class="token number">16.18</span><span class="token number">.0</span> # 指定版本<span class="token number">16.18</span><span class="token number">.0</span> installing <span class="token punctuation">:</span> node<span class="token operator">-</span>v16<span class="token punctuation">.</span><span class="token number">18.0</span> mkdir <span class="token punctuation">:</span> <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>n<span class="token operator">/</span>versions<span class="token operator">/</span>node<span class="token operator">/</span><span class="token number">16.18</span><span class="token number">.0</span> fetch <span class="token punctuation">:</span> https<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>nodejs<span class="token punctuation">.</span>org<span class="token operator">/</span>dist<span class="token operator">/</span>v16<span class="token punctuation">.</span><span class="token number">18.0</span><span class="token operator">/</span>node<span class="token operator">-</span>v16<span class="token punctuation">.</span><span class="token number">18.0</span><span class="token operator">-</span>linux<span class="token operator">-</span>x64<span class="token punctuation">.</span>tar<span class="token punctuation">.</span>xz copying <span class="token punctuation">:</span> node<span class="token operator">/</span><span class="token number">16.18</span><span class="token number">.0</span> installed <span class="token punctuation">:</span> v16<span class="token punctuation">.</span><span class="token number">18.0</span> <span class="token punctuation">(</span><span class="token keyword">with</span> npm <span class="token number">8.19</span><span class="token number">.2</span><span class="token punctuation">)</span> Note<span class="token punctuation">:</span> the node command changed location and the old location may be remembered <span class="token keyword">in</span> your current shell<span class="token punctuation">.</span> old <span class="token punctuation">:</span> <span class="token operator">/</span>usr<span class="token operator">/</span>bin<span class="token operator">/</span>node <span class="token keyword">new</span> <span class="token punctuation">:</span> <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>bin<span class="token operator">/</span>node If <span class="token string">"node --version"</span> shows the old version then start a <span class="token keyword">new</span> <span class="token class-name">shell</span><span class="token punctuation">,</span> or reset the location hash <span class="token keyword">with</span><span class="token punctuation">:</span> hash <span class="token operator">-</span><span class="token function">r</span> <span class="token punctuation">(</span><span class="token keyword">for</span> bash<span class="token punctuation">,</span> zsh<span class="token punctuation">,</span> ash<span class="token punctuation">,</span> dash<span class="token punctuation">,</span> and ksh<span class="token punctuation">)</span> <span class="token function">rehash</span> <span class="token punctuation">(</span><span class="token keyword">for</span> csh and tcsh<span class="token punctuation">)</span> root@<span class="token constant">VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span># hash <span class="token operator">-</span>r root@<span class="token constant">VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span># node <span class="token operator">-</span>v v16<span class="token punctuation">.</span><span class="token number">18.0</span> root@<span class="token constant">VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span># npm <span class="token operator">-</span>v <span class="token number">8.19</span><span class="token number">.2</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>2、安装configurable-http-proxy。</p> <p>可以用npm装:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-undefined"><code class=" language-undefined">npm install -g configurable-http-proxy <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>不过推荐用conda装,会把其它依赖包一起装上,它也会安装一个node.js版本11,也可以用,注意要切换并安装到相应的虚拟环境中,这里是"gpu"。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># conda install configurable-http-proxy</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># conda list configurable-http-proxy</span> <span class="token comment"># packages in environment at /usr/local/anaconda3/envs/gpu:</span> <span class="token comment">#</span> <span class="token comment"># Name Version Build Channel</span> configurable<span class="token operator">-</span>http<span class="token operator">-</span>proxy <span class="token number">4.0</span><span class="token number">.1</span> node6_0 <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># configurable-http-proxy -V</span> <span class="token number">4.0</span><span class="token number">.1</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># </span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>3、在虚拟环境中安装JupyterHub等。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># conda install jupyter jupyterlab jupyterhub</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># conda list jupyter</span> <span class="token comment"># packages in environment at /usr/local/anaconda3/envs/gpu:</span> <span class="token comment">#</span> <span class="token comment"># Name Version Build Channel</span> jupyter <span class="token number">1.0</span><span class="token number">.0</span> py39h06a4308_8 jupyter_client <span class="token number">7.3</span><span class="token number">.5</span> py39h06a4308_0 jupyter_console <span class="token number">6.4</span><span class="token number">.3</span> pyhd3eb1b0_0 jupyter_core <span class="token number">4.11</span><span class="token number">.1</span> py39h06a4308_0 jupyter_server <span class="token number">1.18</span><span class="token number">.1</span> py39h06a4308_0 jupyter_telemetry <span class="token number">0.1</span><span class="token number">.0</span> py_0 jupyterhub <span class="token number">2.0</span><span class="token number">.0</span> pyhd3eb1b0_0 jupyterlab <span class="token number">3.4</span><span class="token number">.4</span> py39h06a4308_0 jupyterlab_pygments <span class="token number">0.1</span><span class="token number">.2</span> py_0 jupyterlab_server <span class="token number">2.15</span><span class="token number">.2</span> py39h06a4308_0 jupyterlab_widgets <span class="token number">1.0</span><span class="token number">.0</span> pyhd3eb1b0_1 <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>4、配置JupyterHub。</p> <p>新建目录/etc/jupyterhub,在该目录下新建一个配置文件,编辑文件。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># mkdir /etc/jupyterhub</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># cd /etc/jupyterhub</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">/</span>etc<span class="token operator">/</span>jupyterhub<span class="token comment"># jupyterhub --generate-config</span> <span class="token constant">Writing</span> default config to<span class="token punctuation">:</span> jupyterhub_config<span class="token punctuation">.</span>py <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">/</span>etc<span class="token operator">/</span>jupyterhub<span class="token comment"># vi jupyterhub_config.py</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>内容如下:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># Added by Jean 2022/10/31 c.Authenticator.whitelist = {'ubuntu'} # 允许使用Jupyterhub的用户列表,逗号分隔。 c.Authenticator.admin_users = {'ubuntu'} #Jupyterhub的管理员用户列表 c.Spawner.notebook_dir = '/home/{username}' #浏览器登录后进入用户的主目录 c.Spawner.default_url = '/lab' # 使用Jupyterlab而不是Notebook c.JupyterHub.extra_log_file = '/var/log/jupyterhub.log' <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>5、用root用户后台启动JupyterHub。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">/</span>etc<span class="token operator">/</span>jupyterhub<span class="token comment"># jupyterhub -f /etc/jupyterhub/jupyterhub_config.py &</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>6、在浏览器中访问,输入的是Linux系统中已有的用户名,网址是<a href="https://links.jianshu.com/go?to=http%3A%2F%2Fip%3A8000" target="_blank">http://ip:8000</a>,后面再配SSL加密。<br> </p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-e9a1ef224701a9b6.PNG" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="110567" data-image-index="14" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">JupyterHub中运行Jupyter Lab</div> </div> <br> JupyterHub里可以打开终端窗口,执行各种操作,用户的身份就是登录的用户。如果SSH端口被屏蔽,这样就可以通过HTTP端口建立隧道。执行su命令就可以root。<p></p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash">(base) ubuntu@VM-0-14-ubuntu:~$ su --help Usage: su [options] [LOGIN] Options: -c, --command COMMAND pass COMMAND to the invoked shell -h, --help display this help message and exit -, -l, --login make the shell a login shell -m, -p, --preserve-environment do not reset environment variables, and keep the same shell -s, --shell SHELL use SHELL instead of the default in passwd (base) ubuntu@VM-0-14-ubuntu:~$ su --preserve-environment Password: (base) root@VM-0-14-ubuntu:~# <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px;"> <div class="image-container-fill" style="padding-bottom: 53.65%;"></div> <div class="image-view" data-width="1920" data-height="1030"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-a9340f980403d025.png" data-original-width="1920" data-original-height="1030" data-original-format="image/png" data-original-filesize="109262" data-image-index="15" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">JupyterHub中打开终端窗口</div> </div> <p>7、配置SSL加密。</p> <p> 这是配好后SSL加密连接登录的截图,可以打开网址前面的锁图标看证书链的内容,前面的截图可见,如果是非加密连接,网址前面显示的是“不安全”提示。此处自签的数字证书是签给IP,因为这个虚拟主机还没有申请域名。</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 394px;"> <div class="image-container-fill" style="padding-bottom: 56.25%;"></div> <div class="image-view" data-width="1920" data-height="1080"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-b430487c92f55e4d.PNG" data-original-width="1920" data-original-height="1080" data-original-format="image/png" data-original-filesize="210245" data-image-index="16" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">用自签证书给JupyterHub建立SSL加密通道</div> </div> <br> <p>1)先讲讲JupyterHub配置。在配置文件中增加两行指出使用的服务器密钥文件和证书文件即可,后面再讲用openssl自建CA及签发该数字证书。因为是root用户,server.key没有指定访问密码。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># Added by Jean for SSL 2022/03/19 c.JupyterHub.ssl_key = '/root/cert/server.key' c.JupyterHub.ssl_cert = '/root/cert/server.crt' <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre> </div> <p>重启JupyterHub后,把自建CA的根证书拷出并导入浏览器(后面讲),用<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fip%3A8000" target="_blank">https://ip:8000</a>访问即可,如上图所示。</p> <p>2)自建CA签发自签服务器证书。</p> <p><a href="https://links.jianshu.com/go?to=https%3A%2F%2Fwww.cnblogs.com%2Fpcxie%2Fp%2F12900666.html" target="_blank">参阅资料</a>。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-kotlin"><code class=" language-kotlin"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~# cd <span class="token operator">/</span><span class="token function">root</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~# mkdir <span class="token function">cert</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~# cd <span class="token function">cert</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert# mkdir demoCA <span class="token operator">&&</span> cd <span class="token function">demoCA</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert<span class="token operator">/</span>demoCA# mkdir <span class="token keyword">private</span> <span class="token function">newcerts</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert<span class="token operator">/</span>demoCA# touch index<span class="token punctuation">.</span><span class="token function">txt</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert<span class="token operator">/</span>demoCA# echo <span class="token string">'01'</span> <span class="token operator">></span> <span class="token function">serial</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert<span class="token operator">/</span>demoCA# cd <span class="token keyword">private</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert<span class="token operator">/</span>demoCA<span class="token operator">/</span><span class="token keyword">private</span># openssl genrsa <span class="token operator">-</span><span class="token keyword">out</span> cakey<span class="token punctuation">.</span>pem <span class="token number">2048</span> Generating RSA <span class="token keyword">private</span> key<span class="token punctuation">,</span> <span class="token number">2048</span> bit long <span class="token function">modulus</span> <span class="token punctuation">(</span><span class="token number">2</span> primes<span class="token punctuation">)</span> <span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token punctuation">.</span><span class="token operator">++</span><span class="token operator">++</span><span class="token operator">+</span> <span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">..</span><span class="token operator">++</span><span class="token operator">++</span><span class="token operator">+</span> e <span class="token keyword">is</span> <span class="token function">65537</span> <span class="token punctuation">(</span><span class="token number">0x010001</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert<span class="token operator">/</span>demoCA<span class="token operator">/</span><span class="token keyword">private</span># openssl req <span class="token operator">-</span>sha256 <span class="token operator">-</span>new <span class="token operator">-</span>x509 <span class="token operator">-</span>days <span class="token number">3650</span> <span class="token operator">-</span>key cakey<span class="token punctuation">.</span>pem <span class="token operator">-</span><span class="token keyword">out</span> cacert<span class="token punctuation">.</span>pem \ <span class="token operator">></span> <span class="token operator">-</span>subj <span class="token string">"/C=CN/ST=GD/L=ZhuHai/O=Jean/OU=Study/CN=RootCA"</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert<span class="token operator">/</span>demoCA<span class="token operator">/</span><span class="token keyword">private</span># ls cacert<span class="token punctuation">.</span>pem cakey<span class="token punctuation">.</span><span class="token function">pem</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert<span class="token operator">/</span>demoCA<span class="token operator">/</span><span class="token keyword">private</span># cd <span class="token operator">..</span> <span class="token operator">&&</span> mv <span class="token punctuation">.</span><span class="token operator">/</span><span class="token keyword">private</span><span class="token operator">/</span>cacert<span class="token punctuation">.</span>pem <span class="token punctuation">.</span><span class="token operator">/</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert<span class="token operator">/</span>demoCA# ls cacert<span class="token punctuation">.</span>pem index<span class="token punctuation">.</span>txt newcerts <span class="token keyword">private</span> serial <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>上面的命令执行了一系列的操作:</p> <p>A、在root用户的HOME目录/root下新建了/root/cert目录。</p> <p>B、然后在其下建立了自建CA的目录结构./demoCA,因为openssl默认的配置文件中,建在当前目录的./demoCA目录下。</p> <p>C、然后产生了CA的密钥cakey.pem。</p> <p>D、签发了CA的自签数字证书cacert.pem,然后移动到./demoCA目录下。后面自建CA签发服务器证书时会到那里找CA根证书,这是openssl默认的配置。</p> <p>E、最后列出了demoCA的目录结构。</p> <p>可以找出openssl默认的配置文件看一下,自建CA在当前目录的./demoCA目录下:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash">(gpu) root@VM-0-14-ubuntu:~# find / -name openssl.cnf /usr/lib/ssl/openssl.cnf /usr/local/anaconda3/pkgs/openssl-1.1.1q-h7f8727e_0/ssl/openssl.cnf /usr/local/anaconda3/ssl/openssl.cnf /usr/local/anaconda3/envs/gpu/ssl/openssl.cnf /usr/local/anaconda3/envs/hub/ssl/openssl.cnf /etc/ssl/openssl.cnf (gpu) root@VM-0-14-ubuntu:~# vi /usr/lib/ssl/openssl.cnf <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-php"><code class=" language-php"><span class="token shell-comment comment">####################################################################</span> <span class="token punctuation">[</span> ca <span class="token punctuation">]</span> default_ca <span class="token operator">=</span> CA_default <span class="token shell-comment comment"># The default ca section</span> <span class="token shell-comment comment">####################################################################</span> <span class="token punctuation">[</span> CA_default <span class="token punctuation">]</span> dir <span class="token operator">=</span> <span class="token punctuation">.</span><span class="token operator">/</span>demoCA <span class="token shell-comment comment"># Where everything is kept</span> certs <span class="token operator">=</span> <span class="token variable">$dir</span><span class="token operator">/</span>certs <span class="token shell-comment comment"># Where the issued certs are kept</span> crl_dir <span class="token operator">=</span> <span class="token variable">$dir</span><span class="token operator">/</span>crl <span class="token shell-comment comment"># Where the issued crl are kept</span> database <span class="token operator">=</span> <span class="token variable">$dir</span><span class="token operator">/</span>index<span class="token punctuation">.</span>txt <span class="token shell-comment comment"># database index file.</span> <span class="token shell-comment comment">#unique_subject = no # Set to 'no' to allow creation of</span> <span class="token shell-comment comment"># several certs with same subject.</span> new_certs_dir <span class="token operator">=</span> <span class="token variable">$dir</span><span class="token operator">/</span>newcerts <span class="token shell-comment comment"># default place for new certs.</span> certificate <span class="token operator">=</span> <span class="token variable">$dir</span><span class="token operator">/</span>cacert<span class="token punctuation">.</span>pem <span class="token shell-comment comment"># The CA certificate</span> serial <span class="token operator">=</span> <span class="token variable">$dir</span><span class="token operator">/</span>serial <span class="token shell-comment comment"># The current serial number</span> crlnumber <span class="token operator">=</span> <span class="token variable">$dir</span><span class="token operator">/</span>crlnumber <span class="token shell-comment comment"># the current crl number</span> <span class="token shell-comment comment"># must be commented out to leave a V1 CRL</span> crl <span class="token operator">=</span> <span class="token variable">$dir</span><span class="token operator">/</span>crl<span class="token punctuation">.</span>pem <span class="token shell-comment comment"># The current CRL</span> private_key <span class="token operator">=</span> <span class="token variable">$dir</span><span class="token operator">/</span><span class="token keyword">private</span><span class="token operator">/</span>cakey<span class="token punctuation">.</span>pem<span class="token shell-comment comment"># The private key</span> <span class="token constant">RANDFILE</span> <span class="token operator">=</span> <span class="token variable">$dir</span><span class="token operator">/</span><span class="token keyword">private</span><span class="token operator">/</span><span class="token punctuation">.</span>rand <span class="token shell-comment comment"># private random number file</span> x509_extensions <span class="token operator">=</span> usr_cert <span class="token shell-comment comment"># The extensions to add to the cert</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>F、生成服务器证书的密钥与证书请求。</p> <p>参考<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F63893662%2Fcant-load-root-rnd-into-rng" target="_blank">帖子1</a>与<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fnode-opcua%2Fnode-opcua-pki%2Fissues%2F7" target="_blank">帖子2</a>,要先执行下面的命令产生/root/.rnd文件,否则产生服务器密钥的命令会出错。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-csharp"><code class=" language-csharp">openssl rand <span class="token operator">-</span><span class="token keyword">out</span> <span class="token operator">/</span>root<span class="token operator">/</span><span class="token punctuation">.</span>rnd <span class="token operator">-</span>hex <span class="token number">256</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p> 切换到./demoCA的父目录/root/cert,然后执行下面的命令产生服务器证书的密钥与证书请求,产生证书请求用配置文件/usr/lib/ssl/openssl.cnf,额外增加了认证的主体别名,Chrome浏览器使用主体别名来检查证书的主体别名与网址是否一致。因为用<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fip" target="_blank">https://ip</a>访问,这里的主体别名为IP.1:106.52.33.185,表示是该证书认证的第一个IP,还可以有IP.2等等。如果是认证域名,可以是DNS.1 = <a href="https://links.jianshu.com/go?to=http%3A%2F%2Fjeanye.cn" target="_blank">jeanye.cn</a>等等,如此类推。产生证书请求文件server.csr。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-kotlin"><code class=" language-kotlin"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert# openssl genrsa <span class="token operator">-</span><span class="token keyword">out</span> server<span class="token punctuation">.</span>key <span class="token function">2048</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> <span class="token label symbol">root@</span>VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token operator">:</span>~<span class="token operator">/</span>cert# openssl req <span class="token operator">-</span>new \ <span class="token operator">></span> <span class="token operator">-</span>sha256 \ <span class="token operator">></span> <span class="token operator">-</span>key server<span class="token punctuation">.</span>key \ <span class="token operator">></span> <span class="token operator">-</span>subj <span class="token string">"/C=CN/ST=GD/L=ZhuHai/O=Jean/OU=Study/CN=106.52.33.185"</span> \ <span class="token operator">></span> <span class="token operator">-</span>reqexts SAN \ <span class="token operator">></span> <span class="token operator">-</span>config <span class="token operator"><</span><span class="token punctuation">(</span>cat <span class="token operator">/</span>usr<span class="token operator">/</span>lib<span class="token operator">/</span>ssl<span class="token operator">/</span>openssl<span class="token punctuation">.</span>cnf \ <span class="token operator">></span> <span class="token operator"><</span><span class="token punctuation">(</span>printf <span class="token string">"[SAN]\nsubjectAltName=IP.1:106.52.33.185"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> \ <span class="token operator">></span> <span class="token operator">-</span><span class="token keyword">out</span> server<span class="token punctuation">.</span>csr <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>G、签署服务器证书。</p> <p> openssl会在默认子目录./demoCA中找到cakey.pem与cacert.pem,按照证书请求文件server.csr的请求,使用配置文件/usr/lib/ssl/openssl.cnf,以及与请求一样的证书扩展(主体别名)签署证书,输出成server.crt。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-csharp"><code class=" language-csharp"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root@VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token operator">/</span>cert# openssl ca <span class="token operator">-</span><span class="token keyword">in</span> server<span class="token punctuation">.</span>csr \ <span class="token operator">></span> <span class="token operator">-</span>md sha256 \ <span class="token operator">></span> <span class="token operator">-</span>extensions SAN \ <span class="token operator">></span> <span class="token operator">-</span>config <span class="token operator"><</span><span class="token punctuation">(</span>cat <span class="token operator">/</span>usr<span class="token operator">/</span>lib<span class="token operator">/</span>ssl<span class="token operator">/</span>openssl<span class="token punctuation">.</span>cnf \ <span class="token operator">></span> <span class="token operator"><</span><span class="token punctuation">(</span>printf <span class="token string">"[SAN]\nsubjectAltName=IP.1:106.52.33.185"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> \ <span class="token operator">></span> <span class="token operator">-</span><span class="token keyword">out</span> server<span class="token punctuation">.</span>crt <span class="token class-name">Using</span> configuration <span class="token keyword">from</span> <span class="token operator">/</span>dev<span class="token operator">/</span>fd<span class="token operator">/</span><span class="token number">63</span> <span class="token class-name">Check</span> that the request matches the signature <span class="token class-name">Signature</span> ok <span class="token class-name">Certificate</span> Details<span class="token punctuation">:</span> <span class="token class-name">Serial</span> Number<span class="token punctuation">:</span> <span class="token number">1</span> <span class="token punctuation">(</span><span class="token number">0x1</span><span class="token punctuation">)</span> <span class="token class-name">Validity</span> <span class="token class-name">Not</span> Before<span class="token punctuation">:</span> <span class="token class-name">Nov</span> <span class="token number">2</span> <span class="token number">09</span><span class="token punctuation">:</span><span class="token number">47</span><span class="token punctuation">:</span><span class="token number">58</span> <span class="token number">2022</span> <span class="token class-name">GMT</span> <span class="token class-name">Not</span> After <span class="token punctuation">:</span> <span class="token class-name">Nov</span> <span class="token number">2</span> <span class="token number">09</span><span class="token punctuation">:</span><span class="token number">47</span><span class="token punctuation">:</span><span class="token number">58</span> <span class="token number">2023</span> <span class="token class-name">GMT</span> Subject<span class="token punctuation">:</span> countryName <span class="token operator">=</span> <span class="token class-name">CN</span> stateOrProvinceName <span class="token operator">=</span> <span class="token class-name">GD</span> organizationName <span class="token operator">=</span> <span class="token class-name">Jean</span> organizationalUnitName <span class="token operator">=</span> <span class="token class-name">Study</span> commonName <span class="token operator">=</span> <span class="token number">106.52</span><span class="token number">.33</span><span class="token number">.185</span> <span class="token class-name">X509v3</span> extensions<span class="token punctuation">:</span> <span class="token class-name">X509v3</span> <span class="token class-name">Subject</span> <span class="token class-name">Alternative</span> Name<span class="token punctuation">:</span> <span class="token class-name">IP</span> Address<span class="token punctuation">:</span><span class="token number">106.52</span><span class="token number">.33</span><span class="token number">.185</span> <span class="token class-name">Certificate</span> <span class="token keyword">is</span> to be certified until <span class="token class-name">Nov</span> <span class="token number">2</span> <span class="token number">09</span><span class="token punctuation">:</span><span class="token number">47</span><span class="token punctuation">:</span><span class="token number">58</span> <span class="token number">2023</span> GMT <span class="token punctuation">(</span><span class="token number">365</span> days<span class="token punctuation">)</span> <span class="token class-name">Sign</span> the certificate<span class="token punctuation">?</span> <span class="token punctuation">[</span>y<span class="token operator">/</span>n<span class="token punctuation">]</span><span class="token punctuation">:</span>y <span class="token number">1</span> <span class="token keyword">out</span> of <span class="token number">1</span> certificate requests certified<span class="token punctuation">,</span> commit<span class="token punctuation">?</span> <span class="token punctuation">[</span>y<span class="token operator">/</span>n<span class="token punctuation">]</span>y <span class="token class-name">Write</span> <span class="token keyword">out</span> database with <span class="token number">1</span> <span class="token keyword">new</span> entries <span class="token class-name">Data</span> <span class="token class-name">Base</span> Updated <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root@VM<span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token operator">/</span>cert# ls demoCA server<span class="token punctuation">.</span>crt server<span class="token punctuation">.</span>csr server<span class="token punctuation">.</span>key <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>H、自建CA根证书导入浏览器。</p> <p> 把自建CA的根证书/root/cert/demoCA/cacert.pem下载到客户端(比如Win10),在浏览器(比如Chrome)中导入到受信任根证书颁证机构中。</p> <p>Google浏览器:</p> <p> 设置->隐私设置和安全性->安全->高级->管理证书->受信任根证书颁证机构->导入->下一步->浏览->所有文件(*.*)</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 394px;"> <div class="image-container-fill" style="padding-bottom: 56.25%;"></div> <div class="image-view" data-width="1920" data-height="1080"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-34bdb975893b50ea.PNG" data-original-width="1920" data-original-height="1080" data-original-format="image/png" data-original-filesize="264024" data-image-index="17" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">导入自建CA根证书到浏览器受信任根证书颁发机构列表</div> </div> <br> <p>I、浏览器中输入网址https://106.52.33.185:8000访问,输入用户名/密码登录。</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 394px;"> <div class="image-container-fill" style="padding-bottom: 56.25%;"></div> <div class="image-view" data-width="1920" data-height="1080"><img referrerpolicy="no-referrer" data-original-src="//upload-images.jianshu.io/upload_images/28576403-dd0b3eaed1b5905c.png" data-original-width="1920" data-original-height="1080" data-original-format="image/png" data-original-filesize="153563" data-image-index="18" style="cursor: zoom-in;" class="image-loading"></div> </div> <div class="image-caption">输入用户名/密码登录,启动自己的Jupyter Lab实例</div> </div> <p>8、配置JupyterHub为开机自启动服务。</p> <p>1)建立服务配置文件。</p> <p>先看看conda虚拟环境"gpu"的PATH设置:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-tsx"><code class=" language-tsx"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root@<span class="token constant">VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span># echo $<span class="token constant">PATH</span> <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>anaconda3<span class="token operator">/</span>envs<span class="token operator">/</span>gpu<span class="token operator">/</span>bin<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>anaconda3<span class="token operator">/</span>condabin<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>cuda<span class="token operator">-</span><span class="token number">11.2</span><span class="token operator">/</span>bin<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>sbin<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>bin<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>sbin<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>bin<span class="token punctuation">:</span><span class="token operator">/</span>sbin<span class="token punctuation">:</span><span class="token operator">/</span>bin<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>games<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>games<span class="token punctuation">:</span><span class="token operator">/</span>snap<span class="token operator">/</span><span class="token function">bin</span> <span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root@<span class="token constant">VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span># <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre> </div> <p>然后新建一个系统守护进程的配置文件:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># vi /etc/systemd/system/jupyterhub.service</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>内容如下,几个要点。</p> <p>A、以root运行。</p> <p>B、设定PATH路径,因为开机启动进程没有登录的过程,不会执行/etc/profile等设置环境变量,把上面的PATH拷进去。</p> <p>C、用全路径引用执行jupyterhub。</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">[</span><span class="token constant">Unit</span><span class="token punctuation">]</span> <span class="token constant">Description</span><span class="token operator">=</span><span class="token constant">Jupyterhub</span> service <span class="token constant">After</span><span class="token operator">=</span>syslog<span class="token punctuation">.</span>target network<span class="token punctuation">.</span>target <span class="token punctuation">[</span><span class="token constant">Service</span><span class="token punctuation">]</span> <span class="token constant">User</span><span class="token operator">=</span>root <span class="token constant">Environment</span><span class="token operator">=</span><span class="token string">"PATH=/usr/local/anaconda3/envs/gpu/bin:/usr/local/anaconda3/condabin:/usr/local/cuda-11.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"</span> <span class="token constant">ExecStart</span><span class="token operator">=</span><span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>anaconda3<span class="token operator">/</span>envs<span class="token operator">/</span>gpu<span class="token operator">/</span>bin<span class="token operator">/</span>jupyterhub <span class="token operator">-</span>f <span class="token operator">/</span>etc<span class="token operator">/</span>jupyterhub<span class="token operator">/</span>config<span class="token punctuation">.</span>py <span class="token punctuation">[</span><span class="token constant">Install</span><span class="token punctuation">]</span> <span class="token constant">WantedBy</span><span class="token operator">=</span>multi<span class="token operator">-</span>user<span class="token punctuation">.</span>target <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre> </div> <p>然后让服务配置文件生效:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># systemctl enable jupyterhub.service</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>然后可以用下面几个命令来管理服务:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-bash"><code class=" language-bash"># systemctl status jupyterhub.service # systemctl start jupyterhub.service # systemctl stop jupyterhub.service <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre> </div> <p>用下面的命令来查看服务的日志:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-ruby"><code class=" language-ruby"><span class="token punctuation">(</span>gpu<span class="token punctuation">)</span> root<span class="token variable">@VM</span><span class="token operator">-</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">14</span><span class="token operator">-</span>ubuntu<span class="token punctuation">:</span><span class="token operator">~</span><span class="token comment"># journalctl -u jupyterhub.service -f</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>上面Jupyterhub的配置文件中,日志也另外输出到以下的文件:</p> <div class="_2Uzcx_"> <button class="VJbwyy" type="button" aria-label="复制代码"><i aria-label="icon: copy" class="anticon anticon-copy"><svg viewbox="64 64 896 896" focusable="false" class="" data-icon="copy" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32zM704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg></i></button><pre class="line-numbers language-swift"><code class=" language-swift">c<span class="token punctuation">.</span><span class="token builtin">JupyterHub</span><span class="token punctuation">.</span>extra_log_file <span class="token operator">=</span> <span class="token string">'/var/log/jupyterhub.log'</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre> </div> <p>所以也可以打开日志文件来看。</p> <p>这样,每次服务器重启,Jupyterhub都会自动启动了。</p> <p>本篇到此结束,Linux GPU虚拟主机与GPU、Python深度学习运行与开发环境相关的部分就配好了,Rstudio、Shiny等其它部分另起文章。</p> </article>
GPU Linux虚拟主机GN7型安装配置文档 定稿
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-11-15
修改于
2024-11-12
上一篇:软件:常用 Linux 软件汇总,值得收藏
下一篇:Llama2开源大模型的新篇章以及在阿里云的实践