保姆级教程:深度学习环境配置指南!(Windows、Mac、Ubuntu全讲解)
极市平台
共 9263字,需浏览 19分钟
·
2021-01-21 22:10
![](https://filescdn.proginn.com/06e0fe8edbb87a708179148ccd2ac806/a4ba8e3188becb92d8705109dccbee66.webp)
极市导读
俗话说,环境配不对,学习两行泪。本文为保姆级别的教程,详细介绍了Windows、Mac和Ubuntu的深度学习环境配置方法及问题,帮助卡在环境配置的小伙伴们,解决入门难关。 >>加入极市CV技术交流群,走在计算机视觉的最前沿
一、Windows系统深度学习环境配置
1.1 打开Anaconda Prompt
![](https://filescdn.proginn.com/9bbe456c463c9999123a9b7c6f2d4cee/d6632f5715f7314a163004ae7baa8e8b.webp)
![](https://filescdn.proginn.com/6d480c77434a152397a398084e7c39c5/83750e5f658aca037f1ad9acc3243d8f.webp)
![](https://filescdn.proginn.com/b2cfac9df16ee06aa21698c1171fde51/54c5dbd38935052558bacbc0f4c3c952.webp)
![](https://filescdn.proginn.com/ac759c34250dec7d1c5a3f58c1b5eb07/cd1772eb6789efc22afc2accb9fcf5b7.webp)
from future import print_function
import torch
x = torch.rand(5, 3)
print(x)
![](https://filescdn.proginn.com/a2526e36a04446cf83386f9130bc9dec/9215921e2ea3b928c004ddd06e4e1a84.webp)
import torch
torch.cuda.is_available()
用下面命令创建虚拟环境报错
conda create -n py37_torch131 python=3.7
![](https://filescdn.proginn.com/ac8fdd6d5c3d7f1e3c34a9dc60e99105/f67fcdde50d067d53d63e99d4c5149ab.webp)
PackagesNotFoundError: The following packages are not available from current channels
![](https://filescdn.proginn.com/0d5177524d4e489145d2bf94d1f542ce/0eeeaa34050af6dbd574cc0580532b92.webp)
conda下载太慢问题
cuda安装
pytorch安装
conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
jupyter note如何进入虚拟环境
python -m ipykernel install --user --name py27-caffe-notebook
jupyter kernelspec list
jupyter kernelspec remove kernelname
python -m ipykernel install --name kernelname
conda安装一半总失败
conda install --offline 包名
二、Mac深度学习环境配置
2.1 安装Anaconda
![](https://filescdn.proginn.com/cbdb390bed4acacc99042edc9a0f52bc/6ccaa98597152b6983c0a147b8abc74f.webp)
2.2 确认下载情况
![](https://filescdn.proginn.com/487a6ca743130f0743ba254fecddaa40/76e87069dd7e913b1fe147ce281209f9.webp)
python --version
jupyter notebook
![](https://filescdn.proginn.com/dc4c77006c18fd5bb323868ddb7f5a97/5141d52be0f58186f43dcaf2e708a408.webp)
![](https://filescdn.proginn.com/ce5e56e9394bbbfa962e2d6c1b5565fd/eb838186dff057e503bb983be80bc1c4.webp)
2.2.1 常见问题
cd ~
cat .bash_profile
export PATH= ...
2.3 虚拟环境和包的下载
![](https://filescdn.proginn.com/71a4cec9cede789cef8eab73ccb05914/54abffefeb5f2986b359c95144321714.webp)
![](https://filescdn.proginn.com/b0b75a5b97c8248ec514d66099374b23/940cf163bb0f127524dcdf5c3553ddf9.webp)
![](https://filescdn.proginn.com/1320ab8f9e16846b258510920055d9eb/456cf27e07ceaaf22c93160f348959e7.webp)
![](https://filescdn.proginn.com/ac73a0f675c4e0d8a16d22f506eb059b/bc996ff4698f1914f10b925a6f52ae24.webp)
conda create --name env_name
conda env list
conda activate env_name
conda deactivate
![](https://filescdn.proginn.com/c50099d4669c905c07685eb4d21b2571/cc895358580643edb310ad3df2618ec4.webp)
conda install pytorch torchvision -c pytorch
python3
import torch
import torchvision
print(torch.__version__)
print(torchvision.__version__)
![](https://filescdn.proginn.com/66e068ef18a87714d35e1bb57536acba/6e6720a17d153e5a2ce5b91327b47dd5.webp)
quit()
三、Ubuntu深度学习环境配置
![](https://filescdn.proginn.com/17aed18ea9a73ac6e26b1a370a691305/2e10d6b4bfa83bc41cebf129fc99e3b7.webp)
![](https://filescdn.proginn.com/8eae6daf60982da0b2aa9b2342a7fe3f/7934129a181f64a676704dcd0967f37e.webp)
![](https://filescdn.proginn.com/141275da333d670b2b7508d9ed1c8a75/8811b7adc6bbc64ec4fbd0ba47b668bf.webp)
![](https://filescdn.proginn.com/24198e88643eb260a5f0b8a73509c90b/567c6e98864d4f8707b21143138e9ed3.webp)
![](https://filescdn.proginn.com/982b877f65ed962fe39b490155d66ce1/6313a7304f1f704afff5b3e0939cb28b.webp)
![](https://filescdn.proginn.com/b3c62bf13b2bee226bde2733f19635fc/21392c93c1b465039c26468a49bb43cb.webp)
# 区分anaconda python与系统内置python
alias python3="/usr/bin/python3.5"
alias python2="/usr/bin/python2.7"
. /home/cxd/anaconda3/etc/profile.d/conda.sh
![](https://filescdn.proginn.com/68ffc07bd8d4d3109663217b6cb52aa3/efb0a20cb98b372d5b49e92856864c2c.webp)
![](https://filescdn.proginn.com/97e88d5079c019d2de6dc41170cc8010/cc426fff7fa66a8ceda3528ca33766c7.webp)
![](https://filescdn.proginn.com/e4ba878d92ba854426661bc878424d30/abb6eacdd3533936ac5d46408c577d08.webp)
![](https://filescdn.proginn.com/2d42ea81659c88432b4c23b8c7dcf3fc/444cacc2ddc3e5c02e768a9da4affff2.webp)
![](https://filescdn.proginn.com/163151daa9582f9bd29c8469cdd9ba6d/9e5afd2fac6b0768c3dbcac7e4e55a22.webp)
![](https://filescdn.proginn.com/7544cd0bf71feade937fe8125b85aa17/f57517a1e865b5cbb3695ad5d4453b25.webp)
![](https://filescdn.proginn.com/146f08d902cd0bdf7be326841d21549b/91dea4bbbdf83669c55c084b1186b7f2.webp)
![](https://filescdn.proginn.com/b55b14209c936c3b9c03fbbb4ef8d884/07d82a2d7eb3f51e4cbced3142ca19c7.webp)
3.2 pytorch cpu版本安装
![](https://filescdn.proginn.com/8c1d2abde96c0c45741bc868594ad5f9/7b07c72e0047815bfbbf0c8cb840dbff.webp)
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
conda install pytorch torchvision cpuonly -c pytorch
![](https://filescdn.proginn.com/f3626ba23d888349ec905e53210e1531/0396e9e262f2455411ce411b86db18ad.webp)
import torch
print(torch.__version__)
![](https://filescdn.proginn.com/6b9bb2662e198db3890d8dcd0e924d17/00d456e3aabc2f7272132e79674cb0e8.webp)
3.3 pytorch-gpu安装
3.3.1 GPU驱动安装
检测显卡类型
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00001C8Dsv00001028sd0000086Fbc03sc02i00
vendor : NVIDIA Corporation
model : GP107M [GeForce GTX 1050 Mobile]
driver : nvidia-driver-390 - distro non-free
driver : nvidia-driver-435 - distro non-free
driver : nvidia-driver-440 - distro non-free recommended
driver : xserver-xorg-video-nouveau - distro free builtin
== /sys/devices/pci0000:00/0000:00:14.3 ==
modalias : pci:v00008086d0000A370sv00008086sd000042A4bc02sc80i00
vendor : Intel Corporation
model : Wireless-AC 9560 [Jefferson Peak]
manual_install: True
driver : backport-iwlwifi-dkms - distro free
安装驱动
sudo ubuntu-drivers autoinstall
sudo apt install nvidia-440
3.3.2 安装cuda
Table 1. CUDA Toolkit and Compatible Driver Versions
CUDA Toolkit Linux x86_64 Driver Version Windows x86_64 Driver Version
CUDA 10.2.89 >= 440.33 >= 441.22
CUDA 10.1 (10.1.105 general release, and updates) >= 418.39 >= 418.96
CUDA 10.0.130 >= 410.48 >= 411.31
CUDA 9.2 (9.2.148 Update 1) >= 396.37 >= 398.26
CUDA 9.2 (9.2.88) >= 396.26 >= 397.44
CUDA 9.1 (9.1.85) >= 390.46 >= 391.29
CUDA 9.0 (9.0.76) >= 384.81 >= 385.54
CUDA 8.0 (8.0.61 GA2) >= 375.26 >= 376.51
CUDA 8.0 (8.0.44) >= 367.48 >= 369.30
CUDA 7.5 (7.5.16) >= 352.31 >= 353.66
CUDA 7.0 (7.0.28) >= 346.46 >= 347.62
安装gcc
gcc --version
![](https://filescdn.proginn.com/2f5e0cc4c7eae43d0b613a5c9683902e/4e61cf50fc27fe0ee966561eefefb59f.webp)
![](https://filescdn.proginn.com/c510304ef2dda698fc9ae78ed40e1f96/cac8505fe0c2b95f1c1a4c2667f792f7.webp)
sudo apt-get install gcc-7.0
sudo apt-get install g++-7.0
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 50
sudo update-alternatives --config gcc
选择 路径 优先级 状态
------------------------------------------------------------
* 0 /usr/bin/gcc-9 50 自动模式
1 /usr/bin/g++-9 50 手动模式
2 /usr/bin/gcc-7 50 手动模式
安装cuda
sudo sh cuda_你的版本_linux.run
配置环境变量
sudo vim ~/.bashrc
export PATH=/usr/local/cuda-10.2/bin${PATH:+:$PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
nvcc -V
3.3.3 安装cudnn
![](https://filescdn.proginn.com/7b47ca8123fce5f05bb36e80c6c3c309/ed6c802412ff2fec071d09ae93c8c007.webp)
sudo cp cuda/include/cudnn.h /usr/local/cuda-10.2/include #解压后的文件夹名字为cuda-10.2
sudo cp cuda/lib64/libcudnn* /usr/local/cuda-10.2/lib64
sudo chmod a+r /usr/local/cuda-10.2/include/cudnn.h /usr/local/cuda-10.2/lib64/libcudnn*
3.3.4 安装pytorch-gpu
![](https://filescdn.proginn.com/ae022efb7d2007e18591d45e609f9fc1/3e5cbe1958041283bd3c4fa1a2b7db5b.webp)
# 选择自己对应的cuda版本
conda install pytorch torchvision cudatoolkit=10.2
pip install torch torchvision -i https://pypi.mirrors.ustc.edu.cn/simple
四、写在最后
推荐阅读
![](https://filescdn.proginn.com/e0db2fb4d39d69d3a6a31833bb04bb41/8b51ccd0ae670e788b479603fb395375.webp)
评论