SlideShare a Scribd company logo
1 of 50
Download to read offline
Deep Learning in Theano
Massimo Quadrana
PhD Student @ Politecnico di Milano
Research Intern @ Telefonica I+D
massimo.quadrana@polimi.it @mxqdr
Original slides are available here: https://goo.gl/VLYsnR
Before starting
OS: Linux / Mac OS (sorry Windows guys :) )
Required software:
python 2.7x, git, openblas
Optional software (for faster math and better packages/virtualenv support):
Anaconda (https://www.continuum.io/downloads)
Anaconda Intel MKL (free student licence) (https://www.continuum.
io/anaconda-academic-subscriptions-available)
Before starting
Open your terminal and create a new virtualenv
> virtualenv -p /usr/bin/python2.7 theano-env
Activate the virtualenv
> source theano-env/bin/activate
Install the Theano package with dependences
> pip install Theano
(To exit the the virtualenv)
> deactivate
Before starting
To check if your Theano env is correctly configured run the following
python -c 'import theano'
It should complete without errors
Before starting
Get the lab code here
> git clone https://github.com/mquad/DNN_Lab_UPF
Structure of the repo:
● exercise/: directory with the code for the lab (it won’t run)
● complete/: directory with the code completed with the missing parts (it should
run :-) )
● notebooks/: some jupyther notebooks to show you some cool stuff
If you spot any error, or you have any feature request, open a new issue. I’ll do my
best to maintain the repo up-to-date :-)
Outline
Image classification
● Logistic Regression
● “Modern” Multi-layer NN
● Convolutional Neural networks
Sequence Modeling
● Character Based RNN
Open your editor and write the following. Save it as example_mul.py, then run
python example_mul.py
Theano intro
The official documentation:
http://deeplearning.net/software/theano/index.html
Theano intro
MNIST Dataset
60000 grayscale images (28 x 28 pixels each)
10 classes
8
Inputs Computation Outputs
Model
Logistic Regression on MNIST
0.1
T.dot(X, W) + b
softmax(X)
0. 0.10. 0.0. 0.0. 0.10.7
Zero One Two Three Four Five Six Seven Eight Nine
Logistic Regression on MNIST
Open exercise/logreg_raw.py
Many parts have already been coded for you (library import, data import and split,
evaluation)
Write the code for the Logistic Regression classifier
LogReg: input vars and model parameters
Shared variables in Theano maintain their state across functions.
Use them to store your model’s parameters.
If execute on GPU, shared variables are stored into the GPU memory for faster
access.
LogReg: model and cost function
Softmax: generalization of sigmoid over multiple classes
Predicted class: class with maximum expected probability
LogReg: model and cost function
Cross-entropy loss
y one-hot encoding of the correct class of input features x (y_i = 1 iif class of x is i)
here we keep y integer, and use indexing of y_hat to save computations
Note: average loss over the minibatch (the cost must be scalar)
LogReg: SGD
T.grad() does the automatic differentiation of the loss function
updates tells Theano how to update the model (shared) parameters (it can be a
list of tuples, a dict or OrderedDict)
LogReg: Training, Loss and Predictions
LogReg: Softmax
exp function can easily overflow: subtract by the maximum x value to get more
stable results (without any effects on correctness)
LogReg:
File logreg.py contains a cleaner version of the Logistic Regression classifier.
init(): defines model parameters
model(): defines our model
fit() and predict(): fits the model on training data and predict the class given new
data
Logistic Regression on MNIST
0.1
T.dot(X, w)
softmax(X)
0. 0.10. 0.0. 0.0. 0.10.7
Zero One Two Three Four Five Six Seven Eight Nine
Test accuracy: ~92%
“Modern” multi-layer network
0.0
h0 = relu(T.dot(X, Wh0) + b0)
y = softmax(T.dot(h1, Wy) + by)
0. 0.10. 0.0. 0.0. 0.0.9
Zero One Two Three Four Five Six Seven Eight Nine
h1 = relu(T.dot(h0, Wh1) + b1)
Noise
Noise
Noise
(or augmentation)
“Modern” multi-layer network
Open and complete mlp.py. The missing parts are:
● init(): initialize the MLP parameters
● model(): define the model using dropouts
● dropout(): apply dropout to the input
● apply_momentum(): apply momentum over the given updates
MLP: init()
MLP: model()
MLP: dropout()
MLP: apply_momentum()
“Modern” multi-layer network
0.0
h0 = relu(T.dot(X, Wh0) + b0)
y = softmax(T.dot(h1, Wy) + by)
0. 0.10. 0.0. 0.0. 0.0.9
Zero One Two Three Four Five Six Seven Eight Nine
h1 = relu(T.dot(h0, Wh1) + b1)
Noise
Noise
Noise
(or augmentation)
Test accuracy: ~98%
Convolutional Neural Networks
from deeplearning.net
CNNs in Theano
Open convnet.py and complete the following parts
● get_conv_output_shape(): compute the output shape of the convolutional
layer
● init(): complete the initialization of the convolutional filters
● model(): define entirely the cnn model
● adagrad(): define the update rules for adagrad
● rmsprop(): define the update rules for rmsprop (easy if you do adagrad first)
Dealing with Convolutions
Inputs have 3 dimensions:
width, height (spatial dimensions W)
and depth
Convolutions are
● local in width and height (receptive field F)
● full in depth
Dealing with Convolutions
Convolution hyper-parameters
● depth: number of neurons connected to the same input region
● stride: space btw depth columns in the spatial dimensions
● padding: how to treat borders (not covered in the examples)
The spatial size of the output volume is given by the formula
(W - F + 2P) / S + 1
Our CNN (variation of LeNet5)
INPUT, CONV(5,5)*, MAX POOL, CONV(5,5)*, MAX POOL, FC*
*The actual number of filters and Fully Connected layers is programmable
CNNs: get_conv_output_volume()
We don’t consider padding for simplicity
CNNs: init()
First CONV(5,5), MAX POOL
CNNs: init()
Analogously for the second CONV(5,5), MAX POOL
CNNs: model()
CNNs: adagrad()
CNNs: rmsprop()
Convolutional Neural Networks
from deeplearning.net
Test accuracy: 99.5%
SGD/Adagrad/Rmsprop in training convnets
Recurrent Neural Networks
Recurrent Neural Networks
Open char_rnn/char_rnn_vanilla.py and complete the following
● init(): define and initialize the parameters of the Vanilla RNN
● model(): compute the updates of hidden states of the RNN
● model_sample(): compute the updates of the hidden state of the RNN after
only one step
RNN: init()
RNN: model()
theano.scan() defines symbolic loops in Theano.
It has 4 main arguments (+ several additional ones):
● fn: function to be applied at every iteration
● sequences: variables scan has to iterate over (iteration is done over the first
dimension of each variable)
● outputs_info: initial state of the of the outputs computed recurrently
● non_sequences: list of additional arguments passed to fn
At each iteration, fn receives the parameters in the following order:
sequences (if any), outputs_info (if needed), non_sequences (if any)
RNN: model()
RNN: model_sample()
RNN - LSTM
RNN - LSTM
Under the complete/ folder you have the code for the LSTM version of char-rnn
● char_rnn_lstm.py: standard LSTM
● char_rnn_lstm_fast.py: fast LSTM, makes better usage of vectorized
operations (>2x faster)
● sampler.py: to sample from your RNN
EXERCISE: They differ from VanillaRNN in their init(), model(), model_sample()
and sampler() methods. Try to figure out how to pass from one model to the other.
Additional remarks
How to choose the optimal hyperparameters of my DNN?
● Grid search (overly expensive)
● Bayesian Optimization (effective but quite complex)
● Random search (cheap, effective and easy to implement)
Check out mlp_opt.py to run random
hyperparameter search for the MLP.
EXERCISE: Try with CNNs, RNNs
Additional Remarks (2)
Packages worth checking
● Built on-top of Theano: Lasagne, Keras
● Standalone packages: Caffe (Berkely), Tensorflow (Google), CNTK
(Microsoft)
Repositories
● gitxiv.com
Credits
The slides and code used in this lab were inspired by some great works done by
some great Deep Learning researchers
Alec Recford’s slides: “Introduction to Deep Learning with Python”, http://www.
slideshare.net/indicods/general-sequence-learning-with-recurrent-neural-
networks-for-next-ml
Andrey Karpathy’s blog post “The unreasonable effectiveness of Recurrent
Neural Networks”, http://karpathy.github.io/2015/05/21/rnn-effectiveness/
Andrey Karpathy’s char-rnn repo, https://github.com/karpathy/char-rnn

