## 题目如下：

https://blog.csdn.net/weixin_42296976/article/details/81146551

```%读取训练数据，这里按行排列16个输入
%特征值归一化,数据归一化及将数据映射到[0,1]或[-1,1]区间或更小的区间
[input,minI,maxI] = premnmx( [f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16]')  ;
%构造输出矩阵
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 = 90 ;
net.trainparam.goal = 0.01 ;
net.trainParam.lr = 0.01 ;
%开始训练
net = train( net, input , output' ) ;
%读取测试数据
%测试数据归一化
testInput = tramnmx ( [t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16]' , 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 )```

data：

```1 1 1 1 1 -1 -1 1 1 1 1 1 1 -1 -1 1 1
1 -1 1 1 1 -1 -1 1 1 1 1 1 1 -1 -1 1 1
1 1 1 1 1 -1 1 1 1 1 1 1 1 1 -1 1 1
1 1 1 1 1 1 -1 1 1 1 1 1 1 -1 -1 1 1
1 1 1 1 1 -1 -1 1 1 1 1 1 1 -1 1 1 1
1 1 -1 1 1 -1 -1 1 1 1 1 1 1 -1 -1 1 1
1 1 1 1 1 -1 -1 1 1 -1 1 1 1 -1 -1 1 1
1 1 1 1 1 -1 -1 1 1 1 -1 1 1 -1 -1 1 1
1 1 1 1 1 -1 -1 1 1 1 1 1 -1 -1 -1 1 1
1 1 1 1 1 -1 -1 1 1 1 1 1 1 -1 -1 -1 1
1 1 1 1 1 -1 -1 1 1 1 1 1 1 -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1 -1 1 1 -1 -1 1 1 1 1 1 3
1 1 1 1 1 -1 -1 1 -1 -1 -1 1 1 1 1 1 3
1 1 1 1 1 -1 -1 1 1 -1 -1 1 1 1 1 1 3
1 1 1 1 1 1 -1 1 1 -1 -1 1 1 1 1 1 3
1 1 1 1 1 1 -1 1 1 -1 -1 1 1 1 1 1 3
1 1 1 1 1 -1 -1 1 1 -1 -1 1 1 1 1 1 3
1 1 1 1 1 -1 -1 1 1 -1 -1 1 1 1 1 1 3
1 1 1 1 1 -1 -1 1 1 -1 -1 1 1 1 1 1 3
1 1 1 1 1 1 -1 1 1 -1 -1 1 1 1 1 1 3
1 1 -1 1 1 -1 1 1 1 -1 -1 1 1 1 1 1 3
1 -1 1 1 1 -1 -1 1 1 -1 1 1 1 1 1 1 3
1 1 1 1 1 -1 -1 1 1 1 -1 1 1 -1 1 1 3
1 1 1 1 1 -1 -1 1 1 -1 -1 1 1 1 1 1 3```

## 涉及知识点：

1.BP（Back-propagation）神经网络：训练中可以通过反馈（反向传播）自行减小与目标的误差，直至达到训练前所要求误差目标的一种神经网络。其过程就是根据当前网络的学习率作为修正幅度，对每个神经元的残差在输入变量的梯度方向上求解误差值并进行修正。

2.预处理（归一化处理）：将数据映射到[0,1]或[-1,1]或更小的区间中。归一化可以防止有的数据过大减缓神经网络的收敛，导致训练时间超出预期。同时数据范围的大小同样会影响数据的作用。

（1） y = (x – min)/(max – min) ，min为x的最小值，max为x的最大值，将数据归一到 [ 0,1 ] 区间，此时激活函数应为S型函数（ sigmoid

（2） y = 2(x – min)/(max – min) – 1 ，将数据归一到 [ -1,1 ] 区间，此时激活函数应为 双极S形函数

3.使用Matlab实现神经网络：建立前馈神经网络使用到以下3个函数：

newff：

train：训练一个神经网络

X:网络实际输入

Y:网络应有输出

tr:训练跟踪信息

Y1:网络实际输出

E:误差矩阵

sim：使用网络进行仿真

net：网络

X:输入网络的KxN矩阵 K为网络个数 N为数据样本

Y：输出矩阵QxN Q为网络输出个数