前言
隨著電腦運算能力的增強, 特別是GPU的出現, 讓深度學習這個領域變得非常火紅, 進而成為目前人工智慧的主流, 由於他實在很強大, 現在不管是Google, Amazon, 或是Microsoft都在積極地推出深度學習的相關應用,
難道深度學習只有像Google這種大公司才玩得起嗎? (答案是: No)
建立一套深度學習網路並沒有想像中那麼難, 以下就教各位如何快速地建立一個深度學習的環境
Step 1. 安裝NVidia驅動程式
一開始可以先用ubuntu-drivers devices列出你的顯卡需要裝什麼驅動, 如果沒有特別的偏好, 直接用 autoinstall來裝就可以了
ubuntu-drivers devices sudo ubuntu-drivers autoinstall
安裝完後建議重開機
Step 2. 檢查是否安裝成功
nvidia-smi 可以用來監測GPU的使用率
nvidia-smi
如果驅動有安裝成功, 基本上可以到目前GPU的使用狀況
Step 3. 安裝 CUDA & CUDNN
# Add NVIDIA package repositorysudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pubwget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.1.85-1_amd64.debsudo apt install ./cuda-repo-ubuntu1604_9.1.85-1_amd64.debwget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.debsudo apt install ./nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.debsudo apt update# Install CUDA and tools. Include optional NCCL 2.xsudo apt install cuda9.0 cuda-cublas-9-0 cuda-cufft-9-0 cuda-curand-9-0 \cuda-cusolver-9-0 cuda-cusparse-9-0 libcudnn7=7.2.1.38-1+cuda9.0 \libnccl2=2.2.13-1+cuda9.0 cuda-command-line-tools-9-0# Optional: Install the TensorRT runtime (must be after CUDA install)sudo apt updatesudo apt install libnvinfer4=4.1.2-1+cuda9.0
Step 4. 安裝virtualenv
建立虛擬環境sudo apt updatesudo apt install python3-dev python3-pipsudo pip3 install -U virtualenv # system-wide install
virtualevn --system-site-packages -p python3 ./venvsource ./venv/bin/activatepip install --upgrade pip
Step 5. 安裝tensorflow-gpu & keras
pip install --upgrade tensorflowpip install keras
Step 6. 跑範例測試環境有沒有建起來
建立test.py, 將以下的範例程式碼複製貼到test.py裡面, 然後執行python3 test.py
from keras.datasets import mnistfrom keras.utils import np_utilsdef load_data():(x_train, y_train), (x_test, y_test) = mnist.load_data()x_train = x_train.reshape(x_train.shape[0], 28*28)x_test = x_test.reshape(x_test.shape[0], 28*28)x_train = x_train.astype('float32')x_test = x_test.astype('float32')x_train = x_train/255x_test = x_test/255y_train = np_utils.to_categorical(y_train, 10)y_test = np_utils.to_categorical(y_test, 10)return (x_train, y_train), (x_test, y_test)import numpy as npfrom keras.models import Sequentialfrom keras.layers.core import Dense,Activationfrom keras.optimizers import Adamdef build_model():model = Sequential()model.add(Dense(input_dim=28*28,units=500,activation='relu'))model.add(Dense(units=500,activation='relu'))model.add(Dense(units=500,activation='relu'))model.add(Dense(units=10,activation='softmax'))model.summary()return model(x_train,y_train),(x_test,y_test)=load_data()model = build_model()model.fit(x_train,y_train,batch_size=100,epochs=20)score = model.evaluate(x_train,y_train)print ('\nTrain Acc:', score[1])score = model.evaluate(x_test,y_test)print ('\nTest Acc:', score[1])
留言
張貼留言