TensorFlow學(xué)習(xí)筆記[1]: TensorFlow安裝及在Jupyter notebook中用Pythond代碼并生成散列點(diǎn)圖
摘要:現(xiàn)在不掌握點(diǎn)人工智能方面的技術(shù),簡(jiǎn)單都不好意思出門(mén)了。前面寫(xiě)《Google人工智能平臺(tái)TensorFlow介紹》的時(shí)候,對(duì)TensorFlow有了初步的認(rèn)識(shí),最近抽空進(jìn)行較為深入的學(xué)習(xí)。這將是一個(gè)系列筆記,以《TensorFlow for Machine Intelligence》這本書(shū)為主線,將該書(shū)的精華摘錄下來(lái),同時(shí)做相應(yīng)的練習(xí)!本文是本系列的第一篇,主要記錄TensorFlow的簡(jiǎn)單安裝及使用Jupyter notebook執(zhí)行Python代碼并生成散列圖的過(guò)程。
TensorFlow基本知識(shí)
與TensorFlow類(lèi)似的機(jī)器學(xué)習(xí)庫(kù)還有Theano和Caffe。學(xué)習(xí)TensorFlow最好具有如下基礎(chǔ)知識(shí):導(dǎo)數(shù),微積分,線性代數(shù),程序知識(shí),懂Python和C/C++更好!
DistBelief是TensorFlow的前身,后者是為了克服前者的不足而開(kāi)發(fā)的,于2015年11月開(kāi)源發(fā)布。目前TensorFlow被應(yīng)用于自然語(yǔ)言處理,人工智能,計(jì)算機(jī)視覺(jué)和預(yù)測(cè)分析。
NumPy is the fundamental package for scientific computing with Python
Jupyter is a web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text.
Matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.
Matplotlib is a charting library that allows you to create dynamic, custom visualizations in Python.
TensorFlow安裝
雖然在《Google人工智能平臺(tái)TensorFlow介紹》已經(jīng)介紹過(guò)安裝,這里為了記錄的完整性,給出一個(gè)簡(jiǎn)化版的安裝過(guò)程。
- $ sudo apt-get install python-pip python-dev python-virtualenv
- $ virtualenv --system-site-packages ~/tensorflow
- $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0rc0-cp27-no ne-linux_x86_64.whl
- $ sudo pip install --upgrade $TF_BINARY_URL
#安裝Jupyter Notebook
- $ sudo python2 -m pip install ipykernel
- $ sudo python2 -m ipykernel install
- $ sudo apt-get install build-essential
- $ sudo pip install jupyter
#安裝matplotlib
- $ sudo apt-get build-dep python-matplotlib python-tk
#激活tensorflow虛擬環(huán)境
- $ source tensorflow/bin/activate
-
- #如下這步必不可少(書(shū)中沒(méi)有),否則會(huì)報(bào)ImportError: No module named matplotlib.pyplot
- $ sudo apt-get install python-matplotlib
-
- (tensorflow)$ mkdir tf-notebooks
- (tensorflow)$ cd tf-notebooks
#啟動(dòng) jupyter notebook
- (tensorflow)$ jupyter notebook
坑1:直接按照書(shū)中的命令"jupyter notebook"執(zhí)行,會(huì)報(bào)socket.error: [Errno 99] Cannot assign requested address錯(cuò)誤,就指定IP地址啟動(dòng)。如下:
(tensorflow)$ jupyter notebook --ip=120.25.234.39
坑2:必須以sudo啟動(dòng),否則后面操作的是會(huì)報(bào)權(quán)限錯(cuò)誤
(tensorflow)$ sudo jupyter notebook --ip=120.25.234.xx
注:120.25.234.xx是你的IP地址。
坑3:根據(jù)上面的安裝,后面會(huì)報(bào)如下錯(cuò)誤:
ImportError: No module named matplotlib.pyplot
解決辦法:sudo apt-get install python-matplotlib
Juypyter notebook啟動(dòng)成功后會(huì)有類(lèi)似如下提示:
在Jupyter nootebook中用Python代碼生成散列點(diǎn)圖
可以通過(guò)提示的網(wǎng)址在瀏覽器里進(jìn)入Jupyter nootebook。在右側(cè)可以創(chuàng)建notebook,在里面可執(zhí)行如下代碼片段:
代碼片段,可直接在Jupyter中執(zhí)行,結(jié)果就是一些點(diǎn)散列圖
- import tensorflow as tf
- import numpy as np
- import matplotlib.pyplot as plt
-
- #表示直接在瀏覽器中顯示matplotlib圖表
- %matplotlib inline
-
- a = tf.random_normal([2,20]) #定義2x20的隨機(jī)數(shù)矩陣
- sess = tf.Session() #啟動(dòng)一個(gè)tensorflow會(huì)話
- out = sess.run(a) # 用在sess會(huì)話里執(zhí)行a,結(jié)果放out里
- x, y = out
-
- plt.scatter(x, y) #用pyplot創(chuàng)建一系列散列點(diǎn),坐標(biāo)為x和y
- plt.show()
在文檔中生成可視化的散列點(diǎn)圖,如下所示:
參考資料:
[1] <TensorFlow for Machine Intelligence> A Hands-On Introduction to Learning Algorithms Bleeding Edge Press 2016