Skip to content

Commit

Permalink
linear output
Browse files Browse the repository at this point in the history
  • Loading branch information
lmarzora committed May 9, 2017
1 parent e1038ed commit 6a6fe40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
22 changes: 16 additions & 6 deletions multilayer_perceptron.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
%setup
for i = 2:length(neurons_per_layer)
m = m + 1;
weights{m} = (rand(neurons_per_layer(i), neurons_per_layer(i-1)+1) .- 0.5)./neurons_per_layer(i-1);
%weights{m} = (2*(rand(neurons_per_layer(i), neurons_per_layer(i-1)+1) .- 0.5))./100;
weights{m} = (rand(neurons_per_layer(i), neurons_per_layer(i-1)+1) .- 0.5)./(neurons_per_layer(i-1));
layer_entry{m} = [-1, zeros(1, neurons_per_layer(i-1))];
h{m} = [-1 ,zeros(1, neurons_per_layer(i-1))];
end

%last layer
M = m;

for iteration = 1:max_iterations
tic;
%select random entry
for index = randperm(n);
%get layers output
Expand All @@ -51,9 +51,19 @@
fflush(1);
end
h{M} = weights{M} * layer_entry{M}';
output(index) = activation_func(h{M});

%no linear
%output(index) = activation_func(h{M});
%get errors
%d{M} = activation_der(h{M})*(expected_output(index) - output(index));

%linear
output(index) = h{M};
%h{M};
%get errors
d{M} = activation_der(h{M})*(expected_output(index) - output(index));
d{M} = (expected_output(index) - output(index));
%d{M};

for i = M-1:-1:1
d{i} = (activation_der(h{i})' .* (d{i+1} * weights{i+1}(:,2:end)));
end
Expand All @@ -66,7 +76,7 @@
end
%get iteration error
error_per_iteration(iteration) = sum((expected_output - output).^2)/n;
error_per_iteration(iteration)
[error_per_iteration(iteration),iteration,toc]
fflush(1);
if error_per_iteration(iteration) <= tolerance
return
Expand Down
12 changes: 6 additions & 6 deletions test.m
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
1;
source multilayer_perceptron.m
terrain = dlmread('terrain/terrain03.data');
[f,fder] = activation_exp(1);
net = [2,45,50,1];
[weights,output,mse] = multilayer_perceptron_learn(terrain(:,1:2)',terrain(:,3)',net ,f,fder,1,1e4,5e-4);
[f,fder] = activation_tanh(1);
net = [2,200,50,10,1];
[weights,output,mse] = multilayer_perceptron_learn(terrain(:,1:2)',terrain(:,3)',net ,f,fder,.05,1e4,2e-4);
x = [-3:0.01:3];
y = x;
%dlmwrite('weights.csv',cell2mat(weights));

for i = 1:length(x)
for j = 1:length(y)
out(i,j) = get_output([x(i);y(j)],weights,net,f);
z(i,j) = get_output([x(i);y(j)],weights,net,f);
end
end

plot3(terrain(:,1),terrain(:,2),terrain(:,3),'.')
plot3(terrain(:,1),terrain(:,2),terrain(:,3),'.','markersize',12)
hold on;
surf(x,y,out);
surf(x,y,z);
hold off;

0 comments on commit 6a6fe40

Please sign in to comment.