notebook 是 Donald Knuth 在 1984 年提出的文字表達化編程的一種形式。在文字表達化編程中,直接在代碼旁寫出敘述性文檔,而不是另外編寫單獨的文檔。html
Jetson Nano中已經安裝了Python3.6版本java
# 安裝pip3 sudo apt-get install python3-pip python3-dev
安裝後pip是9.01版本,須要把它升級到最新版,升級後pip版本爲19.1.1。node
# 升級pip3 python3 -m pip install --upgrade pip pip3 install --upgrade --user
升級後會有一個小Bug,須要手動改一下
打開pip3文件python
sudo vim /usr/bin/pip3
將原來的linux
from pip import main if __name__ == '__main__': sys.exit(main())
改爲git
from pip import __main__ if __name__ == '__main__': sys.exit(__main__._main())
修改結束後保存。運行 pip3 -V
成功後顯示github
pip3 -V pip 19.1.1 from /home/albert/.local/lib/python3.6/site-packages/pip (python 3.6)
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
安裝擴展web
pip3 install tqdm jupyter_contrib_nbextensions --user #安裝關聯的 JavaScript 和 CSS 文件: jupyter contrib nbextension install --user # 安裝主題,安裝完成後,咱們將會有一個jt的命令。 pip3 install --upgrade jupyterthemes --user # jt 的 help ,以後能夠自行配置主題 jt --help
生成配置文件redis
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 或虛擬機,本身用不須要安全設置
在終端或控制檯中輸入
jupyter notebook --ip=192.168.1.115
服務器會在你運行此命令的目錄中啓動。
http://192.168.1.115:8888/?token=d2e1e7e1e6e59f20725237958ade0c1f9f24b3a31cfaec5f
經過在服務器主頁上選中 notebook 旁邊的複選框,而後點擊「Shutdown」(關閉),你就能夠關閉各個 notebook。可是,在這樣作以前,請確保你保存了工做!不然,在你上次保存後所作的任何更改都會丟失。下次運行 notebook 時,你還須要從新運行代碼。
經過在終端中按兩次 Ctrl + C,能夠關閉整個服務器。再次提醒,這會當即關閉全部運行中的 notebook,所以,請確保你保存了工做!
工具欄包含了保存按鈕,但 notebook 也會按期自動保存。標題右側會註明最近一次的保存。你可使用保存按鈕手動進行保存,也能夠按鍵盤上的 Esc,而後按 s。按 Esc 鍵會變爲命令模式,而 s 是「保存」的快捷鍵。
像代碼單元格同樣,按 Shift + Enter 或 Ctrl + Enter 可運行 Markdown 單元格,這會將 Markdown 呈現爲格式化文本。加入文本可以讓你直接在代碼旁寫出敘述性文檔,以及爲代碼和思路編寫文檔。
Magic-What?
注意:這些 Magic 關鍵字是特定於普通 Python 內核的關鍵字。若是使用其餘內核,這些關鍵字頗有可能無效。
Magic-How?
%matplotlib
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")
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
pip3 install jupyterlab --user
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
瀏覽器訪問 http://localhost:8888
# numpy scipy pandas matplotlib sklearn sudo apt install python3-numpy python3-scipy python3-pandas python3-matplotlib python3-sklearn libhdf5-serial-dev hdf5-tools
#安裝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
查看 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'
%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
%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
用於教學目的的簡單神經網絡的在線演示、實驗的圖形化平臺,很是強大的可視化了神經網絡的訓練過程。
http://playground.tensorflow.org/
pip3 install tensorboard --user
運行手寫數字識別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種可視化:
左邊是如下選項,包括 split on undercores (用下劃線分開顯示) 、 data downloadlinks(數據下載連接)、smoothing (圖像的光滑程度) 以及 horizontal axis (水平軸)的表示,其中水平軸有三種(step 表明迭代次數,relative表明訓練集和測試集的相對值,wall表明時間)
右邊的圖分別是 準確率 交叉熵損失函數值 每一層的偏置(biases)和權重(weights),包括每次迭代中的最大值、最小值、平均值和標準差
展現了訓練數據集和測試集通過預處理後圖片的樣子
對理解神經網絡結構最有幫助的一個面板,它直觀地展現了數據流圖。節點之間的連線即爲數據流,連線越粗,說明在兩個節點之間流動的張量(tensor)越多
在面板左側,能夠選擇迭代步驟。能夠用不一樣color(顏色)來表示不一樣的 structure(整個流圖的結構),或者用不一樣的 color 來表示不一樣的 device (設備)。
當咱們選擇特定的某次迭代時,能夠顯示出各個節點的 computer time (計算時間) 以及 memory (內存消耗)
用平面來表示來自特定層的激活先後、權重和偏置的分佈
主要是立體地展示來自特定層的激活先後、權重和偏置的分佈
顧名思義, 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)
特性
邊 有兩種鏈接關係:數據依賴和控制依賴。其中,實線邊表示數據依賴,表明數據,即張量。任意維度的數據統稱爲張量。張量在數據流圖中從前向後流動一遍就完成了一次前行傳播(forward propagation),而 殘差(數理統計中,殘差指實際觀察值與訓練估計值之間的差)從後向前流動一遍就完成了一次反向傳播
還有一種邊,通常畫爲虛線邊,稱爲 控制依賴 (control dependency),能夠用於控制操做的運行,這被用來確保 happens-before 關係,這類邊上沒有數據流過,但源節點必須在目的節點開始前完成執行。經常使用代碼:
tf.Graph.control_dependencies(control_inputs)
有關圖以及張量的實現源碼在 /tensorflow/python/framework/ops.py
又稱爲算子,表明一個操做,通常用來表示施加的數學運算,也能夠表示 數據輸入(feed in)的起點以及 輸出(push out) 的終點,或者讀取/寫入 持久變量(persistent variable)的終點。
與操做相關的代碼位於 /tensorflow/python/ops/
目錄下
把操做任務描述成 有向無環圖
啓動圖的第一步是建立一個會話。要建立並運行操做的類,在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 操做是輸入計算的節點和填充必要的數據後,進行運算並輸出運算結果
指一塊能夠用來運算而且擁有本身的地址空間的硬件,爲了實現分佈式執行操做,充分利用計算資源,能夠明確指定操做在哪一個設備上執行。
with tf.Session() as sess: with tf.device("/gpu:1") result = sess.run([product]) print result
與設備有關的代碼位於 /tensorflow/python/framwork/device.py
目前加速訓練的優化方法都是基於梯度降低的,只是細節上有些差別。梯度降低是求函數極值得一種方法,學習到最後就是求損失函數的極值問題。
TensorFlow 提供了不少 優化器(optimizer)
其中 BGD 、 SGD 、Momentum 和 Nesterov Momentum 是手動指定學習率的,其他算法可以自動調節學習率
全稱是 batch gradient descent 即批梯度降低,優勢是使用全部訓練數據計算,可以保證收斂,而且不須要逐漸減少學習率;缺點是,每一步都須要使用全部的訓練數據,隨着訓練的進行,速度會愈來愈慢
stochastic gradient descent 即隨機梯度降低,將數據集隨機拆分紅一個個批次(batch),隨機抽取一批數據來更新參數,因此也稱爲 MBGD (minibatch gradient descent)。在訓練集很大時仍然以較快的速度收斂。缺點:(1)抽取時不可避免地梯度會有偏差,須要手動調整學習率,可是選擇合適的學習率比較困難。(2)容易收斂到局部最優
模擬物理學中動量的概念,更新時在必定程度上保留以前的更新方向,利用當前的批次再微調本次的更新參數,所以引入了一個新變量 v(速度),做爲前幾回梯度的累加 ,所以,能過更新學習率,在降低初期,先後梯度方向一致時,可以加速學習;在降低的中後期,在局部最小值的附近來回震盪時,可以抑制震盪,加快收斂
自適應學習率優化方法
自適應地爲每一個參數分配不一樣的學習率,可以控制每一個維度的梯度方向。優勢是可以實現學習率的自動更改:若是本次更新時梯度大,學習率就衰減得快一些;若是此次更新時梯度較小,學習率衰減就慢一些。缺電:學習率單調遞減,在訓練後期學習率很是小,而且須要手動設置一個全局的初始學習率
爲了解決Adagrad 的缺點,用一階的方法近似模擬二階牛頓法
引入一個衰減函數,使每個回合都衰減必定的比例。在實踐中,對 循環神經網絡 (RNN)效果很好。
源於 自適應矩估計(adaptive moment estimation)。Adam 法根據損失函數針對每一個參數的梯度的一階矩估計動態調整每一個參數的學習率。
在不怎麼調整參數的狀況下,Adagrad法比 SGD法和 Momentum 法 更穩定,性能更優;精確調整參數的狀況下,SGD法和Momentum 法在收斂速度和準確性上優於 Adagrad法。
在現代深度學習框架中,Torch 是典型的命令式的,Caffe、MXNet 採用了兩種編程模式混合的方法,而 TensorFlow 徹底採用符號式編程
符號式計算通常是先定義各類變量,而後創建一個數據流圖,在數據流圖中規定各個變量之間的計算關係,最後須要對數據流圖進行編譯,把須要運算的輸入放進去後,在整個模型中造成數據流,從而造成輸出值
圖的運行只發生在 會話 (session) 中。開啓會話後,就能夠用數據去填充節點,進行運算;關閉會話後,就不能計算了。會話提供了操做運行和Tensor 求值的環境。