Neural Lab 1
Neural Lab 1
Matlab has a suite of programs designed to build neural networks (the Neural Networks Toolbox). Ad-
ditionally, there are demonstrations available through Matlab’s help feature.
In this lab, we will only work with three layer “feed forward” nets (these are the nets we discussed in
class).
There are three steps to using neural networks: Design, Training, and Testing (the same for modeling in
general!).
• Design:
– Define the number of nodes in each of the three layers (input, hidden, output).
– Define what σ is for each of the nodes (usually all nodes in a layer will all use the same σ). These
are called the transfer functions.
– Tell Matlab what optimization (or training) routine to use. Generally, we will use either traingdx,
which is gradient descent, or trainlm (for Levenburg-Marquardt, which is a combination of gra-
dient descent and Newton’s Method).
– Optional parts of the design: Error function (Mean Square Error is the default), plot the progress
of training, etc.
• Training: Once the network has been designed, we “train” the network (by optimizing the error
function).
This process determines the “best” set of weights and biases for our data set.
• Testing: We need to test our network to see if it has found a good balance between memorization
(accuracy) and generalization.
1
Sample Design and Training Session 1:
2
Sample Design and Training Session 2:
This session uses a “validation” data set to test the training. That is, we retain some of our data to see
if we’re getting a good balance between memorization and generalization.
In this graph, you might see a rather bad fit- Probably too many nodes in the hidden layer. Modify that
number and re-run the script until you get a nice fit.
3
Sample Design and Training Session 3: Character Recognition
To run this code, first write the function plotletters.m on the next page so we can visualize the
characters.
The following defines a network with 10 nodes in the hidden layer, and 26 nodes in the output layer.
We’ll use σ(x) = 1+e1−x (which is logsig) in both the hidden and output layers. We’ll use a gradient descent
algorithm for training:
net = newff(minmax(alphabet),[10 26],{’logsig’ ’logsig’},’traingdx’);
P = alphabet;
noisyP=alphabet+randn(size(alphabet))*0.4;
T = targets;
[net,tr] = train(net,P,T);
[net,tr] = train(net,noisyP,T);
A2 = sim(net,noisyP);
for j=1:26 %Number of noisy letters
A3 = compet(A2(:,j));
answer(j) = find(compet(A3) == 1);
end
NetLetters=alphabet(:,answer);
plotletters(NetLetters);
4
This is the function plotletters so we can visualize the characters...
function plotletters(alphabet)
[m,n]=size(alphabet);
if m~=35
error(’plotletters needs columns 35 numbers long’);
end
figure
MM=colormap(gray);
MM=MM(end:-1:1,:);
colormap(MM);
nn=min([n,25]);
for j=1:nn
subplot(5,5,j)
imagesc(reshape(alphabet(:,j),5,7)’);
axis equal
axis off
end