Help with simple neural network

12 views (last 30 days)
Kristoffer Lindvall
Kristoffer Lindvall on 7 Dec 2018
Hello! I have been trying to get into machine learning and I thought I would try to code my own code with a simple example. If I can understand this simple case then the bigger problems will Ni
clear all
% Simple neural netword that fits
% the function F = x(1)+cos(x(2));
% randomize weights and bias
W2 = rand(3,2); b2 = rand(3,1);
W3 = rand(3,3); b3 = rand(3,1);
W4 = rand(1,3); b4 = rand();
% randomize data
x = rand(2,10000001);
LearnEta = 0.1;
epoch = 1;
batch_nr = 1000;
cost = zeros(1,epoch*batch_nr);
for e=1:epoch
for i=1:batch_nr
k = (e-1)*batch_nr+i;
% forward propagation
a1 = x(:,k);
z2 = W2*a1+b2;
a2 = sigmoid(z2);
z3 = W3*a2+b3;
a3 = sigmoid(z3);
z4 = W4*a3+b4;
a4 = sigmoid(z4);
% calculate derivates
d4 = (a4-func(a1)).*(a4.*(1-a4));
d3 = (W4'*d4).*(a3.*(1-a3));
d2 = (W3'*d3).*(a2.*(1-a2));
% Backward propagation
W4 = W4 - LearnEta*d4.*a3';
W3 = W3 - LearnEta*d3.*a2';
W2 = W2 - LearnEta*d2.*a1';
b4 = b4 - LearnEta*d4;
b3 = b3 - LearnEta*d3;
b2 = b2 - LearnEta*d2;
cost(k) = 0.5*(func(a1)-a4)^2;
end
end
disp(cost(epoch*batch_nr))
x = x(:,batch_nr+1);
a1 = x;
z2 = W2*a1+b2;
a2 = sigmoid(z2);
z3 = W3*a2+b3;
a3 = sigmoid(z3);
z4 = W4*a3+b4;
a4 = sigmoid(z4);
disp(a4-func(x));
scatter(linspace(1,length(cost),length(cost)),cost)
function F = func(x)
F = x(1)+cos(x(2));
end
function s = sigmoid(x)
s = 1.0 ./ (1.0 + exp(-x));
end

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!