Introduction to Theano a Fast Python Library for Modelling and Training
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Theano A Fast Python Library for Modelling and Training Pascal Lamblin Institut des algorithmes d'apprentissage de Montréal Montreal Institute for Learning Algorithms Université de Montréal August 4th, Deep Learning Summer School 2015, Montréal Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Overview Programming Tutorials Motivation Basic Usage Graph denition and Syntax Graph structure Strong typing Dierences from Python/NumPy Graph Transformations Substitution and Cloning Gradient Shared variables Make it fast! Optimizations Code Generation GPU Advanced Topics Looping: the scan operation Extending Theano Recent Features Features Coming Soon 2 / 57 Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Objectives These tutorials should be the occasion for you to: I Learn about software tools to implement deep learning algorithms I Get some hand-on experience with these tools I Play with simple implementation of existing algorithms I Ask questions and get help from developers and researchers http://github.com/mila-udem/summerschool2015/ 3 / 57 Wednesday, August 5th (day 3) I GPU programming (NVIDIA) Friday, August 7th (day 5) I Fuel: a library for machine learning datasets (Vincent Dumoulin) I Deep neural networks in Theano (Pascal Lamblin) Monday, August 10th (day 8) I Debugging with Theano (Frédéric Bastien) I Convolutional networks (Arnaud Bergeron) Tuesday, August 11th (day 9) I Scan: Loops in Theano (Pierre Luc Carrier) I Recurrent neural networks (Philémon Brakel) Wednesday, August 12th (day 10) I Overow session Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Tutorials Schedule Tuesday, August 4th (day 2) I Introduction to Theano, Theano examples (Pascal Lamblin) 4 / 57 Friday, August 7th (day 5) I Fuel: a library for machine learning datasets (Vincent Dumoulin) I Deep neural networks in Theano (Pascal Lamblin) Monday, August 10th (day 8) I Debugging with Theano (Frédéric Bastien) I Convolutional networks (Arnaud Bergeron) Tuesday, August 11th (day 9) I Scan: Loops in Theano (Pierre Luc Carrier) I Recurrent neural networks (Philémon Brakel) Wednesday, August 12th (day 10) I Overow session Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Tutorials Schedule Tuesday, August 4th (day 2) I Introduction to Theano, Theano examples (Pascal Lamblin) Wednesday, August 5th (day 3) I GPU programming (NVIDIA) 5 / 57 Monday, August 10th (day 8) I Debugging with Theano (Frédéric Bastien) I Convolutional networks (Arnaud Bergeron) Tuesday, August 11th (day 9) I Scan: Loops in Theano (Pierre Luc Carrier) I Recurrent neural networks (Philémon Brakel) Wednesday, August 12th (day 10) I Overow session Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Tutorials Schedule Tuesday, August 4th (day 2) I Introduction to Theano, Theano examples (Pascal Lamblin) Wednesday, August 5th (day 3) I GPU programming (NVIDIA) Friday, August 7th (day 5) I Fuel: a library for machine learning datasets (Vincent Dumoulin) I Deep neural networks in Theano (Pascal Lamblin) 6 / 57 Tuesday, August 11th (day 9) I Scan: Loops in Theano (Pierre Luc Carrier) I Recurrent neural networks (Philémon Brakel) Wednesday, August 12th (day 10) I Overow session Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Tutorials Schedule Tuesday, August 4th (day 2) I Introduction to Theano, Theano examples (Pascal Lamblin) Wednesday, August 5th (day 3) I GPU programming (NVIDIA) Friday, August 7th (day 5) I Fuel: a library for machine learning datasets (Vincent Dumoulin) I Deep neural networks in Theano (Pascal Lamblin) Monday, August 10th (day 8) I Debugging with Theano (Frédéric Bastien) I Convolutional networks (Arnaud Bergeron) 7 / 57 Wednesday, August 12th (day 10) I Overow session Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Tutorials Schedule Tuesday, August 4th (day 2) I Introduction to Theano, Theano examples (Pascal Lamblin) Wednesday, August 5th (day 3) I GPU programming (NVIDIA) Friday, August 7th (day 5) I Fuel: a library for machine learning datasets (Vincent Dumoulin) I Deep neural networks in Theano (Pascal Lamblin) Monday, August 10th (day 8) I Debugging with Theano (Frédéric Bastien) I Convolutional networks (Arnaud Bergeron) Tuesday, August 11th (day 9) I Scan: Loops in Theano (Pierre Luc Carrier) I Recurrent neural networks (Philémon Brakel) 8 / 57 Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Tutorials Schedule Tuesday, August 4th (day 2) I Introduction to Theano, Theano examples (Pascal Lamblin) Wednesday, August 5th (day 3) I GPU programming (NVIDIA) Friday, August 7th (day 5) I Fuel: a library for machine learning datasets (Vincent Dumoulin) I Deep neural networks in Theano (Pascal Lamblin) Monday, August 10th (day 8) I Debugging with Theano (Frédéric Bastien) I Convolutional networks (Arnaud Bergeron) Tuesday, August 11th (day 9) I Scan: Loops in Theano (Pierre Luc Carrier) I Recurrent neural networks (Philémon Brakel) Wednesday, August 12th (day 10) I Overow session 9 / 57 Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Theano vision Mathematical symbolic expression compiler I Easy to dene expressions I Expressions mimic NumPy's syntax and semantics I Possible to manipulate those expressions I Substitutions I Gradient, R operator I Stability optimizations I Fast to compute values for those expressions I Speed optimizations I Use fast back-ends (CUDA, BLAS, custom C code) I Tools to inspect and check for correctness 10 / 57 Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Current status I Mature: Theano has been developed and used since January 2008 (7 yrs old) I Driven hundreds of research papers I Good user documentation I Active mailing list with participants worldwide I Core technology for Silicon Valley start-ups I Many contributors from dierent places I Used to teach university classes I Has been used for research at large companies Theano: deeplearning.net/software/theano/ Deep Learning Tutorials: deeplearning.net/tutorial/ 11 / 57 Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Related projects Many libraries ar build on top of Theano (mostly machine learning) I Blocks I Keras I Lasagne I Morb I Pylearn2 I PyMC 3 I sklearn-theano I theano-rnn I ... 12 / 57 Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Basic usage Theano denes a language, a compiler, and a library. I Dene a symbolic expression I Compile a function that can compute values I Execute that function on numeric values 13 / 57 Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Dening an expression I Symbolic, strongly-typed inputs import theano from theano import tensor as T x=T.vector(’x’) W=T.matrix(’W’) b=T.vector(’b’) I NumPy-like syntax to build expressions dot=T.dot(x, W) out=T.nnet.sigmoid(dot+ b) 14 / 57 Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Graph visualization (1) debugprint(dot) dot [@A] '' |x [@B] |W [@C] debugprint(out) sigmoid [@A] '' |Elemwise{add,no_inplace} [@B] '' |dot [@C] '' | |x [@D] | |W [@E] |b [@F] 15 / 57 Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Compiling a Theano function Build a callable that compute outputs given inputs f= theano.function(inputs=[x, W], outputs=dot) g= theano.function([x, W, b], out) h= theano.function([x, W, b], [dot, out]) i= theano.function([x, W, b], [dot+ b, out]) 16 / 57 Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics Graph visualization (2) theano.printing.debugprint(f) theano.printing.debugprint(g) CGemv{inplace} [@A] '' 3 Elemwise{ScalarSigmoid}[(0, 0)] [@A] '' 2 |AllocEmpty{dtype='float64'} [@B] '' 2 |CGemv{no_inplace} [@B] '' 1 | |Shape_i{1} [@C] '' 1 |b [@C] | |W [@D] |TensorConstant{1.0} [@D] |TensorConstant{1.0} [@E] |InplaceDimShuffle{1,0} [@E] 'W.T' 0 |InplaceDimShuffle{1,0} [@F] 'W.T' 0 | |W [@F] | |W [@D] |x [@G] |x [@G] |TensorConstant{1.0} [@D] |TensorConstant{0.0} [@H] theano.printing.pydotprint(g) theano.printing.pydotprint(f) 17 / 57 Overview Graph denition and Syntax Programming Tutorials Graph Transformations Motivation Make it fast! Basic Usage Advanced Topics pydotprint(f) name=W TensorType(float6 , matri!" InplaceDimShuffle{1,0} Shape#i{1} TensorType(float6 , matri!" TensorType(int6 , scalar" name=W.T TensorType(float6 , matri!" (lloc)mpty{*type=+float6 +} &al=1.0 TensorType(float6 , scalar" name=! TensorType(float6 , &ector" &al=0.0 TensorType(float6 , scalar" ' 0 TensorType(float6