SlideShare a Scribd company logo
1 of 19
Download to read offline
Find bugs in the herd with
debuggable Tensorflow code
Yi Wei
yiwei@prowler.io
1
Tensorflow code is difficult to debug and verify
● Tensor values are multi-dimensional arrays
● TensorBoard graph visualization has hundred of nodes and edges
● Many (or not enough) tips from the Internet, but I never know whether my
code is correct with those tipis
2
Specification to the rescue
● Reasoning Tensorflow code is difficult since the debugger does
not know what is correct.
● Specification defines correctness of the code.
● Correctness w.r.t. algorithm definition, not whether the code can
learn a model
3
Ask not what the debugger can do for you,
ask what you can do for the debugger
Three assertion techniques to verify correctness
● Tensor shape assertions to validate data shapes
● Tensor dependency assertions to validate graph structure
● Tensor equation assertions to numerical calculations
4
Technique 1: shape assertions
Write an assert to check the shape of every tensor you introduce.
prediction_tensor = q_function.output_tensor
assert prediction_tensor.shape.to_list() == [batch_size, action_dimension]
target_tensor = reward_tensor + discount * bootstrapped_tesnor
assert target_tensor.shape.to_list() == [batch_size, action_dimension]
loss_tensor = tf.losses.mean_squared_error(target_tensor, prediction_tensor)
assert loss_tensor.shape.to_list() == []
5
Next we need to validate graph structure, but how?
TensorBoard gives complicated visualization, not practical for most of us
6
Technique 2: Tensor group dependency
7
We developed a Python package TensorGroupDependency:
● Visualizes part of the graph involving only your tensors
● Helps you to check tensor dependency correctness
● Automatically generates tensor graph structural assertions.
● This is the key step to make the whole process practical!
Use of TensorGroupDependency
d = TensorGroupDependency()
d.add(q_function, 'q_function')
d.add(q_function.output_tensor, 'q_value_tensor')
d.add(prediction_tensor, 'prediction_tensor')
d.add(target_tensor, 'target_tensor')
d.add(loss_tensor, 'loss_tensor')
dot = d.generate_dot_representation()
print(dot)
8
Visualization from TensorGroupDependency
● Tensors as nodes
● Dependency between tensors as edges
● You must explain why edges exist
● Have assertions automatically generated
9
Automatically generated assertions
d.generate_assertions(target_exp='d')
d.assert_immediate_ancestors('q_function.variables', set())
d.assert_immediate_ancestors('target_tensor', set())
d.assert_immediate_ancestors('q_value_tensor', {'q_function.variables'})
d.assert_immediate_ancestors('loss_tensor', {'target_tensor', 'prediction_tensor'})
d.assert_immediate_ancestors('prediction_tensor', {'q_value_tensor'})
10
Visualization for the TD(λ) critic
11
Visualization for the Generalized Advantage Estimation critic
12
Visualization for the StarCraft2 learner
Graphs become smaller and smaller because of composability.
13
Open source TensorGroupDependency
● TensorGroupDependency is the key component that
make the whole process practical
● We are in preparation of open-sourcing
TensorGroupDependency
● Drop me an email at yiwei@prowler.io if interested
14
Technique 3: tensor equations
Write an assertion to check every equation in your algorithm.
_, prediction, target, loss = sess.run(
[parameter_update_operations, prediction_tensor, target_tensor, loss_tensor],
feed_dict={})
mean_square_error = np.mean(np.power(target - prediction, 2))
np.testing.assert_almost_equal(loss, mean_square_error, decimal=1)
15
Effectiveness of quality assurance techniques
Learning module
Coding
time(h)
Debugging
time(h)
Bugs detected by (and time % spent)
shape
asserts (10%)
tensor graph
(40%)
tensor evals
(50%)
MC critic 5 1 3 2 1
Bootstrapping critic 7 0.5 4 1 2
GAE critic 2 0.5 2 0 0
TD(λ) critic 3 2 5 0 1
StarCraft2 learner 7 1 1 1 0
Total 24 5 15 4 4
16
Reasons for the bug-detecting effectiveness
● Practical specification writing
○ Specification defines correctness, no way around it.
○ Explicitly writing down specification helps you and the debugger
○ Practical is the keyword
● Fault localization
○ Locating where a fault originates in Tensorflow code is difficult
○ At each stage of specification, you only need to focus on places within that stage.
● Clear to-do list style engineering process
○ Each assertion stage has finite steps bounded by the tensors you introduce, usually a dozen
○ You know exactly when the verification process ends -- when you’ve validated your code!
○ When people know the exact steps, they are a lot more efficient.
17
Assertions enable advanced testing
● Ingredients of a test: input construction and oracle
● Machine learning code uses numbers as input
● Since we already have the oracle, generating tests are easy
18
Conclusions yiwei@prowler.io
19

