1.安裝 Jupyter notebook 和 Jupyter Lab
notebook 是 Donald Knuth 在 1984 年提出的文字表達化編程的一種形式。在文字表達化編程中,直接在代碼旁寫出敘述性文檔,而不是另外編寫單獨的文檔。
1.1 安裝pip3
Jetson Nano中已經安裝了Python3.6版本
# 安裝pip3
sudo apt-get install python3-pip python3-dev
安裝后pip是9.01版本,需要把它升級到最新版,升級后pip版本為19.1.1。
# 升級pip3
python3 -m pip install --upgrade pip
pip3 install --upgrade --user
升級后會有一個小Bug,需要手動改一下
打開pip3文件
sudo vim /usr/bin/pip3
將原來的
from pip import main
if __name__ == '__main__':
sys.exit(main())
改成
from pip import __main__
if __name__ == '__main__':
sys.exit(__main__._main())
修改結束后保存。運行 pip3 -V
成功后顯示
pip3 -V
pip 19.1.1 from /home/albert/.local/lib/python3.6/site-packages/pip (python 3.6)
1.2 安裝 Jupyter notebook
pip3 install jupyter notebook --user
Successfully installed Send2Trash-1.5.0 backcall-0.1.0 bleach-3.1.0 defusedxml-0.6.0 entrypoints-0.3 ipykernel-5.1.0 ipython-7.5.0 ipython-genutils-0.2.0 ipywidgets-7.4.2 jedi-0.13.3 jinja2-2.10.1 jsonschema-3.0.1 jupyter-1.0.0 jupyter-client-5.2.4 jupyter-console-6.0.0 jupyter-core-4.4.0 mistune-0.8.4 nbconvert-5.5.0 nbformat-4.4.0 notebook-5.7.8 pandocfilters-1.4.2 parso-0.4.0 pexpect-4.7.0 pickleshare-0.7.5 prometheus-client-0.6.0 prompt-toolkit-2.0.9 ptyprocess-0.6.0 pygments-2.3.1 pyrsistent-0.15.1 pyzmq-18.0.1 qtconsole-4.4.3 terminado-0.8.2 testpath-0.4.2 tornado-6.0.2 traitlets-4.3.2 wcwidth-0.1.7 widgetsnbextension-3.4.2
jupyter --help
usage: jupyter [-h] [--version] [--config-dir] [--data-dir] [--runtime-dir]
[--paths] [--json]
[subcommand]
Jupyter: Interactive Computing
positional arguments:
subcommand the subcommand to launch
optional arguments:
-h, --help show this help message and exit
--version show the jupyter command's version and exit
--config-dir show Jupyter config dir
--data-dir show Jupyter data dir
--runtime-dir show Jupyter runtime dir
--paths show all Jupyter paths. Add --json for machine-readable
format.
--json output paths as machine-readable json
Available subcommands: bundlerextension console contrib kernel kernelspec
migrate nbconvert nbextension nbextensions_configurator notebook qtconsole run
serverextension troubleshoot trust
jupyter notebook --help
The Jupyter HTML Notebook.
This launches a Tornado based HTML Notebook Server that serves up an
HTML5/Javascript Notebook client.
Subcommands
-----------
Subcommands are launched as `jupyter-notebook cmd [args]`. For information on
using subcommand 'cmd', do: `jupyter-notebook cmd -h`.
list
List currently running notebook servers.
stop
Stop currently running notebook server for a given port
password
Set a password for the notebook server.
Options
-------
Arguments that take values are actually convenience aliases to full
Configurables, whose aliases are listed on the help line. For more information
on full configurables, see '--help-all'.
--debug
set log level to logging.DEBUG (maximize logging output)
--generate-config
generate default config file
-y
Answer yes to any questions instead of prompting.
--no-browser
Don't open the notebook in a browser after startup.
--pylab
DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
--no-mathjax
Disable MathJax
MathJax is the javascript library Jupyter uses to render math/LaTeX. It is
very large, so you may want to disable it if you have a slow internet
connection, or for offline use of the notebook.
When disabled, equations etc. will appear as their untransformed TeX source.
--allow-root
Allow the notebook to be run from root user.
--script
DEPRECATED, IGNORED
--no-script
DEPRECATED, IGNORED
--log-level=<Enum> (Application.log_level)
Default: 30
Choices: (0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL')
Set the log level by value or name.
--config=<Unicode> (JupyterApp.config_file)
Default: ''
Full path of a config file.
--ip=<Unicode> (NotebookApp.ip)
Default: 'localhost'
The IP address the notebook server will listen on.
--port=<Int> (NotebookApp.port)
Default: 8888
The port the notebook server will listen on.
--port-retries=<Int> (NotebookApp.port_retries)
Default: 50
The number of additional ports to try if the specified port is not
available.
--transport=<CaselessStrEnum> (KernelManager.transport)
Default: 'tcp'
Choices: ['tcp', 'ipc']
--keyfile=<Unicode> (NotebookApp.keyfile)
Default: ''
The full path to a private key file for usage with SSL/TLS.
--certfile=<Unicode> (NotebookApp.certfile)
Default: ''
The full path to an SSL/TLS certificate file.
--client-ca=<Unicode> (NotebookApp.client_ca)
Default: ''
The full path to a certificate authority certificate for SSL/TLS client
authentication.
--notebook-dir=<Unicode> (NotebookApp.notebook_dir)
Default: ''
The directory to use for notebooks and kernels.
--browser=<Unicode> (NotebookApp.browser)
Default: ''
Specify what command to use to invoke a web browser when opening the
notebook. If not specified, the default browser will be determined by the
`webbrowser` standard library module, which allows setting of the BROWSER
environment variable to override it.
--pylab=<Unicode> (NotebookApp.pylab)
Default: 'disabled'
DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
To see all available configurables, use `--help-all`
Examples
--------
jupyter notebook # start the notebook
jupyter notebook --certfile=mycert.pem # use SSL/TLS certificate
jupyter notebook password # enter a password to protect the server
安裝擴展
pip3 install tqdm jupyter_contrib_nbextensions --user
#安裝關聯的 JavaScript 和 CSS 文件:
jupyter contrib nbextension install --user
# 安裝主題,安裝完成后,我們將會有一個jt的命令。
pip3 install --upgrade jupyterthemes --user
# jt 的 help ,之后可以自行配置主題
jt --help
1.3 啟動 notebook 服務器並配置遠程訪問
1.3.1 配置Jupyter
生成配置文件
jupyter notebook --generate-config
生成密碼
ipython
from notebook.auth import passwd
passwd()
設定一個密碼,會生成一個sha1的秘鑰
Out[2]: 'sha1:0fb67bb71f8f:9525f730807d01c04ea963492b0e3340de7b9d67'
修改默認配置文件
vim ~/.jupyter/jupyter_notebook_config.py
jupyter_notebook_config.py 文件全是注釋,所以直接在第一行前插入以下內容:
c.NotebookApp.ip='*' # 就是設置所有ip皆可訪問
c.NotebookApp.password = u'sha1:0fb67bb71f8f:9525f730807d01c04ea963492b0e3340de7b9d67' #剛才復制的那個sha1密文
c.NotebookApp.open_browser = False # 禁止自動打開瀏覽器
c.NotebookApp.port = 8888 #指定為NAT端口映射的端口號
個人建議:屏蔽掉密碼那一行,如果不是服務器安裝,而只是自己的Jetson Nano 或虛擬機,自己用不需要安全設置
1.3.2 啟動 notebook
在終端或控制台中輸入
jupyter notebook --ip=192.168.1.115
服務器會在你運行此命令的目錄中啟動。
http://192.168.1.115:8888/?token=d2e1e7e1e6e59f20725237958ade0c1f9f24b3a31cfaec5f
1.3.3 關閉 Jupyter
關閉running的notebook
通過在服務器主頁上選中 notebook 旁邊的復選框,然后點擊“Shutdown”(關閉),你就可以關閉各個 notebook。但是,在這樣做之前,請確保你保存了工作!否則,在你上次保存后所做的任何更改都會丟失。下次運行 notebook 時,你還需要重新運行代碼。
關閉整個服務器
通過在終端中按兩次 Ctrl + C,可以關閉整個服務器。再次提醒,這會立即關閉所有運行中的 notebook,因此,請確保你保存了工作!
1.3.4 保存Notebook
工具欄包含了保存按鈕,但 notebook 也會定期自動保存。標題右側會注明最近一次的保存。你可以使用保存按鈕手動進行保存,也可以按鍵盤上的 Esc,然后按 s。按 Esc 鍵會變為命令模式,而 s 是“保存”的快捷鍵。
1.4 Notebook用法
1.4.1 Markdown單元格
像代碼單元格一樣,按 Shift + Enter 或 Ctrl + Enter 可運行 Markdown 單元格,這會將 Markdown 呈現為格式化文本。加入文本可讓你直接在代碼旁寫出敘述性文檔,以及為代碼和思路編寫文檔。
1.4.2 Magic關鍵詞
Magic-What?
- 1.Magic 關鍵字是可以在單元格中運行的特殊命令,能讓你控制 notebook 本身或執行系統調用(例如更改目錄)。
- 2.Magic 命令的前面帶有一個或兩個百分號(% 或 %%),分別對應行 Magic 命令和單元格 Magic 命令。行 Magic 命令僅應用於編寫 Magic 命令時所在的行,而單元格 Magic 命令應用於整個單元格。
注意:這些 Magic 關鍵字是特定於普通 Python 內核的關鍵字。如果使用其他內核,這些關鍵字很有可能無效。
Magic-How?
- 1.交互工作方式:畫圖前先運行如下命令,可以直接顯示Matplotlib的圖形
%matplotlib
- 2.代碼計時
有時候,你可能要花些精力優化代碼,讓代碼運行得更快。在此優化過程中,必須對代碼的運行速度進行計時。可以使用 Magic 命令timeit
測算函數的運行時間。如果要測算整個單元格的運行時間,請使用%%timeit
,也可以使用timeit包測量程序運行時間:
# calculate time using
import timeit
start = timeit.default_timer()
# runing program
# output time using
end = timeit.default_timer()
tdf = end -start
timeh = tdf // 3600
timem = (tdf % 3600) // 60
times = tdf % 60
print("The total cost time is : " , int(timeh) , "h" , int(timem) , "m" ,times, "s")
- 3.在notebook 中嵌入可視化內容
- 4.在 notebook 中進行調試
對於 Python 內核,可以使用 Magic 命令 %pdb 開啟交互式調試器。出錯時,你能檢查當前命名空間中的變量。
1.5 Jupyter Lab 安裝
Jupyter源於Ipython Notebook,是使用Python(也有R、Julia、Node等其他語言的內核)進行代碼演示、數據分析、可視化、教學的很好的工具,對Python的愈加流行和在AI領域的領導地位有很大的推動作用。
Jupyter Lab是Jupyter的一個拓展,提供了更好的用戶體驗,例如可以同時在一個瀏覽器頁面打開編輯多個Notebook,Ipython console和terminal終端,並且支持預覽和編輯更多種類的文件,如代碼文件,Markdown文檔,json,yml,csv,各種格式的圖片,vega文件(一種使用json定義圖表的語言)和geojson(用json表示地理對象),還可以使用Jupyter Lab連接Google Drive等雲存儲服務,極大得提升了生產力。
我們可以通過Try Jupyter網站(https://jupyter.org/try)試用Jupyter Lab
1.5.1 安裝Jupyter Lab
pip3 install jupyterlab --user
1.5.2 運行Jupyter Lab
jupyter-lab
Jupyter Lab會繼承Jupyter Notebook的配置(地址,端口,密碼等)
jupyter-lab --help
JupyterLab - An extensible computational environment for Jupyter.
This launches a Tornado based HTML Server that serves up an HTML5/Javascript
JupyterLab client.
JupyterLab has three different modes of running:
* Core mode (`--core-mode`): in this mode JupyterLab will run using the JavaScript
assets contained in the installed `jupyterlab` Python package. In core mode, no
extensions are enabled. This is the default in a stable JupyterLab release if you
have no extensions installed.
* Dev mode (`--dev-mode`): uses the unpublished local JavaScript packages in the
`dev_mode` folder. In this case JupyterLab will show a red stripe at the top of
the page. It can only be used if JupyterLab is installed as `pip install -e .`.
* App mode: JupyterLab allows multiple JupyterLab "applications" to be
created by the user with different combinations of extensions. The `--app-dir` can
be used to set a directory for different applications. The default application
path can be found using `jupyter lab path`.
Subcommands
-----------
Subcommands are launched as `jupyter-notebook cmd [args]`. For information on
using subcommand 'cmd', do: `jupyter-notebook cmd -h`.
build
clean
path
paths
workspace
workspaces
Options
-------
Arguments that take values are actually convenience aliases to full
Configurables, whose aliases are listed on the help line. For more information
on full configurables, see '--help-all'.
--debug
set log level to logging.DEBUG (maximize logging output)
--generate-config
generate default config file
-y
Answer yes to any questions instead of prompting.
--no-browser
Don't open the notebook in a browser after startup.
--pylab
DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
--no-mathjax
Disable MathJax
MathJax is the javascript library Jupyter uses to render math/LaTeX. It is
very large, so you may want to disable it if you have a slow internet
connection, or for offline use of the notebook.
When disabled, equations etc. will appear as their untransformed TeX source.
--allow-root
Allow the notebook to be run from root user.
--script
DEPRECATED, IGNORED
--no-script
DEPRECATED, IGNORED
--core-mode
Start the app in core mode.
--dev-mode
Start the app in dev mode for running from source.
--watch
Start the app in watch mode.
--log-level=<Enum> (Application.log_level)
Default: 30
Choices: (0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL')
Set the log level by value or name.
--config=<Unicode> (JupyterApp.config_file)
Default: ''
Full path of a config file.
--ip=<Unicode> (NotebookApp.ip)
Default: 'localhost'
The IP address the notebook server will listen on.
--port=<Int> (NotebookApp.port)
Default: 8888
The port the notebook server will listen on.
--port-retries=<Int> (NotebookApp.port_retries)
Default: 50
The number of additional ports to try if the specified port is not
available.
--transport=<CaselessStrEnum> (KernelManager.transport)
Default: 'tcp'
Choices: ['tcp', 'ipc']
--keyfile=<Unicode> (NotebookApp.keyfile)
Default: ''
The full path to a private key file for usage with SSL/TLS.
--certfile=<Unicode> (NotebookApp.certfile)
Default: ''
The full path to an SSL/TLS certificate file.
--client-ca=<Unicode> (NotebookApp.client_ca)
Default: ''
The full path to a certificate authority certificate for SSL/TLS client
authentication.
--notebook-dir=<Unicode> (NotebookApp.notebook_dir)
Default: ''
The directory to use for notebooks and kernels.
--browser=<Unicode> (NotebookApp.browser)
Default: ''
Specify what command to use to invoke a web browser when opening the
notebook. If not specified, the default browser will be determined by the
`webbrowser` standard library module, which allows setting of the BROWSER
environment variable to override it.
--pylab=<Unicode> (NotebookApp.pylab)
Default: 'disabled'
DISABLED: use %pylab or %matplotlib in the notebook to enable matplotlib.
--app-dir=<Unicode> (LabApp.app_dir)
Default: '/home/albert/.local/share/jupyter/lab'
The app directory to launch JupyterLab from.
To see all available configurables, use `--help-all`
Examples
--------
jupyter lab # start JupyterLab
jupyter lab --dev-mode # start JupyterLab in development mode, with no extensions
jupyter lab --core-mode # start JupyterLab in core mode, with no extensions
jupyter lab --app-dir=~/myjupyterlabapp # start JupyterLab with a particular set of extensions
jupyter lab --certfile=mycert.pem # use SSL/TLS certificate
1.5.3 訪問Jupyter Lab
瀏覽器訪問 http://localhost:8888
2.安裝TensorFlow GPU
2.1 安裝機器學習依賴包
# numpy scipy pandas matplotlib sklearn
sudo apt install python3-numpy python3-scipy python3-pandas python3-matplotlib python3-sklearn libhdf5-serial-dev hdf5-tools
2.2 安裝TensorFlow GPU版並加入環境變量
#安裝TensorFlow GPU版本
pip3 install --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v42 tensorflow-gpu==1.13.1+nv19.3 --user
然后是漫長的安裝過程
Successfully installed absl-py-0.7.1 astor-0.7.1 gast-0.2.2 grpcio-1.20.1 h5py-2.9.0 keras-applications-1.0.7 keras-preprocessing-1.0.9 markdown-3.1 mock-2.0.0 pbr-5.2.0 protobuf-3.7.1 tensorboard-1.13.1 tensorflow-estimator-1.13.0 tensorflow-gpu-1.13.1+nv19.3 termcolor-1.1.0 werkzeug-0.15.2
由於安裝時使用了 --user
參數,即
pip3 install --user package_name
這樣會將Python3 程序包安裝到 $HOME/.local 路徑下,其中包含三個子文件夾:bin,lib 和 share。
需要修改 .bash_profile 文件使得 $HOME/.local/bin 目錄下的程序加入到環境變量中
sudo vim ~/.bashrc
在最后添加
export PATH=$HOME/.local/bin:$PATH
對了最后別忘了source一下這個文件。
source ~/.bashrc
2.3 測試TensorFlow
查看 TensorFlow 版本,可知當前版本為1.13.1
python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'1.13.1'
2.3.1 線性回歸例程測試:
%matplotlib inline
# 圖片內聯且以矢量格式顯示
# %config InlineBackend.figure_format = 'svg'
# 計算運行時間
# calculate time using
import timeit
start = timeit.default_timer()
import tensorflow as tf
import numpy
import matplotlib.pyplot as plt
rng = numpy.random
learning_rate = 0.01
training_epochs = 1000
display_step = 50
#數據集x
train_X = numpy.asarray([3.3,4.4,5.5,7.997,5.654,.71,6.93,4.168,9.779,6.182,7.59,2.167,
7.042,10.791,5.313,9.27,3.1])
#數據集y
train_Y = numpy.asarray([1.7,2.76,3.366,2.596,2.53,1.221,1.694,1.573,3.465,1.65,2.09,
2.827,3.19,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]
X = tf.placeholder("float")
Y = tf.placeholder("float")
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")
pred = tf.add(tf.multiply(X, W), b)
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
# 訓練數據
for epoch in range(training_epochs):
for (x, y) in zip(train_X, train_Y):
sess.run(optimizer, feed_dict={X: x, Y: y})
print ("優化完成!")
training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
print ("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b),)
#可視化顯示
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
plt.legend()
plt.show()
# output time using
end = timeit.default_timer()
tdf = end -start
timeh = tdf // 3600
timem = (tdf % 3600) // 60
times = tdf % 60
print("The total cost time is : " , int(timeh) , "h" , int(timem) , "m" ,times, "s")
結果:
優化完成!
Training cost= 0.17327271 W= 0.17206146 b= 1.3159091
The total time is : 0 h 0 m 36.63376809600004 s
2.3.2 非線性回歸例程測試
%matplotlib inline
# 圖片內聯且以矢量格式顯示
# %config InlineBackend.figure_format = 'svg'
# 計算運行時間
# calculate time using
import timeit
start = timeit.default_timer()
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x_data = np.linspace(-0.5, 0.5, 200)[:, np.newaxis]
noise = np.random.normal(0, 0.02, x_data.shape)
y_data = np.square(x_data) + noise
x = tf.placeholder(tf.float32, [None, 1])
y = tf.placeholder(tf.float32, [None, 1])
# 輸入層一個神經元,輸出層一個神經元,中間10個
# 第一層
Weights_L1 = tf.Variable(tf.random.normal([1, 10]))
Biases_L1 = tf.Variable(tf.zeros([1, 10]))
Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + Biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)
# 第二層
Weights_L2 = tf.Variable(tf.random.normal([10, 1]))
Biases_L2 = tf.Variable(tf.zeros([1, 1]))
Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + Biases_L2
pred = tf.nn.tanh(Wx_plus_b_L2)
# 損失函數
loss = tf.reduce_mean(tf.square(y - pred))
# 訓練
train = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(2000):
sess.run(train, feed_dict={x: x_data, y: y_data})
print("第{0}次,loss = {1}".format(i, sess.run(loss,feed_dict={x: x_data, y: y_data})))
pred_vaule = sess.run(pred, feed_dict={x: x_data})
plt.figure()
plt.scatter(x_data, y_data)
plt.plot(x_data, pred_vaule, 'r-', lw=5)
# output time using
end = timeit.default_timer()
tdf = end -start
timeh = tdf // 3600
timem = (tdf % 3600) // 60
times = tdf % 60
print("The total cost time is : " , int(timeh) , "h" , int(timem) , "m" ,times, "s")
結果 :
第0次,loss = 0.602253794670105
...
第1999次,loss = 0.00045431163744069636
The total cost time is : 0 h 0 m 24.807942360999732 s
2.3.3 TensorBoard 可視化
用於教學目的的簡單神經網絡的在線演示、實驗的圖形化平台,非常強大的可視化了神經網絡的訓練過程。
http://playground.tensorflow.org/
安裝 TensorBoard
pip3 install tensorboard --user
TensorBoard可視化例程
運行手寫數字識別MNIST的入門例子作為演示
雖然TensorFlow運行過程中可以自動下載數據集,但是經常容易斷開連接且下載速度很慢。可以先下載數據集,MNIST數據集的官網是Yann LeCun's website
下載數據集 :
#訓練集圖片
wget http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
#訓練集圖片對應的數字標簽
wget http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
#測試集圖片
wget http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
#測試集圖片對應的數字標簽
wget http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
並放在當前目錄下的 MNIST_data
文件夾下
下載 Tensorflow 源碼和例子
git clone https://github.com/tensorflow/tensorflow.git
# 當網絡質量較差無法下載時,可以從國內鏡像下載
git clone https://gitee.com/mirrors/tensorflow.git
# 復制例子到當前目錄
cp tensorflow/tensorflow/examples/tutorials/mnist/mnist_with_summaries.py ./
修改 mnist_with_summaries.py
文件中關於數據集和 TensorBoard log 目錄的配置:
將如下內容
mnist = input_data.read_data_sets(FLAGS.data_dir,fake_data=FLAGS.fake_data)
parser.add_argument(
'--data_dir',
type=str,
default=os.path.join(os.getenv('TEST_TMPDIR', '/tmp'),
'tensorflow/mnist/input_data'),
help='Directory for storing input data')
parser.add_argument(
'--log_dir',
type=str,
default=os.path.join(os.getenv('TEST_TMPDIR', '/tmp'),
'tensorflow/mnist/logs/mnist_with_summaries'),
help='Summaries log directory')
修改為:
mnist = input_data.read_data_sets("./MNIST_data",fake_data=FLAGS.fake_data)
parser.add_argument(
'--data_dir',
type=str,
default=os.path.join(os.getenv('TEST_TMPDIR', './'),
'MNIST_data'),
help='Directory for storing input data')
parser.add_argument(
'--log_dir',
type=str,
default=os.path.join(os.getenv('TEST_TMPDIR', './'),
'logs'),
help='Summaries log directory')
然后開始訓練並輸出log
python3 mnist_with_summaries.py
Accuracy at step 990: 0.9685
然后打開 TensorBoard
tensorboard --logdir=./logs
TensorBoard 1.13.1 at http://localhost:6006 (Press CTRL+C to quit)
可以查看支持的7種可視化:
- SCALARS : 展示訓練過程中的准確率、損失值、權重/偏置的變化情況
- IMAGES : 展示訓練過程中記錄的圖像
- AUDIO : 展示訓練過程中記錄的音頻
- GRAPHS : 展示模型的數據流圖,以及訓練在各個設備上消耗的內存和時間
- DISTRIBUTIONS : 展示訓練過程中記錄的數據的分布圖
- HISTOGRAMS : 展示訓練過程中記錄的數據的柱狀圖
- EMBEDDINGS : 展示詞向量(如 Word2Vec )后的投影分布
SCALARS 面板
左邊是以下選項,包括 split on undercores (用下划線分開顯示) 、 data downloadlinks(數據下載鏈接)、smoothing (圖像的光滑程度) 以及 horizontal axis (水平軸)的表示,其中水平軸有三種(step 代表迭代次數,relative代表訓練集和測試集的相對值,wall代表時間)
右邊的圖分別是 准確率 交叉熵損失函數值 每一層的偏置(biases)和權重(weights),包括每次迭代中的最大值、最小值、平均值和標准差
IMAGE 面板
展示了訓練數據集和測試集經過預處理后圖片的樣子
GRAPHS 面板
對理解神經網絡結構最有幫助的一個面板,它直觀地展示了數據流圖。節點之間的連線即為數據流,連線越粗,說明在兩個節點之間流動的張量(tensor)越多
在面板左側,可以選擇迭代步驟。可以用不同color(顏色)來表示不同的 structure(整個流圖的結構),或者用不同的 color 來表示不同的 device (設備)。
當我們選擇特定的某次迭代時,可以顯示出各個節點的 computer time (計算時間) 以及 memory (內存消耗)
DISTRIBUTIONS 面板
用平面來表示來自特定層的激活前后、權重和偏置的分布
HISTOGRAMS 面板
主要是立體地展現來自特定層的激活前后、權重和偏置的分布
3. TensorFlow 基礎
顧名思義, TensorFlow 是指 “張量的流動”。TensorFlow 的數據流圖是由 節點(node) 和邊 (edge)組成的有向無環圖(directed acycline graph , DAG) TensorFlow 由 Tensor 和 Flow 兩部分構成,Tensor代表了數據流圖中的邊 ,而 Flow 這個動作代表了數據流圖中節點所做的操作
TensorFlow 支持卷積神經網絡(convolutional neural network , CNN)和循環神經網絡(recurrent neural network , RNN)以及 RNN 的一個特例 長短期記憶網絡 (long short-term memory , LSTM)
特性
- 高度的靈活性 :采用數據流圖(date flow graph)
- 可移植性
- 自動求微分:只需要定義預測模型的結構和目標函數
- 多語言支持:核心部分使用C++實現
- 最優化性能
3.1 數據流圖
3.1.1 邊
邊 有兩種連接關系:數據依賴和控制依賴。其中,實線邊表示數據依賴,代表數據,即張量。任意維度的數據統稱為張量。張量在數據流圖中從前向后流動一遍就完成了一次前行傳播(forward propagation),而 殘差(數理統計中,殘差指實際觀察值與訓練估計值之間的差)從后向前流動一遍就完成了一次反向傳播
還有一種邊,一般畫為虛線邊,稱為 控制依賴 (control dependency),可以用於控制操作的運行,這被用來確保 happens-before 關系,這類邊上沒有數據流過,但源節點必須在目的節點開始前完成執行。常用代碼:
tf.Graph.control_dependencies(control_inputs)
有關圖以及張量的實現源碼在 /tensorflow/python/framework/ops.py
3.1.2 節點
又稱為算子,代表一個操作,一般用來表示施加的數學運算,也可以表示 數據輸入(feed in)的起點以及 輸出(push out) 的終點,或者讀取/寫入 持久變量(persistent variable)的終點。
與操作相關的代碼位於 /tensorflow/python/ops/
目錄下
3.1.3 圖
把操作任務描述成 有向無環圖
3.1.4 會話
啟動圖的第一步是創建一個會話。要創建並運行操作的類,在Python的API中使用 tf.Session,在C++的API中使用tensorflow::Session。示例:
with tf.Session() as sess:
result = sess.run([product])
print result
在調用 Session 對象的 run() 方法來執行圖時,傳入一些 Tensor ,這個過程叫做 填充 (feed);返回的結果類型根據輸入的類型而定,這個過程叫做取回(fetch)
與會話有關的代碼位於 /tensorflow/python/client/session.py
會話主要有兩個 API 接口: Extend 和 Run 。Extend 操作是在 Graph 中添加節點和邊 ,Run 操作是輸入計算的節點和填充必要的數據后,進行運算並輸出運算結果
3.1.5 設備
指一塊可以用來運算並且擁有自己的地址空間的硬件,為了實現分布式執行操作,充分利用計算資源,可以明確指定操作在哪個設備上執行。
with tf.Session() as sess:
with tf.device("/gpu:1")
result = sess.run([product])
print result
與設備有關的代碼位於 /tensorflow/python/framwork/device.py
3.2 優化方法
目前加速訓練的優化方法都是基於梯度下降的,只是細節上有些差異。梯度下降是求函數極值得一種方法,學習到最后就是求損失函數的極值問題。
TensorFlow 提供了很多 優化器(optimizer)
- class tf.train.GradiwentDescentOptimizer # 梯度下降法(BGD 和 SGD)
- class tf.train.AdadeltaOptimizer
- class tf.train.AdagradDAOptimizer
- class tf.train.MomentumOptimizer
- class tf.train.AdamOptimizer
- class tf.train.FtrlOptimizer
- class tf.train.RMSPropOptimizer
其中 BGD 、 SGD 、Momentum 和 Nesterov Momentum 是手動指定學習率的,其余算法能夠自動調節學習率
3.2.1 BGD
全稱是 batch gradient descent 即批梯度下降,優點是使用所有訓練數據計算,能夠保證收斂,並且不需要逐漸減小學習率;缺點是,每一步都需要使用所有的訓練數據,隨着訓練的進行,速度會越來越慢
3.2.2 SGD
stochastic gradient descent 即隨機梯度下降,將數據集隨機拆分成一個個批次(batch),隨機抽取一批數據來更新參數,所以也稱為 MBGD (minibatch gradient descent)。在訓練集很大時仍然以較快的速度收斂。缺點:(1)抽取時不可避免地梯度會有誤差,需要手動調整學習率,但是選擇合適的學習率比較困難。(2)容易收斂到局部最優
3.2.3 Momentum
模擬物理學中動量的概念,更新時在一定程度上保留之前的更新方向,利用當前的批次再微調本次的更新參數,因此引入了一個新變量 v(速度),作為前幾次梯度的累加 ,因此,能過更新學習率,在下降初期,前后梯度方向一致時,能夠加速學習;在下降的中后期,在局部最小值的附近來回震盪時,能夠抑制震盪,加快收斂
3.2.4 Nesterov Momentum
自適應學習率優化方法
3.2.5 Adagrad
自適應地為每個參數分配不同的學習率,能夠控制每個維度的梯度方向。優點是能夠實現學習率的自動更改:如果本次更新時梯度大,學習率就衰減得快一些;如果這次更新時梯度較小,學習率衰減就慢一些。缺電:學習率單調遞減,在訓練后期學習率非常小,並且需要手動設置一個全局的初始學習率
3.2.6 Adadelta
為了解決Adagrad 的缺點,用一階的方法近似模擬二階牛頓法
3.2.7 RMSProp
引入一個衰減函數,使每一個回合都衰減一定的比例。在實踐中,對 循環神經網絡 (RNN)效果很好。
3.2.8 Adam
源於 自適應矩估計(adaptive moment estimation)。Adam 法根據損失函數針對每個參數的梯度的一階矩估計動態調整每個參數的學習率。
3.2.9 方法比較
在不怎么調整參數的情況下,Adagrad法比 SGD法和 Momentum 法 更穩定,性能更優;精確調整參數的情況下,SGD法和Momentum 法在收斂速度和准確性上優於 Adagrad法。
3.3設計理念:
3.3.1 將圖的定義和圖的運行完全分開
在現代深度學習框架中,Torch 是典型的命令式的,Caffe、MXNet 采用了兩種編程模式混合的方法,而 TensorFlow 完全采用符號式編程
符號式計算一般是先定義各種變量,然后建立一個數據流圖,在數據流圖中規定各個變量之間的計算關系,最后需要對數據流圖進行編譯,把需要運算的輸入放進去后,在整個模型中形成數據流,從而形成輸出值
3.3.2 涉及的運算都要放在圖中
圖的運行只發生在 會話 (session) 中。開啟會話后,就可以用數據去填充節點,進行運算;關閉會話后,就不能計算了。會話提供了操作運行和Tensor 求值的環境。
參考資料
- 安裝、配置Jupyter Notebook快速入門教程
- 訪問虛擬機中的jupyter
- JupyterLab——更具生產力的Jupyter環境
- 玩轉Jetson Nano(三)安裝TensorFlow GPU
- 《TensorFlow技術解析與實戰》 李嘉璇 著 2017年4月版