搭建深度學習基本環境(Ubuntu系統下)
本人第一次寫博客啦~
在ubuntu系統上安裝Anaconda3
- 下載linux安裝包:
1、從官網下載(如果電腦是64位,記得下載64-bit)
2、命令行(注意網站的更新):
wget https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh
- 安裝:
bash Anaconda3-5.2.0-Linux-x86_64.sh
如果是採用官網下載的話,則需要先進入“下載”目錄中:
cd Downloads
後面的操作就是一直是yes或者是”enter”等傻瓜式操作啦~
- 檢測安裝是否成功:
直接在terminal輸入:
python
還可以用“conda list”查看可用的packages。如果在這一步的python沒有更新的話,那說明.bashrc的更新還沒有生效,命令行輸入:
source ./bashrc
在ubuntu系統上安裝Tensorflow
建議這塊的安裝參考官網 [ tensorflow install ]
注意:由於實驗室的服務器已經安裝好了CUDA和cuDNN,因此我只需要安裝Anaconda3和Tensorflow即可,如果服務器或者Ubuntu中沒有CUDA和cuDNN的話,還需要先安裝好CUDA和cuDNN再安裝Tensorflow。
- CPU:
首先確認下自己的python 版本和pip:
python3 -V
pip3 -V
通常ubuntu系統都會自動安裝pip或者是python,也可以自己安裝一下:
sudo apt-get install python3-pip python3-dev # for Python 3.n
然後按照官網指示安裝即可:
sudo pip3 install -U tensorflow # Python 3.n
檢驗安裝是否成功:
python -c "import tensorflow as tf; print(tf.__version__)"
- GPU:
1、首先我是按照官網上提到的“Use pip in Anaconda”方法盡心安裝的:
$ conda create -n tensorflow pip python=3.6.5(注意這裏一定要明確版本)
$ source activate tensorflow
(tensorflow)$ pip install --ignore-installed --upgrade tfBinaryURL
這裏tfBinaryURL即是 URL of the TensorFlow Python package.
例如,我輸入的命令位(對應3.6.5版本):
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.10.1-cp36-cp36m-linux_x86_64.whl
檢測是否安裝成功:
python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
如果出現啦”Hello,TensorFlow”的話,那麼就是安裝成功啦~
2、然而,很可惜我運行的時候出現了”libcublas.So.9.0~~”這樣的錯誤,一查結果發現這是因爲我安裝的tensorflow1.10.0版本對應的是CUDA 9.0版本,而服務器上安裝的是CUDA8.0,版本不匹配。後來,爲了穩妥起見,首先將python3.6.5改爲python3.5.6版本:
conda install python3.5
然後還是按照官網上的方法先創建一下tensorflow環境,然後安裝tensorflow1.3.0版本:
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-1.3.0-cp36-cp36m-linux_x86_64.whl
結果發現會提示“~is not supported ~ in the platform”等這樣的錯誤,發現這是因爲官網的URL of tensorflow已經更新至1.10.0版本啦。
如何安裝older tensorflow呢?我後來在 [ stackoverflow~Install older version tensorflow ]上找到了答案,也就是直接在終端輸入:
pip install tensorflow==1.3.0
檢測是否安裝成功:
import tensorflow as tf
hello = tf.constant(‘Hello, TensorFlow!’)
sess = tf.Session()
print(sess.run(hello))
$ python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
結果在第三句代碼後出現了很多warning:
2017-12-25 20:11:49.485908: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations.
2017-12-25 20:11:49.486234: W c:\l\work\tensorflow-1.1.0\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE2 instructions, but these are available on your machine and could speed up CPU computations.
這些warning出現主要是因爲machine memory不夠大,程序運行速度會比較慢等原因,但不會影響到程序運行。不想這些warning出現,則可以在命令前加入:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
結果最終輸出的是:
b'Hello Tensorflow!'
前面出現一個’b’的原因是“Python 3.x makes a clear distinction between the types”:
str = '...' literals = a sequence of Unicode characters (UTF-16 or UTF-32, depending on how Python was compiled)
bytes = b'...' literals = a sequence of octets (integers between 0 and 255)
如果不想出現’b’,則將最後一句命令改爲:
print(sess.run(hello).decode())
這樣就會出現完美的:
'Hello Tensorflow!'
祝各位搭建環境順利啊~