Matlab-神经网络训练 [英] Matlab - Neural network training
问题描述
我正在创建带有反向传播的2层神经网络. NN应该从20001x17向量中获取数据,该向量在每一行中都包含以下信息:
I'm working on creating a 2 layer neural network with back-propagation. The NN is supposed to get its data from a 20001x17 vector that holds following information in each row:
-前16个单元格包含从0到15的整数,这些整数用作变量,以帮助我们确定看到这些变量时要表达的26个字母中的哪个字母.例如,以下一系列16个值表示字母A:[2 8 4 5 2 7 5 3 1 6 0 8 2 7 2 7].
-The first 16 cells hold integers ranging from 0 to 15 which act as variables to help us determine which one of the 26 letters of the alphabet we mean to express when seeing those variables. For example a series of 16 values as follows are meant to represent the letter A: [2 8 4 5 2 7 5 3 1 6 0 8 2 7 2 7].
-第17个单元格包含一个介于1到26之间的数字,代表我们想要的字母. 1代表A,2代表B等.
-The 17th cell holds a number ranging from 1 to 26 representing the letter of the alphabet we want. 1 stands for A, 2 stands for B etc.
NN的输出层包含26个输出.每次给NN输入类似于上述输入的输入时,都应该输出一个1x26向量,该向量在除对应于输入值要表示的字母的那个单元格之外的所有单元格中都包含零.例如,输出[1 0 0 ... 0]将是字母A,而[0 0 0 ... 1]将是字母Z.
The output layer of the NN consists of 26 outputs. Every time the NN is fed an input like the one described above it's supposed to output a 1x26 vector containing zeros in all but the one cell that corresponds to the letter that the input values were meant to represent. for example the output [1 0 0 ... 0] would be letter A, whereas [0 0 0 ... 1] would be the letter Z.
在提供代码之前,有些重要的事情:我需要使用traingdm函数,并且隐藏层号(目前)固定为21.
Some things that are important before i present the code: I need to use the traingdm function and the hidden layer number is fixed (for now) at 21.
尝试创建上述概念,我编写了以下matlab代码:
Trying to create the above concept i wrote the following matlab code:
%%%%%%%%
%Start of code%
%%%%%%%%
%
%Initialize the input and target vectors
%
p = zeros(16,20001);
t = zeros(26,20001);
%
%Fill the input and training vectors from the dataset provided
%
for i=2:20001
for k=1:16
p(k,i-1) = data(i,k);
end
t(data(i,17),i-1) = 1;
end
net = newff(minmax(p),[21 26],{'logsig' 'logsig'},'traingdm');
y1 = sim(net,p);
net.trainParam.epochs = 200;
net.trainParam.show = 1;
net.trainParam.goal = 0.1;
net.trainParam.lr = 0.8;
net.trainParam.mc = 0.2;
net.divideFcn = 'dividerand';
net.divideParam.trainRatio = 0.7;
net.divideParam.testRatio = 0.2;
net.divideParam.valRatio = 0.1;
%[pn,ps] = mapminmax(p);
%[tn,ts] = mapminmax(t);
net = init(net);
[net,tr] = train(net,p,t);
y2 = sim(net,pn);
%%%%%%%%
%End of code%
%%%%%%%%
现在是我的问题:我希望我的输出如前所述,例如y2向量的每一列都应该代表一个字母.我的代码虽然没有做到这一点.相反,它产生的结果在0到1之间变化很大,值从0.1到0.9.
Now to my problem: I want my outputs to be as described, namely each column of the y2 vector for example should be a representation of a letter. My code doesn't do that though. Instead it produced results that vary greatly between 0 and 1, values from 0.1 to 0.9.
我的问题是:我需要做一些转换吗,我不是吗?意思是,我是否必须将输入和/或输出数据转换为可以实际查看我的NN是否正确学习的形式?
My question is: is there some conversion i need to be doing that i am not? Meaning, do i have to convert my input and/or output data to a form by which i can actually see if my NN is learning correctly?
任何输入将不胜感激.
推荐答案
这很正常.您的输出层使用对数S型传递函数,它将始终为您提供0到1之间的中间输出.
This is normal. Your output layer is using a log-sigmoid transfer function, and that will always give you some intermediate output between 0 and 1.
通常要做的是寻找具有最大值(换句话说,最可能的字符)的输出.
What you would usually do would be to look for the output with the largest value -- in other words, the most likely character.
这意味着,对于y2
中的每一列,您正在寻找包含该行中最大值的行的索引.您可以如下计算:
This would mean that, for every column in y2
, you're looking for the index of the row that contains the largest value in that row. You can compute this as follows:
[dummy, I]=max(y2);
I
是一个向量,其中包含每一行中最大值的索引.
I
is then a vector containing the indexes of the largest value in each row.
这篇关于Matlab-神经网络训练的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!