Matlab 神经网络权重
这将是一个很长的问题: 我已经在MATLAB中编写了一个代码,用于用一个隐藏层更新MLP的权重。代码如下:Matlab 神经网络权重,matlab,neural-network,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 gi
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中提供的fisherris
数据集上测试它,该数据集可以通过load fisherris
命令访问。我将meas
重命名为X
,并将species
更改为150×3
矩阵,其中每行描述物种名称(例如,第一行是[1 0]
)
我使用以下函数计算输出层的误差:
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次迭代,我在这个网络上测试了这个,它给出了正确的值,但是当我在Fisherris数据上测试它时,成本不断增加。我不知道哪里出了问题。输出数据的范围是什么?输出数据是一个[x y z]形式的向量,其中x,y,z属于{0,1}