神经网络(BP)系列(1)关于样本的归一和反归一

来源:互联网 发布:云计算能干什么 编辑:程序博客网 时间:2024/06/04 19:33

神经网络(BP)系列(1)关于样本的归一和反归一

这个系列主要针对使用matlab 神经网络工具箱,对一些初学者容易理解错误的地方进行解析。
我的解析也可能有理解不对的地方,希望大家批评指正.
这个系列主要针对使用matlab 神经网络工具箱,对一些初学者容易理解错误的地方进行解析。
1.   神经网络一列为一个样本,所以对于matlab 而言,要求输入和输出的列数必须一样的
经常有人问起的问题


Error using ==> network/train
Targets are incorrectly sized for network.
Matrix must have 1 rows.
解决:要求P T 的列数一样 ,如果不一样 P=p’ t=t’ 转置一下
2   归一
澄清一个对归一的错误理解1
样本矩阵为9行4列。9组样本,4个变量。现在归一化:
x=[68.7   66.6   5610   19.2;
89.9   90.8   4500   11.8;
120.8   120.6   6800   20.6;
169   40.4   6160   40.6;
180.8   69.8   7330   33.4;
190.3   130.2   7320   31.6;
109.8   151.1   5754   86.1;
33.2   61.4   8255   22.6;
111.7   126.6   7040   13.6;]
写法一:
for i=1:9
x1(i,:)=(x(i,:)-min(x(i,:)))/(max(x(i,:))-min(x(i,:)))
end
结果:
0.0089   0.0085   1.0000   0
0.0174   0.0176   1.0000   0
0.0148   0.0148   1.0000   0
0.0210   0     1.0000   0.0000
0.0202   0.0050   1.0000   0
0.0218   0.0135   1.00 00   0
0.0042   0.0115   1.0000   0
0.0013   0.0047   1.0000   0
0.0140   0.0161   1.0000   0

写法二:
x=x'
for i=1:4
x1(i,:)=(x(i,:)-min(x(i,:)))/(max(x(i,:))-min(x(i,:)))
end
结果:
Columns 1 through 8

0.2260   0.3609   0.5576   0.8644   0.9395   1.0000   0.4876   0
0.2367   0.4553   0.7245   0     0.2656   0.8112   1.0000   0.1897
0.2956   0     0.6125   0.4421   0.7537   0.7510   0.3340   1.0000
0.0996   0     0.1184   0.3876   0.2907   0.2665   1.0000   0.1454

Column 9

0.4997
0.7787
0.6764
0.0242
注意:写法 2为正确的归一化

对归一的错误理解2
数据集分为训练集和测试集,对训练集和测试集分别做归一处理
所以就会有人问 如果我的测试集只有一个数据 如何归一呀
最大最小值从那里找呀
正确的理解是:
训练集和测试集的归一标准是一样的
建议:
如果训练集和测试集是一起归一的 可以自己编程实现归一
如果是训练集和测试集是分开的,最好是使用matlab自带的premnmx、postmnmx、tramnmx 函数
如果是自己编程的话 ,请注意训练集和测试集的归一标准需要一样
premnmx、postmnmx、tramnmx 函数
的使用例子如下:
Example

Here is the code to normalize a given data set so
that the inputs and targets will fall in the
range [-1,1], using PREMNMX, and the code to train a network
with the normalized data.

p = [-10 -7.5 -5 -2.5 0 2.5 5 7.5 10];
t = [0 7.07 -10 -7.07 0 7.07 10 7.07 0];
[pn,minp,maxp,tn,mint,maxt] = premnmx(p,t);
net = newff(minmax(pn),[5 1],{'tansig' 'purelin'},'trainlm');
net = train(net,pn,tn);

If we then receive new inputs to apply to the trained
network, we will use TRAMNMX to transform them
first. Then the transformed inputs can be used
to simulate the previously trained network. The
network output must also be unnormalized using
POSTMNMX.

p2 = [4 -7];
[p2n] = tramnmx(p2,minp,maxp);
an = sim(net,pn);
[a] = postmnmx(an,mint,maxt);

这个是归一到-1 和 1 之间 那我要归一到0 1 之间怎么办
有人说可以使用加绝对值就归一到 0 1之间了
我觉得加绝对值可能会有些问题
比较好的方式是变换
P 在-1 1 之间
Pp=(p+1)/2 就可以归一到0 1之间
至于要归一到0.1 0.9 之间 选取合适的变换就可以做到了
原创粉丝点击