More Related Content

What's hot

PyTorch for Deep Learning Practitioners
PyTorch for Deep Learning PractitionersPyTorch for Deep Learning Practitioners
PyTorch for Deep Learning PractitionersBayu Aldi Yansyah
 
Introduction To TensorFlow | Deep Learning Using TensorFlow | CloudxLab
Introduction To TensorFlow | Deep Learning Using TensorFlow | CloudxLabIntroduction To TensorFlow | Deep Learning Using TensorFlow | CloudxLab
Introduction To TensorFlow | Deep Learning Using TensorFlow | CloudxLabCloudxLab
 
D3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningD3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningOswald Campesato
 
Distributed implementation of a lstm on spark and tensorflow
Distributed implementation of a lstm on spark and tensorflowDistributed implementation of a lstm on spark and tensorflow
Distributed implementation of a lstm on spark and tensorflowEmanuel Di Nardo
 
Tensorflow - Intro (2017)
Tensorflow - Intro (2017)Tensorflow - Intro (2017)
Tensorflow - Intro (2017)Alessio Tonioni
 
D3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningD3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningOswald Campesato
 
Alex Smola at AI Frontiers: Scalable Deep Learning Using MXNet
Alex Smola at AI Frontiers: Scalable Deep Learning Using MXNetAlex Smola at AI Frontiers: Scalable Deep Learning Using MXNet
Alex Smola at AI Frontiers: Scalable Deep Learning Using MXNetAI Frontiers
 
