2016-08-23 15:32:19 +00:00
|
|
|
export Model, back!, update!, param
|
|
|
|
|
|
|
|
# Basic model API
|
|
|
|
|
|
|
|
abstract Model
|
|
|
|
|
|
|
|
back!(m::Model, ∇) = error("Backprop not implemented for $(typeof(m))")
|
2016-08-24 14:41:30 +00:00
|
|
|
update!(m, η) = m
|
2016-08-23 15:32:19 +00:00
|
|
|
|
2016-08-31 01:37:53 +00:00
|
|
|
graph(m) = nothing
|
|
|
|
|
2016-08-23 15:32:19 +00:00
|
|
|
# Model parameters
|
|
|
|
|
|
|
|
type Param{T}
|
|
|
|
x::T
|
|
|
|
Δx::T
|
|
|
|
end
|
|
|
|
|
|
|
|
param(x) = Param(x, zero(x))
|
|
|
|
|
|
|
|
state(p::Param) = p.x
|
|
|
|
|
|
|
|
function accumulate!(p::Param, Δ)
|
|
|
|
p.Δx .+= Δ
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
|
|
|
|
function update!(p::Param, η)
|
2016-08-24 14:41:17 +00:00
|
|
|
p.x .-= p.Δx .* η
|
2016-08-23 22:56:31 +00:00
|
|
|
p.Δx[:] = 0
|
2016-08-23 15:32:19 +00:00
|
|
|
return p
|
|
|
|
end
|
|
|
|
|
|
|
|
state(x) = x
|
|
|
|
accumulate!(x, Δ) = x
|
|
|
|
|
2016-10-04 21:23:10 +00:00
|
|
|
@forward Param.x Base.size
|
|
|
|
|
2016-11-14 15:42:29 +00:00
|
|
|
function Base.show(io::IO, p::Param)
|
|
|
|
print(io, "Param", size(p.x))
|
|
|
|
end
|
|
|
|
|
2016-08-23 15:32:19 +00:00
|
|
|
# Anonymous models
|
|
|
|
|
|
|
|
export Capacitor
|
|
|
|
|
|
|
|
type Capacitor <: Model
|
|
|
|
graph::IVertex{Any}
|
|
|
|
end
|
|
|
|
|
2016-09-06 15:23:39 +00:00
|
|
|
# TODO: Julia implementation that interprets the graph
|
2016-08-23 15:32:19 +00:00
|
|
|
|
|
|
|
graph(cap::Capacitor) = cap.graph
|