深度學習庫安裝與使用
Theano
windows下
- Download Anaconda now!
- conda install mingw libpython
- pip install theano
Keras
Windows下
通過 conda install keras
或 pip install keras
直接安裝。(會預設的給你安裝keras最新版本和所需要的theano)
因為windows版本的tensorflow剛剛才推出,所以目前支援性不太好。但是keras的backend 同時支援tensorflow和theano. 並且預設是tensorflow, 因此在win本上需要更改backend為theano才能執行。Linux中切換backend同理!
- 將C:\Anaconda2\Lib\site-packages\keras\backend\__init__.py的line 27修改:
# Default backend: TensorFlow.
#_BACKEND = 'tensorflow'
_BACKEND = 'theano'
then, python-> import keras
- 出現 tensorflow提示錯誤的話,需要修改下面的位置的內容: C:\Users\Administrator\.keras\keras.json
{
"image_dim_ordering":"tf",
"epsilon" :1e-07,
"floatx":"float32",
"backend":"tensorflow"
}
{
"image_dim_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "theano"
}
切換後端
如果你至少執行過一次Keras,你將在下面的目錄下找到Keras的配置檔案:
~/.keras/keras.json
如果該目錄下沒有該檔案,你可以手動建立一個
檔案的預設配置如下:
{
"image_dim_ordering ":"tf",
"epsilon":1e-07,
"floatx":"float32",
"backend":"tensorflow"
}
將backend欄位的值改寫為你需要使用的後端:theano或tensorflow,即可完成後端的切換
我們也可以通過定義環境變數KERAS_BACKEND來覆蓋上面配置檔案中定義的後端:
KERAS_BACKEND=tensorflow python -c "from keras import backend;"
Using TensorFlow backend.
keras.json 細節
{
"image_dim_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
你可以更改以上~/.keras/keras.json中的配置
- image_dim_ordering:字串,”tf”或”th”,該選項指定了Keras將要使用的維度順序,可通過keras.backend.image_dim_ordering()來獲取當前的維度順序。對2D資料來說,tf假定維度順序為(rows,cols,channels)而th假定維度順序為(channels, rows, cols)。對3D資料而言,tf假定(conv_dim1, conv_dim2, conv_dim3, channels),th則是(channels, conv_dim1, conv_dim2, conv_dim3)
- epsilon:浮點數,防止除0錯誤的小數字
- floatx:字串,”float16”, “float32”, “float64”之一,為浮點數精度
- backend:字串,所使用的後端,為”tensorflow”或”theano”
win10 bash下配置環境
- bash Anaconda….sh
- source ~/.bashrc
- sudo /home/sun/anaconda2/bin/pip install theano
- sudo /home/sun/anaconda2/bin/pip install keras
- sudo /home/sun/anaconda2/bin/pip install –upgrade gensim
Cuda
安裝CUDA
檢視系統資訊
$ uname -m && cat /etc/*release
驗證NVIDIA顯示卡
$ lspci | grep -i nvidia
安裝gcc及g++
就是安裝C++開發環境,因為CUDA是基於C/C++開發的,當然現在也支援很多其他語言
$ sudo apt-get install gcc g++
安裝linux kernel header及開發包
$ sudo apt-get install linux-headers-$(uname -r)
為了避免出現標頭檔案相關的問題,推薦安裝上build-essential:
$ sudo apt-get install build-essential
合併:
$ sudo apt-get install gcc g++ linux-headers-$(uname -r) build-essential -y
官方文件
Installation Instructions:
`sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb`
`sudo apt-get update`
`sudo apt-get install cuda`
3.6. Ubuntu
1. Perform the pre-installation actions.
2. Install repository meta-data
When using a proxy server with aptitude, ensure that wget is set up to use the
same proxy settings before installing the cuda-repo package.
$ sudo dpkg -i cuda-repo-<distro>_<version>_<architecture>.deb
- Update the Apt repository cache
$ sudo apt-get update
- Install CUDA
$ sudo apt-get install cuda
- Perform the post-installation actions
驗證
重啟顯示卡
[email protected]:/etc/network# systemctl status nvidia-persistenced.service
驅動
apt install software-properties-common
–> add-apt-repository ppa:graphics-drivers/ppa
(可選)
apt install ubuntu-drivers-common
–> ubuntu-drives list
–> ubuntu-drivers autoinstall
nvidia-detector
[email protected]:/etc/network# nvidia-smi
Wed Mar 15 16:06:11 2017
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 378.13 Driver Version: 378.13 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 Tesla K20c Off | 0000:03:00.0 Off | 0 |
| 40% 48C P0 57W / 225W | 0MiB / 4742MiB | 95% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
cuda
安裝:
./cuda*.run
cd ~/NVIDIA_CUDA-8.0_Samples/
–> make all
cd ~/NVIDIA_CUDA-8.0_Samples/1_Utilities/deviceQuery
–> nvcc -I '../../common/inc/' deviceQuery.cpp -o deviceQuery
–> ./deviceQuery
nvcc -V
環境變數
vim /etc/bash.bashrc
export CUDA_HOME=/usr/local/cuda-8.0
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
vim ~/.bashrc