Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

values_as_in_model doesn't pick up prefixes #788

Closed
penelopeysm opened this issue Jan 25, 2025 · 1 comment · Fixed by #787
Closed

values_as_in_model doesn't pick up prefixes #788

penelopeysm opened this issue Jan 25, 2025 · 1 comment · Fixed by #787
Labels
bug Something isn't working high priority

Comments

@penelopeysm
Copy link
Member

penelopeysm commented Jan 25, 2025

MWE

using DynamicPPL, Distributions

@model function submodel()
  x ~ Normal(0, 1)
  return x
end

@model function parentmodel()
  x1 ~ to_submodel(prefix(submodel(), :a), false)
  x2 ~ to_submodel(prefix(submodel(), :b), false)
  return x1 + x2
end

m = parentmodel()
vi = VarInfo(m)
values_as_in_model(m, false, vi)
# OrderedDict{Any, Any} with 1 entry:
#   x => -1.28582

This leads to the following issue when sampling in Turing:

using Turing

@model function submodel()
  x ~ Normal(0, 1)
  return x
end

@model function parentmodel()
  x1 ~ to_submodel(DynamicPPL.prefix(submodel(), :a), false)
  x2 ~ to_submodel(DynamicPPL.prefix(submodel(), :b), false)
  return x1 + x2
end

sample(parentmodel(), NUTS(), 1000; check_model=false)
┌ Info: Found initial step size
└   ϵ = 3.2
Sampling 100%|████████████████████████████████████████████████████████████████████| Time: 0:00:04
Chains MCMC chain (1000×13×1 Array{Float64, 3}):

Iterations        = 501:1:1500
Number of chains  = 1
Samples per chain = 1000
Wall duration     = 5.01 seconds
Compute duration  = 5.01 seconds
parameters        = x
internals         = lp, n_steps, is_accept, acceptance_rate, log_density, hamiltonian_energy, hamiltonian_energy_error, max_hamiltonian_energy_error, tree_depth, numerical_error, step_size, nom_step_size

Summary Statistics
  parameters      mean       std      mcse    ess_bulk   ess_tail      rhat   ess_per_sec
      Symbol   Float64   Float64   Float64     Float64    Float64   Float64       Float64

           x    0.0263    0.9781    0.0294   1095.9498   817.8880    1.0003      218.6652

Quantiles
  parameters      2.5%     25.0%     50.0%     75.0%     97.5%
      Symbol   Float64   Float64   Float64   Float64   Float64

           x   -1.8107   -0.6148    0.0471    0.6746    1.9686
@penelopeysm
Copy link
Member Author

penelopeysm commented Jan 25, 2025

Note that this doesn't happen with the old syntax

using DynamicPPL, Distributions

@model function submodel()
  x ~ Normal(0, 1)
  return x
end

@model function parentmodel2()
  x1 = @submodel prefix="a" submodel()
  x2 = @submodel prefix="b" submodel()
  return x1 + x2
end

m2 = parentmodel2()
vi2 = VarInfo(m2)
values_as_in_model(m2, false, vi2)
OrderedDict{Any, Any} with 2 entries:
  a.x => 0.947846
  b.x => 1.14385

@penelopeysm penelopeysm transferred this issue from TuringLang/Turing.jl Jan 25, 2025
@penelopeysm penelopeysm changed the title Submodel prefixes don't appear in sampled chain values_as_in_model doesn't pick up prefixes Jan 25, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working high priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant