一个简单的BP神经网络matlab程序(附函数详解)

说明:20180604更新

1、此文的程序来自博客:http://www.cnblogs.com/heaad/archive/2011/03/07/1976443.html

2、本人对其中涉及到的函数进行了详细说明。

3、此程序中部分函数的使用方式是高版本Matlab中不推荐的

文中给出了当前高版本Matlab中的使用方式,并给出了相关博客地址,具体如下:

归一化方式mapminmax :   http://blog.youkuaiyun.com/lkj345/article/details/50352385

newff函数的使用: http://www.cnblogs.com/xxfcz/p/4482813.html

4、使用过程中,可能出现的问题

https://blog.youkuaiyun.com/ckzhb/article/details/78615712

5、所需的IRIS数据集

https://download.youkuaiyun.com/download/ckzhb/10458430

https://github.com/xiaoxiaoxiaohui/Resource/tree/master/水仙花数据集

 

具体程序如下:

%读取训练数据
[f1,f2,f3,f4,class] = textread('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('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 )

 

 

详细说明:

 

1、文本文件读取函数

 

评论 88
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值