Skip to content
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

fix rename adbackend to adtype #60

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function LogDensityProblems.capabilities(::Type{<:NormalLogNormal})
end
```

Since the support of `x` is constrained to be positive, and VI is best done in the unconstrained Euclidean space, we need to use a *bijector* to transform `x` into unconstrained Euclidean space. We will use the [`Bijectors.jl`](https://github.com/TuringLang/Bijectors.jl) package for this purpose.
Since the support of `x` is constrained to be positive and VI is best done in the unconstrained Euclidean space, we need to use a *bijector* to transform `x` into unconstrained Euclidean space. We will use the [`Bijectors.jl`](https://github.com/TuringLang/Bijectors.jl) package for this purpose.
This corresponds to the automatic differentiation variational inference (ADVI) formulation[^KTRGB2017].
```julia
using Bijectors
Expand Down Expand Up @@ -99,7 +99,7 @@ q, stats, _ = AdvancedVI.optimize(
elbo,
q_transformed,
max_iter;
adbackend = ADTypes.AutoForwardDiff(),
adtype = ADTypes.AutoForwardDiff(),
optimizer = Optimisers.Adam(1e-3)
)

Expand Down
6 changes: 3 additions & 3 deletions docs/src/elbo/repgradelbo.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ _, stats_cfe, _ = AdvancedVI.optimize(
q0_trans,
max_iter;
show_progress = false,
adbackend = AutoForwardDiff(),
adtype = AutoForwardDiff(),
optimizer = Optimisers.Adam(1e-3)
);

Expand All @@ -199,7 +199,7 @@ _, stats_stl, _ = AdvancedVI.optimize(
q0_trans,
max_iter;
show_progress = false,
adbackend = AutoForwardDiff(),
adtype = AutoForwardDiff(),
optimizer = Optimisers.Adam(1e-3)
);

Expand Down Expand Up @@ -264,7 +264,7 @@ _, stats_qmc, _ = AdvancedVI.optimize(
q0_trans,
max_iter;
show_progress = false,
adbackend = AutoForwardDiff(),
adtype = AutoForwardDiff(),
optimizer = Optimisers.Adam(1e-3)
);

Expand Down
2 changes: 1 addition & 1 deletion docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ q_trans, stats, _ = AdvancedVI.optimize(
q0_trans,
n_max_iter;
show_progress = false,
adbackend = AutoForwardDiff(),
adtype = AutoForwardDiff(),
optimizer = Optimisers.Adam(1e-3)
);
nothing
Expand Down
4 changes: 2 additions & 2 deletions src/AdvancedVI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ export estimate_objective


