2012-04-16 41 views
0

对matlab来说是新的。
有人可以解释我下面的代码。这段代码是用于训练神经网络关于matlab代码的说明

N = xlsread('data.xls','Sheet1'); 
N = N(1:150,:); 
UN = xlsread('data.xls','Sheet2'); 
UN = UN(1:150,:); 
traindata = [N ; UN]; 
save('traindata.mat','traindata'); 
label = []; 
for i = 1 : size(N,1)*2 
if(i <= size(N,1)) 
%  label = [label ;sum(traindata(i,:))/size(traindata(i,:),2)]; 
    label = [label ;sum(traindata(i,:))/10]; 
else 
%  label = [label ;sum(traindata(i,:))/size(traindata(i,:),2)]; 
    label = [label ;sum(traindata(i,:))/10]; 
end 
end 
weightMat = BpTrainingProcess(4,0.0001,0.1,0.9,15,[size(traindata,1) 1],traindata,label); 

回答

0

我找不到内置对应于BpTrainingProcess()一个神经网络的工具箱,所以这一定是你必须在本地访问的文件(或者你需要从获得给你这个代码的人)。它可能将几个函数调用串联到神经网络工具箱函数中,或者可能是反向传播训练方法的原始实现。

否则,代码有一些缺点。首先,内部的if-else语句实际上并没有做任何事情。即使是被注释掉的行也会留下完全无用的if-else设置。它看起来像if-else旨在让您对从Excel文件的Sheet1加载的数据与从Sheet2加载的数据执行不同的标签标准化。也许这对你很重要,但目前在程序中没有发生。

最后,代码使用label的空数组,并将行追加到空数组。这是不必要的,因为你已经知道将会有多少行(总共可以达到size(N,1)*2 = 150*2 = 300行,你可以简单地设置label=zeros(300,1),然后在for循环的每次迭代中使用通常的索引:label(i) = ...。这节省了时间和空间,但可以说将没有多大关系了300行数据集(假设每行的长度不是太大)。

我把文件旁边,下面的代码。

% The functionn 'xlsread()' reads data from an Excel file. 
% Here it is storing the values from Sheet 1 of the file 'data.xls' 
% into the variable N, and then using the syntax N = N(1:150,:) to 
% change N from being all of the data into being only the first 
% 150 rows of the data 
N = xlsread('data.xls','Sheet1'); 
N = N(1:150,:); 

% Now do the same thing for Sheet 2 from the Excel file. 
UN = xlsread('data.xls','Sheet2'); 
UN = UN(1:150,:); 

% This concatenates the two different data arrays together, making 
% one large array where N is the top half and UN is the bottom half. 
% This is basically just stacking N on top of UN into one array. 
traindata = [N ; UN]; 

% This saves a copy of the newly stacked array into the Matlab data file 
% 'traindata.mat'. From now on, you should be able to load the data from 
% this file, without needing to read it from the Excel sheet above. 
save('traindata.mat','traindata'); 

% This makes an empty array which will have new things appended to it below. 
label = []; 

% Because UN and N have the same number of rows, then the training data 
% has twice as many rows. So this sets up a for loop that will traverse 
% all of these rows of the training data. The 'size()' function can be 
% used to get the different dimensions of an array. 
for i = 1 : size(N,1)*2 

    % Here, an if statement is used to check if the current row number, i, 
    % is less than or equal to than the number of rows in N. This implies 
    % that this part of the if-statement is only for handling the top half 
    % of 'trainingdata', that is, the stuff coming from the variable N. 

    if(i <= size(N,1)) 
     % The line below was already commented out. Maybe it had an old use 
     % but is no longer needed? 
     % label = [label ;sum(traindata(i,:))/size(traindata(i,:),2)]; 

     % This syntax will append new rows to the variable 'label', which 
     % started out as an empty array. This is usually bad practice, memory-wise 
     % and also for readability. 

     % Here, the sum of the training data is being computed, and divided by 10 
     % in every case, and then appended as a new row in 'label'. Hopefully, 
     % if you are familiar with the data, you will know why the data in 'N' 
     % always needs to be divided by 10. 
     label = [label ;sum(traindata(i,:))/10]; 

    % Otherwise, if i > # of rows then handle the data differently. 
    % Really this means the code below treats only data from the variable UN. 
    else 
     % The line below was already commented out. Maybe it had an old use 
     % but is no longer needed? 
     % label = [label ;sum(traindata(i,:))/size(traindata(i,:),2)]; 

     % Just like above, the data is being divided by 10. Given that there 
     % is nothing different about the code here, and how it modifies 'label' 
     % there is no need for the if-else statements, and they only waste time. 
     label = [label ;sum(traindata(i,:))/10]; 

    % This is needed to show the end of the if-else block. 
    end 

% This is needed to show the end of the for-loop. 
end 


% This appears to be a Back-Propagation Neural Network training function. 
% This doesn't match any built-in Matlab function I can find, but you might 
% check in the Neural Network toolbox to see if the local function 
% BpTrainingProcess is a wrapper for a collection of built-in training functions. 

weightMat = BpTrainingProcess(4, 0.0001, 0.1, 0.9, 15, 
           [size(traindata,1) 1], traindata,label); 

这里是一个link的例子Matlab神经网络工具箱功能的反向传播训练。你可能想看看周围的文档,看看它是否有任何类似BpTrainingProcess()的内部。

+0

非常感谢你 – darsha 2012-04-16 06:37:11