Anaconda+JupyterLab+Venv+GPU配置
服务配置
- 运行配置。
- 生成配置
jupyter_lab_config.py
文件。1
jupyter lab --generate-config
- 开启一个
ipython
窗口,生成密码哈希值1
2from notebook.auth import passwd
passwd() - 访问生成的配置文件,目录为
用户目录/.jupyter/jupyter_lab_config.py
,修改配置文件1
2
3
4
5c.ServerApp.ip = '0.0.0.0'
c.ServerApp.port = 26000
c.ServerApp.open_browser = False
c.ServerApp.password = '【上一步生成的密码哈希值】'
c.ServerApp.notebook_dir = '【希望的启动目录】'
- 编写启动脚本
rjupyter
(可添加在环境变量),具有不重复启动、无多余日志功能。1
2
3
4if test $( netstat -nltp | grep 26000 | wc -l ) -eq 0
then
nohup jupyter lab >/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
内核切换
将自己创建的虚拟环境添加到Jupyter
- 安装内核自动管理工具
1
conda install nb_conda_kernels
- 创建虚拟环境。
1
conda create -n 虚拟环境名 python=3.6 ipykernel
- 在JupyterLab中选择虚拟环境名称对应的内核即可。
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)))
安装插件
- 简体中文界面
1
pip install jupyterlab-language-pack-zh-CN
Anaconda+JupyterLab+Venv+GPU配置
https://zhongshijie1995.github.io/posts/30006/