2016-08-23 15:32:19 +00:00
|
|
|
|
export Model, back!, update!, param
|
|
|
|
|
|
|
|
|
|
# Basic model API
|
|
|
|
|
|
2016-12-15 22:31:27 +00:00
|
|
|
|
"""
|
|
|
|
|
(m::Model)(X...) => Y
|
|
|
|
|
|
|
|
|
|
A "model" is a function with state. For example, a logistic regression is the
|
|
|
|
|
function
|
|
|
|
|
|
|
|
|
|
x -> σ(x * W + b)
|
|
|
|
|
|
|
|
|
|
where `W` and `b` are a trainable matrix and vector of weights repectively. The
|
|
|
|
|
`Model` abstract type is used loosely; in general the concept of a model is
|
|
|
|
|
closer to a protocol, and models don't need to inherit from this type. Normal
|
|
|
|
|
Julia functions are models with 0 parameters, for example.
|
|
|
|
|
"""
|
2016-08-23 15:32:19 +00:00
|
|
|
|
abstract Model
|
|
|
|
|
|
2016-12-15 22:31:27 +00:00
|
|
|
|
"""
|
|
|
|
|
back!(m::Model, ΔY, X...) => ΔX
|
|
|
|
|
|
|
|
|
|
Backpropagate the gradient `ΔY` through the model `m`, accumulating the
|
|
|
|
|
gradients of any parameters. Returns the gradient of the input `X`. Gradients
|
|
|
|
|
may be arrays or tuples of arrays (for multiple inputs/outputs).
|
|
|
|
|
"""
|
|
|
|
|
back!(m::Model, Δ, xs...) = error("Backprop not implemented for $(typeof(m))")
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
update!(m::Model, η) => m
|
|
|
|
|
|
|
|
|
|
Update the parameters of the model `m` using the accumulated gradients from
|
|
|
|
|
`back!`, using the learning rate `η`.
|
|
|
|
|
"""
|
2016-08-24 14:41:30 +00:00
|
|
|
|
update!(m, η) = m
|
2016-08-23 15:32:19 +00:00
|
|
|
|
|
2016-12-15 22:31:27 +00:00
|
|
|
|
"""
|
|
|
|
|
graph(m::Model) => ::IVertex{Any} | nothing
|
|
|
|
|
|
|
|
|
|
Returns the graph representation of the model, if any. Most models are built
|
|
|
|
|
from lower-level components and can simply implement this method to get most of
|
|
|
|
|
Flux's functionality. If this method isn't available, functionality like
|
|
|
|
|
backpropagation or conversion for backend must be implemented on a case-by-case
|
|
|
|
|
basis. Alternatively, one can implement this method and override individual
|
|
|
|
|
methods as necessary.
|
|
|
|
|
"""
|
2016-08-31 01:37:53 +00:00
|
|
|
|
graph(m) = nothing
|
|
|
|
|
|
2016-08-23 15:32:19 +00:00
|
|
|
|
# Model parameters
|
|
|
|
|
|
2016-12-15 22:31:27 +00:00
|
|
|
|
"""
|
|
|
|
|
A `Param` object stores a parameter array along with an accumulated delta to
|
|
|
|
|
that array. When converting to backends like TensorFlow, identical `Param`s will
|
|
|
|
|
result in identical variable objects, making model reuse trivial.
|
|
|
|
|
"""
|
2016-08-23 15:32:19 +00:00
|
|
|
|
type Param{T}
|
|
|
|
|
x::T
|
|
|
|
|
Δx::T
|
|
|
|
|
end
|
|
|
|
|
|
2016-12-15 22:31:27 +00:00
|
|
|
|
"""
|
|
|
|
|
param(x::T) => ::Param{T}
|
|
|
|
|
|
|
|
|
|
Convenience method for creating a `Param` object for a given array.
|
|
|
|
|
"""
|
2016-08-23 15:32:19 +00:00
|
|
|
|
param(x) = Param(x, zero(x))
|
|
|
|
|
|
|
|
|
|
state(p::Param) = p.x
|
|
|
|
|
|
2016-12-15 22:31:27 +00:00
|
|
|
|
"""
|
|
|
|
|
accumulate!(p::Param, Δ) => p
|
|
|
|
|
|
|
|
|
|
Accumulates the update `Δ` on `p`. The value of `p` won't change until
|
|
|
|
|
`update!`.
|
|
|
|
|
"""
|
2016-08-23 15:32:19 +00:00
|
|
|
|
function accumulate!(p::Param, Δ)
|
2016-12-15 22:57:36 +00:00
|
|
|
|
p.Δx += Δ
|
2016-08-23 15:32:19 +00:00
|
|
|
|
return p
|
|
|
|
|
end
|
|
|
|
|
|
2016-12-15 22:31:27 +00:00
|
|
|
|
"""
|
|
|
|
|
update!(p::Param)
|
|
|
|
|
|
|
|
|
|
Apply the accumulated updates to the value of the parameter.
|
|
|
|
|
"""
|
2016-08-23 15:32:19 +00:00
|
|
|
|
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
|
|
|
|
|
|
2017-02-20 23:15:27 +00:00
|
|
|
|
Base.size(p::Param) = size(p.x)
|
|
|
|
|
Base.size(p::Param, n) = size(p.x, n)
|
2016-10-04 21:23:10 +00:00
|
|
|
|
|
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-11-15 16:58:59 +00:00
|
|
|
|
(m::Capacitor)(xs...) = interpret(reifyparams(m.graph), xs...)
|
2016-08-23 15:32:19 +00:00
|
|
|
|
|
|
|
|
|
graph(cap::Capacitor) = cap.graph
|