• 安装Conda和配置Jupiter


    Conda和Jupiter是服务器研发算法必备。

    安装Conda

    miniconda可以首次安装,也可以复用环境。

    首次安装

    安装miniconda,其它版本miniconda或者anaconda均可,命令如下:

    bash /nfs/user/files/Miniconda3-py38_4.10.3-Linux-x86_64.sh
    
    • 1

    实际下载地址:https://repo.anaconda.com/miniconda/,约98.8M

    修改路径,默认即可:

    [/home/user/miniconda3] >>> 
    
    • 1

    注意:配置在共享区域,速度较慢,不推荐。

    复用环境

    复制.condarc.bashrc至当前环境,TORCH_HOME是torch模型的存储目录,如下:

    cp /nfs/user/files/.condarc /home/user/
    
    # 添加配置命令
    # my configs
    export PATH=$PATH:~/.local/bin
    export TORCH_HOME=/nfs/user/workspace/torch_home/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    其中,.condarc是conda的配置:

    channels:
      - defaults
    show_channel_urls: true
    default_channels:
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
    custom_channels:
      conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
    channel_priority: disabled
    allow_conda_downgrades: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    激活conda:

    source /home/user/.bashrc
    
    • 1

    创建conda环境,--clone可选,建议使用tmux,建议备份环境:

    tmux new -s conda-init
    conda create -n torch-new --clone /nfs/user/conda/envs/torch-new/  # 创建
    
    conda activate # 激活
    conda deactivate  # 不激活
    
    # 备份环境
    cp -r /home/user/miniconda3 /nfs/user/conda_env/miniconda3-jupyter-20220626
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    配置pip源:

    mkdir ~/.pip
    vim ~/.pip/pip.conf
    
    [global]
    trusted-host =  mirrors.aliyun.com
    index-url = https://mirrors.aliyun.com/pypi/simple
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    安装pytorch,使用pip安装较慢,建议使用conda:

    • 时间比较长,使用tmux持续窗口。
    • 必须安装pytorch cuda 11版本,否则无法正常运行,具体cuda版本,参考nvidia-smi。
    conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
    
    • 1

    测试GPU是否可用,数据是否可用,参考,两个都需要测试。

    # 测试环境
    ngpu= 1
    # Decide which device we want to run on
    device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
    print("驱动为:",device)
    print("GPU型号: ",torch.cuda.get_device_name(0))
    
    
    # 测试张量
    import 	torch
    import  time
    print(torch.__version__)
    print(torch.cuda.is_available())
    
    a = torch.randn(100, 10)
    b = torch.randn(10, 200)
    device = torch.device('cuda')
    a = a.to(device)
    b = b.to(device)
    c = torch.matmul(a, b)
    print(a.device, c.device)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    conda卸载

    删除conda文件夹,删除.bashrc的启动命令,和删除配置文件,即可:

    vim .bashrc
    rm -rf ~/.condarc ~/.conda ~/.continuum
    
    • 1
    • 2

    配置Jupiter

    Jupyter安装

    pip install ipython
    pip install jupyter
    
    • 1
    • 2

    配置Jupyter环境,参考After installing with pip, “jupyter: command not found”

    vim ~/.bashrc
    
    # shift+g跳转到文件尾部,gg跳转到文件头部
    
    # jupyter
    export PATH=$PATH:~/.local/bin
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    密码设置为123

    ipython
    from notebook.auth import passwd
    passwd()
    
    # 每次都不同
    'argon2:$argon2id$v=19$m=10240,t=10,p=8$xxx'
    
    exit
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    配置密码:

    mkdir ~/.jupyter
    vim ~/.jupyter/jupyter_notebook_config.py
    
    # 说明
    c.NotebookApp.ip='*' #设置访问notebook的ip,*表示所有IP,这里设置ip为都可访问  
    c.NotebookApp.password = u'argon2:$argon2id$v=19$m=10240,t=10,p=8$xxx' #填写刚刚生成的密文  
    c.NotebookApp.open_browser = False # 禁止notebook启动时自动打开浏览器(在linux服务器一般都是ssh命令行访问,没有图形界面的。所以,启动也没啥用)  
    c.NotebookApp.port =8889 #指定访问的端口,默认是8888。  
    
    # 实际,不能有空格或汉字
    c.NotebookApp.ip='*'
    c.NotebookApp.password=u'argon2:$argon2id$v=19$m=10240,t=10,p=8$6g3OBHTPXjv0nRqo6osQ/Q$tw34Z4uJeMfHJY+jHiqfg8oV7QimObuZnB67upVmNnM'
    c.NotebookApp.open_browser=False
    c.NotebookApp.port=8889
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    进入工作环境,先尝试启动jupyter,再切换nohup模式启动:

    cd /nfs/user
    
    jupyter notebook --allow-root
    nohup jupyter notebook --allow-root > nohup.jupyter-53.out &
    
    ps aux | grep notebook  # 查看是否启动
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    访问地址,需要替换为真实IP地址,使用ifconfig查询eth0,例如:http://172.30.0.53:8889/,密码123

    配置conda环境到jupyter,参考jupyter notebook 如何配置conda环境:

    python -m ipykernel install --user --name torch-new --display-name "torch-new"
    
    • 1

    其他jupyter的kernel环境命令:

    jupyter kernelspec list  # 显示所用kernel
    jupyter kernelspec remove torch-new  # 删除kernel
    
    • 1
    • 2
  • 相关阅读:
    【论文笔记】基于深度学习的移动机器人自主导航实验平台
    蛤蟆先生去看心理医生笔记
    Macos数据库管理:Navicat Premium 中文
    【机器学习基础】多元线性回归(适合初学者的保姆级文章)
    springBoot入门
    Spring、Spring MVC、Spring boot、Spring Cloud面试题(史上最全面试题,精心整理100家互联网企业,面试必过)
    Iptables匹配条件 - 示例1
    如何在机器学习中使用数据集编程
    2023年 图表示学习、知识图谱相关SCI专刊与会议整理
    硬核Fiddler抓包工具大型攻略(二)Fiddler进阶篇
  • 原文地址:https://blog.csdn.net/u012515223/article/details/125471196