一. pytorch安装

1、查看CUDA版本

安装pytorch之前需要确定集群的CUDA版本,根据CUDACUDA版本去pytorch官网上选择合适的版本,根据提供的conda命令行安装即可

查看CUDA版本的方式

使用nvidia-smi查询驱动版本,如图所示:

python使用node js nodejs pytorch_linux

2.pytorch安装

去pytorch网址选择合适的版本,然后使用conda安装即可:

Start Locally | PyTorch

python使用node js nodejs pytorch_jupyter_02

二、jupyter notebook安装及使用(linux安装浏览器版本)

编写python脚本时,使用vim直接编写会存在许多不方便之处,推荐使用jupyter notebook进行python脚本编写,暂时用到以下优点:

1)可tab补全命令;

2)可直接选择合适的环境;

3)可选择性执行某几行命令;

4)在脚本运行时可继续编写脚本;

5)对长脚本选择性隐藏;

6)可在浏览器中打开

1.jupyter notebook的安装

使用conda安装jupyter notebook即可

2.jupyter notebook安装时遇到的问题:

遇到了无法浏览器跳转页面的问题

参考: 解决

备注:此方法为在linux集群上安装firefox浏览器,调用的为集群上安装浏览器,实际使用时很卡,最终选择将集群的jupyter 映射到本地进行操作,详细内容可见后面3部分描述。

第一步:在linux安装浏览器,安装的为火狐浏览器
tar -xvjf Firefox-latest-x86_64.tar.bz2
然后将相应的文件ln到bin下方便直接调用
ln -s /path/01.Software/firefox/firefox /path/bin
生成jupyter_notebook_config.py文件
第二步:
使用命令 jupyter notebook --generate-config,生成 jupyter_notebook_config.py文件,附带该文件的路径。
(base) [xx@xxx ~]$ jupyter notebook --generate-config
Writing default config to: /home/xx/.jupyter/jupyter_notebook_config.py
在/home/xx/.jupyter/jupyter_notebook_config.py文件中写入:
在# c.NotebookApp.password = ''后面写入:
import webbrowser
webbrowser.register('firefox',None, webbrowser.GenericBrowser("/path/firefox/firefox"))
c.NotebookApp.browser = 'firefox'

第三步:然后再执行jupyter notebook即可

3.最终选择的将jupyternotebook映射到本地浏览器的方法说明

参考:

将jupyter映射到本地浏览器操作的方式( 本地访问Linux服务器上的jupyter notebook_机器不爱学习-CSDN博客)(最终参考使用的这个版本)

第一步:获取ipython密码并记住
(pytorch) [xx@xxx ~]$ ipython
Python 3.7.11 (default, Jul 27 2021, 14:32:16)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.29.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'argon2:$argon2id$v=19$m=10240,t=10,p=8$A154/6xNq82MNG1DSFkuLA$fwulDNATERTRAGxMbRLbAUfA'  ##记住这个密码
第二步:更改/home/xx/.jupyter/jupyter_notebook_config.py 文件中相应的配置:
c.NotebookApp.password = u'argon2:$argon2id$v=19$m=10240,t=10,p=8$A154/6xNq82MNG1DSFkuLA$fwulDNATERTRAGxMbRLbAUfA'
c.NotebookApp.allow_remote_access = True
c.NotebookApp.open_browser = False
c.NotebookApp.port = 1234   #这个可以根据自己喜欢随便定义
第三步:开放防火墙端口(若没开的话,找集群管理员开放一下)
开通方式可参考:
)

第四步:使用jupyter notebook

在终端直接打jupyter notebook即可,粘贴http://1xx.xx.3.45:1234/到本地浏览器上使用即可。

4.根据喜好设置jupyter风格

通过命令行窗口或 Anaconda Prompt 窗口

1)安装 Jupyter 主题

pip install jupyterthemes

2)更新 Jupyter 主题 (可选)

pip install --upgrade jupyterthemes

3)查看可用的 Jupyter 主题

jt -l

4)更换 Jupyter 主题

jt -t chesterish  -f fira -fs 10 -cellw 90% -ofs 9 -dfs 9 -T -T

-t 主题 -f(字体)  -fs(字体大小) -cellw(占屏比或宽度)  -ofs(输出段的字号)  -T(显示工具栏)  -T(显示自己主机名)

5.设置jupyter 隐藏输入行

对于用Jupyter Notebook编写的python脚本,如果可以将较长的代码部分隐藏,只显示Markdown说明文本、和代码块的运行结果,便可将脚本直接用作动态分析报告。即在保持可读性的同时,可通过刷新原数据/调整代码即时生成新的报告内容。

实现显示/隐藏代码块的步骤:

1)安装jupyter_contrib_nbextentions

(Ref: https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/install.html#install-the-python-package)
conda安装命令:conda install -c conda-forge jupyter_contrib_nbextensions
python安装命令:pip install jupyter_contrib_nbextensions

2)安装jupyter_nbextentions_contributor,以便在Jupyter Notebook页面设置nbextentions

(Ref: https://github.com/Jupyter-contrib/jupyter_nbextensions_configurator)
conda安装命令:conda install -c conda-forge jupyter_nbextensions_configurator
python安装命令:pip install jupyter_nbextensions_configurator

3)安装完成后,点击Jupyter Notebook首页的Nbextensions标签,打开在Nbextensions configuration设置页面。

1)勾选Hide input(显示/隐藏选定代码块)

2)勾选Hide input all(显示/隐藏所有代码块)

参考:作者:xrdcc