棋靈王,2001
座位分配
• 投影片會出現的網址: https://goo.gl/wDUfBk
windows Mac, Ubuntu, Linux
我
Introduction to
黃子瑋
about me
這兩年做的和深度學習相關的事:
• image processing
• medical image
• predicts human age given brain MRI images
• mechanical engineering
• predicts system friction given environment parameters
• ITRI projects
• 3D object recognition, image deblurring
• 打雜
• GPU cluster management (low cost, but high performance and usability)
• isbi-2017.avi
• isbi-2017.jpg
outline
• 蜻蜓點水比較各個 framework
• 介紹 PyTorch
• 安裝 PyTorch
• 確認環境作動正常
• 介紹 PyTorch 架構
• 實作手寫數字辨識
• 歡迎提問,不懂的不會只有你一個
一定要看!十種網友最推的深度學習環境
• Caffe
• Keras
• MatConvNet
• CNTK
• MXNet
• TensorFlow
• Chainer
• PyTorch
• Theano
一定要看!十種網友最推的深度學習環境
• Keras
• CNTK
• MXNet
• TensorFlow
• Chainer
• PyTorch
Keras
keras
TensorFlow CNTK Theano
Caffe / torch7
• trace code 的好選擇
• 你真的知道 back-propagation 在算什麼嗎?
• Static graph, written in C / C++
• 大量使用 BLAS, CUBLAS library
https://github.com/torch/nn
torch
• lua 很難搞
• lua 的現成 function 很少
• 列出某資料夾的檔案
• 讀取文字檔
• 讀取CSV
• (╯°Д°)╯ ┻━┻
• Tensor 的 index 很醜 [{{},{},{}}]
numpy
• NumPy is the fundamental package for scientific computing with Python.
• PyTorch, Tensorflow, chainer, mxnet, Theano 都可以把資料以 numpy
格式交換
• PIL, matplotlib, scikit-image 也是以 numpy 為交換媒介。
http://www.numpy.org/
Static vs. Dynamic graph frameworks
• Static: define and run
• Caffe
• torch
• Tensorflow
• Dynamic: define by run
• Chainer
• pytorch
compiled language vs. interpreted language
resource allocation
run step by step
speed
debug 不方便
• tensorflow 的 sess.run()
• caffe 的 prototxt
TensorFlow vs. PyTorch /static vs. dynamic
TensorFlow vs. PyTorch 就好比:
Chainer inspires PyTorch
Comparison
udn
小結 (dynamic graph 的特性)
• PyTorch ~= torch + autograd + chainer + numpy
• PyTorch 靈活,適合研究。
• 速度比 caffe2 慢,不適合部署。
answer by Yangqing Jia (creates caffe/caffe2)
cs231n (有名的深度學習線上課程)表示:
學弟表示:
學長 , Tensorflow 怎麼這麼慢啊?
我也不知道耶XD
Install PyTorch
• install anaconda3:(用 python3,世界更美好)
• conda install pytorch torchvision cuda80 -c soumith
• conda install pytorch torchvision -c soumith
without CUDA
anaconda
• 是一種 python 的套件管理程式 (另一個是 pip)
• 不需管理員權限即可安裝想要的套件
• 就算弄壞了也不會害到機器上的其他人
• 安裝完後打開 terminal
• windows:winkey+R 然後輸入 cmd
• Mac:開啟 spotlight, 輸入terminal
• 接著輸入 ipython, 應該會看到 3.6 這個數字
Install PyTorch (in Windows 10)
debug with vscode / PDB / PyCharm / Jupyter
• 以 vscode 為例
• 建立一個專案用的資料夾
• 安裝 extension: Python
• 設定 debugger
• F5 執行到下個中斷點
• F10 執行這一行
打開他
安裝 extension
設定 debugger
或是使用 jupyter (anaconda 有送)
• 啟動 server
• 開啟瀏覽器
notebook 基本操作
enter 是換行,shift-enter 執行 cell
ZZZ
• 休息30分鐘
• 討論時間
• 沒跟上的請舉手
• 蜻蜓點水比較各個 framework
• 介紹 PyTorch
• 安裝 PyTorch
• 確認環境作動正常
• 介紹 PyTorch 架構
• 實作手寫數字辨識
• 歡迎提問,不懂的不會只有你一個
PyTorch modules
torch.autograd dynamic graph 的基礎,負責維持歷
史紀錄
torch.nn nn: Neural Network
torch.util.dataset 負責資料預預處理
single thread
torchvision.dataloader 負責資料讀取以及預處理
multi thread
torchvision 包含一些處理影像的函數, 訓練好
的網路,資料集讀取的類別。
autograd
• ALL history of graph computation is recorded
• in Variable
• Tensor 就是高維矩陣
Variable
torch.Tensor:
input
Var 2Var 1 Var 3
Var 2Var 1 Var 3 L
autograd (forward pass)
• ALL history of graph computation is recorded
• in Variable
• Tensor 就是高維矩陣
Variable
torch.Tensor:
input
pointer to
previous tensor
Var 2Var 1 Var 3
Var 2Var 1 Var 3 L
torch.Tensor:
output
(self)
autograd (backward pass)
• ALL history of graph computation is recorded
• in Variable
• Tensor 就是高維矩陣
Var 2Var 1 Var 3
Var 2Var 1 Var 3 L
Variable
pointer to previous
tensor
torch.Tensor:
output
(self)
torch.Tensor:
input
(previous)
torch.Tensor:
grad_output
autograd (backward pass)
• ALL history of graph computation is recorded
• in Variable
• Tensor 就是高維矩陣
Var 2Var 1 Var 3
Var 2Var 1 Var 3 L
Variable
pointer to previous
tensor
torch.Tensor:
output
(self)
torch.Tensor:
input
(previous)
torch.Tensor:
grad_output
torch.Tensor:
grad_output
nn
• What composes a ReLU layer?
• What composes a Convolutional layer?
• What composes a Linear (FullyConnected) layer?
torch.nn vs. torch.nn.functional
• a bunch of functions/layers:
• ReLU
• Linear
• Conv
• Pooling
• ConvTranspose
• Sigmoid
• LogSoftMax
• …
http://pytorch.org/docs/master/nn.html#torch-nn-functional
Variable
torch.Tensor: input
torch.Tensor:
output
(self)
function
Var 2Var 1
Function object
前一層的輸出
(Linear layer)
Linear (fully connected)
• 𝑌 = 𝑊𝑋 + 𝑏
https://github.com/pytorch/pytorch/blob/master/torch/nn/functional.py
BiasWeight
Function object
前一層的輸出
(這一層的輸入)
(Linear layer)
這一層的輸出
BiasWeight
Function object
前一層的輸出
(這一層的輸入)
(Linear layer)
這一層的輸出
torch.nn.Linear
torch.autograd.variable
torch.nn.functional
Weight
Function object
這一層的梯度
(繼續往回送)
(Linear layer)
前一層的梯度
torch.nn.Linear
torch.autograd.variable
torch.nn.functional
grad_Weight
Bias
grad_Bias
Tensors and Variables
• x = torch.Tensor(3, 5)
• X = Variable(x)
• x.size()
• Z = X+Y
Variable
torch.Tensor:
input
pointer to
previous tensor
torch.Tensor:
output
(self)
a little complex graph: D = B+C, B = A, C = A
B
A D
L
C
B
A D
C
Tensors and Variables
• a = torch.Tensor(3, 5)
• A = Variable(a)
• B = A
• C = A
• D = B+C
• print(D.grad_fn)
create a MNIST network
torchvision.datasets (預-預處理)
• 通常是建立 (filepath, (label/classID/value)) 的成對資訊
• [(‘n1002.png’, 5), (‘n1008.png’, 3), …, (‘n50008.png’, 9),]
torch.utils.data.Dataloader
• 從前一步的 dataset 取得訓練資料
• 真的把檔案從 HDD 讀取出來放到 RAM 裡面
• 預處理(加雜訊 反轉 平移 normalize …)
• 關鍵:num_workers 要大於 0 才會平行處理
• GPU 才不會沒事做
• what are sample, batch, epoch ?
optimization
• 𝑊𝑡+1 = 𝑊𝑡 − 𝜂Δ𝑊
• get all parameter with model.parameters()
• weight, bias, … 都會被抓過來
training loop
• clear previous gradient!!!
• get a batch of data
• forward (output get)
• by sending input tensor to model
• compute loss
• back propagation (gradient get)
• by .backward() call
• update weight
• by optim.step()
save model
• torch.save(model, ‘file.pth’)
• model = torch.load(‘file.pth’)
fine tune / graph surgery / freeze layers
• 假設現在要辨識
• ㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐ
ㄑㄒㄓㄔㄕㄖㄗㄘㄙ ㄧㄨ
ㄩ ㄚㄛㄝ ㄞㄟㄠㄡㄢㄣㄤ
ㄥㄦ
fine tune / graph surgery / freeze layers
• 假設現在要辨識
• ㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐ
ㄑㄒㄓㄔㄕㄖㄗㄘㄙ ㄧㄨ
ㄩ ㄚㄛㄝ ㄞㄟㄠㄡㄢㄣㄤ
ㄥㄦ
numpy indexing
• x[:, :14, :]
• 測試手寫字只看的到上半部辨識的出來嗎?
log training status
• pip install tensorboardX
certified
You can log
• scalar (loss, accuracy)
• image (visualize filter)
• audio
• histogram (of the weight distribution)
• text
• embedding (visualize the result)
• graph structure
http://tensorboard-pytorch.readthedocs.io/en/latest/tensorboard.html
usage
• http://tensorboard-pytorch.readthedocs.io/en/latest/tensorboard.html
一個禮拜之內說明書被看了幾次
https://goo.gl/duUcqs
回家作業
• Fashion MNIST
notes on open source projects
• 當個好人
• 解釋為什麼不能滿足某種 feature request
• 未來實作的可能性/時間表
• 推坑
• Push 到 github 之後:
• 用 Travis-CI 自動化測試
• 用 docstring + readthedocs.io 自動產生文件
Trend
• 像 LLVM 的 NNVM
• intermediate representation
• heterogeneous computing
https://github.com/lanpa/tensorboard-pytorch/issues/7
conclusion
• For research: use Tensorflow or PyTorch
• For deployment: use Tensorflow, MXNet, or Caffe2
thanks
parallel PyTorch (over GPUs)
test
• volatile speed up
dynamic graph
• LSTM

