Skip to content

Instantly share code, notes, and snippets.

@vrld
Created October 31, 2014 12:00
Show Gist options
  • Save vrld/d6b99b68ecd9a6478534 to your computer and use it in GitHub Desktop.
Save vrld/d6b99b68ecd9a6478534 to your computer and use it in GitHub Desktop.
Online Learning Algorithms
type Perceptron
w::Vector{Float64}
Perceptron(d::Int) = new(zeros(d))
Perceptron(w::Vector{Float64}) = new(w)
end
type Winnow
w::Vector{Float64}
η::Float64
Perceptron(d::Int, η::Float64) = new(ones(d)./d, η)
Perceptron(w::Vector{Float64}, η::Float64) = new(w, η)
end
predict(p::Perceptron, x::Vector{Float64}) = sign(p.w ⋅ x)
function learn!(p::Perceptron, x::Vector{Float64}, y::Int)
yy = p.w ⋅ x
if yy * y <= 0
p.w += y*x
end
sign(yy)
end
predict(p::Winnow, x::Vector{Float64}) = sign(2(p.w ⋅ x) - 1)
function learn!(p::Winnow, x::Vector{Float64}, y::Int)
yy = 2(p.w ⋅ x) - 1
if yy * y <= 0
p.w .*= exp(-p.η * 2y.*x)
end
sign(yy)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment