Skip to content

Commit

Permalink
Created main and configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
lsoncini committed May 16, 2017
1 parent 9fcdd07 commit 8b9187a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 66 deletions.
24 changes: 0 additions & 24 deletions configuration

This file was deleted.

13 changes: 13 additions & 0 deletions configuration.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
15;
data_file = 'terrain/terrain03.data';
starting_line = 2;
beta = 15;
activation = 'exp';
net = [2, 45, 50, 1];
eta = 0.1;
max_iterations = 1e2;
cut_error = 1e-4;
alpha = 0;
adaptative_eta = true;
print_error = true;
print_estimation = false;
15 changes: 0 additions & 15 deletions mac.m

This file was deleted.

37 changes: 37 additions & 0 deletions main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
1;
source multilayer_perceptron.m
source configuration.m

terrain = dlmread(data_file);
terrain = terrain(starting_line:end,:);

if activation == 'exp'
[f,fder] = activation_exp(beta);
else
[f,fder] = activation_tanh(beta);
end

[weights, output, mse] = multilayer_perceptron_learn(terrain(:,1:2)', terrain(:,3)', net, f, fder, eta, max_iterations, cut_error, alpha, adaptative_eta);

if print_error
figure(1);
semilogy(mse, 'LineWidth', 2);
title("ECM");
xlabel("Épocas");
ylabel("ECM en escala logarítmica");
end

if print_estimation
figure(2);
x = [-3:0.025:3];
y = x;
for i = 1:length(x)
for j = 1:length(y)
z(i,j) = get_output([x(i);y(j)],weights,net,f);
end
end
surf(x, y, z);
mymap = [0, 0, 0.6; 0.2, 0.8, 0; 0.4, 0.8, 0; 0.5, 0.8, 0; 0.7, 0.7, 0; 0.8, 0.8, 0.8; 1, 1, 1];
colormap(get_map(mymap));
title("Aproximación de la función");
end
31 changes: 15 additions & 16 deletions multilayer_perceptron.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
learning_factor=.5, max_iterations=1000, tolerance=1e-5, alpha=0, adaptative_eta=false, dbug=false)
%number of entries
n = length(entries(1,:));

eta = learning_factor;
%number of layers
m = 0;
a = alpha;
%setup
for i = 2:length(neurons_per_layer)
m = m + 1;
Expand All @@ -49,7 +48,7 @@
if dbug
layer_entry
fflush(1);
end
end
h{M} = weights{M} * layer_entry{M}';

%no linear
Expand All @@ -74,8 +73,8 @@
prev_delta_w = delta_w;
end
for i = 1:M
if iteration > 1
delta_w{i} = learning_factor * d{i}' * layer_entry{i} + a * delta_w{i};
if iteration > 1 && a ~= 0
delta_w{i} = -learning_factor * d{i}' * layer_entry{i} + a * delta_w{i};
else
delta_w{i} = learning_factor * d{i}' * layer_entry{i};
end
Expand All @@ -85,29 +84,29 @@
%get iteration error
error_per_iteration(iteration) = sum((expected_output - output).^2)/n;
if adaptative_eta
if iteration > 10
if error_per_iteration(iteration) > error_per_iteration(iteration - 10)
if iteration > 1
if error_per_iteration(iteration) > error_per_iteration(iteration - 1)
weights = old_weigths;
error_per_iteration(iteration) = error_per_iteration(iteration - 1);
delta_w = prev_delta_w;
learning_factor = 0.9 * learning_factor;
a = 0;
a = 0;
else
a = alpha;
learning_factor = 1.1 * learning_factor;
learning_factor = 0.1 * eta + learning_factor;
end
else
a = alpha;
end
end
[error_per_iteration(iteration),iteration,toc]
[error_per_iteration(iteration),iteration,toc, learning_factor]
fflush(1);
if error_per_iteration(iteration) <= tolerance
return
end
%if error_per_iteration(iteration) <= 5e-4
% learning_factor = .02;
%end
%if error_per_iteration(iteration) <= 4e-4
% learning_factor = .01;
%end
if(learning_factor < 0.001 * eta)
learning_factor = 0.1 * eta;
end
end

end
Expand Down
4 changes: 4 additions & 0 deletions plotter.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
source multilayer_perceptron.m
x = [-5:0.01:5];
y = x;
for i = 1:length(x)
for j = 1:length(y)
z(i,j) = get_output([x(i);y(j)],weights,net,f);
Expand All @@ -7,5 +9,7 @@

plot3(terrain(:,2),terrain(:,1),terrain(:,3),'.','markersize',12)
hold on;
mymap = [0,0,0.6;0.2,0.8,0;0.4,0.8,0;0.5,0.8,0;0.7,0.7,0;0.8,0.8,0.8;1,1,1];
colormap(get_map(mymap));
surf(x,y,z);
hold off;
5 changes: 3 additions & 2 deletions test.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
source multilayer_perceptron.m
terrain = dlmread('terrain/terrain03.data');
terrain = terrain(2:end,:);
[f,fder] = activation_exp(1);
[f,fder] = activation_exp(15);
net = [2,45,50,1];
[weights,output,mse] = multilayer_perceptron_learn(terrain(:,1:2)',terrain(:,3)',net,f,fder,.01,1e3,1e-4,0.9,true);
[weights,output,mse] = multilayer_perceptron_learn(terrain(:,1:2)',terrain(:,3)',net,f,fder,.1,1e4,1e-4,0,true);
x = [-3:0.01:3];
y = x;
%dlmwrite('weights.csv',cell2mat(weights));
Expand All @@ -15,6 +15,7 @@
end
end


plot3(terrain(:,2),terrain(:,1),terrain(:,3),'.','markersize',12)
hold on;
mymap = [0,0,0.6;0.2,0.8,0;0.4,0.8,0;0.5,0.8,0;0.7,0.7,0;0.8,0.8,0.8;1,1,1];
Expand Down
9 changes: 0 additions & 9 deletions test2.m

This file was deleted.

0 comments on commit 8b9187a

Please sign in to comment.