神经网络权重

时间:2014-01-18 16:58:11

标签: matlab neural-network

这将是一个很长的问题:

我在MATLAB中编写了一个代码,用于通过一个隐藏层更新MLP的权重。这是代码:

function [ weights_1,weights_2 ] = changeWeights( X,y,weights_1,weights_2,alpha )
%CHANGEWEIGHTS updates the weight matrices 
%   This function changes the weight of the weight matrix
%   for a given value of alpha using the back propogation algortihm

m = size(X,1) ;     % number of samples in the training set

for i = 1:m
    % Performing the feed-forward step 
    X_i  = [1 X(i,1:end)]    ;  % 1-by-5 input 
    z2_i = X_i*weights_1'    ;  % 1-by-4   
    a2_i = sigmoid(z2_i)     ;  % 1-by-4   
    a2_i = [1 a2_i]          ;  % 1-by-5   
    z3_i = a2_i*weights_2'   ;  % 1-by-3   
    h_i  = sigmoid(z3_i)     ;  % 1-by-3   

% Calculating the delta_output_layer 
    delta_output_layer = ( y(i)' - h_i' )...
        .*sigmoidGradient(z3_i')  ; % 3-by-1 matrix

% Calculating the delta_hidden_layer 
    delta_hidden_layer =  (weights_2'*delta_output_layer)...
        .*sigmoidGradient([1;z2_i']) ; % 5-by-1 matrix 
    delta_hidden_layer = delta_hidden_layer(2:end) ; 

% Updating the weight matrices
    weights_2 = weights_2 + alpha*delta_output_layer*a2_i ; 
    weights_1 = weights_1 + alpha*delta_hidden_layer*X_i  ;
end

end

现在我想在MATLAB中给出的fisheriris数据集上测试它,load fisheriris命令可以接收它。我将meas重命名为X并将species更改为150-by-3矩阵,其中每行描述了物种的名称(例如,第一行为[1 0 0])< / p>

我使用以下函数计算输出图层的错误:

function [ g ] = costFunction( X,y,weights_1,weights_2 )
  %COST calculates the error 
  %   This function calculates the error in the 
  %   output of the neural network 

  % Performing the feed-forward propogation
  m = size(X,1) ; 
  X_temp  = [ones([m 1]) X]   ;  % 150-by-5 matrix 
  z2 = X_temp*weights_1'       ; % 150-by-5-by-5-by-4 
  a2 = sigmoid(z2)       ; 
  a2 = [ones([m 1]) a2]            ; % 150-by-5
  z3 = a2*weights_2'     ; % 150-by-3
  h  = sigmoid(z3)       ; % 150-by-3

  g = 0.5*sum(sum((y-h).^2)) ; 
  g = g/m ; 
end

现在,教授给出了一个有3次迭代的玩具网络的例子,我在该网络上对此进行了测试并给出了正确的值,但是当我在fisheriris数据上测试时,成本不断增加。我无法理解它出错的地方。

0 个答案:

没有答案