Java and Deep Learning (Introduction)
Java and Deep Learning (Introduction)Java and Deep Learning (Introduction)
Java and Deep Learning (Introduction)Oswald Campesato
 
Scaling Deep Learning with MXNet
Scaling Deep Learning with MXNetScaling Deep Learning with MXNet
Scaling Deep Learning with MXNetAI Frontiers
 
Introduction to PyTorch
Introduction to PyTorchIntroduction to PyTorch
Introduction to PyTorchJun Young Park
 
TensorFlow Tutorial | Deep Learning With TensorFlow | TensorFlow Tutorial For...
TensorFlow Tutorial | Deep Learning With TensorFlow | TensorFlow Tutorial For...TensorFlow Tutorial | Deep Learning With TensorFlow | TensorFlow Tutorial For...
TensorFlow Tutorial | Deep Learning With TensorFlow | TensorFlow Tutorial For...Simplilearn
 
Caffe framework tutorial2
Caffe framework tutorial2Caffe framework tutorial2
Caffe framework tutorial2Park Chunduck
 
Introduction to Neural Networks in Tensorflow
Introduction to Neural Networks in TensorflowIntroduction to Neural Networks in Tensorflow
Introduction to Neural Networks in TensorflowNicholas McClure
 
Deep Learning with PyTorch
Deep Learning with PyTorchDeep Learning with PyTorch
Deep Learning with PyTorchMayur Bhangale
 
Learning stochastic neural networks with Chainer
Learning stochastic neural networks with ChainerLearning stochastic neural networks with Chainer
Learning stochastic neural networks with ChainerSeiya Tokui
 
Neural Networks with Google TensorFlow
Neural Networks with Google TensorFlowNeural Networks with Google TensorFlow
Neural Networks with Google TensorFlowDarshan Patel
 
(Kpi summer school 2015) theano tutorial part1
(Kpi summer school 2015) theano tutorial part1(Kpi summer school 2015) theano tutorial part1
(Kpi summer school 2015) theano tutorial part1Serhii Havrylov
 
Deep learning for molecules, introduction to chainer chemistry
Deep learning for molecules, introduction to chainer chemistryDeep learning for molecules, introduction to chainer chemistry
Deep learning for molecules, introduction to chainer chemistryKenta Oono
 

What's hot (20)

PyTorch for Deep Learning Practitioners
PyTorch for Deep Learning PractitionersPyTorch for Deep Learning Practitioners
PyTorch for Deep Learning Practitioners
 
