Conda和Jupiter是服务器研发算法必备。
安装Conda
miniconda可以首次安装,也可以复用环境。
首次安装
安装miniconda,其它版本miniconda或者anaconda均可,命令如下:
bash /nfs/user/files/Miniconda3-py38_4.10.3-Linux-x86_64.sh
实际下载地址:https://repo.anaconda.com/miniconda/,约98.8M
修改路径,默认即可:
[/home/user/miniconda3] >>>
注意:配置在共享区域,速度较慢,不推荐。
复用环境
复制.condarc 和.bashrc 至当前环境,TORCH_HOME 是torch模型的存储目录,如下:
cp /nfs/user/files/.condarc /home/user/
export PATH=$PATH:~/.local/bin
export TORCH_HOME=/nfs/user/workspace/torch_home/
其中,.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
激活conda:
source /home/user/.bashrc
创建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
配置pip源:
mkdir ~/.pip
vim ~/.pip/pip.conf
[global]
trusted-host = mirrors.aliyun.com
index-url = https://mirrors.aliyun.com/pypi/simple
安装pytorch,使用pip安装较慢,建议使用conda:
- 时间比较长,使用tmux持续窗口。
- 必须安装pytorch cuda 11版本,否则无法正常运行,具体cuda版本,参考nvidia-smi。
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
测试GPU是否可用,数据是否可用,参考,两个都需要测试。
ngpu= 1
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)
conda卸载
删除conda文件夹,删除.bashrc的启动命令,和删除配置文件,即可:
vim .bashrc
rm -rf ~/.condarc ~/.conda ~/.continuum
配置Jupiter
Jupyter安装
pip install ipython
pip install jupyter
配置Jupyter环境,参考After installing with pip, “jupyter: command not found”
vim ~/.bashrc
# shift+g跳转到文件尾部,gg跳转到文件头部
# jupyter
export PATH=$PATH:~/.local/bin
密码设置为123
ipython
from notebook.auth import passwd
passwd()
'argon2:$argon2id$v=19$m=10240,t=10,p=8$xxx'
exit
配置密码:
mkdir ~/.jupyter
vim ~/.jupyter/jupyter_notebook_config.py
c.NotebookApp.ip='*'
c.NotebookApp.password = u'argon2:$argon2id$v=19$m=10240,t=10,p=8$xxx'
c.NotebookApp.open_browser = False
c.NotebookApp.port =8889
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
进入工作环境,先尝试启动jupyter,再切换nohup模式启动:
cd /nfs/user
jupyter notebook --allow-root
nohup jupyter notebook --allow-root > nohup.jupyter-53.out &
ps aux | grep notebook
访问地址,需要替换为真实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"
其他jupyter的kernel环境命令:
jupyter kernelspec list
jupyter kernelspec remove torch-new
|