# Practical Deep Learning for Coders
:::info
:earth_asia: **前言**
本筆記是fast.ai的學習紀錄。
* [主頁](https://www.fast.ai/)
* [線上課程](https://course.fast.ai/)
* [軟體說明文件](https://docs.fast.ai/)
* [論壇](https://forums.fast.ai/)
* [論壇的課程討論區](https://forums.fast.ai/t/lesson-1-official-topic/95287)
* [書籍](https://www.amazon.com/Deep-Learning-Coders-fastai-PyTorch/dp/1492045527)
:::
## 1: Getting started
![](https://hackmd.io/_uploads/B14EJ27an.png =80%x)
* [Is it a bird? Creating a model from your own data](https://www.kaggle.com/code/jhoward/is-it-a-bird-creating-a-model-from-your-own-data)
* [Jupyter Notebook 101](https://www.kaggle.com/code/jhoward/jupyter-notebook-101)
* [Slides.pdf](https://github.com/muellerzr/Practical-Deep-Learning-for-Coders-2.0/blob/master/Computer%20Vision/01_Slides.pdf)
* [1: Getting started - Questions](/yF5ZR3FJRwqr_3UI_XUuvA)
## 2: Deployment
> 部署(Deployment)指的是將經過訓練和測試的深度學習模型,應用到實際應用程序或生產環境中的過程。
> 本節說明如何匯出訓練好的模型並部署成一個互動式網頁,另外也說明如何執行資料清理,提高影像辨識模型的準確度。
![](https://hackmd.io/_uploads/r1_4khXT3.png =80%x)
* [Saving a basic fastai model](https://www.kaggle.com/code/jhoward/saving-a-basic-fastai-model/notebook)
* [Gradio + HuggingFace Spaces: A Tutorial](https://www.tanishq.ai/blog/gradio_hf_spaces_tutorial)
* [Is it a shark?](https://www.kaggle.com/dan199537/is-it-a-shark/edit)
* [2: Deployment - Questions](/Xyvoky-OQrqitgdiW-qr9g)
* [Introduction to Image Augmentation using fastai](https://www.kaggle.com/code/init27/introduction-to-image-augmentation-using-fastai)
## 3: Neural net foundations
> 本節介紹一個基本的類神經網路的組成。
> ![](https://hackmd.io/_uploads/S1m9RusT2.png =80%x)
> * 訓練深度學習模型的主要目的是透過類神經網路的計算得到最佳的模型權重和偏差。可以想像成我們想解決的分類或推理問題都可以總結成一條非常複雜的方程式y=f(x),其中f(x)包含了大量的係數,而透過訓練我們可以得到一條跟f(x)非常相似的方程式f'(x),其中f'(x)包含的係數不一定與現實中f(x)的係數們一致,但已經是我們從手邊的數據中可以計算出的最佳解。
> * 訓練前要準備好訓練集和驗證集兩套數據,其中都應該要事先標記好各自的正解為何。
> * 訓練開始時,訓練集中的對象會被解析成一連串的輸入數據,從輸入層進入到模型中(例如一張28x28的圖片會以像素為單位,形成一個長度784的矩陣作為模型的輸入)。
> ![](https://hackmd.io/_uploads/Sk4mktj62.png =80%x)
> * 訓練開始時,訓練集中的數據會以batch為單位做訓練、算梯度、更新權重和偏差。
> * 每個"神經元(neuron)"的輸入是輸入層或前一層的輸出,進入神經元後會各自被乘以一個權重再加上一個偏差,並在加總後經過激勵函數(例如ReLU函數)形成輸出給輸出層或下一層所用。
> * 經過層層計算,最後輸出層得到的預期結果,會與訓練集中該對象標記的正解,兩者用損失函數(如MSE)計算出一個loss,作為本批次訓練的性能評估指標。
> * 利用反向傳播算法,計算本批次訓練的loss相對於權重的梯度,這些梯度數值會告訴我們等會應該如何微調參數以進一步減小損失。
> ![](https://hackmd.io/_uploads/B1efeFoah.png =50%x)
> * 選擇適當的learning rate,利用梯度下降算法更新權重和偏差。
> ![](https://hackmd.io/_uploads/SkT5ltipn.png =50%x)
> ![](https://hackmd.io/_uploads/SJaMWFo63.jpg =80%x)
> * 將前面的過程重複進行,直到做完所有batch,即稱為完成一個epoch的訓練。
> * 最後用驗證集的數據計算metrics,檢查本epoch得到的模型參數是否有欠擬合或過擬合的問題。
> * 重複進行幾個epoch,直到驗證出來的metric符合需求。
> * 模型的訓練過程中牽涉到大量的矩陣乘法和梯度計算,這正是GPU擅長的。
![](https://hackmd.io/_uploads/rkgnTuop3.png =80%x)
* [Which image models are best?](https://www.kaggle.com/code/jhoward/which-image-models-are-best/)
* [How does a neural net really work?](https://www.kaggle.com/code/jhoward/how-does-a-neural-net-really-work)
* [Titanic - Machine Learning from Disaster](https://www.kaggle.com/competitions/titanic/)
* [Neural Network In 5 Minutes](https://www.youtube.com/watch?v=bfmFfD2RIcg)
* [複習一下ML的整個訓練過程](https://ithelp.ithome.com.tw/articles/10218158)
* [A Neural Network Playground](https://playground.tensorflow.org/#activation=tanh&batchSize=10&dataset=circle®Dataset=reg-plane&learningRate=0.03®ularizationRate=0&noise=0&networkShape=4,2&seed=0.53967&showTestData=false&discretize=false&percTrainData=50&x=true&y=true&xTimesY=false&xSquared=false&ySquared=false&cosX=false&sinX=false&cosY=false&sinY=false&collectStats=false&problem=classification&initZero=false&hideText=false)
* [3: Neural net foundations - Questions](/CMItdSafSUO2kbOvFYBFdg)
## 4: Natural Language (NLP)
> 本節介紹NLP,基本上訓練流程和前面教得差不多,但因為NLP是對文字做運算所以前處理的時候多了tokenization和numericalization兩步驟。
> 另外本節改用Hugging Face Transformers來做遷移式學習,講師的用意是讓大家試用除了fast.ai之外的其他API,來複習先前學的那套深度學習概念和流程。
![](https://hackmd.io/_uploads/ryW2zWRA2.png =80%x)
* [Getting started with NLP for absolute beginners](https://www.kaggle.com/code/jhoward/getting-started-with-nlp-for-absolute-beginners)
* [4: Natural Language (NLP) - Questions](/HuuMMe7DQ5aRCmE3U1527Q)
## 5: From-scratch model
> 本節說明如何不靠fast.ai的API,從無到有建立一個深度學習模型。
> 這麼做的目的是在未來套用pytorch、fast.ai等框架的時候,我們仍然對其背後的運作原理有概念,也才比較知道怎麼去修正或改善模型。
![](https://hackmd.io/_uploads/B13-5VvJ6.png =80%x)
* [Linear model and neural net from scratch](https://www.kaggle.com/code/jhoward/linear-model-and-neural-net-from-scratch)
* [Why you should use a framework](https://www.kaggle.com/code/jhoward/why-you-should-use-a-framework)
* [5: From-scratch model - Questions]()
## 6: Random forests
> 本節介紹除了深度學習之外的另一種機器學習方法-集成學習(ensemble learning),並說明binary split、decision tree、random forest、bagging/boosting等概念。
> 另外本節也介紹了fastkaggle框架,以及有哪些細節可以更進一步提升深度學習模型的準確度。
![](https://hackmd.io/_uploads/SyQroPwJa.png)
:::spoiler 課程中的Q&A
* Does increasing the number of trees always translate to a better error?
* 是,但加到一個程度後效益會不明顯,而且會影響未來做推理時的時間。講師說他的隨機森林通常不會用超過一百顆樹。
![](https://hackmd.io/_uploads/rJoZPIPy6.png =70%x)
* Could we create a bagging model which includes fast.ai deep learning models?
* 可以,bagging只是一種整合模型的方法,並沒有限用於哪種模型。未來也會有類似的範例。
* Would you ever exclude a tree from a forest you've had if it had a bad Out of Bag Error?
* 不可以,一旦刪除樹就表示你加入了偏見,這樣就不符合Bagging的運作原理。
* Can you overfit a Random Forest?
* 通常不會,加越多樹不會把模型變糟。但你如果沒有建太多樹卻又讓樹長得很深,就有可能過擬合。
* Do you create different Kaggle notebooks for each model that you try?
* 是,講師說他一個Kaggle只會拿來提交一次,所以也不太另外做版本控制。
* 對AutoML的看法?
* 從來不用AutoML(I like being highly intentional, you know, I like to think more like a scientist and have hypotheses and test them carefully and come up with conclusions, which then I implement.)
:::
* [How random forests really work](https://www.kaggle.com/code/jhoward/how-random-forests-really-work/)
* [First Steps: Road to the Top, Part 1](https://www.kaggle.com/code/jhoward/first-steps-road-to-the-top-part-1)
* [Small models: Road to the Top, Part 2](https://www.kaggle.com/code/jhoward/small-models-road-to-the-top-part-2)
* [Ch9. Tabular Modeling Deep Dive](https://github.com/fastai/fastbook/blob/master/09_tabular.ipynb)
* [6: Random forests - Questions]()