More Related Content

What's hot

02 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_0202 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_02
Pooja Gupta
 
Data Structure
Data StructureData Structure
Data Structure
sheraz1
 

What's hot (19)

Fundamentals of algorithms
Fundamentals of algorithmsFundamentals of algorithms
Fundamentals of algorithms
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
CPP10 - Debugging
CPP10 - DebuggingCPP10 - Debugging
CPP10 - Debugging
 
AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...
AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...
AUTOCODECOVERGEN: PROTOTYPE OF DATA DRIVEN UNIT TEST GENRATION TOOL THAT GUAR...
 
02 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_0202 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_02
 
9 testing-seatwork-premid
9 testing-seatwork-premid9 testing-seatwork-premid
9 testing-seatwork-premid
 
Greedy Algoritham
Greedy AlgorithamGreedy Algoritham
Greedy Algoritham
 
Intake 37 6
Intake 37 6Intake 37 6
Intake 37 6
 
Oop lec 1
Oop lec 1Oop lec 1
Oop lec 1
 
Lesson 3
Lesson 3Lesson 3
Lesson 3
 
Cmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comCmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.com
 
Verification challenges and methodologies - SoC and ASICs
Verification challenges and methodologies - SoC and ASICsVerification challenges and methodologies - SoC and ASICs
Verification challenges and methodologies - SoC and ASICs
 
Data Structure
Data StructureData Structure
Data Structure
 
Python Lecture 2
Python Lecture 2Python Lecture 2
Python Lecture 2
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
Cmis 102 Effective Communication / snaptutorial.com
Cmis 102  Effective Communication / snaptutorial.comCmis 102  Effective Communication / snaptutorial.com
Cmis 102 Effective Communication / snaptutorial.com
 
Csphtp1 04
Csphtp1 04Csphtp1 04
Csphtp1 04
 
Fundamental of Algorithms
Fundamental of Algorithms Fundamental of Algorithms
Fundamental of Algorithms
 
ASIC SoC Verification Challenges and Methodologies
ASIC SoC Verification Challenges and MethodologiesASIC SoC Verification Challenges and Methodologies
ASIC SoC Verification Challenges and Methodologies
 

Similar to TensorFlow London 15: Find bugs in the herd with debuggable TensorFlow code

(Py)testing the Limits of Machine Learning
(Py)testing the Limits of Machine Learning(Py)testing the Limits of Machine Learning
(Py)testing the Limits of Machine Learning
Rebecca Bilbro
 
Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)
Babul Mirdha
 

Similar to TensorFlow London 15: Find bugs in the herd with debuggable TensorFlow code (20)

Software Testing for Data Scientists
Software Testing for Data ScientistsSoftware Testing for Data Scientists
Software Testing for Data Scientists
 
Bound and Checked
Bound and CheckedBound and Checked
Bound and Checked
 
ML in Android
ML in AndroidML in Android
ML in Android
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO Standard
 
Telecom Churn Analysis
Telecom Churn AnalysisTelecom Churn Analysis
Telecom Churn Analysis
 
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
 
EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklist
 
C3 w2
C3 w2C3 w2
C3 w2
 
How to complement TDD with static analysis
How to complement TDD with static analysisHow to complement TDD with static analysis
How to complement TDD with static analysis
 
wk5ppt2_Iris
wk5ppt2_Iriswk5ppt2_Iris
wk5ppt2_Iris
 
Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...
Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...
Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...
 
Tdd in practice
Tdd in practiceTdd in practice
Tdd in practice
 
Ml2 train test-splits_validation_linear_regression
Ml2 train test-splits_validation_linear_regressionMl2 train test-splits_validation_linear_regression
Ml2 train test-splits_validation_linear_regression
 
Algorithms overview
Algorithms overviewAlgorithms overview
Algorithms overview
 
20100309 01 - Maintenance and re-engineering (McCabe)
20100309 01 - Maintenance and re-engineering (McCabe)20100309 01 - Maintenance and re-engineering (McCabe)
20100309 01 - Maintenance and re-engineering (McCabe)
 
