Возможности среды Matlab для построения нейронных сетей, использование предоставленными возможностями, страница 7

% TESTING THE ELMAN NETWORK

% =========================

%    SIM simulates an Elman network for as many timesteps

%    as input vectors in P.

a = sim(net,Pseq);

%    The network outputs and targets can then be plotted.

time = 1:length(p);

plot(time,t,'--',time,cat(2,a{:}))

title('Testing Amplitute Detection')

xlabel('Time Step')

ylabel('Target - -  Output ---')

10.3.2.jpg

%    The network does a fairly good job, if not perfect.

pause % Strike any key to test the network's generalization...

%    CHECKING GENERALIZATION

%    =======================

%    We will try waveforms with amplitudes of 1.6 and 1.2.

p3 = sin(1:20)*1.6;     % Input wave with amplitude of 1.6

t3 = ones(1,20)*1.6;    % We would like the output to be 1.6.

p4 = sin(1:20)*1.2;     % Try input wave with amplitude of 1.2

t4 = ones(1,20)*1.2;    % We would like the output to be 1.2.

%    Repeating each twice results in the series of test inputs.

pg = [p3 p4 p3 p4];

tg = [t3 t4 t3 t4];

pgseq = con2seq(pg);

pause % Strike any key to see generalization results...

%    GENERALIZATION RESULTS

%    ======================

%    SIM is used to simulate the network to these inputs.

a = sim(net,pgseq);

%    The network outputs and targets are plotted.

time = 1:length(pg);

plot(time,tg,'--',time,cat(2,a{:}))

title('Testing Generalization')

xlabel('Time Step')

ylabel('Target - -  Output ---')

10.3.3.jpg

%    The network does not do as well for amplitudes for

%    which it was not trained.

pause % Strike any key for conclusions...

%    CONCLUSIONS

%    ===========

%    The Elman recurrent network can learn to recognize time%    varying patterns.

%    In this case the network did a fairly good job with only

%    10 neurons in the recurrent layer, and 500 training epochs.

%    More recurrent neurons and longer training times could be

%    used to increase the network's accuracy on the training data.

%    Training the network on more amplitudes will result in

%    a network that generalizes better.

%    Type HELP ELMAN for a list of all Elman functions.

echo off

End of APPELM1

2.9.4.  Character recognition

Иногда очень полезно, если машина выполняет распознавание по образцу. В частности, машины, которые умеют читать символы, являются весьма рентабельными. Машина, считывающая банковские счета, может обработать гораздо больше счетов, чем человек, за равное время. Этот тип приложений экономит время и деньги, и устраняет необходимость человеку выполнять столь монотонную работу.

Данный пример демонстрирует, как данная задача может быть решена при помощи сети обратного распространения.

%    NEWFF   - Creates feed-forward networks.

%    SIM   - Simulates feed-forward networks.

%    CHARACTER RECOGNITION:

%    Using the above functions a feed-forward network is trained

%    to recognize character bit maps, in the presence of noise.

pause % Strike any key to continue...

%    DEFINING THE MODEL PROBLEM

%    ==========================

%    The script file PRPROB defines a matrix ALPHABET

%    which contains the bit maps of the 26 letters of the

%    alphabet.

%    This file also defines target vectors TARGETS for

%    each letter.  Each target vector has 26 elements with

%    all zeros, except for a single 1.  A has a 1 in the

%    first element, B in the second, etc.

[alphabet,targets] = prprob;

pause % Strike any key to define the network...

%    DEFINING THE NETWORK

%    ====================

%    The character recognition network will have 25 TANSIG

%    neurons in its hidden layer.

net = newff(alphabet,targets,25);

pause % Strike any key to train the network...

%    TRAINING THE NETWORK WITHOUT NOISE

%    ==================================

%    The network will be trained without dividing data up into

%    training and validation sets, because this is a small problem

%    with only 26 samples.

%

%    Training begins...please wait...

net1 = net;

net1.divideFcn = '';

[net1,tr] = train(net1,alphabet,targets);

%    ...and finally finishes.

pause % Strike any key to train the network with noise...

%    TRAINING THE NETWORK WITH NOISE

%    ===============================

%    The network will be trained on the original letters