我编写了以下反向传播算法来对两个输入恒等函数进行建模
clc
% clear
nh = 3; % neurons in hidden layer
ni = 2; % neurons in input layer
eta = .001; % the learning rate
traningSize =100;
for x=1:traningSize
input = rand(traningSize,ni);
test = input;
end
nk = size(test,2); % neurons in output layer
b1 = rand(1,nh);%+ .5;
b2 = rand(1,nk);%- .5;
w1 = rand(nh,ni) + .5;
w2 = rand(nk,nh) - .5;
figure
hold on;
for iter = 1 :5000
errSq = 0;
for x = 1: traningSize
a0 = input(x,:);
ex = test(x,:);
[a1, a2]= feedForward(a0,w1,w2,b1,b2);
del2 = (a2-ex) .* (1-a2) .* (a2);
del1 = (del2 * w2) .* (1-a1) .* (a1);
delB2 = del2;
delB1 = del1;
delW2 = zeros(nk,nh);
for i = 1:nh
for j = 1:nk
delW2 = a1(i) * del2(j);
end
end
for i = 1:ni
for j = 1:nh
delW1 = a0(i) * del1(j);
end
end
b2 = b2 - eta * delB2;
b1 = b1 - eta * delB1;
w2 = w2 - eta * delW2;
w1 = w1 - eta * delW1;
errSq = errSq + sum(a2-ex) .* sum(a2-ex);
end
cost = errSq /(2 * traningSize);
plot(iter,cost,'o');
if cost < 0.005
cost
break
end
end
cost
前馈功能:
function [a1, a2] = feedForward(a0,w1,w2,b1,b2)
z1 = a0 * w1' + b1;
a1 = sig(z1);
z2 = a1 * w2' + b2;
a2 = sig(z2);
end
成本函数图
现在我在搞砸什么?
是一些程序错误没有引起我的注意吗?还是我错误地执行了算法?
当我测试得到的权重时,计算出的成本是经过训练的,但结果完全错误
蓝色 > 预期输出;红色 > 神经网络的输出
还有为什么成本价值有时会先升后降(如图 1 所示)