(Py)testing the Limits of Machine Learning
(Py)testing the Limits of Machine Learning(Py)testing the Limits of Machine Learning
(Py)testing the Limits of Machine Learning
 
Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)
 
Coverage and Introduction to UVM
Coverage and Introduction to UVMCoverage and Introduction to UVM
Coverage and Introduction to UVM
 
Rspec
RspecRspec
Rspec
 
Cs 568 Spring 10 Lecture 5 Estimation
Cs 568 Spring 10  Lecture 5 EstimationCs 568 Spring 10  Lecture 5 Estimation
Cs 568 Spring 10 Lecture 5 Estimation
 

More from Seldon

TensorFlow London 18: Dr Alastair Moore, Towards the use of Graphical Models ...
TensorFlow London 18: Dr Alastair Moore, Towards the use of Graphical Models ...TensorFlow London 18: Dr Alastair Moore, Towards the use of Graphical Models ...
TensorFlow London 18: Dr Alastair Moore, Towards the use of Graphical Models ...
Seldon
 
Tensorflow London 13: Barbara Fusinska 'Hassle Free, Scalable, Machine Learni...
Tensorflow London 13: Barbara Fusinska 'Hassle Free, Scalable, Machine Learni...Tensorflow London 13: Barbara Fusinska 'Hassle Free, Scalable, Machine Learni...
Tensorflow London 13: Barbara Fusinska 'Hassle Free, Scalable, Machine Learni...
Seldon
 
Tensorflow London 13: Zbigniew Wojna 'Deep Learning for Big Scale 2D Imagery'
Tensorflow London 13: Zbigniew Wojna 'Deep Learning for Big Scale 2D Imagery'Tensorflow London 13: Zbigniew Wojna 'Deep Learning for Big Scale 2D Imagery'
Tensorflow London 13: Zbigniew Wojna 'Deep Learning for Big Scale 2D Imagery'
Seldon
 

More from Seldon (20)

CD4ML and the challenges of testing and quality in ML systems
CD4ML and the challenges of testing and quality in ML systemsCD4ML and the challenges of testing and quality in ML systems
CD4ML and the challenges of testing and quality in ML systems
 
TensorFlow London: Cutting edge generative models
TensorFlow London: Cutting edge generative modelsTensorFlow London: Cutting edge generative models
TensorFlow London: Cutting edge generative models
 
Tensorflow London: Tensorflow and Graph Recommender Networks by Yaz Santissi
Tensorflow London: Tensorflow and Graph Recommender Networks by Yaz SantissiTensorflow London: Tensorflow and Graph Recommender Networks by Yaz Santissi
Tensorflow London: Tensorflow and Graph Recommender Networks by Yaz Santissi
 
TensorFlow London: Progressive Growing of GANs for increased stability, quali...
TensorFlow London: Progressive Growing of GANs for increased stability, quali...TensorFlow London: Progressive Growing of GANs for increased stability, quali...
TensorFlow London: Progressive Growing of GANs for increased stability, quali...
 
TensorFlow London 18: Dr Daniel Martinho-Corbishley, From science to startups...
TensorFlow London 18: Dr Daniel Martinho-Corbishley, From science to startups...TensorFlow London 18: Dr Daniel Martinho-Corbishley, From science to startups...
TensorFlow London 18: Dr Daniel Martinho-Corbishley, From science to startups...
 
TensorFlow London 18: Dr Alastair Moore, Towards the use of Graphical Models ...
TensorFlow London 18: Dr Alastair Moore, Towards the use of Graphical Models ...TensorFlow London 18: Dr Alastair Moore, Towards the use of Graphical Models ...
TensorFlow London 18: Dr Alastair Moore, Towards the use of Graphical Models ...
 
Seldon: Deploying Models at Scale
Seldon: Deploying Models at ScaleSeldon: Deploying Models at Scale
Seldon: Deploying Models at Scale
 
TensorFlow London 17: How NASA Frontier Development Lab scientists use AI to ...
TensorFlow London 17: How NASA Frontier Development Lab scientists use AI to ...TensorFlow London 17: How NASA Frontier Development Lab scientists use AI to ...
TensorFlow London 17: How NASA Frontier Development Lab scientists use AI to ...
 
