-
Notifications
You must be signed in to change notification settings - Fork 19
Remove the type ParamSpaceSGD
#205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,68 +1,7 @@ | ||||||
|
||||||
""" | ||||||
ParamSpaceSGD( | ||||||
objective::AbstractVariationalObjective, | ||||||
adtype::ADTypes.AbstractADType, | ||||||
optimizer::Optimisers.AbstractRule, | ||||||
averager::AbstractAverager, | ||||||
operator::AbstractOperator, | ||||||
) | ||||||
|
||||||
This algorithm applies stochastic gradient descent (SGD) to the variational `objective` over the (Euclidean) space of variational parameters. | ||||||
|
||||||
The trainable parameters in the variational approximation are expected to be extractable through `Optimisers.destructure`. | ||||||
This requires the variational approximation to be marked as a functor through `Functors.@functor`. | ||||||
|
||||||
!!! note | ||||||
Different objective may impose different requirements on `adtype`, variational family, `optimizer`, and `operator`. It is therefore important to check the documentation corresponding to each specific objective. Essentially, each objective should be thought as forming its own unique algorithm. | ||||||
|
||||||
# Arguments | ||||||
- `objective`: Variational Objective. | ||||||
- `adtype`: Automatic differentiation backend. | ||||||
- `optimizer`: Optimizer used for inference. | ||||||
- `averager` : Parameter averaging strategy. | ||||||
- `operator` : Operator applied to the parameters after each optimization step. | ||||||
|
||||||
# Output | ||||||
- `q_averaged`: The variational approximation formed from the averaged SGD iterates. | ||||||
|
||||||
# Callback | ||||||
The callback function `callback` has a signature of | ||||||
|
||||||
callback(; rng, iteration, restructure, params, averaged_params, restructure, gradient) | ||||||
|
||||||
The arguments are as follows: | ||||||
- `rng`: Random number generator internally used by the algorithm. | ||||||
- `iteration`: The index of the current iteration. | ||||||
- `restructure`: Function that restructures the variational approximation from the variational parameters. Calling `restructure(params)` reconstructs the current variational approximation. | ||||||
- `params`: Current variational parameters. | ||||||
- `averaged_params`: Variational parameters averaged according to the averaging strategy. | ||||||
- `gradient`: The estimated (possibly stochastic) gradient. | ||||||
|
||||||
""" | ||||||
struct ParamSpaceSGD{ | ||||||
Obj<:AbstractVariationalObjective, | ||||||
AD<:ADTypes.AbstractADType, | ||||||
Opt<:Optimisers.AbstractRule, | ||||||
Avg<:AbstractAverager, | ||||||
Op<:AbstractOperator, | ||||||
} <: AbstractVariationalAlgorithm | ||||||
objective::Obj | ||||||
adtype::AD | ||||||
optimizer::Opt | ||||||
averager::Avg | ||||||
operator::Op | ||||||
end | ||||||
|
||||||
struct ParamSpaceSGDState{P,Q,GradBuf,OptSt,ObjSt,AvgSt} | ||||||
prob::P | ||||||
q::Q | ||||||
iteration::Int | ||||||
grad_buf::GradBuf | ||||||
opt_st::OptSt | ||||||
obj_st::ObjSt | ||||||
avg_st::AvgSt | ||||||
end | ||||||
const ParamSpaceSGD = Union{ | ||||||
<:KLMinRepGradDescent,<:KLMinRepGradProxDescent,<:KLMinScoreGradDescent | ||||||
} | ||||||
|
||||||
function init(rng::Random.AbstractRNG, alg::ParamSpaceSGD, q_init, prob) | ||||||
(; adtype, optimizer, averager, objective, operator) = alg | ||||||
|
@@ -76,7 +15,17 @@ function init(rng::Random.AbstractRNG, alg::ParamSpaceSGD, q_init, prob) | |||||
obj_st = init(rng, objective, adtype, q_init, prob, params, re) | ||||||
avg_st = init(averager, params) | ||||||
grad_buf = DiffResults.DiffResult(zero(eltype(params)), similar(params)) | ||||||
return ParamSpaceSGDState(prob, q_init, 0, grad_buf, opt_st, obj_st, avg_st) | ||||||
if alg isa KLMinRepGradDescent | ||||||
return KLMinRepGradDescentState(prob, q_init, 0, grad_buf, opt_st, obj_st, avg_st) | ||||||
elseif alg isa KLMinRepGradProxDescent | ||||||
return KLMinRepGradProxDescentState( | ||||||
prob, q_init, 0, grad_buf, opt_st, obj_st, avg_st | ||||||
) | ||||||
elseif alg isa KLMinScoreGradDescent | ||||||
return KLMinScoreGradDescentState(prob, q_init, 0, grad_buf, opt_st, obj_st, avg_st) | ||||||
else | ||||||
nothing | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe throw a warning or error message here instead of letting it fail silently?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should never hit the |
||||||
end | ||||||
end | ||||||
|
||||||
function output(alg::ParamSpaceSGD, state) | ||||||
|
@@ -104,9 +53,19 @@ function step( | |||||
params = apply(operator, typeof(q), opt_st, params, re) | ||||||
avg_st = apply(averager, avg_st, params) | ||||||
|
||||||
state = ParamSpaceSGDState( | ||||||
prob, re(params), iteration, grad_buf, opt_st, obj_st, avg_st | ||||||
) | ||||||
state = if alg isa KLMinRepGradDescent | ||||||
KLMinRepGradDescentState(prob, re(params), iteration, grad_buf, opt_st, obj_st, avg_st) | ||||||
elseif alg isa KLMinRepGradProxDescent | ||||||
KLMinRepGradProxDescentState( | ||||||
prob, re(params), iteration, grad_buf, opt_st, obj_st, avg_st | ||||||
) | ||||||
elseif alg isa KLMinScoreGradDescent | ||||||
KLMinScoreGradDescentState( | ||||||
prob, re(params), iteration, grad_buf, opt_st, obj_st, avg_st | ||||||
) | ||||||
else | ||||||
nothing | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above.
Suggested change
|
||||||
end | ||||||
|
||||||
if !isnothing(callback) | ||||||
averaged_params = value(averager, avg_st) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.