SlideShare a Scribd company logo
How to Build your
First Neural Network
Keras & TensorFlow
Hichem Felouat
hichemfel@gmail.com
Hichem Felouat - hichemfel@gmail.com 2
Perceptron
• The inputs and output are numbers and each input connection is associated
with a weight.
• Compute the weighted sum of its inputs then applies an activation
function to that sum and outputs the result.
Hichem Felouat - hichemfel@gmail.com 3
Algorithm for Training a Perceptron
Hichem Felouat - hichemfel@gmail.com 4
Algorithm for Training a Perceptron
Hichem Felouat - hichemfel@gmail.com 5
Limitations of Perceptron
A perceptron can only separate linearly separable classes, but it is unable to
separate non-linear class boundaries.
Example: Let the following problem of
binary classification(problem of the XOR).
Clearly, no line can separate the
two classes!
Solution :
• Use two lines instead of one!
• Use an intermediary layer of neurons
in the NN.
Hichem Felouat - hichemfel@gmail.com 6
The Multilayer Perceptron MLP
• The signal flows only in one direction
(from the inputs to the outputs), so this
architecture is an example of a
feedforward neural network (FNN).
• When an ANN contains a deep stack of
hidden layers, it is called a deep
neural network (DNN).
Hichem Felouat - hichemfel@gmail.com 7
The Multilayer Perceptron MLP
Hichem Felouat - hichemfel@gmail.com 8
The Multilayer Perceptron MLP
• In 1986, the backpropagation training algorithm was introduced,
which is still used today.
• The backpropagation consists of only two passes through the
network (one forward, one backward), the backpropagation
algorithm is able to compute the gradient of the network’s error
with regard to every single model parameter. In other words, it
can find out how each connection weight and each bias term
should be tweaked in order to reduce the error.
David Rumelhart et al. “Learning Internal Representations by Error Propagation,” (Defense Technical Information Center technical report, September 1985).
Hichem Felouat - hichemfel@gmail.com 9
Popular Activation functions for MLP
Hichem Felouat - hichemfel@gmail.com 10
Neural Network vocabulary
1) Cost Function
2) Gradient Descent
3) Learning Rate
4) Backpropagation
5) Batches
6) Epochs
Hichem Felouat - hichemfel@gmail.com 11
Regression MLPs
Hichem Felouat - hichemfel@gmail.com 12
Regression MLPs
1) If you want to predict a single value (e.g., the price of a
house, given many of its features), then you just need a
single output neuron, its output is the predicted value.
2) For multivariate regression (i.e., to predict multiple
values at once), you need one output neuron per output
dimension. For example, to locate the center of an object in
an image, you need to predict 2D coordinates, so you need
two output neurons.
1
2
Hichem Felouat - hichemfel@gmail.com 13
Regression MLPs
Hyperparameter Typical value
input neurons One per input feature
hidden layers Depends on the problem, but typically 1 to 5
neurons per hidden layer Depends on the problem, but typically 10 to 100
output neurons 1 per prediction dimension
Hidden activation ReLU (relu)
Output activation None, or ReLU/softplus (if positive outputs) or
logistic/tanh (if bounded outputs)
Loss function MSE (mean_squared_error) or MAE/Huber (if
outliers)
Typical regression MLP architecture:
Hichem Felouat - hichemfel@gmail.com 14
Classification MLPs
Hichem Felouat - hichemfel@gmail.com 15
Classification MLPs
Binary classification:
• We just need a single output neuron using the logistic activation function 0
or 1.
Multilabel Binary Classification:
• We need one output neuron for each positive class.
For example: you could have an email classification system that predicts
whether each incoming email is ham or spam and simultaneously predicts
whether it is an urgent or nonurgent email. In this case, you would need two
output neurons, both using the logistic activation function.
Hichem Felouat - hichemfel@gmail.com 16
Multiclass Classification:
We need to have one output neuron per class, and we should
use the softmax activation function for the whole output layer.
Classification MLPs
For example:
classes 0 through 9 for digit
image classification [28, 28].
Hichem Felouat - hichemfel@gmail.com 17
Classification MLPs
Hyperparameter Binary classification Multilabel binary
classification
Multiclass
classification
input neurons One per input feature One per input feature One per input feature
hidden layers neurons
per hidden layer
Depends on the
problem
Depends on the
problem
Depends on the
problem
output neurons 1 1 per label 1 per class
Hidden activation ReLU (relu) ReLU (relu) ReLU (relu)
Output layer activation Logistic (sigmoid) Logistic (sigmoid) Softmax (softmax)
Loss function Cross entropy Cross entropy Cross entropy
Typical classification MLP architecture
Binary classification : categorical_crossentropy
Multiclass classification : sparse_categorical_crossentropy
Hichem Felouat - hichemfel@gmail.com 18
Classification MLPs
If the training set was very skewed, with some classes
being overrepresented and others underrepresented, it
would be useful to set the class_weight argument when
calling the fit().
sample_weight: Per-instance weights could be useful if
some instances were labeled by experts while others were
labeled using a crowdsourcing platform: you might want to
give more weight to the former.
Hichem Felouat - hichemfel@gmail.com 19
If you are not satisfied with the performance of your model, you should
go back and tune the hyperparameters.
• Try another optimizer.
• Try tuning model hyperparameters such as the number of layers, the
number of neurons per layer, and the types of activation functions to
use for each hidden layer.
• Try tuning other hyperparameters, such as the number of epochs and the
batch size.
Classification MLPs
 Once you are satisfied with your model’s validation accuracy, you should
