在這篇文章中沒有直接使用MXNet官方提供的docker image,而是從一個乾淨的nvidia/cuda鏡像開始,一步一步部署mxnet須要的相關軟件環境,這樣作是爲了更加細緻的瞭解mxnet的運行環境,方便後續咱們更加靈活的去修改相關的配置。html
docker run -itd --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=2,3 --name=mxnet-cu90 -p 9999:8888 -p 1234:22 -v /docker_share/:/root/share nvidia/cuda:9.0-cudnn7-devel bash docker exec -it mxnet-cu90 bash
這裏鏡像使用了nvida最新的cuda9.0的鏡像,使用--runtime=nvidia
是爲了使用nvidia-docker在容器中使用GPU資源, 環境變量NVIDIA_VISIBLE_DEVICES用來控制在容器中可見的GPU的id,這裏選擇2和3,是由於個人機器上一共有4塊GPU卡,容器中只使用後兩塊。-v
選項是爲了方便主機與容器之間進行數據交換而掛載的一塊空間,使用-p
選擇進行主機與容器之間的端口映射,這裏分別映身了容器中的22號端口是爲了使用ssh進行遠程訪問,以及8888端口是jupyter-notebook默認使用的端口號。python
容器中默認的軟件包的更新源都是國外的,在國外使用起來速度較慢,這裏更新爲阿里雲的便可。也能夠直接在/etc/apt/sources.list
文件中將http://archive.ubuntu.com/ubuntu/直接替換爲http://cn.archive.ubuntu.com/ubuntu/。git
apt-get update apt-get install -y vim mv /etc/apt/sources.list /etc/apt/sources.list.bark #備份 vim /etc/apt/sources.list #修改 apt-get update #更新
阿里雲的源:github
deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
sudo apt-get update && sudo apt-get install -y build-essential git libgfortran3 apt-get install -y vim git openssh-server # 安裝 vim git ssh遠程登陸 apt-get install -y bash-completion man # 安裝命令行的自動提示的功能,以及幫助手冊的查看工具 adduser username #建立新用戶 adduser username sudo #把用戶加入到sudo權限 service ssh start # 啓動ssh服務 exit #退出容器 docker exec mxnet-cu90 /usr/sbin/sshd # 在host上開啓容器的sshd服務
這樣咱們後續就能夠經過ssh -p 1234 username@172.17.2.50
訪問容器了。docker
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh #下載 sh Miniconda3-latest-Linux-x86_64.sh #安裝 conda config --prepend channels http://mirrors.ustc.edu.cn/anaconda/pkgs/free/ # 配置國內科大的源
進一步閱讀:apache
mkdir ~/.pip vim ~/.pip/pip.conf
粘貼以下內容,添加阿里雲的源ubuntu
[global] trusted-host=mirrors.aliyun.com index-url=https://mirrors.aliyun.com/pypi/simple/
使用conda提供的虛擬環境機制在容器裏建立一個虛擬環境配置文件environment.yml,文件內容以下:vim
name: mxnet dependencies: - python=3 - jupyter - matplotlib - pandas - pip: - requests - mxnet-cu90>=1.0.1b20180125
而後經過下面的命令,建立虛擬環境mxnet並激活使用。bash
conda env create -f environment.yml # 安裝運行mxnet所須要的Python包 source activate mxnet # 激活虛擬環境,Windows下不須要 source
使用notedownssh
source activate mxnet # 激活mxnet環境 pip install https://github.com/mli/notedown/tarball/master # 安裝插件 jupyter notebook --generate-config # 生成jupyter notebook的配置文件 echo "c.NotebookApp.contents_manager_class = 'notedown.NotedownContentsManager' " >> ~/.jupyter/jupyter_notebook_config.py # 在配置文件最後增長一行
安裝notebook每一個cell的計時程序
source activate mxnet # 激活gluon環境 pip install jupyter_contrib_nbextensions jupyter contrib nbextension install --user jupyter nbextension enable execute_time/ExecuteTime
import mxnet as mx import mxnet.ndarray as nd a = nd.ones((2,3), ctx=mx.gpu()) a.asnumpy()
若是整個環境安裝正確,則運行上面的代碼,不會報錯,且有以下的輸出:
array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32)
在步驟6中,咱們演示瞭如何使用虛擬環境工具conda以及python的包管理工具pip來安裝mxnet最新的GPU版本,咱們能夠獲得一個mxnet的python開發環境。咱們也能夠從源碼開始,編譯安裝。
安裝MXNet的依賴庫
apt-get install -y libopenblas-dev liblapack-dev libopencv-dev
下載mxnet源碼並編譯
$ git clone --recursive https://github.com/apache/incubator-mxnet $ cd incubator-mxnet $ make -j $(nproc) USE_OPENCV=1 USE_BLAS=openblas USE_CUDA=1 USE_CUDA_PATH=/usr/local/cuda USE_CUDNN=1
編譯python接口
sudo apt-get install -y python-dev python-setuptools python-pip libgfortran3 # 安裝python相關的環境 cd python pip install --upgrade pip pip install -e . #經過目錄下的requirement.txt來管理pip安裝的包 sudo apt-get install graphviz #安裝graphviz用來計算圖的顯示 pip install graphviz