1

Phil Brierley (www.philbrierley.com) による NN バックプロパゲーション コードの修正版を使用してみました。XOR問題を解こうとすると、完全に機能します。しかし、出力 = x1^2 + x2^2 (出力 = 入力の二乗和) の形式の問題を解こうとすると、結果が正確ではありません。入力と出力を -1 から 1 の間でスケーリングしました。同じプログラムを実行するたびに異なる結果が得られます (wts のランダムな初期化によるものだと理解しています) が、結果は大きく異なります。学習率を変更してみましたが、それでも結果は収束します。

以下のコードを与えました

%---------------------------------------------------------
% MATLAB neural network backprop code
% by Phil Brierley
%--------------------------------------------------------
clear; clc; close all;


%user specified values
hidden_neurons = 4;
epochs = 20000;

input = [];
for i =-10:2.5:10
for j = -10:2.5:10
input = [input;i j];
end
end

output  = (input(:,1).^2 + input(:,2).^2);
output1 = output;

% Maximum input and output limit and scaling factors
m1  = -10;   m2  = 10;
m3  = 0;     m4  = 250;
c = -1;      d   = 1;

%Scale input and output
for i =1:size(input,2)
I   = input(:,i);
scaledI = ((d-c)*(I-m1) ./ (m2-m1)) + c;
input(:,i) = scaledI;
end
for i =1:size(output,2)
I   = output(:,i);
scaledI = ((d-c)*(I-m3) ./ (m4-m3)) + c;
output(:,i) = scaledI;
end

train_inp = input;
train_out = output;

%read how many patterns and add bias
patterns  = size(train_inp,1);
train_inp = [train_inp ones(patterns,1)];

%read how many inputs and initialize learning rate
inputs = size(train_inp,2);
hlr    = 0.1;

%set initial random weights
weight_input_hidden = (randn(inputs,hidden_neurons) - 0.5)/10;
weight_hidden_output = (randn(1,hidden_neurons) - 0.5)/10;

%Training
err = zeros(1,epochs);

for iter = 1:epochs

alr = hlr;
blr = alr / 10;

%loop through the patterns, selecting randomly
for j = 1:patterns

%select a random pattern
patnum = round((rand * patterns) + 0.5);
if patnum > patterns
patnum = patterns;
elseif patnum < 1
patnum = 1;    
end

%set the current pattern
this_pat = train_inp(patnum,:);
act = train_out(patnum,1);

%calculate the current error for this pattern
hval = (tanh(this_pat*weight_input_hidden))';
pred = hval'*weight_hidden_output';
error = pred - act;

% adjust weight hidden - output
delta_HO = error.*blr .*hval;
weight_hidden_output = weight_hidden_output - delta_HO';

% adjust the weights input - hidden
delta_IH= alr.*error.*weight_hidden_output'.*(1-(hval.^2))*this_pat;
weight_input_hidden = weight_input_hidden - delta_IH';

end
% -- another epoch finished

%compute overall network error at end of each epoch
pred      = weight_hidden_output*tanh(train_inp*weight_input_hidden)';
error     = pred' - train_out;
err(iter) =  ((sum(error.^2))^0.5);

%stop if error is small
if err(iter) < 0.001
fprintf('converged at epoch: %d\n',iter);
break 
end

end

%Output after training
pred  = weight_hidden_output*tanh(train_inp*weight_input_hidden)';
Y     = m3 + (m4-m3)*(pred-c)./(d-c);

% Testing for a new set of input
input_test  = [6 -3.1; 0.5 1; -2 3; 3 -2; -4 5; 0.5 4; 6 1.5];
output_test = (input_test(:,1).^2 + input_test(:,2).^2);
input1      = input_test;

%Scale input
for i =1:size(input1,2)
I   = input1(:,i);
scaledI = ((d-c)*(I-m1) ./ (m2-m1)) + c;
input1(:,i) = scaledI;
end

%Predict output
train_inp1 = input1;
patterns   = size(train_inp1,1);
bias       = ones(patterns,1);
train_inp1 = [train_inp1 bias];
pred1      = weight_hidden_output*tanh(train_inp1*weight_input_hidden)';

%Rescale
Y1  = m3 + (m4-m3)*(pred1-c)./(d-c);

analy_numer = [output_test Y1']
plot(err)

これは私が問題のために得るサンプル出力です

20000 エポック後の状態

analy_numer =

45.6100   46.3174
1.2500   -2.9457
13.0000   11.9958
13.0000    9.7097
41.0000   44.9447
16.2500   17.1100
38.2500   43.9815

もう一度実行すると、異なる結果が得られます。入力の値が小さい場合に見られるように、完全に間違った ans を取得します (負の ans は不可能です)。他の値の精度はまだ不十分です。

誰かが私が間違っていることと修正する方法を教えてもらえますか.

ありがとうラマン

4

0 に答える 0