0

我是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);
4

1 回答 1

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);

这是用于反向传播训练的示例 Matlab 神经网络工具箱函数的链接。您可能想查看那里的文档,看看是否有任何类似于BpTrainingProcess().

于 2012-04-16T03:56:46.130 回答