Skip to content

Commit

Permalink
remove SimpleUnPack dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-Portal committed Aug 22, 2023
1 parent 0c5cc1c commit 29d7d27
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 31 deletions.
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SimpleUnPack = "ce78b400-467f-4804-87d8-8f486da07d0a"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"

Expand Down
1 change: 0 additions & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ LogDensityProblems = "6fdf6af0-433a-55f7-b3ed-c6c6e0b8df7c"
Optimisers = "3bd65402-5787-11e9-1adc-39752487f4e2"
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
SimpleUnPack = "ce78b400-467f-4804-87d8-8f486da07d0a"

[compat]
ADTypes = "0.1.6"
Expand Down
9 changes: 3 additions & 6 deletions docs/src/advi.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ StickingTheLandingEntropy

```@setup stl
using LogDensityProblems
using SimpleUnPack
using PDMats
using Bijectors
using LinearAlgebra
Expand All @@ -134,7 +133,7 @@ struct NormalLogNormal{MX,SX,MY,SY}
end
function LogDensityProblems.logdensity(model::NormalLogNormal, θ)
@unpack μ_x, σ_x, μ_y, Σ_y = model
(; μ_x, σ_x, μ_y, Σ_y) = model
logpdf(LogNormal(μ_x, σ_x), θ[1]) + logpdf(MvNormal(μ_y, Σ_y), θ[2:end])
end
Expand All @@ -151,17 +150,15 @@ n_dims = 10
σ_x = exp.(randn())
μ_y = randn(n_dims)
σ_y = exp.(randn(n_dims))
model = NormalLogNormal(μ_x, σ_x, μ_y, PDMats.PDiagMat(σ_y.^2));
model = NormalLogNormal(μ_x, σ_x, μ_y, Diagonal(σ_y.^2));
d = LogDensityProblems.dimension(model);
μ = randn(d);
L = Diagonal(ones(d));
q0 = AVI.VIMeanFieldGaussian(μ, L)
model = NormalLogNormal(μ_x, σ_x, μ_y, PDMats.PDiagMat(σ_y.^2));
function Bijectors.bijector(model::NormalLogNormal)
@unpack μ_x, σ_x, μ_y, Σ_y = model
(; μ_x, σ_x, μ_y, Σ_y) = model
Bijectors.Stacked(
Bijectors.bijector.([LogNormal(μ_x, σ_x), MvNormal(μ_y, Σ_y)]),
[1:1, 2:1+length(μ_y)])
Expand Down
11 changes: 4 additions & 7 deletions docs/src/started.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ ADVI with `Bijectors.Exp` bijectors is able to infer this model exactly.
Using the `LogDensityProblems` interface, we the model can be defined as follows:
```@example advi
using LogDensityProblems
using SimpleUnPack
struct NormalLogNormal{MX,SX,MY,SY}
μ_x::MX
Expand All @@ -37,7 +36,7 @@ struct NormalLogNormal{MX,SX,MY,SY}
end
function LogDensityProblems.logdensity(model::NormalLogNormal, θ)
@unpack μ_x, σ_x, μ_y, Σ_y = model
(; μ_x, σ_x, μ_y, Σ_y) = model
logpdf(LogNormal(μ_x, σ_x), θ[1]) + logpdf(MvNormal(μ_y, Σ_y), θ[2:end])
end
Expand All @@ -51,14 +50,14 @@ end
```
Let's now instantiate the model
```@example advi
using PDMats
using LinearAlgebra
n_dims = 10
μ_x = randn()
σ_x = exp.(randn())
μ_y = randn(n_dims)
σ_y = exp.(randn(n_dims))
model = NormalLogNormal(μ_x, σ_x, μ_y, PDMats.PDiagMat(σ_y.^2));
model = NormalLogNormal(μ_x, σ_x, μ_y, Diagonal(σ_y.^2));
```

Since the `y` follows a log-normal prior, its support is bounded to be the positive half-space ``\mathbb{R}_+``.
Expand All @@ -67,7 +66,7 @@ Thus, we will use [Bijectors](https://github.com/TuringLang/Bijectors.jl) to mat
using Bijectors
function Bijectors.bijector(model::NormalLogNormal)
@unpack μ_x, σ_x, μ_y, Σ_y = model
(; μ_x, σ_x, μ_y, Σ_y) = model
Bijectors.Stacked(
Bijectors.bijector.([LogNormal(μ_x, σ_x), MvNormal(μ_y, Σ_y)]),
[1:1, 2:1+length(μ_y)])
Expand All @@ -94,8 +93,6 @@ objective = AVI.ADVI(model, n_montecaro; invbij = b⁻¹)
```
For the variational family, we will use the classic mean-field Gaussian family.
```@example advi
using LinearAlgebra
d = LogDensityProblems.dimension(model);
μ = randn(d);
L = Diagonal(ones(d));
Expand Down
1 change: 0 additions & 1 deletion src/AdvancedVI.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

module AdvancedVI

using SimpleUnPack: @unpack, @pack!
using Accessors

using Random: AbstractRNG, default_rng
Expand Down
14 changes: 7 additions & 7 deletions src/distributions/location_scale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,42 @@ Base.length(q::VILocationScale) = length(q.location)
Base.size(q::VILocationScale) = size(q.location)

function StatsBase.entropy(q::VILocationScale)
@unpack location, scale, dist = q
(; location, scale, dist) = q
n_dims = length(location)
n_dims*entropy(dist) + first(logabsdet(scale))
end

function logpdf(q::VILocationScale, z::AbstractVector{<:Real})
@unpack location, scale, dist = q
(; location, scale, dist) = q
sum(zᵢ -> logpdf(dist, zᵢ), scale \ (z - location)) - first(logabsdet(scale))
end

function _logpdf(q::VILocationScale, z::AbstractVector{<:Real})
@unpack location, scale, dist = q
(; location, scale, dist) = q
sum(zᵢ -> logpdf(dist, zᵢ), scale \ (z - location)) - first(logabsdet(scale))
end

function rand(q::VILocationScale)
@unpack location, scale, dist = q
(; location, scale, dist) = q
n_dims = length(location)
scale*rand(dist, n_dims) + location
end

function rand(rng::AbstractRNG, q::VILocationScale, num_samples::Int)
@unpack location, scale, dist = q
(; location, scale, dist) = q
n_dims = length(location)
scale*rand(rng, dist, n_dims, num_samples) .+ location
end

function _rand!(rng::AbstractRNG, q::VILocationScale, x::AbstractVector{<:Real})
@unpack location, scale, dist = q
(; location, scale, dist) = q
rand!(rng, dist, x)
x .= scale*x
return x += location
end

function _rand!(rng::AbstractRNG, q::VILocationScale, x::AbstractMatrix{<:Real})
@unpack location, scale, dist = q
(; location, scale, dist) = q
rand!(rng, dist, x)
x *= scale
return x += location
Expand Down
1 change: 0 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Random123 = "74087812-796a-5b5d-8853-05524746bad3"
ReTest = "e0db7c4e-2690-44b9-bad6-7687da720f89"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
SimpleUnPack = "ce78b400-467f-4804-87d8-8f486da07d0a"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
3 changes: 1 addition & 2 deletions test/advi_locscale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ using Optimisers
using Distributions
using PDMats
using LinearAlgebra
using SimpleUnPack: @unpack

struct TestModel{M,L,S}
model::M
Expand Down Expand Up @@ -48,7 +47,7 @@ include("models/utils.jl")

T = 10000
modelstats = modelconstr(realtype; rng)
@unpack model, μ_true, L_true, n_dims, is_meanfield = modelstats
(; model, μ_true, L_true, n_dims, is_meanfield) = modelstats

b = Bijectors.bijector(model)
b⁻¹ = inverse(b)
Expand Down
2 changes: 1 addition & 1 deletion test/models/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct TestMvNormal{M,S}
end

function LogDensityProblems.logdensity(model::TestMvNormal, θ)
@unpack μ, Σ = model
(; μ, Σ) = model
logpdf(MvNormal(μ, Σ), θ)
end

Expand Down
7 changes: 4 additions & 3 deletions test/models/normallognormal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct NormalLogNormal{MX,SX,MY,SY}
end

function LogDensityProblems.logdensity(model::NormalLogNormal, θ)
@unpack μ_x, σ_x, μ_y, Σ_y = model
(; μ_x, σ_x, μ_y, Σ_y) = model
logpdf(LogNormal(μ_x, σ_x), θ[1]) + logpdf(MvNormal(μ_y, Σ_y), θ[2:end])
end

Expand All @@ -20,7 +20,7 @@ function LogDensityProblems.capabilities(::Type{<:NormalLogNormal})
end

function Bijectors.bijector(model::NormalLogNormal)
@unpack μ_x, σ_x, μ_y, Σ_y = model
(; μ_x, σ_x, μ_y, Σ_y) = model
Bijectors.Stacked(
Bijectors.bijector.([LogNormal(μ_x, σ_x), MvNormal(μ_y, Σ_y)]),
[1:1, 2:1+length(μ_y)])
Expand Down Expand Up @@ -56,7 +56,8 @@ function normallognormal_meanfield(realtype; rng = default_rng())
μ_y = randn(rng, realtype, n_dims)
σ_y = log.(exp.(randn(rng, realtype, n_dims)) .+ 1)

model = NormalLogNormal(μ_x, σ_x, μ_y, PDMats.PDiagMat(σ_y.^2))
#model = NormalLogNormal(μ_x, σ_x, μ_y, PDMats.PDiagMat(σ_y.^2))
model = NormalLogNormal(μ_x, σ_x, μ_y, Diagonal(σ_y.^2))

μ = vcat(μ_x, μ_y)
L = vcat(σ_x, σ_y) |> Diagonal
Expand Down
1 change: 0 additions & 1 deletion test/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ using Optimisers
using Distributions
using PDMats
using LinearAlgebra
using SimpleUnPack: @unpack

struct TestModel{M,L,S}
model::M
Expand Down

0 comments on commit 29d7d27

Please sign in to comment.