Anaconda+JupyterNotebook+Venv+GPU配置
服务配置
- 运行配置。
- 生成配置
jupyter_notebook_config.py
文件。1
jupyter notebook --generate-config
- 开启一个
ipython
窗口,生成密码哈希值1
2from notebook.auth import passwd
passwd() - 访问生成的配置文件,修改配置文件
1
2
3
4
5c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.port = 26000
c.NotebookApp.open_browser = False
c.NotebookApp.password = '【上一步生成的密码哈希值】'
c.NotebookApp.notebook_dir = '【希望的启动目录】'
- 编写启动脚本
rjupyter
(可添加在环境变量),具有不重复启动、无多余日志功能。1
2
3
4if test $( netstat -nltp | grep 26000 | wc -l ) -eq 0
then
nohup jupyter notebook >/dev/null 2>&1 &
fi
依赖源
- 添加镜像源
1
2
3
4
5conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes - (可选)更新依赖
1
conda upgrade --all
内核切换
- 安装包
nb_conda
,重新启动jupyter
1
conda install nb_conda
- 解决根目录重复导致的
jupyter-conda
页报错:修改【Anaconda3安装目录】/pkgs/【nb_conda-2.2.1-py39hf3d152e_4】/lib/【python3.9】/site-packages/nb_conda/envmanager.py
文件,其中【】
是可变的需要参考本机目录。
- 原文:
1
2
3return {
"environments": [root_env] + [get_info(env) for env in info['envs']]
} - 修改后:
1
2
3return {
"environments": [root_env] + [get_info(env) for env in info['envs'] if env != root_env['dir']]
}
- 在
LinuxShell
中查看已有虚拟环境.1
conda info -e
- 在
LinuxShell
中查看切换虚拟环境.1
conda activate 【要切换的虚拟环境名】
将自己创建的虚拟环境添加到Jupyter
- 创建虚拟环境。
1
conda create -n 虚拟环境名 python=3.6
- 在此虚拟环境中安装内核。
1
2
3conda activate 虚拟环境名
conda install ipykernel
conda install nb_conda - 在
jupyter
的Conda
中可见虚拟环境,若启动失败,则需要检查pip list
和可用环境中的是否有依赖版本差异,着重检查跟jupyter
有关的内容。
Pytroch+GPU
- 使用
conda
源安装pytorch
,它将会自动帮助我们匹配安装cudatoolkit
。1
conda install pytorch
- 在
Jupyter
中运行如下代码,进行GPU加速测试:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28import torch
import time
print('pytorch版本[%s],CUDA是否可用[%s]' % (torch.__version__, torch.cuda.is_available())) # 返回pytorch的版本
a = torch.randn(10000, 1000) # 返回10000行1000列的张量矩阵
b = torch.randn(1000, 2000) # 返回1000行2000列的张量矩阵
# 直接CPU计算
t0 = time.time()
c = torch.matmul(a, b)
t1 = time.time()
print('[%s]运行时间[%s],运行结果[%s]' % (a.device, t1 - t0, c.norm(2)))
# 指定GPU计算(数据未压入CUDA,包含初始化)
device = torch.device('cuda')
a = a.to(device)
b = b.to(device)
t0 = time.time()
c = torch.matmul(a, b)
t2 = time.time()
print('[%s]运行时间[%s](含CUDA初始化),运行结果[%s]' % (a.device, t2 - t0, c.norm(2)))
# 指定GPU计算(数据已压入CUDA,不含初始化,仅计算时间)
t0 = time.time()
c = torch.matmul(a, b)
t3 = time.time()
print('[%s]运行时间[%s](不含CUDA初始化),运行结果[%s]' % (a.device, t3 - t0, c.norm(2)))
jupyter_contrib_nbextensions
- 通过以下命令安装
jupyter
和启用扩展配置。1
2
3
4pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user - 根据需要在
Nbextensions
开启扩展。
Table of Contents (2)
笔记本目录Collapsible Headings
笔记本目录折叠Codefolding
代码折叠Hinterland
代码编辑提示
Anaconda+JupyterNotebook+Venv+GPU配置
https://zhongshijie1995.github.io/posts/30004/