Professional Documents
Culture Documents
Neural Networks: Introduction & Matlab Examples
Neural Networks: Introduction & Matlab Examples
Neural Networks: Introduction & Matlab Examples
Ricardo de Castro
Technische Universität München
Lehrstuhl für Elektrische Antriebssysteme
Agenda
• Introduction & Motivation
• Case Study
• Matlab example: MPC solution via Neural Networks
References
[1] Hagan et al. Neural Network Design, 2nd edition, 2014 online version: https://hagan.okstate.edu/nnd.html
[2] Abu-Mostafa et al. Learning from Data, a Short Course, 2012.
[3] Mathworks, Neural Network Toolbox User‘s Guide (2017)
3
Some Problems…
Computer vision Medical Diagnosis
symptoms:
fever, headache,
blood pressure, …
4 diagnosis
biochemical analysis,
age, height, …
Control
Finance (e.g. prediction) (e.g., prediction / system identification)
stockPrice[k],
u[k], u[k-1],… u[k-N],
stockPrice[k-1], stockPrice[k+1] y[k+1]
y[k], y[k-1], …, y[k-M]
…
stockPrice[k-N]
u = control input, y=output, k=time index
4
Motivation Example: Credit Approval
Application Information
credit approved
credit rejected
Formalism:
model output,
Candidate Model
Learning Algorithm
(e.g. Neural Net)
Classification Problems
Regression Problems ( )
( − 1)
Output continuous variable ∈ [0, ∞)
…
( − 1)
8
Neuron (Single Input)
= ( + )
Input Output
• ∈ ℝ • ∈ ℝ
0 < 0
= =
1 ≥ 0
1
= −
1+ =
+
11
Neuron: Multiple Inputs
= ( ) = ( + )
Bias:
Inputs: , ,…,
Inputs: = …
Number of inputs:
12
Single-Layer Neural Network (NN)
element-wise representation
= , + , +⋯ , +
= , + , +⋯ , +
…
= , + , +⋯ , +
Notation:
Number of inputs= Number of outputs= = weight connecting neuron
,
Remark: each of the inputs is connected to a with input
neuron through a weight ,
13
Single-Layer Neural Network (NN)
vector representation
= ( + )
= number of inputs
= number of outputs
14
Create and Evaluate Single-Layer NN (I)
Matlab Code
P = [1;
2]; % data set - input (R=2)
Y = 1; % data set - output (S=1)
net = linearlayer;
% Creates a single layer of linear neurons
% OUTPUT: net = neural network object
15
Create and Evaluate Single-Layer NN (II)
Matlab Code
% net = Neural network object
define activation function
net.layers{1}.transferFcn ='hardlim';
16
Matlab Example
Consider a two-input network with one neuron and the following parameters:
−1
= = 3 2 b = 1.2
1
Write a Matlab script that computes the output of the network for the following
activation functions:
a) Hard limit
b) Linear
c) Log-Sigmoid
Answers:
a) 1.000
b) 0.200
c) 0.197
17
Learning Algorithm: Problem Setup
Error function: ∗
= ∗
∗
= optimal NN parameters
1
( )= − ( )
2
( )= +
18
Learning Algorithm: Batch Solution
Solution 1:
1
min ( ) = min − ( ) = =
2 1 1
A. vector representation
1
= = 1
min ( ) = min − ( − ) ⋮ , ⋮
1
B. first-order optimality condition
=0
∗ =
Least-squares Solution
19
Learning Algorithm: On-line Solution
Solution 2:
1
min ( ) = min − ( ) = =
2 1 1
A. Split error function
1 1
min ( ) = min = ( − )
2
( ) = −( − )
1 1
( ) = ( ) + ( − 1 )
1 aka least-mean squares (LMS) algorithm
Remarks:
• data points are processed one at a time (useful for real-time applications)
• learning rate needs to be chosen with care to ensure algorithm convergence [Hagan, Chap. 10]
20
On-line Learning Algorithm: Matlab API
Matlab Code
%net = Neural network object
output
Plant
control
input
-
Neural Network
NN output
Learning
Algorithm
22
Online System Identification via NN (II)
Goal: design and train a single-layer NN capable of approximating the output of this plant
Proposed structure for the NN
• Single layer
• Inputs: = −1 −2
• Activation function: linear
Results:
1 -4
0 50 100 150
0
5
-1
error=y-y(NN)
-2
0
-3
-4 -5
0 50 100 150 0 50 100 150
index index
Final weights/bias
= 6.23 −5.06 3.58 , = 0.0883
24
Multi-Layer Neural Network (I)
Example: 2-layer NN
= number of inputs
Element-wise representation
Layer 1 Layer 2
Number of neurons
Outputs , ,… , ,…
Weights
= 1, … , ; = 1, … , = 1, … , ; = 1, … ,
Bias , = 1, … , , = 1, … , 25
Activation function
Multi-Layer Neural Network (II)
Example: 2-layer NN
= ( + ) = +
Vector representation
Layer 1 Layer 2
Number of neurons
Outputs
Weights
Bias
Activation function
26
Multi-Layer Neural Network (III)
Hidden layer Output layer
= ( + ) = +
= ( + )+
Remarks:
Two-layer NN:
= , = ( + )+
parameters =( , , , ) Problem: min ( )
Error function: ∗
= optimal NN parameters
1
( )= − ( )
2
( )= ,
28
Learning Algorithm: Backpropagation
Solution :
1 = ,
min ( ) = min − ( )
2
A. Split error function
1 1
min ( ) = min = ( − )
2
, = ( + )+
Backpropagation algorithm
compute gradients of ( , ) using chain rule
(see [Hagan, Chap. 11] for details) 29
Matlab API: Create a 2-layer NN
Matlab Code
P = [1;
2]; % data set - input (R=2)
Y = 1; % data set - output
= 10 = 2, =1
hiddensize = [10];
net = feedforwardnet(hiddensize);
% Creates a multi-layer neuron network
% INPUT
% hiddensize = row vector with one or more
% hidden layer sizes
% OUTPUT: net = neural network object
% Remark: net.b{i} = bias of layer i; net.LW{i,j} = weights conecting layer i with layer j 31
Matlab API: Training Algorithm
Matlab Code
Data
Set
Learning/Training
, , , Algorithm
Data
Set
Learning/Training
, , , Algorithm
net.trainParam.showWindow = 0; % deactivate interactive GUI
net.trainParam.showCommandLine=1; % Generate command-line output
net.divideFcn = ’’; % use entire data set for training
[net]=train(net, P,Y);
% train trains a network net according to net.trainFcn and net.trainParam.
% INPUT
% net= neural network object
% P = [R-by-N] data set - input
% Y = [S-by-N] data set - output
% OUTPUT
% net = (trained) neural network output 33
Matlab Example: Nonlinear Fitting
Solution*: Regularization
• Idea: penalize large values of NN parameters (weights/bias)
Problem: min (1 − ) +
regularization term =( , , , ,…)
Matlab Code
net.performParam.regularization = 0.1; % sets value of lambda( )
*Additional solutions: early stopping, reduce number of neurons (see Hagen,Chap.13, for details)
36
Matlab Example: Nonlinear Fitting (III)
e) adapt the previous Matlab script in order to train neural network with regularization
• use = 10
37
Matlab Example: Nonlinear Fitting (IV)
=0
= 10
38
Matlab API: Auxiliary Commands
Matlab Code
gensim(net);
% net = neural network object
view(net);
Generation of a
graphical view
39
MPC-NN: Motivation
Previous lecture: the MPC Toolbox was employed to design a controller that brings the
state of this system to the origin while fulfilling input and state constraints.
Issue: the computational time of the MPC is too high due to the numerical optimization
routines
Task: design a NN that learns the MPC’s control law, enabling us to quickly compute the
optimal control action
40
MPC-NN: Design Approach
1) Design MPC Controller
control
input states
MPC Plant
2) Design NN
MPC
-
error Training
Algorithm
Neural Network
parameters
41
MPC-NN: Matlab Example
1 2
0.5 1
0
0
u
u
-0.5
-1
-1
-1.5 -2
-1 -1
-0.5 -0.5
-10 0 -10
0 -5
-5
0.5 0 0.5 0
5 x2 5
x2 1 1 10 x1
10 x1
43
MPC-NN: Matlab Example (III)
x1
x2
time
44
Summary
Introduction
• Data sets, learning algorithms, candidate models
Key Matlab Functions
Single-Layer Neural Network
• Fundamentals: neuron, activation function, layer linearlayer()
configure()
• Matlab example: constructing & evaluating NN net.layer{i}.transferFcn=...
• Learning algorithms sim()
46
Homework: Expected Results
= 0.5
= 0.0
30 20
20
10
10
0
y
y
0
-10
-10
-20 -20
1 1
0.5 1 0.5 1
0 0.5 0 0.5
0 0
-0.5 -0.5
-0.5 -0.5
p2 -1 -1 p1 p2 -1 -1 p1
47