实验中总是有奇奇怪怪的环境要求,本文用于记录创建环境的基本操作及后续遇到的一些问题,便于后续学习。
一、虚拟环境基本操作
创建虚拟环境即python版本:
conda create -n xxxx python=3.X
查看所有虚拟环境:
conda env list
激活虚拟环境:
activate xxxx
虚拟环境安装包:(pip安装可能与conda安装方式有区别,最好不要混用)
conda install xxxx
虚拟环境查看已安装的包:
conda list
虚拟环境设置镜像源:
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes
pip config set global.index-url http://pypi.tuna.tsinghua.edu.cn/simple
查看镜像源:
conda config --show-source
删除镜像源:
conda config --remove-key channels 删除全部源
conda config --remove channels 'http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/'
删除虚拟环境:
conda remove -n xxxx --all
二、在虚拟环境中安装不同版本的cuda、cudnn、pytorch、tensorflow
一定要检查安装的版本是否匹配!!!如果有问题,建议从创建虚拟环境开始重新安装一遍。
1.tensorflow
先去官网查看自己所需版本:在 Windows 环境中从源代码构建 | TensorFlow (google.cn)
再看本机显卡最高支持的cuda版本:win+r+cmd+输入命令nvidia-smi
假设我这里需要tensorflow1.15,那么我需要cuda版本就是10,cudnn版本是7.4
按顺序执行以下命令:
conda install tensorflow==1.15
conda install cudatoolkit=10
conda install cudnn
2.pytorch
pytorch就比较简单了,去官网:Start Locally | PyTorch找自己所需的版本,官网那就有安装的命令了:
但要注意cuda版本匹配问题。
三、模型训练中遇到的问题
由于我习惯用pytorch,这里遇到的问题基本都发生在pytorch中:(虽然不太可能,但实验前先检查数据:各数组规模是否匹配,数据标签是否一一对应数据)
Dataloader中使用num workers会卡死
原因不明,在jupyter中没有报错,只能重启内核;去掉这个参数就行。
模型训练中准确率不上升
检查学习率是否过小,batch size过小,花费时间多,同时梯度震荡严重,不利于收敛;batch size过大,不同batch的梯度方向没有任何变化,容易陷入局部极小值。
*报错:
1.PackagesNotFoundError: The following packages are not available from current channels
将conda-forge channel添加到你的channel列表中:
conda config --append channels conda-forge
报错原因大致分为:网络、设置问题、环境不对。实在安装不了包就用pip吧,pip有冲突就等一会从头开始,实测比找原因省时间。
参考:
完美解决(最简单):不同虚拟环境不同CUDA版本的安装问题(tensorflow-gpu与pytorch)_如何在conda不同虚拟环境中装不一样的cuda和cudann-CSDN博客