Skip to content

Instantly share code, notes, and snippets.

@Moddus
Created May 18, 2015 09:46
Show Gist options
  • Save Moddus/68f972b4cbe2a189838c to your computer and use it in GitHub Desktop.
Save Moddus/68f972b4cbe2a189838c to your computer and use it in GitHub Desktop.
using adaptive_systeme
data = [[1 0 0],[1 0 1], [1 1 0], [1 1 1]]
result_OR = [0; 1; 1; 1]
result_AND = [0; 0; 0; 1]
result_XOR = [0; 1; 1; 0]
p = Perzeptron(.5, .1, result_OR, 1000, lineare)
training(p, data)
println(calculate(p, data[1, 1:end]'))
println(calculate(p, data[2, 1:end]'))
println(calculate(p, data[3, 1:end]'))
println(calculate(p, data[4, 1:end]'))
module adaptive_systeme
using Match
export Perzeptron, training, calculate, lineare
type Perzeptron
theta
my
d
iter
sign
w
Perzeptron(theta, my, d, iter, sign) = new(theta, my, d, iter, sign, [[-theta],[1],[1]])
end
calculate(p::Perzeptron, x) = p.sign((p.w'*x)[1])
delta(p::Perzeptron, x, d) = p.my*(d-calculate(p,x))
function training(p::Perzeptron, data)
count = 0
while count != p.iter
count += 1
for i = 1:length(data[1:end,1])
xi = data[i, 1:end]'
p.w += delta(p, xi, p.d[i]) * xi
end
end
p.w
end
function lineare(x::Number)
c = 2
@match x begin
x > c => 1
x < -c => 0
_ => 1/(2*c)*(x+c)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment