Skip to content

Instantly share code, notes, and snippets.

@andrewdanks
Last active August 29, 2015 14:00
Show Gist options
  • Save andrewdanks/11132096 to your computer and use it in GitHub Desktop.
Save andrewdanks/11132096 to your computer and use it in GitHub Desktop.
type Perceptron
learning_rate
threshold
weights::Array{Float64, 1}
max_iters::Integer
function Perceptron(num_units::Integer=1, threshold::Float64=0.0, learning_rate::Float64=0.1, max_iters::Integer=1000)
weights = init_weights(num_units)
new(learning_rate, threshold, weights, max_iters)
end
end
function train!(perceptron::Perceptron, X::Array{Float64, 2}, y::Array{Int64, 1})
for iter = 1:perceptron.max_iters
optimal_region = true
for i = 1:size(X, 1)
x = X[i,:][:]
target = y[i]
output = calculate_output(perceptron, x)
err = calculate_error(target, output)
if err != 0
optimal_region = false
update_weights!(perceptron, err, x)
end
end
if optimal_region
println("Converged")
break
end
end
end
function test{Float64}(perceptron::Perceptron, X::Array{Float64, 2}, y::Array{Int64, 1})
errors = 0
num_cases = size(X, 1)
for i = 1:num_cases
x = X[i,:][:]
target = y[i]
output = calculate_output(perceptron, x)
if target != output
errors += 1
end
end
return errors / num_cases
end
function init_weights(num_weights::Integer)
zeros(num_weights)
end
function update_weights!(perceptron::Perceptron, err::Real, x::Array{Float64, 1})
perceptron.weights += perceptron.learning_rate * err .* x
end
function calculate_error(target::Real, output::Real)
target - output
end
function calculate_output{Float64}(perceptron::Perceptron, x::Array{Float64, 1})
float64(dot(perceptron.weights, x) > perceptron.threshold)
end
# Example
X = [1.0 0.0 0.0; 1.0 0.0 1.0; 1.0 1.0 0.0; 1.0 1.0 1.0]
y = [1, 1, 1, 0]
perceptron = Perceptron(3, 0.5, 0.1, 10)
train!(perceptron, X, y)
error_rate = test(perceptron, X, y)
println(error_rate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment