You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
It may happen that one wants to optimise a variable x over the positive reals. Naively performing gradient descent won't work, because the variable may go negative. A commonly-used workaround is to instead optimise log(x); that is, parametrise the domain of the variable using some invertible transformf such that f_inverse(x) is unconstrained:
x =1.0
x_log =Leaf(Tape(), log(x))
x_ =exp(x_log)
y =f(x_)
x′ =exp(step(∇(y), x_log, optimiser))
This, however, is rather clumsy to write. I therefore propose a type TransformedLeaf, which implements the above approach, but hides transforming the variable back and forth:
type TransformedLeaf{T} <:Node{T}
val::T
tape::Tape
pos::Int
inverse::Node{T}
f::FunctionendfunctionTransformedLeaf(tape::Tape, x, f::Function, f_inv::Function)
before =Leaf(tape, f_inv(x))
after =f(before)
returnTransformedLeaf(after.val, after.tape, after.pos, before, f)
endpositive(tape::Tape, x) =TransformedLeaf(tape, x, x ->exp.(x), x ->log.(x))
step(t::Tape, x::Node, opt::Optimiser) = x.val +step(update!(opt, t[x]))
step(t::Tape, x::TransformedLeaf, opt::Optimiser) =
x.f(x.inverse.val +step(update!(opt, t[x.inverse])))
Now, one can simply
x =1.0
x_ =positive(x)
y =f(x_)
x′ =step(∇(y), x, optimiser)
The text was updated successfully, but these errors were encountered:
It may happen that one wants to optimise a variable
x
over the positive reals. Naively performing gradient descent won't work, because the variable may go negative. A commonly-used workaround is to instead optimiselog(x)
; that is, parametrise the domain of the variable using some invertible transformf
such thatf_inverse(x)
is unconstrained:This, however, is rather clumsy to write. I therefore propose a type
TransformedLeaf
, which implements the above approach, but hides transforming the variable back and forth:Now, one can simply
The text was updated successfully, but these errors were encountered: