bp神经网络matlab实例(bp神经网络matlab实例)

  • 格式:doc
  • 大小:25.10 KB
  • 文档页数:23

bp神经网络matlab实例(bp神经网络matlab实例)

Case 1 training BP network by momentum gradient descent

algorithm.

Training samples are defined as follows:

Input vector as

P =[-1 -2 31

-1 15 -3]

The target vector is t = [-1 -1 1 1]

Solution: the MATLAB program of this example is as follows:

Close all

Clear

Echo on

CLC

% NEWFF - generating a new feedforward neural network

% TRAIN -- training BP neural network

% SIM -- Simulation of BP neural network

Pause

Start by hitting any key

CLC

Percent defines training samples

% P as input vector

P=[-1, -2, 3, 1; -1, 1, 5, -3];

% T is the target vector

T=[-1, -1, 1, 1];

Pause;

CLC

% create a new feedforward neural network

Net=newff (minmax (P), [3,1],

{'tansig','purelin'},'traingdm')

The current input layer weights and thresholds

InputWeights=net.IW{1,1}

Inputbias=net.b{1}

The current network layer weights and thresholds

LayerWeights=net.LW{2,1}

Layerbias=net.b{2}

Pause

CLC

% set training parameters

Net.trainParam.show = 50;

Net.trainParam.lr = 0.05;

Net.trainParam.mc = 0.9;

Net.trainParam.epochs = 1000;

Net.trainParam.goal = 1e-3;

Pause

CLC

% call TRAINGDM algorithm to train BP network

[net, tr]=train (net, P, T);

Pause

CLC

Simulation of BP network by%

A = sim (net, P)

Calculate the simulation error

E = T - A

MSE=mse (E)

Pause

CLC

Echo off

Example 2 adopts Bayesian regularization algorithm to improve

the generalization ability of BP network. In this case, we used

two kinds of training methods, namely L-M algorithm (trainlm)

and the Bias regularization algorithm (trainbr), is used to

train the BP network, so that it can fit attached to a white

noise sine sample data. Among them, the sample data can be

generated as follows MATLAB statements:

Input vector: P = [-1:0.05:1];

Target vector: randn ('seed', 78341223);

T = sin (2*pi*P) +0.1*randn (size (P));

Solution: the MATLAB program of this example is as follows:

Close all

Clear

Echo on

CLC

% NEWFF - generating a new feedforward neural network

% TRAIN -- training BP neural network

% SIM -- Simulation of BP neural network

Pause

Start by hitting any key

CLC

% define training sample vector

% P as input vector

P = [-1:0.05:1];

% T is the target vector

Randn ('seed', 78341223); T = sin (2*pi*P) +0.1*randn (size

(P));

Draw the sample data points

Plot (P, T, +);

Echo off

Hold on;

Plot (P, sin (2*pi*P), ':');

Draw sine curves without noise

Echo on

CLC

Pause

CLC

% create a new feedforward neural network

Net=newff (minmax (P), [20,1], {'tansig','purelin'});

Pause

CLC

Echo off

CLC

Disp ('1. L-M optimization algorithm TRAINLM'); disp ('2.

Bayesian regularization algorithm TRAINBR');

Choice=input (\ "please select training algorithm (1,2): ');

Figure (GCF);

If (choice==1)

Echo on

CLC

% using L-M optimization algorithm TRAINLM

Net.trainFcn='trainlm';

Pause

CLC

% set training parameters

net.trainparam.epochs = 500;

net.trainparam.goal = 1e-6;

NET(.NET);

%重新初始化

暂停

中图分类号

“(选择= = 2)

回声

中图分类号

%采用贝叶斯正则化算法trainbr

trainfcn = 'trainbr”网;

暂停

中图分类号

%设置训练参数

net.trainparam.epochs = 500;

randn(“种子”,192736547);

NET(.NET);

%重新初始化