0
我試圖實現一個具有sigmoid函數的神經網絡 但是下面的代碼不工作 這是神經網絡的訓練部分。 它沒有正確更新權重 這段代碼有什麼問題?sigmoid神經網絡
clc; clear all; close all;
% load train_data1
train_data1=[-0.498800000000000,-0.257500000000000;-0.492800000000000,-0.274300000000000;-0.470300000000000,-0.282600000000000;-0.427400000000000,-0.474000000000000;-0.420400000000000,-0.518000000000000;-0.326300000000000,-1.13230000000000;-0.317300000000000,-0.875300000000000;-0.295000000000000,-1.02770000000000;-0.267600000000000,-0.882800000000000;-0.260500000000000,-0.976500000000000;-0.216100000000000,-0.970400000000000;-0.207000000000000,-0.813800000000000;-0.164000000000000,-0.696600000000000;-0.159900000000000,-0.793300000000000;-0.122000000000000,-0.764400000000000;-0.0729000000000000,-0.435300000000000;-0.00640000000000000,-0.0546000000000000;0.132200000000000,0.710300000000000;0.137100000000000,0.587000000000000;0.160300000000000,0.819200000000000;0.230600000000000,0.989200000000000;0.286800000000000,0.737700000000000;0.334000000000000,0.943500000000000;0.375200000000000,0.688200000000000;0.429700000000000,0.567800000000000];
train_data1 = sortrows(train_data1);
% normalize data to [0,1]
data1=[train_data1];
max1=max(max(data1));
min1=min(min(data1));
train_data2 = (train_data1 - min1)/(max1 - min1);
x = train_data2(:,1); % train input data
r = train_data2(:,2); % train output data
hidden_neurons = 2;
maxepochcount = 1000;
datacount1 = size(x,1);
% add a bias as an input
bias = ones(datacount1,1);
% x = [x bias];
% read how many inputs
inputcount = size(x,2);
% ---------- data loaded -----------
% ---------- set weights -----------
% set initial random weights
WI = (randn(inputcount,hidden_neurons) - 0.5)/10;
WO = (randn(1,hidden_neurons) - 0.5)/10;
%-----------------------------------
%--- Learning Starts Here! ---------
%-----------------------------------
eta1 = 0.5;
eta2 = eta1/5;
% do a number of epochs
for iter = 1:maxepochcount
% loop through the data
for j = 1:datacount1
% read the current sample
I = x(j,:);
D = r(j,1);
% calculate the error for this sample
H = (sigmoid(I * WI))';
O = H' * WO';
error = D-O;
% adjust weight between hidden & output
delta_i = O.*(1-O).*(D-O); % D actual, O calculated output
% Calculate error for each node in layer_(n-1)
delta_j = H.*(1-H).*(WO.'*delta_i); % H.' is the output of hidden layer
% Adjust weights in matrices sequentially
WO = WO + eta2.*delta_i*(H.') % H.' is the output of hidden layer
WI = WI + eta1.*(delta_j*(I))' % I.' is the inputs
% % adjust weight between hidden & output
% delta_HO = error.*eta2 .* hidden_val;
% WO = WO - delta_HO';
% % adjust the weights between input & hidden
% delta_IH = eta1 .* error .* WO' .* (1 - (H .^ 2)) * I;
% WI = WI - delta_IH';
end
O = sigmoid(WO*sigmoid(x * WI)');
% error(iter) = (sum(error .^ 2))^0.5;
if rem(iter,100)==0 % Every 100 epochs, show how training is doing
plot(x,O, 'color','red','linewidth',2); hold on;
drawnow;
iter
end
% return
end
您是否使用過調試器來遍歷代碼,並確保每個步驟的值都符合您的期望? – beaker
在循環結束處有一個繪圖函數,用於查看每個時期之後的網絡結果。我使用tanh版本的代碼,它工作正常,但sigmoid功能doesnot.I懷疑重量更新部分的代碼 –
是有必要將輸入和輸出值標準化爲[0,1]爲S形網絡激活功能。其實我的數據值介於-1,+ 1之間 –