[students AI workshop] Pytorch

Editor's Notes

  • #3 有問題可以互相幫忙
  • #5 找家教,教育訓練 小幫手名額
  • #6 low cost but high performance
  • #7 不能做太準
  • #8 我會的東西很雜
  • #9 因為程式很簡單 就像寫一般的python一樣 所以重點放在前面 根據經驗 一百人的場子的教學活動。實作會是debug大會
  • #10 我隨便寫的 你用過什麼? 有些沒什麼人用了
  • #11 你用過什麼?
  • #12 先天缺點:必須支援不同的 backend 所以功能有所限制
  • #16 簡單的分類法:
  • #17 tensorflow 的
  • #20 幾乎一樣
  • #21 pytorch is faster and prosperous, for research facebook use caffe2 for production
  • #23 原來是做自動駕駛
  • #24 買硬體+蓋房子就有300億JPY
  • #25 dynamic graph
  • #39 output 就是這個variable 的值
  • #40 output 就是這個variable 的值 複製 input
  • #41 紫+綠 : 這一層要回傳的grad
  • #43 kernel bias weight, bias
  • #44 沒參數的就很方便
  • #45 我們來舉一個例子:
  • #53 F for functional 我假設你用過 其他的framework,python 也夠熟
  • #57 optim.step()
  • #63 這東西原本叫
  • #68 不用看說明書也會用
  • #75 把他想成是 LLVM 也會有 IR 前後端分離