Troubleshooting Deep Neural Networks -...

141
Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks Troubleshooting Deep Neural Networks Josh Tobin (with Sergey Karayev and Pieter Abbeel) 1 A Field Guide to Fixing Your Model

Transcript of Troubleshooting Deep Neural Networks -...

Page 1: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Troubleshooting Deep Neural Networks

Josh Tobin (with Sergey Karayev and Pieter Abbeel)

!1

A Field Guide to Fixing Your Model

Page 2: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Help me make this guide better!Help me find:• Things that are unclear• Missing debugging tips, tools, tricks, strategies• Anything else that will make the guide betterFeedback to: • joshptobin [at] gmail.com• Twitter thread (https://twitter.com/josh_tobin_)

!2

Page 3: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why talk about DL troubleshooting?

XKCD, https://xkcd.com/1838/

!3

Page 4: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why talk about DL troubleshooting?

!4

Page 5: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why talk about DL troubleshooting?

Common sentiment among practitioners:

80-90% of time debugging and tuning

10-20% deriving math or implementing things

!5

Page 6: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why is DL troubleshooting so hard?

!6

Page 7: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Suppose you can’t reproduce a resultLearning curve from the paper

He, Kaiming, et al. "Deep residual learning for image recognition." Proceedings of the IEEE conference on computer vision and pattern recognition. 2016.

Your learning curve

0. Why is troubleshooting hard?

!7

Page 8: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why is your performance worse?

Poor model performance

0. Why is troubleshooting hard?

!8

Page 9: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why is your performance worse?

Poor model performance

Implementation bugs

0. Why is troubleshooting hard?

!9

Page 10: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Most DL bugs are invisible0. Why is troubleshooting hard?

!10

Page 11: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Labels out of order!

Most DL bugs are invisible0. Why is troubleshooting hard?

(real bug I spent 1 day on early in my PhD)

!11

Page 12: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why is your performance worse?

Poor model performance

Implementation bugs

0. Why is troubleshooting hard?

!12

Page 13: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why is your performance worse?

Poor model performance

Implementation bugs

Hyperparameter choices

0. Why is troubleshooting hard?

!13

Page 14: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Andrej Karpathy, CS231n course notes

Models are sensitive to hyperparameters0. Why is troubleshooting hard?

!14

Page 15: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Andrej Karpathy, CS231n course notes

Models are sensitive to hyperparametersPerformance of a 30-layer ResNet with

different weight initializations

He, Kaiming, et al. "Delving deep into rectifiers: Surpassing human-level performance on imagenet classification." Proceedings of the IEEE international conference on computer vision. 2015.

0. Why is troubleshooting hard?

!15

Page 16: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why is your performance worse?0. Why is troubleshooting hard?

Poor model performance

Implementation bugs

Hyperparameter choices

!16

Page 17: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why is your performance worse?

Data/model fit

0. Why is troubleshooting hard?

Poor model performance

Implementation bugs

Hyperparameter choices

!17

Page 18: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Data from the paper: ImageNet

Data / model fitYours: self-driving car images

0. Why is troubleshooting hard?

!18

Page 19: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why is your performance worse?0. Why is troubleshooting hard?

Data/model fit

Poor model performance

Implementation bugs

Hyperparameter choices

!19

Page 20: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Why is your performance worse?

Dataset construction

0. Why is troubleshooting hard?

Data/model fit

Poor model performance

Implementation bugs

Hyperparameter choices

!20

Page 21: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Constructing good datasets is hardAmount of lost sleep over...

PhD Tesla

Slide from Andrej Karpathy’s talk “Building the Software 2.0 Stack” at TrainAI 2018, 5/10/2018

0. Why is troubleshooting hard?

!21

Page 22: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Common dataset construction issues

• Not enough data

• Class imbalances

• Noisy labels

• Train / test from different distributions

• (Not the main focus of this guide)

0. Why is troubleshooting hard?

!22

Page 23: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Takeaways: why is troubleshooting hard?

• Hard to tell if you have a bug

• Lots of possible sources for the same degradation in performance

• Results can be sensitive to small changes in hyperparameters and dataset makeup

0. Why is troubleshooting hard?

!23

Page 24: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Strategy for DL troubleshooting

!24

Page 25: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Key mindset for DL troubleshooting

Pessimism.

!25

Page 26: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Key idea of DL troubleshooting

Since it’s hard to disambiguate errors…

…Start simple and gradually ramp up complexity

!26

Page 27: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Tune hyper-parameters

Implement & debug

Strategy for DL troubleshooting

Start simple Evaluate

Improve model/data

Meets re-quirements

!27

Page 28: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Quick summaryStart

simple

Overview

• Choose the simplest model & data possible (e.g., LeNet on a subset of your data)

!28

Page 29: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Quick summary

Implement & debug

Start simple

Overview

• Choose the simplest model & data possible (e.g., LeNet on a subset of your data)

• Once model runs, overfit a single batch & reproduce a known result

!29

Page 30: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Quick summary

Implement & debug

Start simple

Evaluate

Overview

• Choose the simplest model & data possible (e.g., LeNet on a subset of your data)

• Once model runs, overfit a single batch & reproduce a known result

• Apply the bias-variance decomposition to decide what to do next

!30

Page 31: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Tune hyp-eparams

Quick summary

Implement & debug

Start simple

Evaluate

Overview

• Choose the simplest model & data possible (e.g., LeNet on a subset of your data)

• Once model runs, overfit a single batch & reproduce a known result

• Apply the bias-variance decomposition to decide what to do next

• Use coarse-to-fine random searches

!31

Page 32: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Tune hyp-eparams

Quick summary

Implement & debug

Start simple

Evaluate

Improve model/data

Overview

• Choose the simplest model & data possible (e.g., LeNet on a subset of your data)

• Once model runs, overfit a single batch & reproduce a known result

• Apply the bias-variance decomposition to decide what to do next

• Use coarse-to-fine random searches

• Make your model bigger if you underfit; add data or regularize if you overfit

!32

Page 33: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

We’ll assume you already have…

• Initial test set

• A single metric to improve

• Target performance based on human-level performance, published results, previous baselines, etc

!33

Page 34: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

We’ll assume you already have…

• Initial test set

• A single metric to improve

• Target performance based on human-level performance, published results, previous baselines, etc

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy

Running example

!34

Page 35: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Tune hyper-parameters

Implement & debug

Strategy for DL troubleshooting

Start simple Evaluate

Improve model/data

Meets re-quirements

!35

Page 36: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Normalize inputs

Starting simple

Choose a simple architecture

Simplify the problem

Use sensible defaults

Steps

b

a

c

d

1. Start simple

!36

Page 37: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Demystifying neural network architecture selection1. Start simple

Your input data is… Start here Consider using this later

Images LeNet-like architecture

LSTM with one hidden layer

Fully connected neural net with one hidden layer

ResNet

Attention model or WaveNet-like model

Problem-dependent

Images

Sequences

Other

!37

Page 38: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Dealing with multiple input modalities

“This”

“is”

“a”

“cat”

1. Start simple

Input 1

Input 2

Input 3

!38

Page 39: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Dealing with multiple input modalities

“This”

“is”

“a”

“cat”

1. Map each input into a (lower-dimensional) feature space

1. Start simple

Input 2

Input 3

Input 1

!39

Page 40: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Dealing with multiple input modalities

1. Map each input into a (lower-dimensional) feature spaceConvNet Flatten

(64-dim)

(72-dim)

(48-dim)

“This”

“is”

“a”

“cat”

LSTM

1. Start simple

Input 2

Input 3

Input 1

!40

Page 41: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Dealing with multiple input modalities

2. ConcatenateConvNet Flatten Con“cat”

“This”

“is”

“a”

“cat”

LSTM

(184-dim)

1. Start simple

Input 2

Input 3

Input 1

(64-dim)

(72-dim)

(48-dim)

!41

Page 42: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Dealing with multiple input modalities

3. Pass through fully connected layers to outputConvNet Flatten Concat

“This”

“is”

“a”

“cat”

LSTM

FC FC Output

T/F

1. Start simple

Input 2

Input 3

Input 1

(64-dim)

(72-dim)

(48-dim)

(184-dim)

(256-dim)

(128-dim)

!42

Page 43: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Normalize inputs

Starting simple

Choose a simple architecture

Simplify the problem

Use sensible defaults

Steps

b

a

c

d

1. Start simple

!43

Page 44: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Recommended network / optimizer defaults

• Optimizer: Adam optimizer with learning rate 3e-4

• Activations: relu (FC and Conv models), tanh (LSTMs)

• Initialization: He et al. normal (relu), Glorot normal (tanh)

• Regularization: None

• Data normalization: None

1. Start simple

!44

Page 45: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Definitions of recommended initializers• (n is the number of inputs, m is

the number of outputs)

• He et al. normal (used for ReLU)

• Glorot normal (used for tanh)

1. Start simple

N 0,

r2

n

!

<latexit sha1_base64="71oIrQeCI+zKtAqPcxHfa3NQrcI=">AAACFnicbVDLSgMxFM3UV62vqks3wSJU0DJTBF0W3LgqFewDOqVk0kwbmsmMyR2hDPMVbvwVNy4UcSvu/BszbRfaeiBwOOdebs7xIsE12Pa3lVtZXVvfyG8WtrZ3dveK+wctHcaKsiYNRag6HtFMcMmawEGwTqQYCTzB2t74OvPbD0xpHso7mESsF5Ch5D6nBIzUL567AYERJSKpp65gPpTtM+zqewWJ6ytCk2qayDR1FR+O4LRfLNkVewq8TJw5KaE5Gv3ilzsIaRwwCVQQrbuOHUEvIQo4FSwtuLFmEaFjMmRdQyUJmO4l01gpPjHKAPuhMk8Cnqq/NxISaD0JPDOZhdCLXib+53Vj8K96CZdRDEzS2SE/FhhCnHWEB1wxCmJiCKGKm79iOiKmDTBNFkwJzmLkZdKqVhy74txelGr1eR15dISOURk56BLV0A1qoCai6BE9o1f0Zj1ZL9a79TEbzVnznUP0B9bnD/n6n+k=</latexit><latexit sha1_base64="71oIrQeCI+zKtAqPcxHfa3NQrcI=">AAACFnicbVDLSgMxFM3UV62vqks3wSJU0DJTBF0W3LgqFewDOqVk0kwbmsmMyR2hDPMVbvwVNy4UcSvu/BszbRfaeiBwOOdebs7xIsE12Pa3lVtZXVvfyG8WtrZ3dveK+wctHcaKsiYNRag6HtFMcMmawEGwTqQYCTzB2t74OvPbD0xpHso7mESsF5Ch5D6nBIzUL567AYERJSKpp65gPpTtM+zqewWJ6ytCk2qayDR1FR+O4LRfLNkVewq8TJw5KaE5Gv3ilzsIaRwwCVQQrbuOHUEvIQo4FSwtuLFmEaFjMmRdQyUJmO4l01gpPjHKAPuhMk8Cnqq/NxISaD0JPDOZhdCLXib+53Vj8K96CZdRDEzS2SE/FhhCnHWEB1wxCmJiCKGKm79iOiKmDTBNFkwJzmLkZdKqVhy74txelGr1eR15dISOURk56BLV0A1qoCai6BE9o1f0Zj1ZL9a79TEbzVnznUP0B9bnD/n6n+k=</latexit><latexit sha1_base64="71oIrQeCI+zKtAqPcxHfa3NQrcI=">AAACFnicbVDLSgMxFM3UV62vqks3wSJU0DJTBF0W3LgqFewDOqVk0kwbmsmMyR2hDPMVbvwVNy4UcSvu/BszbRfaeiBwOOdebs7xIsE12Pa3lVtZXVvfyG8WtrZ3dveK+wctHcaKsiYNRag6HtFMcMmawEGwTqQYCTzB2t74OvPbD0xpHso7mESsF5Ch5D6nBIzUL567AYERJSKpp65gPpTtM+zqewWJ6ytCk2qayDR1FR+O4LRfLNkVewq8TJw5KaE5Gv3ilzsIaRwwCVQQrbuOHUEvIQo4FSwtuLFmEaFjMmRdQyUJmO4l01gpPjHKAPuhMk8Cnqq/NxISaD0JPDOZhdCLXib+53Vj8K96CZdRDEzS2SE/FhhCnHWEB1wxCmJiCKGKm79iOiKmDTBNFkwJzmLkZdKqVhy74txelGr1eR15dISOURk56BLV0A1qoCai6BE9o1f0Zj1ZL9a79TEbzVnznUP0B9bnD/n6n+k=</latexit><latexit sha1_base64="71oIrQeCI+zKtAqPcxHfa3NQrcI=">AAACFnicbVDLSgMxFM3UV62vqks3wSJU0DJTBF0W3LgqFewDOqVk0kwbmsmMyR2hDPMVbvwVNy4UcSvu/BszbRfaeiBwOOdebs7xIsE12Pa3lVtZXVvfyG8WtrZ3dveK+wctHcaKsiYNRag6HtFMcMmawEGwTqQYCTzB2t74OvPbD0xpHso7mESsF5Ch5D6nBIzUL567AYERJSKpp65gPpTtM+zqewWJ6ytCk2qayDR1FR+O4LRfLNkVewq8TJw5KaE5Gv3ilzsIaRwwCVQQrbuOHUEvIQo4FSwtuLFmEaFjMmRdQyUJmO4l01gpPjHKAPuhMk8Cnqq/NxISaD0JPDOZhdCLXib+53Vj8K96CZdRDEzS2SE/FhhCnHWEB1wxCmJiCKGKm79iOiKmDTBNFkwJzmLkZdKqVhy74txelGr1eR15dISOURk56BLV0A1qoCai6BE9o1f0Zj1ZL9a79TEbzVnznUP0B9bnD/n6n+k=</latexit>

N 0,

r2

n+m

!

<latexit sha1_base64="zIQOpEBJhC7QdJVej5ZiUKz47uk=">AAACGnicbVDLSgMxFM3UV62vqks3wSJUlDJTBF0W3LgqFewDOqVk0kwbmsmMyR2hDPMdbvwVNy4UcSdu/BszbRfaeiBwOOdebs7xIsE12Pa3lVtZXVvfyG8WtrZ3dveK+wctHcaKsiYNRag6HtFMcMmawEGwTqQYCTzB2t74OvPbD0xpHso7mESsF5Ch5D6nBIzULzpuQGBEiUjqqSuYD2X7HLv6XkHi+orQpJomEp/hIE1dxYcjOO0XS3bFngIvE2dOSmiORr/46Q5CGgdMAhVE665jR9BLiAJOBUsLbqxZROiYDFnXUEkCpnvJNFqKT4wywH6ozJOAp+rvjYQEWk8Cz0xmQfSil4n/ed0Y/KtewmUUA5N0dsiPBYYQZz3hAVeMgpgYQqji5q+YjohpBEybBVOCsxh5mbSqFceuOLcXpVp9XkceHaFjVEYOukQ1dIMaqIkoekTP6BW9WU/Wi/VufcxGc9Z85xD9gfX1Axa5oOk=</latexit><latexit sha1_base64="zIQOpEBJhC7QdJVej5ZiUKz47uk=">AAACGnicbVDLSgMxFM3UV62vqks3wSJUlDJTBF0W3LgqFewDOqVk0kwbmsmMyR2hDPMdbvwVNy4UcSdu/BszbRfaeiBwOOdebs7xIsE12Pa3lVtZXVvfyG8WtrZ3dveK+wctHcaKsiYNRag6HtFMcMmawEGwTqQYCTzB2t74OvPbD0xpHso7mESsF5Ch5D6nBIzULzpuQGBEiUjqqSuYD2X7HLv6XkHi+orQpJomEp/hIE1dxYcjOO0XS3bFngIvE2dOSmiORr/46Q5CGgdMAhVE665jR9BLiAJOBUsLbqxZROiYDFnXUEkCpnvJNFqKT4wywH6ozJOAp+rvjYQEWk8Cz0xmQfSil4n/ed0Y/KtewmUUA5N0dsiPBYYQZz3hAVeMgpgYQqji5q+YjohpBEybBVOCsxh5mbSqFceuOLcXpVp9XkceHaFjVEYOukQ1dIMaqIkoekTP6BW9WU/Wi/VufcxGc9Z85xD9gfX1Axa5oOk=</latexit><latexit sha1_base64="zIQOpEBJhC7QdJVej5ZiUKz47uk=">AAACGnicbVDLSgMxFM3UV62vqks3wSJUlDJTBF0W3LgqFewDOqVk0kwbmsmMyR2hDPMdbvwVNy4UcSdu/BszbRfaeiBwOOdebs7xIsE12Pa3lVtZXVvfyG8WtrZ3dveK+wctHcaKsiYNRag6HtFMcMmawEGwTqQYCTzB2t74OvPbD0xpHso7mESsF5Ch5D6nBIzULzpuQGBEiUjqqSuYD2X7HLv6XkHi+orQpJomEp/hIE1dxYcjOO0XS3bFngIvE2dOSmiORr/46Q5CGgdMAhVE665jR9BLiAJOBUsLbqxZROiYDFnXUEkCpnvJNFqKT4wywH6ozJOAp+rvjYQEWk8Cz0xmQfSil4n/ed0Y/KtewmUUA5N0dsiPBYYQZz3hAVeMgpgYQqji5q+YjohpBEybBVOCsxh5mbSqFceuOLcXpVp9XkceHaFjVEYOukQ1dIMaqIkoekTP6BW9WU/Wi/VufcxGc9Z85xD9gfX1Axa5oOk=</latexit><latexit sha1_base64="zIQOpEBJhC7QdJVej5ZiUKz47uk=">AAACGnicbVDLSgMxFM3UV62vqks3wSJUlDJTBF0W3LgqFewDOqVk0kwbmsmMyR2hDPMdbvwVNy4UcSdu/BszbRfaeiBwOOdebs7xIsE12Pa3lVtZXVvfyG8WtrZ3dveK+wctHcaKsiYNRag6HtFMcMmawEGwTqQYCTzB2t74OvPbD0xpHso7mESsF5Ch5D6nBIzULzpuQGBEiUjqqSuYD2X7HLv6XkHi+orQpJomEp/hIE1dxYcjOO0XS3bFngIvE2dOSmiORr/46Q5CGgdMAhVE665jR9BLiAJOBUsLbqxZROiYDFnXUEkCpnvJNFqKT4wywH6ozJOAp+rvjYQEWk8Cz0xmQfSil4n/ed0Y/KtewmUUA5N0dsiPBYYQZz3hAVeMgpgYQqji5q+YjohpBEybBVOCsxh5mbSqFceuOLcXpVp9XkceHaFjVEYOukQ1dIMaqIkoekTP6BW9WU/Wi/VufcxGc9Z85xD9gfX1Axa5oOk=</latexit> !45

Page 46: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Normalize inputs

Starting simple

Choose a simple architecture

Simplify the problem

Use sensible defaultsb

a

c

Steps

d

1. Start simple

!46

Page 47: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Important to normalize scale of input data

• Subtract mean and divide by variance

• For images, fine to scale values to [0, 1] (e.g., by dividing by 255)[Careful, make sure your library doesn’t do it for you!]

1. Start simple

!47

Page 48: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Normalize inputs

Starting simple

Choose a simple architecture

Simplify the problem

Use sensible defaultsb

a

c

Steps

d

1. Start simple

!48

Page 49: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Consider simplifying the problem as well

• Start with a small training set (~10,000 examples)

• Use a fixed number of objects, classes, smaller image size, etc.

• Create a simpler synthetic training set

1. Start simple

!49

Page 50: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Simplest model for pedestrian detection

• Start with a subset of 10,000 images for training, 1,000 for val, and 500 for test

• Use a LeNet architecture with sigmoid cross-entropy loss

• Adam optimizer with LR 3e-4

• No regularization

1. Start simple

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy

Running example

!50

Page 51: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Normalize inputs

Starting simpleChoose a simple

architecture

Simplify the problem

Use sensible defaultsb

a

c

Steps

d

Summary• LeNet, LSTM, or Fully

Connected

• Start with a simpler version of your problem (e.g., smaller dataset)

• Adam optimizer & no regularization

• Subtract mean and divide by std, or just divide by 255 (ims)

1. Start simple

!51

Page 52: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Tune hyper-parameters

Implement & debug

Strategy for DL troubleshooting

Start simple Evaluate

Improve model/data

Meets re-quirements

!52

Page 53: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Implementing bug-free DL models

Get your model to run

Compare to a known result

Overfit a single batchb

a

c

Steps

2. Implement & debug

!53

Page 54: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Preview: the five most common DL bugs• Incorrect shapes for your tensors

Can fail silently! E.g., accidental broadcasting: x.shape = (None,), y.shape = (None, 1), (x+y).shape = (None, None)

• Pre-processing inputs incorrectly E.g., Forgetting to normalize, or too much pre-processing

• Incorrect input to your loss function E.g., softmaxed outputs to a loss that expects logits

• Forgot to set up train mode for the net correctly E.g., toggling train/eval, controlling batch norm dependencies

• Numerical instability - inf/NaNOften stems from using an exp, log, or div operation

2. Implement & debug

!54

Page 55: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

General advice for implementing your modelLightweight implementation

• Minimum possible new lines of code for v1

• Rule of thumb: <200 lines

• (Tested infrastructure components are fine)

2. Implement & debug

!55

Page 56: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

General advice for implementing your modelLightweight implementation

• Minimum possible new lines of code for v1

• Rule of thumb: <200 lines

• (Tested infrastructure components are fine)

2. Implement & debug

Use off-the-shelf components, e.g.,

• Keras

• tf.layers.dense(…) instead of tf.nn.relu(tf.matmul(W, x))

• tf.losses.cross_entropy(…) instead of writing out the exp

!56

Page 57: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

General advice for implementing your modelLightweight implementation

• Minimum possible new lines of code for v1

• Rule of thumb: <200 lines

• (Tested infrastructure components are fine)

2. Implement & debug

Build complicated data pipelines later

• Start with a dataset you can load into memory

Use off-the-shelf components, e.g.,

• Keras

• tf.layers.dense(…) instead of tf.nn.relu(tf.matmul(W, x))

• tf.losses.cross_entropy(…) instead of writing out the exp

!57

Page 58: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Implementing bug-free DL models

Get your model to run

Compare to a known result

Overfit a single batchb

a

c

Steps

2. Implement & debug

!58

Page 59: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Get your model to runa Shape

mismatch

Casting issue

OOM

Other

Common issues Recommended resolution

Step through model creation and inference in a debugger

Scale back memory intensive operations one-by-one

Standard debugging toolkit (Stack Overflow + interactive debugger)

2. Implement & debug

Implementing bug-free DL models

!59

Page 60: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Get your model to runa Shape

mismatch

Casting issue

OOM

Other

Common issues Recommended resolution

Step through model creation and inference in a debugger

Scale back memory intensive operations one-by-one

Standard debugging toolkit (Stack Overflow + interactive debugger)

2. Implement & debug

Implementing bug-free DL models

!60

Page 61: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Debuggers for DL code• Pytorch: easy, use ipdb

• tensorflow: trickier Option 1: step through graph creation

2. Implement & debug

!61

Page 62: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Debuggers for DL code• Pytorch: easy, use ipdb

• tensorflow: trickier Option 2: step into training loop

Evaluate tensors using sess.run(…)

2. Implement & debug

!62

Page 63: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Debuggers for DL code• Pytorch: easy, use ipdb

• tensorflow: trickier Option 3: use tfdb

Stops execution at each sess.run(…) and lets you inspect

python -m tensorflow.python.debug.examples.debug_mnist --debug

2. Implement & debug

!63

Page 64: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Get your model to runa Shape

mismatch

Casting issue

OOM

Other

Common issues Recommended resolution

Step through model creation and inference in a debugger

Scale back memory intensive operations one-by-one

Standard debugging toolkit (Stack Overflow + interactive debugger)

Implementing bug-free DL models2. Implement & debug

!64

Page 65: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Shape mismatch Undefined

shapes

Incorrect shapes

Common issues Most common causes

• Confusing tensor.shape, tf.shape(tensor), tensor.get_shape()

• Reshaping things to a shape of type Tensor (e.g., when loading data from a file)

• Flipped dimensions when using tf.reshape(…) • Took sum, average, or softmax over wrong

dimension • Forgot to flatten after conv layers • Forgot to get rid of extra “1” dimensions (e.g., if

shape is (None, 1, 1, 4) • Data stored on disk in a different dtype than

loaded (e.g., stored a float64 numpy array, and loaded it as a float32)

Implementing bug-free DL models2. Implement & debug

!65

Page 66: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Casting issueData not in

float32

Common issues Most common causes

• Forgot to cast images from uint8 to float32 • Generated data using numpy in float64, forgot to

cast to float32

Implementing bug-free DL models2. Implement & debug

!66

Page 67: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

OOMToo big a

tensor

Too much data

Common issues Most common causes

Duplicating operations

Other processes

• Other processes running on your GPU

• Memory leak due to creating multiple models in the same session

• Repeatedly creating an operation (e.g., in a function that gets called over and over again)

• Too large a batch size for your model (e.g., during evaluation)

• Too large fully connected layers

• Loading too large a dataset into memory, rather than using an input queue

• Allocating too large a buffer for dataset creation

Implementing bug-free DL models2. Implement & debug

!67

Page 68: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Other common errors Other bugs

Common issues Most common causes

• Forgot to initialize variables • Forgot to turn off bias when using batch norm • “Fetch argument has invalid type” - usually you

overwrote one of your ops with an output during training

Implementing bug-free DL models2. Implement & debug

!68

Page 69: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Get your model to run

Compare to a known result

Overfit a single batchb

a

c

Steps

Implementing bug-free DL models2. Implement & debug

!69

Page 70: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Overfit a single batchb

Error goes up

Error oscillates

Common issues

Error explodes

Error plateaus

Most common causes

Implementing bug-free DL models2. Implement & debug

!70

Page 71: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Overfit a single batchb

Error goes up

Error oscillates

Common issues

Error explodes

Error plateaus

• Flipped the sign of the loss function / gradient • Learning rate too high • Softmax taken over wrong dimension

Most common causes

Implementing bug-free DL models2. Implement & debug

!71

Page 72: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Overfit a single batchb

Error goes up

Error oscillates

Common issues

Error explodes

Error plateaus

Most common causes

• Numerical issue. Check all exp, log, and div operations • Learning rate too high

Implementing bug-free DL models2. Implement & debug

!72

Page 73: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Overfit a single batchb

Error goes up

Error oscillates

Common issues

Error explodes

Error plateaus

Most common causes

• Data or labels corrupted (e.g., zeroed, incorrectly shuffled, or preprocessed incorrectly)

• Learning rate too high

Implementing bug-free DL models2. Implement & debug

!73

Page 74: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Overfit a single batchb

Error goes up

Error oscillates

Common issues

Error explodes

Error plateaus

Most common causes

• Learning rate too low • Gradients not flowing through the whole model • Too much regularization • Incorrect input to loss function (e.g., softmax instead of

logits) • Data or labels corrupted

Implementing bug-free DL models2. Implement & debug

!74

Page 75: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Overfit a single batchb

Error goes up

Error oscillates

Common issues

Error explodes

Error plateaus

• Flipped the sign of the loss function / gradient • Learning rate too high • Softmax taken over wrong dimension

Most common causes

• Numerical issue. Check all exp, log, and div operations • Learning rate too high

• Data or labels corrupted (e.g., zeroed or incorrectly shuffled)

• Learning rate too high

• Learning rate too low • Gradients not flowing through the whole model • Too much regularization • Incorrect input to loss function (e.g., softmax instead of

logits) • Data or labels corrupted

Implementing bug-free DL models2. Implement & debug

!75

Page 76: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Get your model to run

Compare to a known result

Overfit a single batchb

a

c

Steps

Implementing bug-free DL models2. Implement & debug

!76

Page 77: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Hierarchy of known results• Official model implementation evaluated on similar

dataset to yoursMore useful

Less useful

You can:

• Walk through code line-by-line and ensure you have the same output

• Ensure your performance is up to par with expectations

2. Implement & debug

!77

Page 78: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Hierarchy of known results• Official model implementation evaluated on similar

dataset to yours

• Official model implementation evaluated on benchmark (e.g., MNIST)

More useful

Less useful

You can:

• Walk through code line-by-line and ensure you have the same output

2. Implement & debug

!78

Page 79: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Hierarchy of known results• Official model implementation evaluated on similar dataset

to yours

• Official model implementation evaluated on benchmark (e.g., MNIST)

• Unofficial model implementation

• Results from the paper (with no code)

• Results from your model on a benchmark dataset (e.g., MNIST)

• Results from a similar model on a similar dataset

• Super simple baselines (e.g., average of outputs or linear regression)

More useful

Less useful

You can:

• Same as before, but with lower confidence

2. Implement & debug

!79

Page 80: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Hierarchy of known results• Official model implementation evaluated on similar

dataset to yours

• Official model implementation evaluated on benchmark (e.g., MNIST)

• Unofficial model implementation

• Results from a paper (with no code)

More useful

Less useful

You can:

• Ensure your performance is up to par with expectations

2. Implement & debug

!80

Page 81: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Hierarchy of known results• Official model implementation evaluated on similar

dataset to yours

• Official model implementation evaluated on benchmark (e.g., MNIST)

• Unofficial model implementation

• Results from the paper (with no code)

• Results from your model on a benchmark dataset (e.g., MNIST)

More useful

Less useful

You can:

• Make sure your model performs well in a simpler setting

2. Implement & debug

!81

Page 82: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Hierarchy of known results• Official model implementation evaluated on similar dataset

to yours

• Official model implementation evaluated on benchmark (e.g., MNIST)

• Unofficial model implementation

• Results from the paper (with no code)

• Results from your model on a benchmark dataset (e.g., MNIST)

• Results from a similar model on a similar dataset

• Super simple baselines (e.g., average of outputs or linear regression)

More useful

Less useful

You can:

• Get a general sense of what kind of performance can be expected

2. Implement & debug

!82

Page 83: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Hierarchy of known results• Official model implementation evaluated on similar dataset

to yours

• Official model implementation evaluated on benchmark (e.g., MNIST)

• Unofficial model implementation

• Results from the paper (with no code)

• Results from your model on a benchmark dataset (e.g., MNIST)

• Results from a similar model on a similar dataset

• Super simple baselines (e.g., average of outputs or linear regression)

More useful

Less useful

You can:

• Make sure your model is learning anything at all

2. Implement & debug

!83

Page 84: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Hierarchy of known results• Official model implementation evaluated on similar dataset

to yours

• Official model implementation evaluated on benchmark (e.g., MNIST)

• Unofficial model implementation

• Results from the paper (with no code)

• Results from your model on a benchmark dataset (e.g., MNIST)

• Results from a similar model on a similar dataset

• Super simple baselines (e.g., average of outputs or linear regression)

More useful

Less useful

2. Implement & debug

!84

Page 85: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Summary: how to implement & debug2. Implement & debug

Get your model to run

Compare to a known result

Overfit a single batchb

a

c

Steps Summary

• Step through in debugger & watch out for shape, casting, and OOM errors

• Look for corrupted data, over-regularization, broadcasting errors

• Keep iterating until model performs up to expectations

!85

Page 86: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Tune hyper-parameters

Implement & debug

Strategy for DL troubleshooting

Start simple Evaluate

Improve model/data

Meets re-quirements

!86

Page 87: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Bias-variance decomposition3. Evaluation

!87

Page 88: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Bias-variance decomposition3. Evaluation

!88

Page 89: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Bias-variance decomposition3. Evaluation

!89

Page 90: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

252 27

5 322 34

Irreducible error

Avoidable bias

(i.e., underfitting)

Train error

Variance (i.e.,

overfitting) Val error

Val set overfitting

Test error0

5

10

15

20

25

30

35

40Breakdown of test error by source

Bias-variance decomposition3. Evaluation

!90

Page 91: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Test error = irreducible error + bias + variance + val overfitting

This assumes train, val, and test all come from the same distribution. What if not?

Bias-variance decomposition3. Evaluation

!91

Page 92: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Handling distribution shift

Train data Test data

Use two val sets: one sampled from training distribution and one from test distribution

3. Evaluation

!92

Page 93: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

The bias-variance tradeoff3. Evaluation

!93

Page 94: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Bias-variance with distribution shift3. Evaluation

!94

Page 95: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Bias-variance with distribution shift

252 27

2 293 32

2 34

Irred

ucibl

e erro

rAv

oidab

le bia

s

(i.e.,

unde

rfitti

ng)

Train

erro

r

Varia

nce

Train

val e

rror

Distr

ibutio

n sh

iftTe

st va

l erro

rVa

l ove

rfitti

ng

Test

erro

r

0

5

10

15

20

25

30

35

40Breakdown of test error by source

3. Evaluation

!95

Page 96: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Train, val, and test error for pedestrian detection

Error source Value

Goal performance 1%

Train error 20%

Validation error 27%

Test error 28%

Train - goal = 19%(under-fitting)

3. Evaluation

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy

Running example

!96

Page 97: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Train, val, and test error for pedestrian detection

Error source Value

Goal performance 1%

Train error 20%

Validation error 27%

Test error 28%

Val - train = 7%(over-fitting)

3. Evaluation

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy

Running example

!97

Page 98: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Train, val, and test error for pedestrian detection

Error source Value

Goal performance 1%

Train error 20%

Validation error 27%

Test error 28%

Test - val = 1%(looks good!)

3. Evaluation

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy

Running example

!98

Page 99: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Test error = irreducible error + bias + variance + distribution shift + val overfitting

Summary: evaluating model performance3. Evaluation

!99

Page 100: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Tune hyper-parameters

Implement & debug

Strategy for DL troubleshooting

Start simple Evaluate

Improve model/data

Meets re-quirements

!100

Page 101: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Address distribution shift

Prioritizing improvements (i.e., applying the bias-variance tradeoff)

Address under-fitting

Re-balance datasets (if applicable)

Address over-fittingb

a

c

Steps

d

4. Prioritize improvements

!101

Page 102: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Addressing under-fitting (i.e., reducing bias)

Try first

Try later

A. Make your model bigger (i.e., add layers or use more units per layer)

B. Reduce regularization

C. Error analysis

D. Choose a different (closer to state-of-the art) model architecture (e.g., move from LeNet to ResNet)

E. Tune hyper-parameters (e.g., learning rate)

F. Add features

4. Prioritize improvements

!102

Page 103: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Train, val, and test error for pedestrian detection

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy (i.e., 1% error)

Error source Value Value

Goal performance 1% 1%

Train error 20% 7%

Validation error 27% 19%

Test error 28% 20%

Add more layers to the ConvNet

4. Prioritize improvements

!103

Page 104: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Train, val, and test error for pedestrian detection

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy (i.e., 1% error)

Error source Value Value Value

Goal performance 1% 1% 1%

Train error 20% 10% 3%

Validation error 27% 19% 10%

Test error 28% 20% 10%

Switch to ResNet-101

4. Prioritize improvements

!104

Page 105: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Train, val, and test error for pedestrian detection

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy (i.e., 1% error)

Error source Value Value Value Value

Goal performance 1% 1% 1% 1%

Train error 20% 10% 3% 0.8%

Validation error 27% 19% 10% 12%

Test error 28% 20% 10% 12%

Tune learning rate

4. Prioritize improvements

!105

Page 106: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Prioritizing improvements (i.e., applying the bias-variance tradeoff)

4. Prioritize improvements

!106

Address distribution shift

Address under-fitting

Re-balance datasets (if applicable)

Address over-fittingb

a

c

Steps

d

Page 107: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Addressing over-fitting (i.e., reducing variance)Try first

Try later

A. Add more training data (if possible!)

B. Add normalization (e.g., batch norm, layer norm)

C. Add data augmentation

D. Increase regularization (e.g., dropout, L2, weight decay)

E. Error analysis

F. Choose a different (closer to state-of-the-art) model architecture

G. Tune hyperparameters

H. Early stopping

I. Remove features

J. Reduce model size

4. Prioritize improvements

!107

Page 108: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Addressing over-fitting (i.e., reducing variance)Try first

Try later

A. Add more training data (if possible!)

B. Add normalization (e.g., batch norm, layer norm)

C. Add data augmentation

D. Increase regularization (e.g., dropout, L2, weight decay)

E. Error analysis

F. Choose a different (closer to state-of-the-art) model architecture

G. Tune hyperparameters

H. Early stopping

I. Remove features

J. Reduce model size

4. Prioritize improvements

Not recommended!

!108

Page 109: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Train, val, and test error for pedestrian detection

Error source Value

Goal performance 1%

Train error 0.8%

Validation error 12%

Test error 12%

4. Prioritize improvements

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy

Running example

!109

Page 110: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Train, val, and test error for pedestrian detection

Error source Value Value

Goal performance 1% 1%

Train error 0.8% 1.5%

Validation error 12% 5%

Test error 12% 6%

Increase dataset size to 250,000

4. Prioritize improvements

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy

Running example

!110

Page 111: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Train, val, and test error for pedestrian detection

Error source Value Value Value

Goal performance 1% 1% 1%

Train error 0.8% 1.5% 1.7%

Validation error 12% 5% 4%

Test error 12% 6% 4%

Add weight decay

4. Prioritize improvements

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy

Running example

!111

Page 112: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Train, val, and test error for pedestrian detection

Error source Value Value Value Value

Goal performance 1% 1% 1% 1%

Train error 0.8% 1.5% 1.7% 2%

Validation error 12% 5% 4% 2.5%

Test error 12% 6% 4% 2.6%

Add data augmentation

4. Prioritize improvements

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy

Running example

!112

Page 113: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Train, val, and test error for pedestrian detection

Error source Value Value Value Value Value

Goal performance 1% 1% 1% 1% 1%

Train error 0.8% 1.5% 1.7% 2% 0.6%

Validation error 12% 5% 4% 2.5% 0.9%

Test error 12% 6% 4% 2.6% 1.0%

Tune num layers, optimizer params, weight initialization, kernel size, weight decay

4. Prioritize improvements

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy

Running example

!113

Page 114: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Prioritizing improvements (i.e., applying the bias-variance tradeoff)

4. Prioritize improvements

!114

Address distribution shift

Address under-fitting

Re-balance datasets (if applicable)

Address over-fittingb

a

c

Steps

d

Page 115: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Addressing distribution shift

Try first

Try later

A. Analyze test-val set errors & collect more training data to compensate

B. Analyze test-val set errors & synthesize more training data to compensate

C. Apply domain adaptation techniques to training & test distributions

4. Prioritize improvements

!115

Page 116: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Error analysisTest-val set errors (no pedestrian detected) Train-val set errors (no pedestrian detected)

4. Prioritize improvements

!116

Page 117: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Error analysisTest-val set errors (no pedestrian detected) Train-val set errors (no pedestrian detected)

Error type 1: hard-to-see pedestrians

4. Prioritize improvements

!117

Page 118: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Error analysisTest-val set errors (no pedestrian detected) Train-val set errors (no pedestrian detected)

Error type 2: reflections

4. Prioritize improvements

!118

Page 119: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Error analysisTest-val set errors (no pedestrian detected) Train-val set errors (no pedestrian detected)

Error type 3 (test-val only): night scenes

4. Prioritize improvements

!119

Page 120: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Error analysisError type Error %

(train-val)Error % (test-val) Potential solutions Priority

1. Hard-to-see pedestrians 0.1% 0.1% • Better sensors Low

2. Reflections 0.3% 0.3%• Collect more data with reflections• Add synthetic reflections to train set• Try to remove with pre-processing• Better sensors

Medium

3. Nighttime scenes 0.1% 1%

• Collect more data at night• Synthetically darken training images• Simulate night-time data• Use domain adaptation

High

4. Prioritize improvements

!120

Page 121: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Domain adaptation

What is it?

Techniques to train on “source” distribution and generalize to another “target” using only unlabeled data or

limited labeled data

When should you consider using it?

• Access to labeled data from test distribution is limited

• Access to relatively similar data is plentiful

4. Prioritize improvements

!121

Page 122: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Types of domain adaptation

Type Use case Example techniques

Supervised You have limited data from target domain

• Fine-tuning a pre-trained model

• Adding target data to train set

Un-supervisedYou have lots of un-labeled data from target domain

• Correlation Alignment (CORAL)

• Domain confusion• CycleGAN

4. Prioritize improvements

!122

Page 123: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Prioritizing improvements (i.e., applying the bias-variance tradeoff)

4. Prioritize improvements

!123

Address distribution shift

Address under-fitting

Re-balance datasets (if applicable)

Address over-fittingb

a

c

Steps

d

Page 124: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Rebalancing datasets4. Prioritize improvements

• If (test)-val looks significantly better than test, you overfit to the val set

• This happens with small val sets or lots of hyper parameter tuning

• When it does, recollect val data

!124

Page 125: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Tune hyper-parameters

Implement & debug

Strategy for DL troubleshooting

Start simple Evaluate

Improve model/data

Meets re-quirements

!125

Page 126: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Hyperparameter optimizationModel & optimizer choices?

Network: ResNet - How many layers? - Weight initialization? - Kernel size? - EtcOptimizer: Adam - Batch size? - Learning rate? - beta1, beta2, epsilon? Regularization - ….

0 (no pedestrian) 1 (yes pedestrian)

Goal: 99% classification accuracy

Running example

5. Hyperparameter optimization

!126

Page 127: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Which hyper-parameters to tune?Choosing hyper-parameters

• More sensitive to some than others• Depends on choice of model• Rules of thumb (only) to the right• Sensitivity is relative to default values!

(e.g., if you are using all-zeros weight initialization or vanilla SGD, changing to the defaults will make a big difference)

Hyperparameter Approximate sensitivityLearning rate High

Optimizer choice LowOther optimizer params

(e.g., Adam beta1) Low

Batch size LowWeight initialization Medium

Loss function HighModel depth Medium

Layer size HighLayer params

(e.g., kernel size) Medium

Weight of regularization MediumNonlinearity Low

5. Hyperparameter optimization

!127

Page 128: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Method 1: manual hyperparam optimization How it works

• Understand the algorithm• E.g., higher learning rate means faster

less stable training• Train & evaluate model• Guess a better hyperparam value & re-

evaluate • Can be combined with other methods

(e.g., manually select parameter ranges to optimizer over)

Advantages

Disadvantages

• For a skilled practitioner, may require least computation to get good result

• Requires detailed understanding of the algorithm

• Time-consuming

5. Hyperparameter optimization

!128

Page 129: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Method 2: grid search H

yper

para

met

er 1

(e.g

., ba

tch

size

)

Hyperparameter 2 (e.g., learning rate)

How it works

Disadvantages

• Super simple to implement• Can produce good results

• Not very efficient: need to train on all cross-combos of hyper-parameters

• May require prior knowledge about parameters to getgood results

Advantages

5. Hyperparameter optimization

!129

Page 130: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Method 3: random search H

yper

para

met

er 1

(e.g

., ba

tch

size

)

Hyperparameter 2 (e.g., learning rate)

How it works

Disadvantages

Advantages

5. Hyperparameter optimization

!130

Page 131: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Method 4: coarse-to-fineH

yper

para

met

er 1

(e.g

., ba

tch

size

)

Hyperparameter 2 (e.g., learning rate)

How it works

Disadvantages

Advantages

5. Hyperparameter optimization

!131

Page 132: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Method 4: coarse-to-fineH

yper

para

met

er 1

(e.g

., ba

tch

size

)

Hyperparameter 2 (e.g., learning rate)

How it works

Disadvantages

Advantages

Best performers

5. Hyperparameter optimization

!132

Page 133: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Method 4: coarse-to-fineH

yper

para

met

er 1

(e.g

., ba

tch

size

)

Hyperparameter 2 (e.g., learning rate)

How it works

Disadvantages

Advantages

5. Hyperparameter optimization

!133

Page 134: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Method 4: coarse-to-fineH

yper

para

met

er 1

(e.g

., ba

tch

size

)

Hyperparameter 2 (e.g., learning rate)

How it works

Disadvantages

Advantages

5. Hyperparameter optimization

!134

Page 135: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Method 4: coarse-to-fineH

yper

para

met

er 1

(e.g

., ba

tch

size

)

Hyperparameter 2 (e.g., learning rate)

How it works

Disadvantages

• Can narrow in on very high performing hyperparameters

• Most used method in practice

• Somewhat manual process

Advantages

etc.

5. Hyperparameter optimization

!135

Page 136: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Method 5: Bayesian hyperparam optHow it works (at a high level)

• Start with a prior estimate of parameter distributions

• Maintain a probabilistic model of the relationship between hyper-parameter values and model performance

• Alternate between:• Training with the hyper-parameter

values that maximize the expected improvement

• Using training results to update our probabilistic model

• To learn more, see:

Advantages

Disadvantages

• Generally the most efficient hands-off way to choose hyperparameters

• Difficult to implement from scratch• Can be hard to integrate with off-the-shelf

tools

https://towardsdatascience.com/a-conceptual-explanation-of-bayesian-model-based-hyperparameter-optimization-for-machine-learning-b8172278050f

5. Hyperparameter optimization

!136

Page 137: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Summary of how to optimize hyperparams

• Coarse-to-fine random searches

• Consider Bayesian hyper-parameter optimization solutions as your codebase matures

5. Hyperparameter optimization

!137

Page 138: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Conclusion

!138

Page 139: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Conclusion

• DL debugging is hard due to many competing sources of error

• To train bug-free DL models, we treat building our model as an iterative process

• The following steps can make the process easier and catch errors as early as possible

!139

Page 140: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Tune hyp-eparams

How to build bug-free DL models

Implement & debug

Start simple

Evaluate

Improve model/data

Overview

• Choose the simplest model & data possible (e.g., LeNet on a subset of your data)

• Once model runs, overfit a single batch & reproduce a known result

• Apply the bias-variance decomposition to decide what to do next

• Use coarse-to-fine random searches

• Make your model bigger if you underfit; add data or regularize if you overfit

!140

Page 141: Troubleshooting Deep Neural Networks - josh-tobin.comjosh-tobin.com/assets/pdf/troubleshooting-deep-neural-networks-01... · Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Josh Tobin. January 2019. josh-tobin.com/troubleshooting-deep-neural-networks

Where to go to learn more• Andrew Ng’s book Machine Learning

Yearning (http://www.mlyearning.org/)

• The following Twitter thread:https://twitter.com/karpathy/status/1013244313327681536

• This blog post: https://pcc.cs.byu.edu/2017/10/02/practical-advice-for-building-deep-neural-networks/

!141