SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
Profiling in
Python
Fabian Pedregosa
Profiling : measure information of a running program (timing,
memory consumption, disk usage, etc.)
Profiling is about avoiding unnecessary optimizations and
focusing on important ones.
It is not enough to known algorithmic to make optimal code:
Profiling is necessary to make meaningful comparisons.
# three equivalent
# ways to add two vectors
# option 1
for i in range(a.size):
a[i] = a[i] + b[i]
# option 2
a = a + b
# option 3
a += b
The main Python profilers
cProfile (in the standard library)
line_profiler: line-by-line timing.
memory_profiler: to get memory usage information (☺)
IPython magic commands.
yep extension profiler: to get timing of compiled extensions (☺)
Example: Gradient-descent algorithm
import numpy as np
from scipy import optimize
def gd(f, grad, x0, rtol=1e-3):
xk = x0
fk = f(xk)
tol = np.inf
while tol >= rtol:
# .. compute gradient ..
gk = grad(xk)
# .. find step size by line-search ..
step_size = optimize.line_search(f, grad, xk, -gk, gfk=gk)[0]
# .. perform update ..
for i in range(xk.size):
xk[i] = xk[i] - step_size * gk[i]
fk_new = f(xk)
tol = np.abs(fk - fk_new) / fk
fk = fk_new
return xk
Question: should I optimize the gradient evaluation, the line-search
or the coefficient update ?
Profiling How-To
1. Create a realistic example
2. Run it with the appropriate profiler.
cProfile
line_profiler
memory_profiler
yep
(most of the time) cProfile is
useless
$ python -m cProfile -s time gd.py
ncalls tottime percall cumtime percall filename:lineno(function)
2 0.555 0.278 0.555 0.278 {method 'randn' of 'mtrand.RandomState' objects}
1 0.465 0.465 1.075 1.075 samples_generator.py:38(make_classification)
106/90 0.254 0.002 0.290 0.003 {built-in method load_dynamic}
65 0.174 0.003 0.174 0.003 {built-in method dot}
449 0.067 0.000 0.067 0.000 {built-in method loads}
2 0.051 0.026 0.051 0.026 {method 'take' of 'numpy.ndarray' objects}
261 0.039 0.000 0.094 0.000 doccer.py:12(docformat)
628/1 0.039 0.000 2.453 2.453 {built-in method exec}
3737 0.036 0.000 0.036 0.000 {built-in method stat}
7 0.031 0.004 0.034 0.005 {method 'get_filename' of 'zipimport.zipimporter' object
1985 0.031 0.000 0.122 0.000 <frozen importlib._bootstrap>:2016(find_spec)
1114/1108 0.030 0.000 0.086 0.000 {built-in method __build_class__}
1 0.026 0.026 0.211 0.211 gd.py:5(gd)
449 0.022 0.000 0.136 0.000 <frozen importlib._bootstrap>:1534(get_code)
7/3 0.020 0.003 0.223 0.074 {method 'load_module' of 'zipimport'.
Enter line_profiler
line-by-line time consumption
decorate the function with @profile:
run the code with kernprof -l -v my_script.py
written by Robert Kern
@profile
def gd(f, grad, x0, rtol=1e-3):
xk = x0
fk = f(xk)
tol = np.inf
while tol >= rtol:
# .. compute gradient ..
gk = grad(xk)
# etc.
$ kernprof -l -v gd.py
Total time: 0.561124 s
File: gd.py
Function: gd at line 4
Line # Hits Time Per Hit % Time Line Contents
==============================================================
4 @profile
5 def gd(f, grad, x0, rtol=1e-12):
6 1 3 3.0 0.0 xk = x0
7 1 998 998.0 0.2 fk = f(xk)
8 1 2 2.0 0.0 tol = np.inf
9 18 28 1.6 0.0 while tol >= rtol:
10 # .. compute gradient ..
11 17 29337 1725.7 5.2 gk = grad(xk)
12
13 # .. find step size by line-search ..
14 17 101399 5964.6 18.1 step_size = optimize.line_search(f, grad, xk,
15
16 # .. perform update ..
17 170017 152034 0.9 27.1 for i in range(xk.size):
18 170000 260621 1.5 46.4 xk[i] = xk[i] - step_size * gk[i]
19 17 16550 973.5 2.9 fk_new = f(xk)
20 17 135 7.9 0.0 tol = np.abs(fk - fk_new) / fk
21 17 17 1.0 0.0 fk = fk_new
Last column contains % of time spent on that line.
More than 70% of time is spent in performing the parameter
vector update!
vectorizing the parameter update
$ kernprof -l -v gd.py
Total time: 0.138711 s
File: gd.py
Function: gd at line 4
Line # Hits Time Per Hit % Time Line Contents
==============================================================
4 @profile
5 def gd(f, grad, x0, rtol=1e-12):
6 1 3 3.0 0.0 xk = x0
7 1 1007 1007.0 0.7 fk = f(xk)
8 1 2 2.0 0.0 tol = np.inf
9 17 34 2.0 0.0 while tol >= rtol:
10 # .. compute gradient ..
11 16 29335 1833.4 21.1 gk = grad(xk)
12
13 # .. find step size by line-search ..
14 16 91213 5700.8 65.8 step_size = optimize.line_search(f, grad, xk,
15
16 # .. perform update ..
17 16 589 36.8 0.4 xk -= step_size * gk
18 16 16363 1022.7 11.8 fk_new = f(xk)
19 16 145 9.1 0.1 tol = np.abs(fk - fk_new) / fk
20 16 20 1.2 0.0 fk = fk_new
from 48.5% to 0.4% !
total time divided by 4, without leaving Python.
memory_profiler
Get memory consumption of a Python program.
Two modes: line-by-line and time-based.
line-by-line similar interface as line_profiler:
$ python -m memory_profiler gd.py
Filename: gd.py
Line # Mem usage Increment Line Contents
================================================
4 352.117 MiB 0.000 MiB @profile
5 def gd(f, grad, x0, rtol=1e-12):
6 352.117 MiB 0.000 MiB xk = x0
7 352.145 MiB 0.027 MiB fk = f(xk)
8 352.145 MiB 0.000 MiB tol = np.inf
9 354.133 MiB 1.988 MiB while tol >= rtol:
10 # .. compute gradient ..
11 354.133 MiB 0.000 MiB gk = grad(xk)
12
13 # .. find step size by line-search ..
14 354.133 MiB 0.000 MiB step_size = optimize.line_search(f, grad, xk, -gk, gfk=
15
16 # .. perform update ..
17 354.133 MiB 0.000 MiB xk -= step_size * gk
18 354.133 MiB 0.000 MiB fk_new = f(xk)
19 354.133 MiB 0.000 MiB tol = np.abs(fk - fk_new) / fk
20 354.133 MiB 0.000 MiB fk = fk_new
memory_profiler
time-based memory consumption.
run script with mprof run my_script.py
plot with mprof plot
memory_profiler, example 2
Two functions, process_data, create_data
import numpy as np
import scipy.signal
@profile
def create_data():
ret = []
for n in range(70):
ret.append(np.random.randn(1, 70, 71, 72))
return ret
@profile
def process_data(data):
data = np.concatenate(data)
detrended = scipy.signal.detrend(data, axis=0)
return detrended
memory_profiler, example 2
Two functions, process_data, create_data
IPython shortcuts
line_profilerand memory_profilerhave IPython shortcuts
(AKA magic commands).
line_profilershortcut: %lprun
memory_profilershortcut: %memit, %mprun
IPython %timeit:
YEP
recursive acronym for YEP extension
profiler
Existing Python profilers only record Python functions.
Many codebases contain significant parts written in Cython/C/C++.
YEP Allows to get timing information for compiled extensions
(Cython/C/C++)
Only one that does it without special compiling/linking.
Integrates within Python the google-perftoolsprofiler.
Does not work on Windows.
YEP
Simple example using scikit-learn
The svm module in scikit-learn uses libsvm.
import numpy as np
from sklearn import svm, datasets
X, y = datasets.make_classification(n_samples=1000)
clf = svm.SVC()
clf.fit(X, y)
run with $ python -m yep -v simple_svm.py
YEP output
$ python -m yep -v simple_svm.py
2 0.7% 0.7% 268 97.5% _PyEval_EvalFrameEx
0 0.0% 0.7% 267 97.1% _PyClassMethod_New
0 0.0% 0.7% 267 97.1% _PyEval_EvalCode
0 0.0% 0.7% 267 97.1% _PyEval_EvalCodeEx
0 0.0% 0.7% 267 97.1% _PyObject_Call
0 0.0% 0.7% 267 97.1% __PyBuiltin_Init
0 0.0% 0.7% 206 74.9% __mh_execute_header
0 0.0% 0.7% 193 70.2% _Py_GetArgcArgv
0 0.0% 0.7% 193 70.2% _Py_Main
0 0.0% 0.7% 193 70.2% _main
0 0.0% 0.7% 193 70.2% start
0 0.0% 0.7% 188 68.4% _PyInit_libsvm
0 0.0% 0.7% 187 68.0% _svm_train
0 0.0% 0.7% 186 67.6% svm::Solver::Solve
48 17.5% 18.2% 184 66.9% svm_csr::Kernel::kernel_precomputed
135 49.1% 67.3% 135 49.1% svm::Kernel::kernel_rbf
0 0.0% 67.3% 74 26.9% _PyEval_CallObjectWithKeywords
0 0.0% 67.3% 74 26.9% _PyImport_ImportModuleLevelObject
0 0.0% 67.3% 74 26.9% __PyObject_CallMethodIdObjArgs
0 0.0% 67.3% 71 25.8% svm::Solver::select_working_set
0 0.0% 67.3% 27 9.8% _PyType_GenericAlloc
25 9.1% 76.4% 25 9.1% _stat
0 0.0% 76.4% 22 8.0% _PyInit_posix
YEP is still a work in progress
many rough edges.
Conclusion
Python has many tools for code profiling.
Some of them are mature and easy to use.
Integration with IPython.
Happy hacking!
Profiling in Python

Contenu connexe

Tendances

HUBUNGAN DIAGRAM
HUBUNGAN DIAGRAMHUBUNGAN DIAGRAM
HUBUNGAN DIAGRAMEDIS BLOG
 
Public Key Cryptography and RSA algorithm
Public Key Cryptography and RSA algorithmPublic Key Cryptography and RSA algorithm
Public Key Cryptography and RSA algorithmIndra97065
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data AnalysisAndrew Henshaw
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine LearningKuppusamy P
 
Cryptography based chat system
Cryptography based chat systemCryptography based chat system
Cryptography based chat systemJagsir Singh
 
Pose estimation from RGB images by deep learning
Pose estimation from RGB images by deep learningPose estimation from RGB images by deep learning
Pose estimation from RGB images by deep learningYu Huang
 
Knowledge Representation & Reasoning AI UNIT 3
Knowledge Representation & Reasoning AI UNIT 3Knowledge Representation & Reasoning AI UNIT 3
Knowledge Representation & Reasoning AI UNIT 3SURBHI SAROHA
 
Machine Learning lecture6(regularization)
Machine Learning lecture6(regularization)Machine Learning lecture6(regularization)
Machine Learning lecture6(regularization)cairo university
 
Pixel RNN to Pixel CNN++
Pixel RNN to Pixel CNN++Pixel RNN to Pixel CNN++
Pixel RNN to Pixel CNN++Dongheon Lee
 
Data mining 8 estimasi linear regression
Data mining 8   estimasi linear regressionData mining 8   estimasi linear regression
Data mining 8 estimasi linear regressionIrwansyahSaputra1
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonAhmed Salama
 
Data mining: Classification and prediction
Data mining: Classification and predictionData mining: Classification and prediction
Data mining: Classification and predictionDataminingTools Inc
 
Goal based and utility based agents
Goal based and utility based agentsGoal based and utility based agents
Goal based and utility based agentsMegha Sharma
 
Slide3.ppt
Slide3.pptSlide3.ppt
Slide3.pptbutest
 

Tendances (20)

HUBUNGAN DIAGRAM
HUBUNGAN DIAGRAMHUBUNGAN DIAGRAM
HUBUNGAN DIAGRAM
 
Public Key Cryptography and RSA algorithm
Public Key Cryptography and RSA algorithmPublic Key Cryptography and RSA algorithm
Public Key Cryptography and RSA algorithm
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data Analysis
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
 
Cryptography based chat system
Cryptography based chat systemCryptography based chat system
Cryptography based chat system
 
Pose estimation from RGB images by deep learning
Pose estimation from RGB images by deep learningPose estimation from RGB images by deep learning
Pose estimation from RGB images by deep learning
 
Prediction of housing price
Prediction of housing pricePrediction of housing price
Prediction of housing price
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Knowledge Representation & Reasoning AI UNIT 3
Knowledge Representation & Reasoning AI UNIT 3Knowledge Representation & Reasoning AI UNIT 3
Knowledge Representation & Reasoning AI UNIT 3
 
Master theorem
Master theoremMaster theorem
Master theorem
 
Machine Learning lecture6(regularization)
Machine Learning lecture6(regularization)Machine Learning lecture6(regularization)
Machine Learning lecture6(regularization)
 
Pixel RNN to Pixel CNN++
Pixel RNN to Pixel CNN++Pixel RNN to Pixel CNN++
Pixel RNN to Pixel CNN++
 
Data mining 8 estimasi linear regression
Data mining 8   estimasi linear regressionData mining 8   estimasi linear regression
Data mining 8 estimasi linear regression
 
Data reduction
Data reductionData reduction
Data reduction
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Clustering in Data Mining
Clustering in Data MiningClustering in Data Mining
Clustering in Data Mining
 
Data mining: Classification and prediction
Data mining: Classification and predictionData mining: Classification and prediction
Data mining: Classification and prediction
 
Goal based and utility based agents
Goal based and utility based agentsGoal based and utility based agents
Goal based and utility based agents
 
Image compression
Image compressionImage compression
Image compression
 
Slide3.ppt
Slide3.pptSlide3.ppt
Slide3.ppt
 

En vedette

Lightning: large scale machine learning in python
Lightning: large scale machine learning in pythonLightning: large scale machine learning in python
Lightning: large scale machine learning in pythonFabian Pedregosa
 
Performance and scalability for machine learning
Performance and scalability for machine learningPerformance and scalability for machine learning
Performance and scalability for machine learningArnaud Rachez
 
Low Complexity Regularization of Inverse Problems - Course #3 Proximal Splitt...
Low Complexity Regularization of Inverse Problems - Course #3 Proximal Splitt...Low Complexity Regularization of Inverse Problems - Course #3 Proximal Splitt...
Low Complexity Regularization of Inverse Problems - Course #3 Proximal Splitt...Gabriel Peyré
 
Hyperparameter optimization with approximate gradient
Hyperparameter optimization with approximate gradientHyperparameter optimization with approximate gradient
Hyperparameter optimization with approximate gradientFabian Pedregosa
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)PyData
 
A journey from a bad kpi to an excellent strategy
A journey from a bad kpi to an excellent strategyA journey from a bad kpi to an excellent strategy
A journey from a bad kpi to an excellent strategyAleksey Savkin
 

En vedette (7)

Lightning: large scale machine learning in python
Lightning: large scale machine learning in pythonLightning: large scale machine learning in python
Lightning: large scale machine learning in python
 
Performance and scalability for machine learning
Performance and scalability for machine learningPerformance and scalability for machine learning
Performance and scalability for machine learning
 
Low Complexity Regularization of Inverse Problems - Course #3 Proximal Splitt...
Low Complexity Regularization of Inverse Problems - Course #3 Proximal Splitt...Low Complexity Regularization of Inverse Problems - Course #3 Proximal Splitt...
Low Complexity Regularization of Inverse Problems - Course #3 Proximal Splitt...
 
Hyperparameter optimization with approximate gradient
Hyperparameter optimization with approximate gradientHyperparameter optimization with approximate gradient
Hyperparameter optimization with approximate gradient
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
A journey from a bad kpi to an excellent strategy
A journey from a bad kpi to an excellent strategyA journey from a bad kpi to an excellent strategy
A journey from a bad kpi to an excellent strategy
 
Los adjetivos
Los adjetivosLos adjetivos
Los adjetivos
 

Similaire à Profiling in Python

Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014PyData
 
Python profiling
Python profilingPython profiling
Python profilingdreampuf
 
Functional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with PythonFunctional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with PythonCarlos V.
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CSteffen Wenz
 
Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)Qiangning Hong
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11Henry Schreiner
 
Nodejs性能分析优化和分布式设计探讨
Nodejs性能分析优化和分布式设计探讨Nodejs性能分析优化和分布式设计探讨
Nodejs性能分析优化和分布式设计探讨flyinweb
 
Numba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyNumba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyTravis Oliphant
 
Down the rabbit hole, profiling in Django
Down the rabbit hole, profiling in DjangoDown the rabbit hole, profiling in Django
Down the rabbit hole, profiling in DjangoRemco Wendt
 
Time Series Analysis for Network Secruity
Time Series Analysis for Network SecruityTime Series Analysis for Network Secruity
Time Series Analysis for Network Secruitymrphilroth
 
Python na Infraestrutura 
MySQL do Facebook

Python na Infraestrutura 
MySQL do Facebook
Python na Infraestrutura 
MySQL do Facebook

Python na Infraestrutura 
MySQL do Facebook
Artur Rodrigues
 
Pygrunn 2012 down the rabbit - profiling in python
Pygrunn 2012   down the rabbit - profiling in pythonPygrunn 2012   down the rabbit - profiling in python
Pygrunn 2012 down the rabbit - profiling in pythonRemco Wendt
 
Profiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsProfiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsemBO_Conference
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudAndrea Righi
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
Machine learning with py torch
Machine learning with py torchMachine learning with py torch
Machine learning with py torchRiza Fahmi
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.pptssuserd64918
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerMarina Kolpakova
 

Similaire à Profiling in Python (20)

Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
 
Python profiling
Python profilingPython profiling
Python profiling
 
Functional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with PythonFunctional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with Python
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
Python高级编程(二)
Python高级编程(二)Python高级编程(二)
Python高级编程(二)
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11
 
Mpi in-python
Mpi in-pythonMpi in-python
Mpi in-python
 
Nodejs性能分析优化和分布式设计探讨
Nodejs性能分析优化和分布式设计探讨Nodejs性能分析优化和分布式设计探讨
Nodejs性能分析优化和分布式设计探讨
 
R and cpp
R and cppR and cpp
R and cpp
 
Numba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyNumba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPy
 
Down the rabbit hole, profiling in Django
Down the rabbit hole, profiling in DjangoDown the rabbit hole, profiling in Django
Down the rabbit hole, profiling in Django
 
Time Series Analysis for Network Secruity
Time Series Analysis for Network SecruityTime Series Analysis for Network Secruity
Time Series Analysis for Network Secruity
 
Python na Infraestrutura 
MySQL do Facebook

Python na Infraestrutura 
MySQL do Facebook
Python na Infraestrutura 
MySQL do Facebook

Python na Infraestrutura 
MySQL do Facebook

 
Pygrunn 2012 down the rabbit - profiling in python
Pygrunn 2012   down the rabbit - profiling in pythonPygrunn 2012   down the rabbit - profiling in python
Pygrunn 2012 down the rabbit - profiling in python
 
Profiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsProfiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf Tools
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
Machine learning with py torch
Machine learning with py torchMachine learning with py torch
Machine learning with py torch
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.ppt
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
 

Plus de Fabian Pedregosa

Random Matrix Theory and Machine Learning - Part 4
Random Matrix Theory and Machine Learning - Part 4Random Matrix Theory and Machine Learning - Part 4
Random Matrix Theory and Machine Learning - Part 4Fabian Pedregosa
 
Random Matrix Theory and Machine Learning - Part 3
Random Matrix Theory and Machine Learning - Part 3Random Matrix Theory and Machine Learning - Part 3
Random Matrix Theory and Machine Learning - Part 3Fabian Pedregosa
 
Random Matrix Theory and Machine Learning - Part 2
Random Matrix Theory and Machine Learning - Part 2Random Matrix Theory and Machine Learning - Part 2
Random Matrix Theory and Machine Learning - Part 2Fabian Pedregosa
 
Random Matrix Theory and Machine Learning - Part 1
Random Matrix Theory and Machine Learning - Part 1Random Matrix Theory and Machine Learning - Part 1
Random Matrix Theory and Machine Learning - Part 1Fabian Pedregosa
 
Average case acceleration through spectral density estimation
Average case acceleration through spectral density estimationAverage case acceleration through spectral density estimation
Average case acceleration through spectral density estimationFabian Pedregosa
 
Adaptive Three Operator Splitting
Adaptive Three Operator SplittingAdaptive Three Operator Splitting
Adaptive Three Operator SplittingFabian Pedregosa
 
Sufficient decrease is all you need
Sufficient decrease is all you needSufficient decrease is all you need
Sufficient decrease is all you needFabian Pedregosa
 
Asynchronous Stochastic Optimization, New Analysis and Algorithms
Asynchronous Stochastic Optimization, New Analysis and AlgorithmsAsynchronous Stochastic Optimization, New Analysis and Algorithms
Asynchronous Stochastic Optimization, New Analysis and AlgorithmsFabian Pedregosa
 
Parallel Optimization in Machine Learning
Parallel Optimization in Machine LearningParallel Optimization in Machine Learning
Parallel Optimization in Machine LearningFabian Pedregosa
 
Breaking the Nonsmooth Barrier: A Scalable Parallel Method for Composite Opti...
Breaking the Nonsmooth Barrier: A Scalable Parallel Method for Composite Opti...Breaking the Nonsmooth Barrier: A Scalable Parallel Method for Composite Opti...
Breaking the Nonsmooth Barrier: A Scalable Parallel Method for Composite Opti...Fabian Pedregosa
 

Plus de Fabian Pedregosa (10)

Random Matrix Theory and Machine Learning - Part 4
Random Matrix Theory and Machine Learning - Part 4Random Matrix Theory and Machine Learning - Part 4
Random Matrix Theory and Machine Learning - Part 4
 
Random Matrix Theory and Machine Learning - Part 3
Random Matrix Theory and Machine Learning - Part 3Random Matrix Theory and Machine Learning - Part 3
Random Matrix Theory and Machine Learning - Part 3
 
Random Matrix Theory and Machine Learning - Part 2
Random Matrix Theory and Machine Learning - Part 2Random Matrix Theory and Machine Learning - Part 2
Random Matrix Theory and Machine Learning - Part 2
 
Random Matrix Theory and Machine Learning - Part 1
Random Matrix Theory and Machine Learning - Part 1Random Matrix Theory and Machine Learning - Part 1
Random Matrix Theory and Machine Learning - Part 1
 
Average case acceleration through spectral density estimation
Average case acceleration through spectral density estimationAverage case acceleration through spectral density estimation
Average case acceleration through spectral density estimation
 
Adaptive Three Operator Splitting
Adaptive Three Operator SplittingAdaptive Three Operator Splitting
Adaptive Three Operator Splitting
 
Sufficient decrease is all you need
Sufficient decrease is all you needSufficient decrease is all you need
Sufficient decrease is all you need
 
Asynchronous Stochastic Optimization, New Analysis and Algorithms
Asynchronous Stochastic Optimization, New Analysis and AlgorithmsAsynchronous Stochastic Optimization, New Analysis and Algorithms
Asynchronous Stochastic Optimization, New Analysis and Algorithms
 
Parallel Optimization in Machine Learning
Parallel Optimization in Machine LearningParallel Optimization in Machine Learning
Parallel Optimization in Machine Learning
 
Breaking the Nonsmooth Barrier: A Scalable Parallel Method for Composite Opti...
Breaking the Nonsmooth Barrier: A Scalable Parallel Method for Composite Opti...Breaking the Nonsmooth Barrier: A Scalable Parallel Method for Composite Opti...
Breaking the Nonsmooth Barrier: A Scalable Parallel Method for Composite Opti...
 

Dernier

Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 

Dernier (20)

Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 

Profiling in Python

  • 2. Profiling : measure information of a running program (timing, memory consumption, disk usage, etc.) Profiling is about avoiding unnecessary optimizations and focusing on important ones. It is not enough to known algorithmic to make optimal code: Profiling is necessary to make meaningful comparisons. # three equivalent # ways to add two vectors # option 1 for i in range(a.size): a[i] = a[i] + b[i] # option 2 a = a + b # option 3 a += b
  • 3. The main Python profilers cProfile (in the standard library) line_profiler: line-by-line timing. memory_profiler: to get memory usage information (☺) IPython magic commands. yep extension profiler: to get timing of compiled extensions (☺)
  • 4. Example: Gradient-descent algorithm import numpy as np from scipy import optimize def gd(f, grad, x0, rtol=1e-3): xk = x0 fk = f(xk) tol = np.inf while tol >= rtol: # .. compute gradient .. gk = grad(xk) # .. find step size by line-search .. step_size = optimize.line_search(f, grad, xk, -gk, gfk=gk)[0] # .. perform update .. for i in range(xk.size): xk[i] = xk[i] - step_size * gk[i] fk_new = f(xk) tol = np.abs(fk - fk_new) / fk fk = fk_new return xk Question: should I optimize the gradient evaluation, the line-search or the coefficient update ?
  • 5. Profiling How-To 1. Create a realistic example 2. Run it with the appropriate profiler. cProfile line_profiler memory_profiler yep
  • 6. (most of the time) cProfile is useless $ python -m cProfile -s time gd.py ncalls tottime percall cumtime percall filename:lineno(function) 2 0.555 0.278 0.555 0.278 {method 'randn' of 'mtrand.RandomState' objects} 1 0.465 0.465 1.075 1.075 samples_generator.py:38(make_classification) 106/90 0.254 0.002 0.290 0.003 {built-in method load_dynamic} 65 0.174 0.003 0.174 0.003 {built-in method dot} 449 0.067 0.000 0.067 0.000 {built-in method loads} 2 0.051 0.026 0.051 0.026 {method 'take' of 'numpy.ndarray' objects} 261 0.039 0.000 0.094 0.000 doccer.py:12(docformat) 628/1 0.039 0.000 2.453 2.453 {built-in method exec} 3737 0.036 0.000 0.036 0.000 {built-in method stat} 7 0.031 0.004 0.034 0.005 {method 'get_filename' of 'zipimport.zipimporter' object 1985 0.031 0.000 0.122 0.000 <frozen importlib._bootstrap>:2016(find_spec) 1114/1108 0.030 0.000 0.086 0.000 {built-in method __build_class__} 1 0.026 0.026 0.211 0.211 gd.py:5(gd) 449 0.022 0.000 0.136 0.000 <frozen importlib._bootstrap>:1534(get_code) 7/3 0.020 0.003 0.223 0.074 {method 'load_module' of 'zipimport'.
  • 7. Enter line_profiler line-by-line time consumption decorate the function with @profile: run the code with kernprof -l -v my_script.py written by Robert Kern @profile def gd(f, grad, x0, rtol=1e-3): xk = x0 fk = f(xk) tol = np.inf while tol >= rtol: # .. compute gradient .. gk = grad(xk) # etc.
  • 8. $ kernprof -l -v gd.py Total time: 0.561124 s File: gd.py Function: gd at line 4 Line # Hits Time Per Hit % Time Line Contents ============================================================== 4 @profile 5 def gd(f, grad, x0, rtol=1e-12): 6 1 3 3.0 0.0 xk = x0 7 1 998 998.0 0.2 fk = f(xk) 8 1 2 2.0 0.0 tol = np.inf 9 18 28 1.6 0.0 while tol >= rtol: 10 # .. compute gradient .. 11 17 29337 1725.7 5.2 gk = grad(xk) 12 13 # .. find step size by line-search .. 14 17 101399 5964.6 18.1 step_size = optimize.line_search(f, grad, xk, 15 16 # .. perform update .. 17 170017 152034 0.9 27.1 for i in range(xk.size): 18 170000 260621 1.5 46.4 xk[i] = xk[i] - step_size * gk[i] 19 17 16550 973.5 2.9 fk_new = f(xk) 20 17 135 7.9 0.0 tol = np.abs(fk - fk_new) / fk 21 17 17 1.0 0.0 fk = fk_new Last column contains % of time spent on that line. More than 70% of time is spent in performing the parameter vector update!
  • 9. vectorizing the parameter update $ kernprof -l -v gd.py Total time: 0.138711 s File: gd.py Function: gd at line 4 Line # Hits Time Per Hit % Time Line Contents ============================================================== 4 @profile 5 def gd(f, grad, x0, rtol=1e-12): 6 1 3 3.0 0.0 xk = x0 7 1 1007 1007.0 0.7 fk = f(xk) 8 1 2 2.0 0.0 tol = np.inf 9 17 34 2.0 0.0 while tol >= rtol: 10 # .. compute gradient .. 11 16 29335 1833.4 21.1 gk = grad(xk) 12 13 # .. find step size by line-search .. 14 16 91213 5700.8 65.8 step_size = optimize.line_search(f, grad, xk, 15 16 # .. perform update .. 17 16 589 36.8 0.4 xk -= step_size * gk 18 16 16363 1022.7 11.8 fk_new = f(xk) 19 16 145 9.1 0.1 tol = np.abs(fk - fk_new) / fk 20 16 20 1.2 0.0 fk = fk_new from 48.5% to 0.4% ! total time divided by 4, without leaving Python.
  • 10. memory_profiler Get memory consumption of a Python program. Two modes: line-by-line and time-based. line-by-line similar interface as line_profiler: $ python -m memory_profiler gd.py Filename: gd.py Line # Mem usage Increment Line Contents ================================================ 4 352.117 MiB 0.000 MiB @profile 5 def gd(f, grad, x0, rtol=1e-12): 6 352.117 MiB 0.000 MiB xk = x0 7 352.145 MiB 0.027 MiB fk = f(xk) 8 352.145 MiB 0.000 MiB tol = np.inf 9 354.133 MiB 1.988 MiB while tol >= rtol: 10 # .. compute gradient .. 11 354.133 MiB 0.000 MiB gk = grad(xk) 12 13 # .. find step size by line-search .. 14 354.133 MiB 0.000 MiB step_size = optimize.line_search(f, grad, xk, -gk, gfk= 15 16 # .. perform update .. 17 354.133 MiB 0.000 MiB xk -= step_size * gk 18 354.133 MiB 0.000 MiB fk_new = f(xk) 19 354.133 MiB 0.000 MiB tol = np.abs(fk - fk_new) / fk 20 354.133 MiB 0.000 MiB fk = fk_new
  • 11. memory_profiler time-based memory consumption. run script with mprof run my_script.py plot with mprof plot
  • 12. memory_profiler, example 2 Two functions, process_data, create_data import numpy as np import scipy.signal @profile def create_data(): ret = [] for n in range(70): ret.append(np.random.randn(1, 70, 71, 72)) return ret @profile def process_data(data): data = np.concatenate(data) detrended = scipy.signal.detrend(data, axis=0) return detrended
  • 14. IPython shortcuts line_profilerand memory_profilerhave IPython shortcuts (AKA magic commands). line_profilershortcut: %lprun memory_profilershortcut: %memit, %mprun IPython %timeit:
  • 15. YEP recursive acronym for YEP extension profiler Existing Python profilers only record Python functions. Many codebases contain significant parts written in Cython/C/C++. YEP Allows to get timing information for compiled extensions (Cython/C/C++) Only one that does it without special compiling/linking. Integrates within Python the google-perftoolsprofiler. Does not work on Windows.
  • 16. YEP Simple example using scikit-learn The svm module in scikit-learn uses libsvm. import numpy as np from sklearn import svm, datasets X, y = datasets.make_classification(n_samples=1000) clf = svm.SVC() clf.fit(X, y) run with $ python -m yep -v simple_svm.py
  • 17. YEP output $ python -m yep -v simple_svm.py 2 0.7% 0.7% 268 97.5% _PyEval_EvalFrameEx 0 0.0% 0.7% 267 97.1% _PyClassMethod_New 0 0.0% 0.7% 267 97.1% _PyEval_EvalCode 0 0.0% 0.7% 267 97.1% _PyEval_EvalCodeEx 0 0.0% 0.7% 267 97.1% _PyObject_Call 0 0.0% 0.7% 267 97.1% __PyBuiltin_Init 0 0.0% 0.7% 206 74.9% __mh_execute_header 0 0.0% 0.7% 193 70.2% _Py_GetArgcArgv 0 0.0% 0.7% 193 70.2% _Py_Main 0 0.0% 0.7% 193 70.2% _main 0 0.0% 0.7% 193 70.2% start 0 0.0% 0.7% 188 68.4% _PyInit_libsvm 0 0.0% 0.7% 187 68.0% _svm_train 0 0.0% 0.7% 186 67.6% svm::Solver::Solve 48 17.5% 18.2% 184 66.9% svm_csr::Kernel::kernel_precomputed 135 49.1% 67.3% 135 49.1% svm::Kernel::kernel_rbf 0 0.0% 67.3% 74 26.9% _PyEval_CallObjectWithKeywords 0 0.0% 67.3% 74 26.9% _PyImport_ImportModuleLevelObject 0 0.0% 67.3% 74 26.9% __PyObject_CallMethodIdObjArgs 0 0.0% 67.3% 71 25.8% svm::Solver::select_working_set 0 0.0% 67.3% 27 9.8% _PyType_GenericAlloc 25 9.1% 76.4% 25 9.1% _stat 0 0.0% 76.4% 22 8.0% _PyInit_posix YEP is still a work in progress many rough edges.
  • 18. Conclusion Python has many tools for code profiling. Some of them are mature and easy to use. Integration with IPython. Happy hacking!