Skip to content

Commit

Permalink
perceptrón simple
Browse files Browse the repository at this point in the history
  • Loading branch information
lmarzora committed May 2, 2017
0 parents commit 7df5348
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions binary_entry_generator.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function entry = binary_entry_generator(n)
for i = [0:n-1]
x = 2^i;
% 2x*r = 2^n
% r = 2^n/2x
% x = 2^i
% r = 2^(n-1-i)
entry(i+1,:) = repmat([ones(1,x),-1*ones(1,x)],1,2^(n-i-1));
end
end
37 changes: 37 additions & 0 deletions simple_perceptron.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
1;
% entry n * 1
% weights 1 * n
function output = get_output(entry,weights,activation_func)
output = activation_func(weights * entry);
end

% entry n * 1
% weights 1 * n
function weights = update_weights(learning_factor, output, expected_output, weights, entry)
weights = weights + learning_factor*(expected_output - output) * entry';
end

function [weights, output] = simple_perceptron_learn(entries, expected_output, activation_func=@sign, learning_factor=.5,
max_iterations=1000, tolerance=1e-5)
n = length(entries(:,1));
% agrego umbral
entries = [-1*ones(1,2^n);entries];
weights = rand(1,n+1) .- 0.5;

for iteration = 1:max_iterations
for index = randperm(2^n);
entry = entries(:,index);
output(index) = get_output(entry,weights,activation_func);
weights = update_weights(learning_factor, output(index), expected_output(index), weights, entry);
end
if (sum(abs(expected_output-output)) <= tolerance)
'solution found'
return;
end
end
'max iterations reached'
end

function x = id(x)
end

0 comments on commit 7df5348

Please sign in to comment.