-
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
Merged
Merged
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e332d8c
remove the type `ParamSpaceSGD`
Red-Portal 1f35cc9
run formatter
Red-Portal c8404b6
run formatter
Red-Portal 0cc7538
run formatter
Red-Portal ede91c6
fix rename file paramspacesgd.jl to interface.jl
Red-Portal 625f429
Merge branch 'remove_paramspacesgd' of github.com:TuringLang/Advanced…
Red-Portal e3c2761
Merge branch 'main' of github.com:TuringLang/AdvancedVI.jl into remov…
Red-Portal 683a09d
throw invalid state for unknown paramspacesgd type
Red-Portal 570fe11
add docstring for union type of paramspacesgd algorithms
Red-Portal 2d5f373
fix remove custom state types for paramspacesgd algorithms
Red-Portal e0221eb
fix remove custom state types for paramspacesgd
Red-Portal e51ab3c
fix file path
Red-Portal e49c680
fix bug in BijectorsExt
Red-Portal 3c5b56f
fix include `SubSampleObjective` as part of `ParamSpaceSGD`
Red-Portal 30f5160
fix formatting
Red-Portal 008c4ea
fix revert adding SubsampledObjective into ParamSpaceSGD
Red-Portal 8a18902
refactor flatten algorithms
Red-Portal b002e1e
fix error update paths in main file
Red-Portal 1ba361f
refactor flatten the tests to reflect new structure
Red-Portal 86baa07
fix file include path in tests
Red-Portal 67e9375
fix missing operator in subsampledobj tests
Red-Portal 9b2eabb
fix formatting
Red-Portal 922e5d7
update docs
Red-Portal 7d3ed86
Merge branch 'remove_paramspacesgd' of github.com:TuringLang/Advanced…
Red-Portal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
|
|
||
| """ | ||
| This family of algorithms (`<:KLMinRepGradDescent`,`<:KLMinRepGradProxDescent`,`<:KLMinScoreGradDescent`) 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`. | ||
| """ | ||
| const ParamSpaceSGD = Union{ | ||
| <:KLMinRepGradDescent,<:KLMinRepGradProxDescent,<:KLMinScoreGradDescent | ||
| } | ||
|
|
||
| function init(rng::Random.AbstractRNG, alg::ParamSpaceSGD, q_init, prob) | ||
| (; adtype, optimizer, averager, objective, operator) = alg | ||
| if q_init isa AdvancedVI.MvLocationScale && operator isa AdvancedVI.IdentityOperator | ||
| @warn( | ||
| "IdentityOperator is used with a variational family <:MvLocationScale. Optimization can easily fail under this combination due to singular scale matrices. Consider using the operator `ClipScale` in the algorithm instead.", | ||
| ) | ||
| end | ||
| params, re = Optimisers.destructure(q_init) | ||
| opt_st = Optimisers.setup(optimizer, params) | ||
| 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 ( | ||
| prob=prob, | ||
| q=q_init, | ||
| iteration=0, | ||
| grad_buf=grad_buf, | ||
| opt_st=opt_st, | ||
| obj_st=obj_st, | ||
| avg_st=avg_st, | ||
| ) | ||
| end | ||
|
|
||
| function output(alg::ParamSpaceSGD, state) | ||
| params_avg = value(alg.averager, state.avg_st) | ||
| _, re = Optimisers.destructure(state.q) | ||
| return re(params_avg) | ||
| end | ||
|
|
||
| function step( | ||
| rng::Random.AbstractRNG, alg::ParamSpaceSGD, state, callback, objargs...; kwargs... | ||
| ) | ||
| (; adtype, objective, operator, averager) = alg | ||
| (; prob, q, iteration, grad_buf, opt_st, obj_st, avg_st) = state | ||
|
|
||
| iteration += 1 | ||
|
|
||
| params, re = Optimisers.destructure(q) | ||
|
|
||
| grad_buf, obj_st, info = estimate_gradient!( | ||
| rng, objective, adtype, grad_buf, obj_st, params, re, objargs... | ||
| ) | ||
|
|
||
| grad = DiffResults.gradient(grad_buf) | ||
| opt_st, params = Optimisers.update!(opt_st, params, grad) | ||
| params = apply(operator, typeof(q), opt_st, params, re) | ||
| avg_st = apply(averager, avg_st, params) | ||
|
|
||
| state = ( | ||
| prob=prob, | ||
| q=re(params), | ||
| iteration=iteration, | ||
| grad_buf=grad_buf, | ||
| opt_st=opt_st, | ||
| obj_st=obj_st, | ||
| avg_st=avg_st, | ||
| ) | ||
|
|
||
| if !isnothing(callback) | ||
| averaged_params = value(averager, avg_st) | ||
| info′ = callback(; | ||
| rng, | ||
| iteration, | ||
| restructure=re, | ||
| params=params, | ||
| averaged_params=averaged_params, | ||
| gradient=grad, | ||
| state=state, | ||
| ) | ||
| info = !isnothing(info′) ? merge(info′, info) : info | ||
| end | ||
| state, false, info | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I only have one comment, which is that it's probably better to keep this as a struct (although I recognise you may have some difficulty in choosing a name for it). NamedTuples are too flexible, if you return one you don't know what fields are in it.
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.
I propose we stick with this for now and improve this later once we can make things more organized. (The new structure in the PR is making a whole lot of things implicit anyway, so I think the fields being implicit doesn't make things much worse)
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.
Sure, I'm not that fussed.