Introduction To TensorFlow | Deep Learning Using TensorFlow | CloudxLab
Introduction To TensorFlow | Deep Learning Using TensorFlow | CloudxLabIntroduction To TensorFlow | Deep Learning Using TensorFlow | CloudxLab
Introduction To TensorFlow | Deep Learning Using TensorFlow | CloudxLab
 
D3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningD3, TypeScript, and Deep Learning
D3, TypeScript, and Deep Learning
 
Distributed implementation of a lstm on spark and tensorflow
Distributed implementation of a lstm on spark and tensorflowDistributed implementation of a lstm on spark and tensorflow
Distributed implementation of a lstm on spark and tensorflow
 
Tensorflow - Intro (2017)
Tensorflow - Intro (2017)Tensorflow - Intro (2017)
Tensorflow - Intro (2017)
 
D3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningD3, TypeScript, and Deep Learning
D3, TypeScript, and Deep Learning
 
Alex Smola at AI Frontiers: Scalable Deep Learning Using MXNet
Alex Smola at AI Frontiers: Scalable Deep Learning Using MXNetAlex Smola at AI Frontiers: Scalable Deep Learning Using MXNet
Alex Smola at AI Frontiers: Scalable Deep Learning Using MXNet
 
Java and Deep Learning (Introduction)
Java and Deep Learning (Introduction)Java and Deep Learning (Introduction)
Java and Deep Learning (Introduction)
 
Introduction to TensorFlow
Introduction to TensorFlowIntroduction to TensorFlow
Introduction to TensorFlow
 
Scaling Deep Learning with MXNet
Scaling Deep Learning with MXNetScaling Deep Learning with MXNet
Scaling Deep Learning with MXNet
 
Introduction to PyTorch
Introduction to PyTorchIntroduction to PyTorch
Introduction to PyTorch
 
TensorFlow Tutorial | Deep Learning With TensorFlow | TensorFlow Tutorial For...
TensorFlow Tutorial | Deep Learning With TensorFlow | TensorFlow Tutorial For...TensorFlow Tutorial | Deep Learning With TensorFlow | TensorFlow Tutorial For...
TensorFlow Tutorial | Deep Learning With TensorFlow | TensorFlow Tutorial For...
 
Caffe framework tutorial2
Caffe framework tutorial2Caffe framework tutorial2
Caffe framework tutorial2
 
Deep Learning for Computer Vision: Software Frameworks (UPC 2016)
Deep Learning for Computer Vision: Software Frameworks (UPC 2016)Deep Learning for Computer Vision: Software Frameworks (UPC 2016)
Deep Learning for Computer Vision: Software Frameworks (UPC 2016)
 
Introduction to Neural Networks in Tensorflow
Introduction to Neural Networks in TensorflowIntroduction to Neural Networks in Tensorflow
Introduction to Neural Networks in Tensorflow
 
Deep Learning with PyTorch
Deep Learning with PyTorchDeep Learning with PyTorch
Deep Learning with PyTorch
 
Learning stochastic neural networks with Chainer
Learning stochastic neural networks with ChainerLearning stochastic neural networks with Chainer
Learning stochastic neural networks with Chainer
 
Neural Networks with Google TensorFlow
Neural Networks with Google TensorFlowNeural Networks with Google TensorFlow
Neural Networks with Google TensorFlow
 
(Kpi summer school 2015) theano tutorial part1
(Kpi summer school 2015) theano tutorial part1(Kpi summer school 2015) theano tutorial part1
(Kpi summer school 2015) theano tutorial part1
 
Deep learning for molecules, introduction to chainer chemistry
Deep learning for molecules, introduction to chainer chemistryDeep learning for molecules, introduction to chainer chemistry
Deep learning for molecules, introduction to chainer chemistry
 

Similar to Deep Learning in theano

Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117Ganesan Narayanasamy
 
Overview of Chainer and Its Features
Overview of Chainer and Its FeaturesOverview of Chainer and Its Features
Overview of Chainer and Its FeaturesSeiya Tokui
 
Introduction to Chainer
Introduction to ChainerIntroduction to Chainer
Introduction to ChainerSeiya Tokui
 
