Flux.jl/src/compiler/diff.jl
2016-10-31 12:38:18 +00:00

31 lines
810 B
Julia
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

addΔ(a, b) = vertex(:+, a, b)
# Special case a couple of operators to clean up output code
const symbolic = Dict()
symbolic[:+] = (Δ, args...) -> map(_->Δ, args)
function ∇v(v::Vertex, Δ)
haskey(symbolic, value(v)) && return symbolic[value(v)](Δ, inputs(v)...)
Δ = vertex(:back!, constant(value(v)), constant(Δ), inputs(v)...)
map(i -> @flow(getindex($Δ, $i)), 1:DataFlow.nin(v))
end
function invert(v::IVertex, Δ = , out = d())
@assert !iscyclic(v)
if isconstant(v)
@assert !haskey(out, value(v))
out[value(v).value] = constant(Δ)
else
Δs = ∇v(v, Δ)
for (v, Δ′) in zip(inputs(v), Δs)
invert(v, Δ′, out)
end
end
return out
end
back!(::typeof(+), Δ, args...) = map(_ -> Δ, args)
back!(::typeof(*), Δ, a, b) = Δ*b', a'*Δ