TensorFlow London 17: Practical Reinforcement Learning with OpenAI
TensorFlow London 17: Practical Reinforcement Learning with OpenAITensorFlow London 17: Practical Reinforcement Learning with OpenAI
TensorFlow London 17: Practical Reinforcement Learning with OpenAI
 
TensorFlow 16: Multimodal Sentiment Analysis with TensorFlow
TensorFlow 16: Multimodal Sentiment Analysis with TensorFlow TensorFlow 16: Multimodal Sentiment Analysis with TensorFlow
TensorFlow 16: Multimodal Sentiment Analysis with TensorFlow
 
TensorFlow 16: Building a Data Science Platform
TensorFlow 16: Building a Data Science Platform TensorFlow 16: Building a Data Science Platform
TensorFlow 16: Building a Data Science Platform
 
Ai in financial services
Ai in financial servicesAi in financial services
Ai in financial services
 
TensorFlow London 14: Ben Hall 'Machine Learning Workloads with Kubernetes an...
TensorFlow London 14: Ben Hall 'Machine Learning Workloads with Kubernetes an...TensorFlow London 14: Ben Hall 'Machine Learning Workloads with Kubernetes an...
TensorFlow London 14: Ben Hall 'Machine Learning Workloads with Kubernetes an...
 
Tensorflow London 13: Barbara Fusinska 'Hassle Free, Scalable, Machine Learni...
Tensorflow London 13: Barbara Fusinska 'Hassle Free, Scalable, Machine Learni...Tensorflow London 13: Barbara Fusinska 'Hassle Free, Scalable, Machine Learni...
Tensorflow London 13: Barbara Fusinska 'Hassle Free, Scalable, Machine Learni...
 
Tensorflow London 13: Zbigniew Wojna 'Deep Learning for Big Scale 2D Imagery'
Tensorflow London 13: Zbigniew Wojna 'Deep Learning for Big Scale 2D Imagery'Tensorflow London 13: Zbigniew Wojna 'Deep Learning for Big Scale 2D Imagery'
Tensorflow London 13: Zbigniew Wojna 'Deep Learning for Big Scale 2D Imagery'
 
TensorFlow London 11: Pierre Harvey Richemond 'Trends and Developments in Rei...
TensorFlow London 11: Pierre Harvey Richemond 'Trends and Developments in Rei...TensorFlow London 11: Pierre Harvey Richemond 'Trends and Developments in Rei...
TensorFlow London 11: Pierre Harvey Richemond 'Trends and Developments in Rei...
 
TensorFlow London 11: Gema Parreno 'Use Cases of TensorFlow'
TensorFlow London 11: Gema Parreno 'Use Cases of TensorFlow'TensorFlow London 11: Gema Parreno 'Use Cases of TensorFlow'
TensorFlow London 11: Gema Parreno 'Use Cases of TensorFlow'
 
Tensorflow London 12: Marcel Horstmann and Laurent Decamp 'Using TensorFlow t...
Tensorflow London 12: Marcel Horstmann and Laurent Decamp 'Using TensorFlow t...Tensorflow London 12: Marcel Horstmann and Laurent Decamp 'Using TensorFlow t...
Tensorflow London 12: Marcel Horstmann and Laurent Decamp 'Using TensorFlow t...
 
TensorFlow London 12: Oliver Gindele 'Recommender systems in Tensorflow'
TensorFlow London 12: Oliver Gindele 'Recommender systems in Tensorflow'TensorFlow London 12: Oliver Gindele 'Recommender systems in Tensorflow'
TensorFlow London 12: Oliver Gindele 'Recommender systems in Tensorflow'
 
TensorFlow London 13.09.17 Ilya Dmitrichenko
TensorFlow London 13.09.17 Ilya DmitrichenkoTensorFlow London 13.09.17 Ilya Dmitrichenko
TensorFlow London 13.09.17 Ilya Dmitrichenko
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