evaluate it on the test set to estimate the generalization error before you
deploy the model to production.
Gradient Descent, Momentum Optimization, Nesterov
Accelerated Gradient, AdaGrad, RMSProp, Adam, Nadam
Hichem Felouat - hichemfel@gmail.com 20
How to Save and Load Your Model
Keras use the HDF5 format to save both the model’s architecture (including
every layer’s hyperparameters) and the values of all the model parameters
for every layer (e.g., connection weights and biases). It also saves the
optimizer (including its hyperparameters and any state it may have).
model.save("my_keras_model.h5")
Loading the model:
model = keras.models.load_model("my_keras_model.h5")
Hichem Felouat - hichemfel@gmail.com 21
How to increase your small image dataset
trainAug = ImageDataGenerator( rotation_range=40, width_shift_range=0.2,
height_shift_range = 0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True,
fill_mode='nearest')
model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"])
H = model.fit_generator( trainAug.flow(trainX, trainY, batch_size=BS),
steps_per_epoch=len(trainX) // BS,validation_data=(testX, testY), validation_steps=len(testX)
// BS, epochs=EPOCHS)
Hichem Felouat - hichemfel@gmail.com 22
Training a Deep DNN
Training a very large deep neural network can be painfully slow.
Here are some ways to speed up training (and reach a better
solution):
• Applying a good initialization strategy for the connection weights.
• Using a good activation function.
• Using Batch Normalization.
• Reusing parts of a pretrained network (possibly built on an
auxiliary task or using unsupervised learning).
• Using faster optimizer.
Ad

Recommended

Predict future time series forecasting
Predict future time series forecasting
Hichem Felouat
 
Transfer Learning
Transfer Learning
Hichem Felouat
 
Build your own Convolutional Neural Network CNN
Build your own Convolutional Neural Network CNN
Hichem Felouat
 
Natural Language Processing (NLP)
Natural Language Processing (NLP)
Hichem Felouat
 
Object detection and Instance Segmentation
Object detection and Instance Segmentation
Hichem Felouat
 
Introduction To Generative Adversarial Networks GANs
Introduction To Generative Adversarial Networks GANs
Hichem Felouat
 
Machine Learning Algorithms
Machine Learning Algorithms
Hichem Felouat
 
Object Detection with Deep Learning - Xavier Giro-i-Nieto - UPC School Barcel...
Object Detection with Deep Learning - Xavier Giro-i-Nieto - UPC School Barcel...
Universitat Politècnica de Catalunya
 
Tensor flow (1)
Tensor flow (1)
景逸 王
 
Tensor board
Tensor board
Sung Kim
 
Accelerating Random Forests in Scikit-Learn
Accelerating Random Forests in Scikit-Learn
Gilles Louppe
 
Xgboost
Xgboost
Vivian S. Zhang
 
Mit6 094 iap10_lec01
Mit6 094 iap10_lec01
Tribhuwan Pant
 
Workshop - Introduction to Machine Learning with R
Workshop - Introduction to Machine Learning with R
Shirin Elsinghorst
 
Mit6 094 iap10_lec02
Mit6 094 iap10_lec02
Tribhuwan Pant
 
Rajat Monga at AI Frontiers: Deep Learning with TensorFlow
Rajat Monga at AI Frontiers: Deep Learning with TensorFlow
AI Frontiers
 
Mit6 094 iap10_lec04
Mit6 094 iap10_lec04
Tribhuwan Pant
 
Mit6 094 iap10_lec03
Mit6 094 iap10_lec03
Tribhuwan Pant
 
Introduction to TensorFlow
Introduction to TensorFlow
Ralph Vincent Regalado
 
A Tour of Tensorflow's APIs
A Tour of Tensorflow's APIs
Dean Wyatte
 
Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117
Ganesan Narayanasamy
 
Gradient Boosted Regression Trees in scikit-learn
Gradient Boosted Regression Trees in scikit-learn
DataRobot
 
Neural Networks with Google TensorFlow
Neural Networks with Google TensorFlow
Darshan Patel
 
TensorFlow example for AI Ukraine2016
TensorFlow example for AI Ukraine2016
Andrii Babii
 
Introducton to Convolutional Nerural Network with TensorFlow
Introducton to Convolutional Nerural Network with TensorFlow
Etsuji Nakai
 
Mat lab workshop
Mat lab workshop
Vinay Kumar
 
Tensorflow - Intro (2017)
Tensorflow - Intro (2017)
Alessio Tonioni
 
Data Wrangling For Kaggle Data Science Competitions
Data Wrangling For Kaggle Data Science Competitions
Krishna Sankar
 
Separating Hype from Reality in Deep Learning with Sameer Farooqui
Separating Hype from Reality in Deep Learning with Sameer Farooqui
Databricks
 
Getting started with Machine Learning
Getting started with Machine Learning
Gaurav Bhalotia
 

More Related Content

What's hot (20)

Tensor flow (1)
Tensor flow (1)
景逸 王
 
Tensor board
Tensor board
Sung Kim
 
Accelerating Random Forests in Scikit-Learn
Accelerating Random Forests in Scikit-Learn
Gilles Louppe
 
Xgboost
Xgboost
Vivian S. Zhang
 
Mit6 094 iap10_lec01
Mit6 094 iap10_lec01
Tribhuwan Pant
 
Workshop - Introduction to Machine Learning with R
Workshop - Introduction to Machine Learning with R
Shirin Elsinghorst
 
Mit6 094 iap10_lec02
Mit6 094 iap10_lec02
Tribhuwan Pant
 
Rajat Monga at AI Frontiers: Deep Learning with TensorFlow
Rajat Monga at AI Frontiers: Deep Learning with TensorFlow
AI Frontiers
 
Mit6 094 iap10_lec04
Mit6 094 iap10_lec04
Tribhuwan Pant
 
Mit6 094 iap10_lec03
Mit6 094 iap10_lec03
Tribhuwan Pant
 
Introduction to TensorFlow
Introduction to TensorFlow
Ralph Vincent Regalado
 
A Tour of Tensorflow's APIs
A Tour of Tensorflow's APIs
Dean Wyatte
 
Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117
Ganesan Narayanasamy
 
Gradient Boosted Regression Trees in scikit-learn
Gradient Boosted Regression Trees in scikit-learn
DataRobot
 
Neural Networks with Google TensorFlow
Neural Networks with Google TensorFlow
Darshan Patel
 
TensorFlow example for AI Ukraine2016
TensorFlow example for AI Ukraine2016
Andrii Babii
 
Introducton to Convolutional Nerural Network with TensorFlow
Introducton to Convolutional Nerural Network with TensorFlow
Etsuji Nakai
 
Mat lab workshop
Mat lab workshop
Vinay Kumar
 
Tensorflow - Intro (2017)
Tensorflow - Intro (2017)
Alessio Tonioni
 
Data Wrangling For Kaggle Data Science Competitions
Data Wrangling For Kaggle Data Science Competitions
Krishna Sankar
 
Tensor flow (1)
Tensor flow (1)
景逸 王
 
Tensor board
Tensor board
Sung Kim
 
Accelerating Random Forests in Scikit-Learn
Accelerating Random Forests in Scikit-Learn
Gilles Louppe
 
Workshop - Introduction to Machine Learning with R
Workshop - Introduction to Machine Learning with R
Shirin Elsinghorst
 
Rajat Monga at AI Frontiers: Deep Learning with TensorFlow
Rajat Monga at AI Frontiers: Deep Learning with TensorFlow
AI Frontiers
 
A Tour of Tensorflow's APIs
A Tour of Tensorflow's APIs
Dean Wyatte
 
Power ai tensorflowworkloadtutorial-20171117
Power ai tensorflowworkloadtutorial-20171117
Ganesan Narayanasamy
 
Gradient Boosted Regression Trees in scikit-learn
Gradient Boosted Regression Trees in scikit-learn
DataRobot
 
Neural Networks with Google TensorFlow
Neural Networks with Google TensorFlow
Darshan Patel
 
TensorFlow example for AI Ukraine2016
TensorFlow example for AI Ukraine2016
Andrii Babii
 
Introducton to Convolutional Nerural Network with TensorFlow
Introducton to Convolutional Nerural Network with TensorFlow
Etsuji Nakai
 
Mat lab workshop
Mat lab workshop
Vinay Kumar
 
Tensorflow - Intro (2017)
Tensorflow - Intro (2017)
Alessio Tonioni
 
Data Wrangling For Kaggle Data Science Competitions
Data Wrangling For Kaggle Data Science Competitions
Krishna Sankar
 

Similar to How to Build your First Neural Network (20)

Separating Hype from Reality in Deep Learning with Sameer Farooqui
Separating Hype from Reality in Deep Learning with Sameer Farooqui
Databricks
 
Getting started with Machine Learning
Getting started with Machine Learning
Gaurav Bhalotia
 
Mlcc #4
Mlcc #4
Chung-Hsiang Ofa Hsueh
 
33.-Multi-Layer-Perceptron.pdf
33.-Multi-Layer-Perceptron.pdf
gnans Kgnanshek
 
Multilayer Perceptron - Elisa Sayrol - UPC Barcelona 2018
Multilayer Perceptron - Elisa Sayrol - UPC Barcelona 2018
Universitat Politècnica de Catalunya
 
Scaling Deep Learning with MXNet
Scaling Deep Learning with MXNet
AI Frontiers
 
AI powered emotion recognition: From Inception to Production - Global AI Conf...
AI powered emotion recognition: From Inception to Production - Global AI Conf...
Vandana Kannan
 
AI powered emotion recognition: From Inception to Production - Global AI Conf...
AI powered emotion recognition: From Inception to Production - Global AI Conf...
Apache MXNet
 
Artificial Intelligence, Machine Learning and Deep Learning
Artificial Intelligence, Machine Learning and Deep Learning
Sujit Pal
 
Deep Learning Interview Questions And Answers | AI & Deep Learning Interview ...
Deep Learning Interview Questions And Answers | AI & Deep Learning Interview ...
Simplilearn
 
Deep Learning Module 2A Training MLP.pptx
Deep Learning Module 2A Training MLP.pptx
vipul6601
 
Neural Networks from Scratch - TensorFlow 101
Neural Networks from Scratch - TensorFlow 101
Gerold Bausch
 
A Scaleable Implementation of Deep Learning on Spark -Alexander Ulanov
A Scaleable Implementation of Deep Learning on Spark -Alexander Ulanov
Spark Summit
 
A Scaleable Implemenation of Deep Leaning on Spark- Alexander Ulanov
A Scaleable Implemenation of Deep Leaning on Spark- Alexander Ulanov
Spark Summit
 
assignment regarding the security of the cyber
assignment regarding the security of the cyber
yasir149288
 
AI/ML Fundamentals to advanced Slides by GDG Amrita Mysuru.pdf
AI/ML Fundamentals to advanced Slides by GDG Amrita Mysuru.pdf
Lakshay14663
 
Deep Learning Study _ FInalwithCNN_RNN_LSTM_GRU.pdf
Deep Learning Study _ FInalwithCNN_RNN_LSTM_GRU.pdf
naveenraghavendran10
 
Deep learning crash course
Deep learning crash course
Vishwas N
 
Deep Learning with Apache MXNet (September 2017)
Deep Learning with Apache MXNet (September 2017)
Julien SIMON
 
Deep learning summary
Deep learning summary
ankit_ppt
 
Separating Hype from Reality in Deep Learning with Sameer Farooqui
Separating Hype from Reality in Deep Learning with Sameer Farooqui
Databricks
 
Getting started with Machine Learning
Getting started with Machine Learning
Gaurav Bhalotia
 
33.-Multi-Layer-Perceptron.pdf
33.-Multi-Layer-Perceptron.pdf
gnans Kgnanshek
 
Scaling Deep Learning with MXNet
Scaling Deep Learning with MXNet
AI Frontiers
 
AI powered emotion recognition: From Inception to Production - Global AI Conf...
AI powered emotion recognition: From Inception to Production - Global AI Conf...
Vandana Kannan
 
AI powered emotion recognition: From Inception to Production - Global AI Conf...
AI powered emotion recognition: From Inception to Production - Global AI Conf...
Apache MXNet
 
Artificial Intelligence, Machine Learning and Deep Learning
Artificial Intelligence, Machine Learning and Deep Learning
Sujit Pal
 
Deep Learning Interview Questions And Answers | AI & Deep Learning Interview ...
Deep Learning Interview Questions And Answers | AI & Deep Learning Interview ...
Simplilearn
 
Deep Learning Module 2A Training MLP.pptx
Deep Learning Module 2A Training MLP.pptx
vipul6601
 
Neural Networks from Scratch - TensorFlow 101
Neural Networks from Scratch - TensorFlow 101
Gerold Bausch
 
A Scaleable Implementation of Deep Learning on Spark -Alexander Ulanov
A Scaleable Implementation of Deep Learning on Spark -Alexander Ulanov
Spark Summit
 
A Scaleable Implemenation of Deep Leaning on Spark- Alexander Ulanov
A Scaleable Implemenation of Deep Leaning on Spark- Alexander Ulanov
Spark Summit
 
assignment regarding the security of the cyber
assignment regarding the security of the cyber
yasir149288
 
AI/ML Fundamentals to advanced Slides by GDG Amrita Mysuru.pdf
AI/ML Fundamentals to advanced Slides by GDG Amrita Mysuru.pdf
Lakshay14663
 
Deep Learning Study _ FInalwithCNN_RNN_LSTM_GRU.pdf
Deep Learning Study _ FInalwithCNN_RNN_LSTM_GRU.pdf
naveenraghavendran10
 
Deep learning crash course
Deep learning crash course
Vishwas N
 
Deep Learning with Apache MXNet (September 2017)
Deep Learning with Apache MXNet (September 2017)
Julien SIMON
 
Deep learning summary
Deep learning summary
ankit_ppt
 
Ad

Recently uploaded (20)

F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
VCE Literature Section A Exam Response Guide
VCE Literature Section A Exam Response Guide
jpinnuck
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
Values Education 10 Quarter 1 Module .pptx
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Intellectual Property Right (Jurisprudence).pptx
Intellectual Property Right (Jurisprudence).pptx
Vishal Chanalia
 
HistoPathology Ppt. Arshita Gupta for Diploma
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
VCE Literature Section A Exam Response Guide
VCE Literature Section A Exam Response Guide
jpinnuck
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
Values Education 10 Quarter 1 Module .pptx
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Intellectual Property Right (Jurisprudence).pptx
Intellectual Property Right (Jurisprudence).pptx
Vishal Chanalia
 
HistoPathology Ppt. Arshita Gupta for Diploma
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
Ad

How to Build your First Neural Network

  • 1. How to Build your First Neural Network Keras & TensorFlow Hichem Felouat [email protected]
  • 2. Hichem Felouat - [email protected] 2 Perceptron • The inputs and output are numbers and each input connection is associated with a weight. • Compute the weighted sum of its inputs then applies an activation function to that sum and outputs the result.
  • 3. Hichem Felouat - [email protected] 3 Algorithm for Training a Perceptron
  • 4. Hichem Felouat - [email protected] 4 Algorithm for Training a Perceptron
  • 5. Hichem Felouat - [email protected] 5 Limitations of Perceptron A perceptron can only separate linearly separable classes, but it is unable to separate non-linear class boundaries. Example: Let the following problem of binary classification(problem of the XOR). Clearly, no line can separate the two classes! Solution : • Use two lines instead of one! • Use an intermediary layer of neurons in the NN.
  • 6. Hichem Felouat - [email protected] 6 The Multilayer Perceptron MLP • The signal flows only in one direction (from the inputs to the outputs), so this architecture is an example of a feedforward neural network (FNN). • When an ANN contains a deep stack of hidden layers, it is called a deep neural network (DNN).
  • 7. Hichem Felouat - [email protected] 7 The Multilayer Perceptron MLP
  • 8. Hichem Felouat - [email protected] 8 The Multilayer Perceptron MLP • In 1986, the backpropagation training algorithm was introduced, which is still used today. • The backpropagation consists of only two passes through the network (one forward, one backward), the backpropagation algorithm is able to compute the gradient of the network’s error with regard to every single model parameter. In other words, it can find out how each connection weight and each bias term should be tweaked in order to reduce the error. David Rumelhart et al. “Learning Internal Representations by Error Propagation,” (Defense Technical Information Center technical report, September 1985).
  • 9. Hichem Felouat - [email protected] 9 Popular Activation functions for MLP
  • 10. Hichem Felouat - [email protected] 10 Neural Network vocabulary 1) Cost Function 2) Gradient Descent 3) Learning Rate 4) Backpropagation 5) Batches 6) Epochs
  • 11. Hichem Felouat - [email protected] 11 Regression MLPs
  • 12. Hichem Felouat - [email protected] 12 Regression MLPs 1) If you want to predict a single value (e.g., the price of a house, given many of its features), then you just need a single output neuron, its output is the predicted value. 2) For multivariate regression (i.e., to predict multiple values at once), you need one output neuron per output dimension. For example, to locate the center of an object in an image, you need to predict 2D coordinates, so you need two output neurons. 1 2
  • 13. Hichem Felouat - [email protected] 13 Regression MLPs Hyperparameter Typical value input neurons One per input feature hidden layers Depends on the problem, but typically 1 to 5 neurons per hidden layer Depends on the problem, but typically 10 to 100 output neurons 1 per prediction dimension Hidden activation ReLU (relu) Output activation None, or ReLU/softplus (if positive outputs) or logistic/tanh (if bounded outputs) Loss function MSE (mean_squared_error) or MAE/Huber (if outliers) Typical regression MLP architecture:
  • 14. Hichem Felouat - [email protected] 14 Classification MLPs
  • 15. Hichem Felouat - [email protected] 15 Classification MLPs Binary classification: • We just need a single output neuron using the logistic activation function 0 or 1. Multilabel Binary Classification: • We need one output neuron for each positive class. For example: you could have an email classification system that predicts whether each incoming email is ham or spam and simultaneously predicts whether it is an urgent or nonurgent email. In this case, you would need two output neurons, both using the logistic activation function.
  • 16. Hichem Felouat - [email protected] 16 Multiclass Classification: We need to have one output neuron per class, and we should use the softmax activation function for the whole output layer. Classification MLPs For example: classes 0 through 9 for digit image classification [28, 28].
  • 17. Hichem Felouat - [email protected] 17 Classification MLPs Hyperparameter Binary classification Multilabel binary classification Multiclass classification input neurons One per input feature One per input feature One per input feature hidden layers neurons per hidden layer Depends on the problem Depends on the problem Depends on the problem output neurons 1 1 per label 1 per class Hidden activation ReLU (relu) ReLU (relu) ReLU (relu) Output layer activation Logistic (sigmoid) Logistic (sigmoid) Softmax (softmax) Loss function Cross entropy Cross entropy Cross entropy Typical classification MLP architecture Binary classification : categorical_crossentropy Multiclass classification : sparse_categorical_crossentropy
  • 18. Hichem Felouat - [email protected] 18 Classification MLPs If the training set was very skewed, with some classes being overrepresented and others underrepresented, it would be useful to set the class_weight argument when calling the fit(). sample_weight: Per-instance weights could be useful if some instances were labeled by experts while others were labeled using a crowdsourcing platform: you might want to give more weight to the former.
  • 19. Hichem Felouat - [email protected] 19 If you are not satisfied with the performance of your model, you should go back and tune the hyperparameters. • Try another optimizer. • Try tuning model hyperparameters such as the number of layers, the number of neurons per layer, and the types of activation functions to use for each hidden layer. • Try tuning other hyperparameters, such as the number of epochs and the batch size. Classification MLPs  Once you are satisfied with your model’s validation accuracy, you should evaluate it on the test set to estimate the generalization error before you deploy the model to production. Gradient Descent, Momentum Optimization, Nesterov Accelerated Gradient, AdaGrad, RMSProp, Adam, Nadam
  • 20. Hichem Felouat - [email protected] 20 How to Save and Load Your Model Keras use the HDF5 format to save both the model’s architecture (including every layer’s hyperparameters) and the values of all the model parameters for every layer (e.g., connection weights and biases). It also saves the optimizer (including its hyperparameters and any state it may have). model.save("my_keras_model.h5") Loading the model: model = keras.models.load_model("my_keras_model.h5")
  • 21. Hichem Felouat - [email protected] 21 How to increase your small image dataset trainAug = ImageDataGenerator( rotation_range=40, width_shift_range=0.2, height_shift_range = 0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) H = model.fit_generator( trainAug.flow(trainX, trainY, batch_size=BS), steps_per_epoch=len(trainX) // BS,validation_data=(testX, testY), validation_steps=len(testX) // BS, epochs=EPOCHS)
  • 22. Hichem Felouat - [email protected] 22 Training a Deep DNN Training a very large deep neural network can be painfully slow. Here are some ways to speed up training (and reach a better solution): • Applying a good initialization strategy for the connection weights. • Using a good activation function. • Using Batch Normalization. • Reusing parts of a pretrained network (possibly built on an auxiliary task or using unsupervised learning). • Using faster optimizer.