Anaconda+JupyterNotebook+Venv+GPU配置

服务配置

  1. 运行配置。
  • 生成配置jupyter_notebook_config.py文件。
    1
    jupyter notebook --generate-config
  • 开启一个ipython窗口,生成密码哈希值
    1
    2
    from notebook.auth import passwd
    passwd()
  • 访问生成的配置文件,修改配置文件
    1
    2
    3
    4
    5
    c.NotebookApp.ip = '0.0.0.0'
    c.NotebookApp.port = 26000
    c.NotebookApp.open_browser = False
    c.NotebookApp.password = '【上一步生成的密码哈希值】'
    c.NotebookApp.notebook_dir = '【希望的启动目录】'
  1. 编写启动脚本rjupyter(可添加在环境变量),具有不重复启动、无多余日志功能。
    1
    2
    3
    4
    if test $( netstat -nltp | grep 26000 | wc -l ) -eq 0
    then
    nohup jupyter notebook >/dev/null 2>&1 &
    fi

依赖源

  1. 添加镜像源
    1
    2
    3
    4
    5
    conda 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
  2. (可选)更新依赖
    1
    conda upgrade --all

内核切换

  1. 安装包nb_conda,重新启动jupyter
    1
    conda install nb_conda
  2. 解决根目录重复导致的jupyter-conda页报错:修改【Anaconda3安装目录】/pkgs/【nb_conda-2.2.1-py39hf3d152e_4】/lib/【python3.9】/site-packages/nb_conda/envmanager.py文件,其中【】是可变的需要参考本机目录。
  • 原文:
    1
    2
    3
    return {
    "environments": [root_env] + [get_info(env) for env in info['envs']]
    }
  • 修改后:
    1
    2
    3
    return {
    "environments": [root_env] + [get_info(env) for env in info['envs'] if env != root_env['dir']]
    }
  1. LinuxShell中查看已有虚拟环境.
    1
    conda info -e
  2. LinuxShell中查看切换虚拟环境.
    1
    conda activate 【要切换的虚拟环境名】

将自己创建的虚拟环境添加到Jupyter

  1. 创建虚拟环境。
    1
    conda create -n 虚拟环境名 python=3.6
  2. 在此虚拟环境中安装内核。
    1
    2
    3
    conda activate 虚拟环境名
    conda install ipykernel
    conda install nb_conda
  3. jupyterConda中可见虚拟环境,若启动失败,则需要检查pip list和可用环境中的是否有依赖版本差异,着重检查跟jupyter有关的内容。

Pytroch+GPU

  1. 使用conda源安装pytorch,它将会自动帮助我们匹配安装cudatoolkit
    1
    conda install pytorch
  2. 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
    28
    import 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

  1. 通过以下命令安装jupyter和启用扩展配置。
    1
    2
    3
    4
    pip install jupyter_contrib_nbextensions
    jupyter contrib nbextension install --user
    pip install jupyter_nbextensions_configurator
    jupyter nbextensions_configurator enable --user
  2. 根据需要在Nbextensions开启扩展。
  • Table of Contents (2) 笔记本目录
  • Collapsible Headings 笔记本目录折叠
  • Codefolding 代码折叠
  • Hinterland 代码编辑提示

Anaconda+JupyterNotebook+Venv+GPU配置
https://zhongshijie1995.github.io/posts/30004/
作者
钟世杰
发布于
2022年3月29日
许可协议
BY BY-SA