本教程搭建集 Tensorflow、Keras、Coffe、PyTorch 等深度學習框架於一身的環境,及jupyter。python
本教程使用nvidia-docker啓動實例,經過本教程能夠從一個全新的Ubuntu系統快速搭建出GPU深度學習環境。linux
https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/
此處默認環境:ubuntu16.04LTSgit
sudo mv /etc/apt/sources.list /etc/apt/sources.list.old sudo vim /etc/apt/sources.list
而後將下面的內容寫入該文件:
須要注意的是:不一樣版本的ubuntu鏡像源不同,能夠在清華鏡像源查詢github
# 默認註釋了源碼鏡像以提升 apt update 速度,若有須要可自行取消註釋 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse # 預發佈軟件源,不建議啓用 # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
使鏡像源生效docker
sudo apt-get update
sudo add-apt-repository ppa:graphics-drivers/ppa sudo apt-get update sudo apt-get install nvidia-384 nvidia-prime
查看是否安裝成功shell
watch nvidia-smi # 該命令可查看GPU使用狀況
https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/
以ubuntu16.04LTS爲例ubuntu
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository \ "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \ $(lsb_release -cs) \ stable" sudo apt-get update sudo apt-get install docker-ce
查看是否安裝成功vim
docker -v
將當前用戶加入到docker用戶組(這樣在執行docker命令的時候就不會出現Permission Denied了)bash
sudo usermod -aG docker ${YOUR_NAME_HERE}
https://github.com/NVIDIA/nvidia-docker框架
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | \ sudo apt-key add - distribution=$(. /etc/os-release;echo $ID$VERSION_ID) curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | \ sudo tee /etc/apt/sources.list.d/nvidia-docker.list sudo apt-get update sudo apt-get install -y nvidia-docker2 sudo pkill -SIGHUP dockerd
這裏的最後一步會重啓docker,並載入nvidia-docker的配置
查看是否安裝成功
nvidia-docker -v
這裏咱們使用了deepo鏡像:https://hub.docker.com/r/ufoym/deepo/
其下的 ufoym/deepo:all-py36-jupyter
,該鏡像收集了大部分深度學習框架,運行在GPU環境,以及配有jupyter。
docker pull ufoym/deepo:all-py36-jupyter
默認配置(不推薦)
nvidia-docker run -it -p 8888:8888 ufoym/deepo:all-py36-jupyter jupyter notebook --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token= --notebook-dir='/root'
外部掛載配置(掛載外部目錄,方便移動數據,不推薦)
# 這裏使用了-v選項用於掛載外部目錄 nvidia-docker run -it -p 8888:8888 --ipc=host -v /data:/data ufoym/deepo:all-py36-jupyter jupyter notebook --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token= --notebook-dir='/data'
博主推薦:後臺運行並掛載外部目錄(須要注意的地方是要把參數-it
改爲-i
,不然沒法運行在後臺)
nohup nvidia-docker run -i -p 8888:8888 --ipc=host -v /data:/data ufoym/deepo:all-jupyter-py36 jupyter notebook --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token= --notebook-dir='/data' &
參數說明
-v /data:/data
:左邊是外部路徑,右邊是內部路徑,例如個人文件放在/home/ubuntu/data
下,須要掛載到docker內部的路徑是/data
,則參數配置應該是-v /home/ubuntu/data:/data
--notebook-dir
:jupyter工做目錄的默認路徑,推薦與上面的docker內部數據路徑相同,即/data
-p 8888:8888
:左邊是外部端口,右邊是docker鏡像端口。若是想將jupyter應用掛載在8080端口,只需修改參數-p 8080:8888
便可--NotebookApp.token
:進入jupyter的密碼,這裏設置的是空
錯誤以下:
FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`. from ._conv import register_converters as _register_converters
解決方案:
進入jupyter terminal並輸入
pip install --upgrade numpy pip install --upgrade h5py
問題解決。
docker exec -it $(docker ps | awk '{print $1}' | sed -n '2p') bash
能夠把這一段代碼用alias連接後方便使用,在~/.profile下添加這一行:
alias pysh="docker exec -it $(docker ps | awk '{print $1}' | sed -n '2p') bash"
讓配置生效
source ~/.profile
再次輸入pysh就能夠快速進入docker shell