花分类的数据集:欢迎下载: https://download.csdn.net/download/zqx951102/11267836
% https://blog.csdn.net/ckzhb/article/details/60571769
% https://www.cnblogs.com/heaad/archive/2011/03/07/1976443.html
%读取训练数据
[f1,f2,f3,f4,class] = textread('F:\MatlabCode\BP神经网络例子\IRIS数据集\trainData.txt' , '%f%f%f%f%f',150);
%特征值归一化 %这里的每列是一个属性/特征的所有输入值,并不是后面函数中输入的一组样本数据。
[input,minI,maxI] = premnmx( [f1 , f2 , f3 , f4 ]') ;
%构造输出矩阵
s = length( class) ;
output = zeros( s , 3 ) ;
for i = 1 : s
output( i , class( i ) ) = 1 ;
end
%创建神经网络
net = newff( minmax(input) , [10 3] , { 'logsig' 'purelin' } , 'traingdx' ) ;
%设置训练参数
net.trainparam.show = 50 ;
net.trainparam.epochs = 500 ;
net.trainparam.goal = 0.01 ;
net.trainParam.lr = 0.01 ;
%开始训练
net = train( net, input , output' ) ;
%读取测试数据
[t1 t2 t3 t4 c] = textread('F:\MatlabCode\BP神经网络例子\IRIS数据集\testData.txt' , '%f%f%f%f%f',150);
%测试数据归一化
testInput = tramnmx ( [t1,t2,t3,t4]' , minI, maxI ) ;
%仿真
Y = sim( net , testInput )
%统计识别正确率
[s1 , s2] = size( Y ) ;
hitNum = 0 ;
for i = 1 : s2
[m , Index] = max( Y( : , i ) ) ;
if( Index == c(i) )
hitNum = hitNum + 1 ;
end
end
sprintf('识别率是 %3.3f%%',100 * hitNum / s2 )
第二个
% https://www.cnblogs.com/heaad/archive/2011/03/07/1976443.html
clear all
P = [-1 -1 2 2; 0 5 0 5];
T = [-1 -1 2 2];
%% 新式语法
net2 = newff(P,T,5,{'tansig', 'purelin'}, 'traingd'); % 隐含层有5个神经元
%p是输入的数据样本 T是隐层到输出层的各自节点数
net2.trainParam.goal = 1e-5;
net2.trainParam.epochs = 300;
net2.trainParam.lr = 0.05;
net2.trainParam.showWindow = 1;
net2.divideFcn = ''; % 为和书本一致,对于样本极少的情况,不要再三分了
% newff.m分成三大块:主程序、新版实现子函数 new_5p1()、旧版实现子函数 new_5p0()。通过仔细比较新旧这两个子函数,发现新版设置了 net.divideFcn 属性,
% 其值为'dividerand'。该函数把样本数据三分为训练集、验证集和测试集,默认比例是6:2:2。于是在我的程序中清除该属性再训练:
net2 = train(net2,P,T);
Y2 = sim(net2,P);
disp(['新式语法 mse: ' num2str(mse(T-Y2))]);
第三个
% https://blog.csdn.net/sinat_38321889/article/details/79182832#commentsedit
% 博客地址
clear;
clc;
X=-1:0.1:1;
D=[-0.9602 -0.5770 -0.0729 0.3771 0.6405 0.6600 0.4609...
0.1336 -0.2013 -0.4344 -0.5000 -0.3930 -0.1647 -.0988...
0.3072 0.3960 0.3449 0.1816 -0.312 -0.2189 -0.3201];
figure;
plot(X,D,'*'); %绘制原始数据分布图
net = newff([-1 1],[10 1],{'tansig','tansig'});
net.trainParam.epochs = 500; %训练的最大次数
net.trainParam.goal = 0.005; %全局最小误差
net = train(net,X,D);
O = sim(net,X);
figure;
plot(X,D,'*',X,O); %绘制训练后得到的结果和误差曲线
V = net.iw{1,1}%输入层到中间层权值
theta1 = net.b{1}%中间层各神经元阈值
W = net.lw{2,1}%中间层到输出层权值
theta2 = net.b{2}%输出层各神经元阈值
% 神经网络学习主要函数说明
% newff:前馈网络创建函数
% 语法:
% net = newff(A,B,{C},’trainFun’,’BLF’,’PF’)。
% A:一个n*2的矩阵,第i行元素为输入信号Xi的最大最小值
% B:一个K维行向量,其元素为网络中各个节点的数量。
% C:一个K维字符串行向量,每一个分量为对应层的神经元的激活函数,默认为“tansig”
% trainFun:为学习规则的采用的训练算法。默认为:“trainlm”
% BLF:BP权值/偏差学习函数。默认为:“learngdm”
% PF:性能函数,默认为“mse”
%
% train函数
% 语法:
% 即网络学习函数:
% [net,tr,YI,E] = train(net,X,Y)
% X:网络实际输入
% Y:网络应有输出
% tr:网络跟踪信息
% YI:网络实际输出
% E:误差矩阵
%
% sim函数
% **语法:**Y = sim(net,X)
% X:输入给网络的K*N矩阵,K为网络输入个数,N为样本数据量
% Y:输出矩阵Q*N,其中Q为网络输出个数
第四个
% https://blog.csdn.net/sinat_38321889/article/details/79182832#commentsedit
% 博客地址
% BP网络函数逼近实例
% 1.首先定义正弦函数,采样率为20Hz,频率为1Hz
k = 1; % 设定正弦信号频率
p = [0:0.05:4];
t = cos(k*pi*p) + 2*sin(pi*p);
plot(p, t, '-')
xlabel('时间')
ylabel('输入信号')
% 2.生成BP网络。用newff函数生成前向型BP网络,设定隐层中神经元数目为10
% 分别选择隐层的传递函数为 tansig,输出层的传递函数为 purelin,
% 学习算法为trainlm表示网络的训练函数。
net =newff(minmax(p),[10,10,1],{'tansig','tansig','purelin'},'trainlm');
% 3.对生成的网络进行仿真并做图显示。 sim是仿真函数
y1 = sim(net,p); plot(p, t, '-', p, y1, '--')
% 4.训练。对网络进行训练,设定训练误差目标为 1e-5,最大迭代次数为300,
% 学习速率为0.05。
net.trainParam.lr=0.05;
net.trainParam.epochs=300;
net.trainParam.goal=1e-5;
[net,tr]=train(net,p,t);
%5.再次对生成的网络进行仿真并做图显示。
y2 = sim(net,p);
plot(p, t, '-', p, y2, '--')