TensorFlow London 15: Find bugs in the herd with debuggable TensorFlow code

  • 1. Find bugs in the herd with debuggable Tensorflow code Yi Wei yiwei@prowler.io 1
  • 2. Tensorflow code is difficult to debug and verify ● Tensor values are multi-dimensional arrays ● TensorBoard graph visualization has hundred of nodes and edges ● Many (or not enough) tips from the Internet, but I never know whether my code is correct with those tipis 2
  • 3. Specification to the rescue ● Reasoning Tensorflow code is difficult since the debugger does not know what is correct. ● Specification defines correctness of the code. ● Correctness w.r.t. algorithm definition, not whether the code can learn a model 3 Ask not what the debugger can do for you, ask what you can do for the debugger
  • 4. Three assertion techniques to verify correctness ● Tensor shape assertions to validate data shapes ● Tensor dependency assertions to validate graph structure ● Tensor equation assertions to numerical calculations 4
  • 5. Technique 1: shape assertions Write an assert to check the shape of every tensor you introduce. prediction_tensor = q_function.output_tensor assert prediction_tensor.shape.to_list() == [batch_size, action_dimension] target_tensor = reward_tensor + discount * bootstrapped_tesnor assert target_tensor.shape.to_list() == [batch_size, action_dimension] loss_tensor = tf.losses.mean_squared_error(target_tensor, prediction_tensor) assert loss_tensor.shape.to_list() == [] 5
  • 6. Next we need to validate graph structure, but how? TensorBoard gives complicated visualization, not practical for most of us 6
  • 7. Technique 2: Tensor group dependency 7 We developed a Python package TensorGroupDependency: ● Visualizes part of the graph involving only your tensors ● Helps you to check tensor dependency correctness ● Automatically generates tensor graph structural assertions. ● This is the key step to make the whole process practical!
  • 8. Use of TensorGroupDependency d = TensorGroupDependency() d.add(q_function, 'q_function') d.add(q_function.output_tensor, 'q_value_tensor') d.add(prediction_tensor, 'prediction_tensor') d.add(target_tensor, 'target_tensor') d.add(loss_tensor, 'loss_tensor') dot = d.generate_dot_representation() print(dot) 8
  • 9. Visualization from TensorGroupDependency ● Tensors as nodes ● Dependency between tensors as edges ● You must explain why edges exist ● Have assertions automatically generated 9
  • 10. Automatically generated assertions d.generate_assertions(target_exp='d') d.assert_immediate_ancestors('q_function.variables', set()) d.assert_immediate_ancestors('target_tensor', set()) d.assert_immediate_ancestors('q_value_tensor', {'q_function.variables'}) d.assert_immediate_ancestors('loss_tensor', {'target_tensor', 'prediction_tensor'}) d.assert_immediate_ancestors('prediction_tensor', {'q_value_tensor'}) 10
  • 11. Visualization for the TD(λ) critic 11
  • 12. Visualization for the Generalized Advantage Estimation critic 12
  • 13. Visualization for the StarCraft2 learner Graphs become smaller and smaller because of composability. 13
  • 14. Open source TensorGroupDependency ● TensorGroupDependency is the key component that make the whole process practical ● We are in preparation of open-sourcing TensorGroupDependency ● Drop me an email at yiwei@prowler.io if interested 14
  • 15. Technique 3: tensor equations Write an assertion to check every equation in your algorithm. _, prediction, target, loss = sess.run( [parameter_update_operations, prediction_tensor, target_tensor, loss_tensor], feed_dict={}) mean_square_error = np.mean(np.power(target - prediction, 2)) np.testing.assert_almost_equal(loss, mean_square_error, decimal=1) 15
  • 16. Effectiveness of quality assurance techniques Learning module Coding time(h) Debugging time(h) Bugs detected by (and time % spent) shape asserts (10%) tensor graph (40%) tensor evals (50%) MC critic 5 1 3 2 1 Bootstrapping critic 7 0.5 4 1 2 GAE critic 2 0.5 2 0 0 TD(λ) critic 3 2 5 0 1 StarCraft2 learner 7 1 1 1 0 Total 24 5 15 4 4 16
  • 17. Reasons for the bug-detecting effectiveness ● Practical specification writing ○ Specification defines correctness, no way around it. ○ Explicitly writing down specification helps you and the debugger ○ Practical is the keyword ● Fault localization ○ Locating where a fault originates in Tensorflow code is difficult ○ At each stage of specification, you only need to focus on places within that stage. ● Clear to-do list style engineering process ○ Each assertion stage has finite steps bounded by the tensors you introduce, usually a dozen ○ You know exactly when the verification process ends -- when you’ve validated your code! ○ When people know the exact steps, they are a lot more efficient. 17
  • 18. Assertions enable advanced testing ● Ingredients of a test: input construction and oracle ● Machine learning code uses numbers as input ● Since we already have the oracle, generating tests are easy 18