"""
estimate_gradient!(rng, obj, adbackend, out, prob, λ, restructure, obj_state)
estimate_gradient!(rng, obj, adtype, out, prob, λ, restructure, obj_state)

Estimate (possibly stochastic) gradients of the variational objective `obj` targeting `prob` with respect to the variational parameters `λ`

# Arguments
- `rng::Random.AbstractRNG`: Random number generator.
- `obj::AbstractVariationalObjective`: Variational objective.
- `adbackend::ADTypes.AbstractADType`: Automatic differentiation backend.
- `adtype::ADTypes.AbstractADType`: Automatic differentiation backend.
- `out::DiffResults.MutableDiffResult`: Buffer containing the objective value and gradient estimates.
- `prob`: The target log-joint likelihood implementing the `LogDensityProblem` interface.
- `λ`: Variational parameters to evaluate the gradient on.
Expand Down
10 changes: 5 additions & 5 deletions src/objectives/elbo/repgradelbo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ estimate_objective(obj::RepGradELBO, q, prob; n_samples::Int = obj.n_samples) =
estimate_objective(Random.default_rng(), obj, q, prob; n_samples)

function estimate_gradient!(
rng ::Random.AbstractRNG,
obj ::RepGradELBO,
adbackend::ADTypes.AbstractADType,
out ::DiffResults.MutableDiffResult,
rng ::Random.AbstractRNG,
obj ::RepGradELBO,
adtype::ADTypes.AbstractADType,
out ::DiffResults.MutableDiffResult,
prob,
λ,
restructure,
Expand All @@ -112,7 +112,7 @@ function estimate_gradient!(
elbo = energy + entropy
-elbo
end
value_and_gradient!(adbackend, f, λ, out)
value_and_gradient!(adtype, f, λ, out)

nelbo = DiffResults.value(out)
stat = (elbo=-nelbo,)
Expand Down
6 changes: 3 additions & 3 deletions src/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The variational approximation can be constructed by passing the variational para
- `objargs...`: Arguments to be passed to `objective`.

# Keyword Arguments
- `adbackend::ADtypes.AbstractADType`: Automatic differentiation backend.
- `adtype::ADtypes.AbstractADType`: Automatic differentiation backend.
- `optimizer::Optimisers.AbstractRule`: Optimizer used for inference. (Default: `Adam`.)
- `rng::AbstractRNG`: Random number generator. (Default: `Random.default_rng()`.)
- `show_progress::Bool`: Whether to show the progress bar. (Default: `true`.)
Expand Down Expand Up @@ -54,7 +54,7 @@ function optimize(
params_init,
max_iter ::Int,
objargs...;
adbackend ::ADTypes.AbstractADType,
adtype ::ADTypes.AbstractADType,
optimizer ::Optimisers.AbstractRule = Optimisers.Adam(),
show_progress::Bool = true,
state_init ::NamedTuple = NamedTuple(),
Expand All @@ -77,7 +77,7 @@ function optimize(
stat = (iteration=t,)

grad_buf, obj_st, stat′ = estimate_gradient!(
rng, objective, adbackend, grad_buf, problem,
rng, objective, adtype, grad_buf, problem,
λ, restructure, obj_st, objargs...
)
stat = merge(stat, stat′)
Expand Down
8 changes: 4 additions & 4 deletions test/inference/repgradelbo_distributionsad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using Test
:RepGradELBOClosedFormEntropy => RepGradELBO(n_montecarlo),
:RepGradELBOStickingTheLanding => RepGradELBO(n_montecarlo, entropy = StickingTheLandingEntropy()),
),
(adbackname, adbackend) ∈ Dict(
(adbackname, adtype) ∈ Dict(
:ForwarDiff => AutoForwardDiff(),
#:ReverseDiff => AutoReverseDiff(),
:Zygote => AutoZygote(),
Expand All @@ -39,7 +39,7 @@ using Test
rng, model, objective, q0, T;
optimizer = Optimisers.Adam(realtype(η)),
show_progress = PROGRESS,
adbackend = adbackend,
adtype = adtype,
)

μ = mean(q)
Expand All @@ -57,7 +57,7 @@ using Test
rng, model, objective, q0, T;
optimizer = Optimisers.Adam(realtype(η)),
show_progress = PROGRESS,
adbackend = adbackend,
adtype = adtype,
)
μ = mean(q)
L = sqrt(cov(q))
Expand All @@ -67,7 +67,7 @@ using Test
rng_repl, model, objective, q0, T;
optimizer = Optimisers.Adam(realtype(η)),
show_progress = PROGRESS,
adbackend = adbackend,
adtype = adtype,
)
μ_repl = mean(q)
L_repl = sqrt(cov(q))
Expand Down
8 changes: 4 additions & 4 deletions test/inference/repgradelbo_locationscale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using Test
:RepGradELBOClosedFormEntropy => RepGradELBO(n_montecarlo),
:RepGradELBOStickingTheLanding => RepGradELBO(n_montecarlo, entropy = StickingTheLandingEntropy()),
),
(adbackname, adbackend) in Dict(
(adbackname, adtype) in Dict(
:ForwarDiff => AutoForwardDiff(),
:ReverseDiff => AutoReverseDiff(),
:Zygote => AutoZygote(),
Expand Down Expand Up @@ -43,7 +43,7 @@ using Test
rng, model, objective, q0, T;
optimizer = Optimisers.Adam(realtype(η)),
show_progress = PROGRESS,
adbackend = adbackend,
adtype = adtype,
)

μ = q.location
Expand All @@ -61,7 +61,7 @@ using Test
rng, model, objective, q0, T;
optimizer = Optimisers.Adam(realtype(η)),
show_progress = PROGRESS,
adbackend = adbackend,
adtype = adtype,
)
μ = q.location
L = q.scale
Expand All @@ -71,7 +71,7 @@ using Test
rng_repl, model, objective, q0, T;
optimizer = Optimisers.Adam(realtype(η)),
show_progress = PROGRESS,
adbackend = adbackend,
adtype = adtype,
)
μ_repl = q.location
L_repl = q.scale
Expand Down
8 changes: 4 additions & 4 deletions test/inference/repgradelbo_locationscale_bijectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using Test
:RepGradELBOClosedFormEntropy => RepGradELBO(n_montecarlo),
:RepGradELBOStickingTheLanding => RepGradELBO(n_montecarlo, entropy = StickingTheLandingEntropy()),
),
(adbackname, adbackend) in Dict(
(adbackname, adtype) in Dict(
:ForwarDiff => AutoForwardDiff(),
:ReverseDiff => AutoReverseDiff(),
#:Zygote => AutoZygote(),
Expand Down Expand Up @@ -48,7 +48,7 @@ using Test
rng, model, objective, q0_z, T;
optimizer = Optimisers.Adam(realtype(η)),
show_progress = PROGRESS,
adbackend = adbackend,
adtype = adtype,
)

μ = q.dist.location
Expand All @@ -66,7 +66,7 @@ using Test
rng, model, objective, q0_z, T;
optimizer = Optimisers.Adam(realtype(η)),
show_progress = PROGRESS,
adbackend = adbackend,
adtype = adtype,
)
μ = q.dist.location
L = q.dist.scale
Expand All @@ -76,7 +76,7 @@ using Test
rng_repl, model, objective, q0_z, T;
optimizer = Optimisers.Adam(realtype(η)),
show_progress = PROGRESS,
adbackend = adbackend,
adtype = adtype,
)
μ_repl = q.dist.location
L_repl = q.dist.scale
Expand Down
16 changes: 8 additions & 8 deletions test/interface/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ using Test
q0 = TuringDiagMvNormal(zeros(Float64, n_dims), ones(Float64, n_dims))
obj = RepGradELBO(10)

adbackend = AutoForwardDiff()
adtype = AutoForwardDiff()
optimizer = Optimisers.Adam(1e-2)

rng = StableRNG(seed)
q_ref, stats_ref, _ = optimize(
rng, model, obj, q0, T;
optimizer,
show_progress = false,
adbackend,
adtype,
)
λ_ref, _ = Optimisers.destructure(q_ref)

Expand All @@ -31,15 +31,15 @@ using Test
model, obj, q0, T;
optimizer,
show_progress = false,
adbackend,
adtype,
)

λ₀, re = Optimisers.destructure(q0)
optimize(
model, obj, re, λ₀, T;
optimizer,
show_progress = false,
adbackend,
adtype,
)
end

Expand All @@ -51,7 +51,7 @@ using Test
rng, model, obj, re, λ₀, T;
optimizer,
show_progress = false,
adbackend,
adtype,
)
@test λ == λ_ref
@test stats == stats_ref
Expand All @@ -67,7 +67,7 @@ using Test
_, stats, _ = optimize(
rng, model, obj, q0, T;
show_progress = false,
adbackend,
adtype,
callback
)
@test [stat.test_value for stat ∈ stats] == test_values
Expand All @@ -83,15 +83,15 @@ using Test
rng, model, obj, q0, T_first;
optimizer,
show_progress = false,
adbackend
adtype
)

q, stats, _ = optimize(
rng, model, obj, q_first, T_last;
optimizer,
show_progress = false,
state_init = state,
adbackend
adtype
)
@test q == q_ref
end
Expand Down
Loading