Skip to content

Commit f62cb86

Browse files
authored
Merge pull request #30 from EarthyScience/dev
store only one PBModel and use create_nsite_applicator
2 parents aca28b0 + d8042bb commit f62cb86

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1417
-244
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ docs/src/**/*.html
1515
docs/src/**/*.ipynb
1616
docs/src/**/*Manifest.toml
1717
docs/src_stash/*.ipynb
18+
docs/src/tutorials/test.*
19+
docs/src/tutorials/tmp*
20+

Project.toml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "HybridVariationalInference"
22
uuid = "a108c475-a4e2-4021-9a84-cfa7df242f64"
33
authors = ["Thomas Wutzler <[email protected]> and contributors"]
4-
version = "1.0.0-DEV"
4+
version = "0.2"
55

66
[deps]
77
Bijectors = "76274a88-744f-5084-9051-94815aaf08c4"
@@ -10,22 +10,28 @@ ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
1010
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
1111
CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
1212
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
13+
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
1314
DistributionFits = "45214091-1ed4-4409-9bcf-fdb48a05e921"
1415
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1516
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
1617
Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
1718
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
19+
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
1820
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
21+
LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
1922
MLDataDevices = "7e8f7934-dd98-4c1a-8fe8-92b47a384d40"
2023
MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54"
2124
Missings = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
25+
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
26+
Optimisers = "3bd65402-5787-11e9-1adc-39752487f4e2"
2227
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
2328
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2429
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
2530
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2631
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
2732
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
2833
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
34+
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
2935

3036
[weakdeps]
3137
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
@@ -47,17 +53,22 @@ ChainRulesCore = "1.25"
4753
Combinatorics = "1.0.2"
4854
CommonSolve = "0.2.4"
4955
ComponentArrays = "0.15.19"
56+
DifferentiationInterface = "0.6.54, 0.7"
5057
DistributionFits = "0.3.9"
5158
Distributions = "0.25.117"
5259
FillArrays = "1.13.0"
5360
Flux = "0.14, 0.15, 0.16"
5461
Functors = "0.4, 0.5"
5562
GPUArraysCore = "0.1, 0.2"
63+
KernelAbstractions = "0.9.34"
5664
LinearAlgebra = "1.10"
65+
LogExpFunctions = "0.3.29"
5766
Lux = "1.4.2"
5867
MLDataDevices = "1.5, 1.6"
5968
MLUtils = "0.4.5"
6069
Missings = "1.2.0"
70+
NaNMath = "1.1.3"
71+
Optimisers = "0.4.6"
6172
Optimization = "3.19.3, 4"
6273
Random = "1.10.0"
6374
SimpleChains = "0.4"
@@ -66,6 +77,7 @@ StaticArrays = "1.9.13"
6677
StatsBase = "0.34.4"
6778
StatsFuns = "1.3.2"
6879
Test = "1.10"
80+
Zygote = "0.7.10"
6981
julia = "1.10"
7082

7183
[workspace]

dev/doubleMM.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ train_dataloader = MLUtils.DataLoader(
4242
(xM, xP, y_o, y_unc, 1:n_site);
4343
batchsize = n_batch, partial = false)
4444
σ_o = exp.(y_unc[:, 1] / 2)
45-
# assign the train_loader, otherwise it eatch time creates another version of synthetic data
45+
# assign the train_loader, otherwise it each time creates another version of synthetic data
4646
prob0 = HybridProblem(prob0_; train_dataloader)
4747
#tmp = HVI.get_hybridproblem_ϕunc(prob0; scenario)
4848
#prob0.covar
@@ -248,7 +248,7 @@ end
248248
(y2_K1global, θsP2_K1global, θsMs2_K1global) = (y, θsP, θsMs);
249249
end
250250

251-
() -> begin # otpimize using LUX
251+
() -> begin # optimize using LUX
252252
#using Lux
253253
g_lux = Lux.Chain(
254254
# dense layer with bias that maps to 8 outputs and applies `tanh` activation
@@ -560,7 +560,8 @@ end
560560
end
561561

562562
#ζi = first(eachrow(Array(chain)))
563-
f_allsites = get_hybridproblem_PBmodel(prob0; scenario, use_all_sites = true)
563+
f = get_hybridproblem_PBmodel(probc; scenario)
564+
f_allsites = create_nsite_applicator(f, n_site)
564565
#ζs = mapreduce(ζi -> transposeMs(ζi, intm_PMs_gen, true), hcat, eachrow(Array(chain)));
565566
ζsP = Array(chain)[:,1:n_θP]'
566567
ζsMst = reshape(Array(chain)[:,(n_θP+1) : end], n_sample_NUTS, n_site, n_θM)

docs/_quarto.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ project:
33
render:
44
- src/tutorials/basic_cpu.qmd
55
- src/tutorials/*.qmd
6+
# julia:
7+
# # workaround for quarto not pciking up the correct environment variable
8+
# # https://github.com/quarto-dev/quarto-cli/issues/13416#issuecomment-3333700286
9+
# env: ["JULIA_DEPOT_PATH=/User/homes/twutz/scratch/twutz/julia_gpu_depots"]
610

711

812

docs/src/explanation/theory_hvi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ In order to learn $\phi_g$, the user needs to provide a batch of $i \in \{1 \ldo
2828
## Estimation using the ELBO
2929

3030
In order to find the parameters of the approximation of the posterior, HVI
31-
minizes the KL divergence between the approximation and the true posterior.
31+
minimizes the KL divergence between the approximation and the true posterior.
3232
This is achieve by maximizing the evidence lower bound (ELBO).
3333

3434
$$\mathcal{L}(\phi) = \mathbb{E}_{q(\theta)} \left[\log p(y,\theta) \right] - \mathbb{E}_{q(\theta)} \left[\log q(\theta) \right]$$
@@ -128,7 +128,7 @@ $\phi = (\phi_P, \phi_g, \phi_u)$, comprises
128128
- $\phi_P = \mu_{\zeta_P}$: the means of the distributions of the transformed global
129129
parameters,
130130
- $\phi_g$: the parameters of the machine learning model, and
131-
- $\phi_u$: paramerization of $\Sigma_\zeta$ that is additional to the means.
131+
- $\phi_u$: parameterization of $\Sigma_\zeta$ that is additional to the means.
132132

133133
### Details
134134
Specifically, $\phi_u= (log\sigma^2_P, log\sigma^2_{M0}, log\sigma^2_{M\eta}, a_P, a_M)$,

docs/src/tutorials/basic_cpu.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ The example process based model (PBM) predicts a double-monod constrained rate
2727
for different substrate concentrations, `S1`, and `S2`.
2828

2929
$$
30-
y = r_0+ r_1 \frac{S_1}{K_1 + S_1} \frac{S_2}{K_2 + S_2}$$
30+
y = r_0+ r_1 \frac{S_1}{K_1 + S_1} \frac{S_2}{K_2 + S_2}
31+
$$
3132

3233
``` julia
3334
function f_doubleMM(θc::CA.ComponentVector{ET}, x) where ET
@@ -49,7 +50,7 @@ access the components by its symbolic names in the provided `ComponentArray`.
4950
HVI requires the evaluation of the likelihood of the predictions.
5051
It corresponds to the cost of predictions given some observations.
5152

52-
The user specifies a function of the negative log-Likehood
53+
The user specifies a function of the negative log-Likelihood
5354
`neg_logden(obs, pred, uncertainty_parameters)`,
5455
where all of the parameters are arrays with columns for sites.
5556

@@ -229,10 +230,10 @@ given a vector of global parameters, and a matrix of site parameters to
229230
invocation of the process based model (PBM), defined at the beginning.
230231

231232
``` julia
232-
f_batch = f_allsites = PBMSiteApplicator(f_doubleMM; θP, θM, θFix, xPvec=xP[:,1])
233+
f_batch = PBMSiteApplicator(f_doubleMM; θP, θM, θFix, xPvec=xP[:,1])
233234

234235
prob = HybridProblem(θP, θM, g_chain_scaled, ϕg0,
235-
f_batch, f_allsites, priors_dict, py,
236+
f_batch, priors_dict, py,
236237
transM, transP, train_dataloader, n_covar, n_site, n_batch)
237238
```
238239

@@ -241,6 +242,11 @@ prob = HybridProblem(θP, θM, g_chain_scaled, ϕg0,
241242
Eventually, having assembled all the moving parts of the HVI, we can perform
242243
the inversion.
243244

245+
``` julia
246+
# silence warning of no GPU backend found (because we did not import CUDA here)
247+
ENV["MLDATADEVICES_SILENCE_WARN_NO_GPU"] = 1
248+
```
249+
244250
``` julia
245251
using OptimizationOptimisers
246252
import Zygote
@@ -313,8 +319,7 @@ The HVI Problem needs to be updated with this new applicatior.
313319

314320
``` julia
315321
f_batch = PBMPopulationApplicator(f_doubleMM_sites, n_batch; θP, θM, θFix, xPvec=xP[:,1])
316-
f_allsites = PBMPopulationApplicator(f_doubleMM_sites, n_site; θP, θM, θFix, xPvec=xP[:,1])
317-
probo_sites = HybridProblem(probo; f_batch, f_allsites)
322+
probo_sites = HybridProblem(probo; f_batch)
318323
```
319324

320325
For numerical efficiency, the number of sites within one batch is part of the
@@ -345,8 +350,7 @@ module `Main` to allow for easier reloading with JLD2.
345350

346351
``` julia
347352
f_batch = PBMPopulationApplicator(DoubleMM.f_doubleMM_sites, n_batch; θP, θM, θFix, xPvec=xP[:,1])
348-
f_allsites = PBMPopulationApplicator(DoubleMM.f_doubleMM_sites, n_site; θP, θM, θFix, xPvec=xP[:,1])
349-
probo2 = HybridProblem(probo; f_batch, f_allsites)
353+
probo2 = HybridProblem(probo; f_batch)
350354
```
351355

352356
``` julia

docs/src/tutorials/basic_cpu.qmd

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ The example process based model (PBM) predicts a double-monod constrained rate
3636
for different substrate concentrations, `S1`, and `S2`.
3737

3838
$$
39-
y = r_0+ r_1 \frac{S_1}{K_1 + S_1} \frac{S_2}{K_2 + S_2}$$
39+
y = r_0+ r_1 \frac{S_1}{K_1 + S_1} \frac{S_2}{K_2 + S_2}
40+
$$
4041

4142
```{julia}
4243
function f_doubleMM(θc::CA.ComponentVector{ET}, x) where ET
@@ -58,7 +59,7 @@ access the components by its symbolic names in the provided `ComponentArray`.
5859
HVI requires the evaluation of the likelihood of the predictions.
5960
It corresponds to the cost of predictions given some observations.
6061

61-
The user specifies a function of the negative log-Likehood
62+
The user specifies a function of the negative log-Likelihood
6263
`neg_logden(obs, pred, uncertainty_parameters)`,
6364
where all of the parameters are arrays with columns for sites.
6465

@@ -239,10 +240,10 @@ given a vector of global parameters, and a matrix of site parameters to
239240
invocation of the process based model (PBM), defined at the beginning.
240241

241242
```{julia}
242-
f_batch = f_allsites = PBMSiteApplicator(f_doubleMM; θP, θM, θFix, xPvec=xP[:,1])
243+
f_batch = PBMSiteApplicator(f_doubleMM; θP, θM, θFix, xPvec=xP[:,1])
243244
244245
prob = HybridProblem(θP, θM, g_chain_scaled, ϕg0,
245-
f_batch, f_allsites, priors_dict, py,
246+
f_batch, priors_dict, py,
246247
transM, transP, train_dataloader, n_covar, n_site, n_batch)
247248
```
248249

@@ -265,7 +266,7 @@ y1 = f_batch(CA.getdata(θP), CA.getdata(θMs), CA.getdata(x_batch))[2]
265266
#using Cthulhu
266267
#@descend_code_warntype f_batch(CA.getdata(θP), CA.getdata(θMs), CA.getdata(x_batch))
267268
prob0 = HVI.DoubleMM.DoubleMMCase()
268-
f_batch0 = get_hybridproblem_PBmodel(prob0; use_all_sites = false)
269+
f_batch0 = get_hybridproblem_PBmodel(prob0)
269270
y1f = f_batch0(θP, θMs, x_batch)[2]
270271
y1 .- y1f # equal
271272
end
@@ -276,6 +277,11 @@ end
276277
Eventually, having assembled all the moving parts of the HVI, we can perform
277278
the inversion.
278279

280+
```{julia}
281+
# silence warning of no GPU backend found (because we did not import CUDA here)
282+
ENV["MLDATADEVICES_SILENCE_WARN_NO_GPU"] = 1
283+
```
284+
279285
```{julia}
280286
using OptimizationOptimisers
281287
import Zygote
@@ -349,8 +355,7 @@ The HVI Problem needs to be updated with this new applicatior.
349355

350356
```{julia}
351357
f_batch = PBMPopulationApplicator(f_doubleMM_sites, n_batch; θP, θM, θFix, xPvec=xP[:,1])
352-
f_allsites = PBMPopulationApplicator(f_doubleMM_sites, n_site; θP, θM, θFix, xPvec=xP[:,1])
353-
probo_sites = HybridProblem(probo; f_batch, f_allsites)
358+
probo_sites = HybridProblem(probo; f_batch)
354359
```
355360

356361
For numerical efficiency, the number of sites within one batch is part of the
@@ -380,8 +385,7 @@ module `Main` to allow for easier reloading with JLD2.
380385

381386
```{julia}
382387
f_batch = PBMPopulationApplicator(DoubleMM.f_doubleMM_sites, n_batch; θP, θM, θFix, xPvec=xP[:,1])
383-
f_allsites = PBMPopulationApplicator(DoubleMM.f_doubleMM_sites, n_site; θP, θM, θFix, xPvec=xP[:,1])
384-
probo2 = HybridProblem(probo; f_batch, f_allsites)
388+
probo2 = HybridProblem(probo; f_batch)
385389
```
386390

387391
```{julia}
@@ -397,4 +401,4 @@ end
397401
#| eval: false
398402
#| echo: false
399403
probo = load(fname, "probo"; iotype = IOStream);
400-
```
404+
```

docs/src/tutorials/blocks_corr.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# How to model indenpendent parameter-blocks in the posterior
1+
# How to model independent parameter-blocks in the posterior
22

33

44
``` @meta

docs/src/tutorials/blocks_corr.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "How to model indenpendent parameter-blocks in the posterior"
2+
title: "How to model independent parameter-blocks in the posterior"
33
engine: julia
44
execute:
55
echo: true
-8.38 KB
Loading

0 commit comments

Comments
 (0)