running stable diffusion on android
running stable diffusion on androidrunning stable diffusion on android
running stable diffusion on androidKoan-Sin Tan
 
Deep Learning, Scala, and Spark
Deep Learning, Scala, and SparkDeep Learning, Scala, and Spark
Deep Learning, Scala, and SparkOswald Campesato
 
Software Frameworks for Deep Learning (D1L7 2017 UPC Deep Learning for Comput...
Software Frameworks for Deep Learning (D1L7 2017 UPC Deep Learning for Comput...Software Frameworks for Deep Learning (D1L7 2017 UPC Deep Learning for Comput...
Software Frameworks for Deep Learning (D1L7 2017 UPC Deep Learning for Comput...Universitat Politècnica de Catalunya
 
Training course lect1
Training course lect1Training course lect1
Training course lect1Noor Dhiya
 
Final training course
Final training courseFinal training course
Final training courseNoor Dhiya
 
Lecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning FrameworksLecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning FrameworksMohamed Loey
 
From Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow EagerFrom Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow EagerGuy Hadash
 
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...South Tyrol Free Software Conference
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlowOswald Campesato
 
Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialJin-Hwa Kim
 
Using R on Netezza
Using R on NetezzaUsing R on Netezza
Using R on NetezzaAjay Ohri
 
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
 
maxbox starter60 machine learning
maxbox starter60 machine learningmaxbox starter60 machine learning
maxbox starter60 machine learningMax Kleiner
 
Separating Hype from Reality in Deep Learning with Sameer Farooqui
 Separating Hype from Reality in Deep Learning with Sameer Farooqui Separating Hype from Reality in Deep Learning with Sameer Farooqui
Separating Hype from Reality in Deep Learning with Sameer FarooquiDatabricks
 
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...David Walker
 

Similar to Deep Learning in theano (20)

Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117
 
Keras and TensorFlow
Keras and TensorFlowKeras and TensorFlow
Keras and TensorFlow
 
Overview of Chainer and Its Features
Overview of Chainer and Its FeaturesOverview of Chainer and Its Features
Overview of Chainer and Its Features
 
Introduction to Chainer
Introduction to ChainerIntroduction to Chainer
Introduction to Chainer
 
running stable diffusion on android
running stable diffusion on androidrunning stable diffusion on android
running stable diffusion on android
 
Deep Learning, Scala, and Spark
Deep Learning, Scala, and SparkDeep Learning, Scala, and Spark
Deep Learning, Scala, and Spark
 
Software Frameworks for Deep Learning (D1L7 2017 UPC Deep Learning for Comput...
Software Frameworks for Deep Learning (D1L7 2017 UPC Deep Learning for Comput...Software Frameworks for Deep Learning (D1L7 2017 UPC Deep Learning for Comput...
Software Frameworks for Deep Learning (D1L7 2017 UPC Deep Learning for Comput...
 
Training course lect1
Training course lect1Training course lect1
Training course lect1
 
Final training course
Final training courseFinal training course
Final training course
 
Lecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning FrameworksLecture 4: Deep Learning Frameworks
Lecture 4: Deep Learning Frameworks
 
From Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow EagerFrom Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow Eager
 
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlow
 
Digit recognizer
Digit recognizerDigit recognizer
Digit recognizer
 
Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorial
 
Using R on Netezza
Using R on NetezzaUsing R on Netezza
Using R on Netezza
 
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
 
maxbox starter60 machine learning
maxbox starter60 machine learningmaxbox starter60 machine learning
maxbox starter60 machine learning
 
Separating Hype from Reality in Deep Learning with Sameer Farooqui
 Separating Hype from Reality in Deep Learning with Sameer Farooqui Separating Hype from Reality in Deep Learning with Sameer Farooqui
Separating Hype from Reality in Deep Learning with Sameer Farooqui
 
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
 

Recently uploaded

List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfisabel213075
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfBalamuruganV28
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTSneha Padhiar
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptxmohitesoham12
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfDrew Moseley
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solidnamansinghjarodiya
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Erbil Polytechnic University
 
signals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsignals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsapna80328
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork
 
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfPaper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfNainaShrivastava14
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Sumanth A
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Romil Mishra
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsResearcher Researcher
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书rnrncn29
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 

Recently uploaded (20)

List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdf
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdf
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solid
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
 
signals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsignals in triangulation .. ...Surveying
signals in triangulation .. ...Surveying
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
 
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfPaper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending Actuators
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 

Deep Learning in theano

  • 1. Deep Learning in Theano Massimo Quadrana PhD Student @ Politecnico di Milano Research Intern @ Telefonica I+D massimo.quadrana@polimi.it @mxqdr Original slides are available here: https://goo.gl/VLYsnR
  • 2. Before starting OS: Linux / Mac OS (sorry Windows guys :) ) Required software: python 2.7x, git, openblas Optional software (for faster math and better packages/virtualenv support): Anaconda (https://www.continuum.io/downloads) Anaconda Intel MKL (free student licence) (https://www.continuum. io/anaconda-academic-subscriptions-available)
  • 3. Before starting Open your terminal and create a new virtualenv > virtualenv -p /usr/bin/python2.7 theano-env Activate the virtualenv > source theano-env/bin/activate Install the Theano package with dependences > pip install Theano (To exit the the virtualenv) > deactivate
  • 4. Before starting To check if your Theano env is correctly configured run the following python -c 'import theano' It should complete without errors
  • 5. Before starting Get the lab code here > git clone https://github.com/mquad/DNN_Lab_UPF Structure of the repo: ● exercise/: directory with the code for the lab (it won’t run) ● complete/: directory with the code completed with the missing parts (it should run :-) ) ● notebooks/: some jupyther notebooks to show you some cool stuff If you spot any error, or you have any feature request, open a new issue. I’ll do my best to maintain the repo up-to-date :-)
  • 6. Outline Image classification ● Logistic Regression ● “Modern” Multi-layer NN ● Convolutional Neural networks Sequence Modeling ● Character Based RNN
  • 7. Open your editor and write the following. Save it as example_mul.py, then run python example_mul.py Theano intro
  • 9. MNIST Dataset 60000 grayscale images (28 x 28 pixels each) 10 classes 8 Inputs Computation Outputs Model
  • 10. Logistic Regression on MNIST 0.1 T.dot(X, W) + b softmax(X) 0. 0.10. 0.0. 0.0. 0.10.7 Zero One Two Three Four Five Six Seven Eight Nine
  • 11. Logistic Regression on MNIST Open exercise/logreg_raw.py Many parts have already been coded for you (library import, data import and split, evaluation) Write the code for the Logistic Regression classifier
  • 12. LogReg: input vars and model parameters Shared variables in Theano maintain their state across functions. Use them to store your model’s parameters. If execute on GPU, shared variables are stored into the GPU memory for faster access.
  • 13. LogReg: model and cost function Softmax: generalization of sigmoid over multiple classes Predicted class: class with maximum expected probability
  • 14. LogReg: model and cost function Cross-entropy loss y one-hot encoding of the correct class of input features x (y_i = 1 iif class of x is i) here we keep y integer, and use indexing of y_hat to save computations Note: average loss over the minibatch (the cost must be scalar)
  • 15. LogReg: SGD T.grad() does the automatic differentiation of the loss function updates tells Theano how to update the model (shared) parameters (it can be a list of tuples, a dict or OrderedDict)
  • 16. LogReg: Training, Loss and Predictions
  • 17. LogReg: Softmax exp function can easily overflow: subtract by the maximum x value to get more stable results (without any effects on correctness)
  • 18. LogReg: File logreg.py contains a cleaner version of the Logistic Regression classifier. init(): defines model parameters model(): defines our model fit() and predict(): fits the model on training data and predict the class given new data
  • 19. Logistic Regression on MNIST 0.1 T.dot(X, w) softmax(X) 0. 0.10. 0.0. 0.0. 0.10.7 Zero One Two Three Four Five Six Seven Eight Nine Test accuracy: ~92%
  • 20. “Modern” multi-layer network 0.0 h0 = relu(T.dot(X, Wh0) + b0) y = softmax(T.dot(h1, Wy) + by) 0. 0.10. 0.0. 0.0. 0.0.9 Zero One Two Three Four Five Six Seven Eight Nine h1 = relu(T.dot(h0, Wh1) + b1) Noise Noise Noise (or augmentation)
  • 21. “Modern” multi-layer network Open and complete mlp.py. The missing parts are: ● init(): initialize the MLP parameters ● model(): define the model using dropouts ● dropout(): apply dropout to the input ● apply_momentum(): apply momentum over the given updates
  • 26. “Modern” multi-layer network 0.0 h0 = relu(T.dot(X, Wh0) + b0) y = softmax(T.dot(h1, Wy) + by) 0. 0.10. 0.0. 0.0. 0.0.9 Zero One Two Three Four Five Six Seven Eight Nine h1 = relu(T.dot(h0, Wh1) + b1) Noise Noise Noise (or augmentation) Test accuracy: ~98%
  • 28. CNNs in Theano Open convnet.py and complete the following parts ● get_conv_output_shape(): compute the output shape of the convolutional layer ● init(): complete the initialization of the convolutional filters ● model(): define entirely the cnn model ● adagrad(): define the update rules for adagrad ● rmsprop(): define the update rules for rmsprop (easy if you do adagrad first)
  • 29. Dealing with Convolutions Inputs have 3 dimensions: width, height (spatial dimensions W) and depth Convolutions are ● local in width and height (receptive field F) ● full in depth
  • 30. Dealing with Convolutions Convolution hyper-parameters ● depth: number of neurons connected to the same input region ● stride: space btw depth columns in the spatial dimensions ● padding: how to treat borders (not covered in the examples) The spatial size of the output volume is given by the formula (W - F + 2P) / S + 1
  • 31. Our CNN (variation of LeNet5) INPUT, CONV(5,5)*, MAX POOL, CONV(5,5)*, MAX POOL, FC* *The actual number of filters and Fully Connected layers is programmable
  • 32. CNNs: get_conv_output_volume() We don’t consider padding for simplicity
  • 34. CNNs: init() Analogously for the second CONV(5,5), MAX POOL
  • 38. Convolutional Neural Networks from deeplearning.net Test accuracy: 99.5%
  • 41. Recurrent Neural Networks Open char_rnn/char_rnn_vanilla.py and complete the following ● init(): define and initialize the parameters of the Vanilla RNN ● model(): compute the updates of hidden states of the RNN ● model_sample(): compute the updates of the hidden state of the RNN after only one step
  • 43. RNN: model() theano.scan() defines symbolic loops in Theano. It has 4 main arguments (+ several additional ones): ● fn: function to be applied at every iteration ● sequences: variables scan has to iterate over (iteration is done over the first dimension of each variable) ● outputs_info: initial state of the of the outputs computed recurrently ● non_sequences: list of additional arguments passed to fn At each iteration, fn receives the parameters in the following order: sequences (if any), outputs_info (if needed), non_sequences (if any)
  • 47. RNN - LSTM Under the complete/ folder you have the code for the LSTM version of char-rnn ● char_rnn_lstm.py: standard LSTM ● char_rnn_lstm_fast.py: fast LSTM, makes better usage of vectorized operations (>2x faster) ● sampler.py: to sample from your RNN EXERCISE: They differ from VanillaRNN in their init(), model(), model_sample() and sampler() methods. Try to figure out how to pass from one model to the other.
  • 48. Additional remarks How to choose the optimal hyperparameters of my DNN? ● Grid search (overly expensive) ● Bayesian Optimization (effective but quite complex) ● Random search (cheap, effective and easy to implement) Check out mlp_opt.py to run random hyperparameter search for the MLP. EXERCISE: Try with CNNs, RNNs
  • 49. Additional Remarks (2) Packages worth checking ● Built on-top of Theano: Lasagne, Keras ● Standalone packages: Caffe (Berkely), Tensorflow (Google), CNTK (Microsoft) Repositories ● gitxiv.com
  • 50. Credits The slides and code used in this lab were inspired by some great works done by some great Deep Learning researchers Alec Recford’s slides: “Introduction to Deep Learning with Python”, http://www. slideshare.net/indicods/general-sequence-learning-with-recurrent-neural- networks-for-next-ml Andrey Karpathy’s blog post “The unreasonable effectiveness of Recurrent Neural Networks”, http://karpathy.github.io/2015/05/21/rnn-effectiveness/ Andrey Karpathy’s char-rnn repo, https://github.com/karpathy/char-rnn