Skip to content

values_as(vi, NamedTuple) doesn't preserve structure #814

@penelopeysm

Description

@penelopeysm
julia> using DynamicPPL, Distributions

julia> @model function f()
           x = (a=1, b=2)
           x.a ~ Normal()
           x.b ~ Normal()
       end
f (generic function with 2 methods)

julia> values_as(VarInfo(f()), NamedTuple)
(var"x.a" = -0.2067696145335533, var"x.b" = -2.788035859751896)

I would have liked (x = (a = ..., b = ...),).

Same thing happens with arrays:

julia> @model function f()
           x = [0.0, 1.0]
           x[1] ~ Normal()
           x[2] ~ Normal()
       end
f (generic function with 2 methods)

julia> values_as(VarInfo(f()), NamedTuple)
(var"x[1]" = -0.9522900124219256, var"x[2]" = 0.626145525096277)

I do realise this is a very non-trivial thing to fix, because of the general problem that we can't reconstruct shape from a list of varnames; but I feel like it's at least worth having a(nother?) think about it. In particular, I'm still wondering about having varnames carry info about their shape, or maybe the varinfo can even maintain a full mapping of variables that are in scope along with their values.

I suppose there are some pathological cases that can break this very idealised situation.

@model function f()
     x = [0.0, 1.0, 2.0]
     x[1] ~ Normal()
     x[3] ~ Normal()
end

values_as(VarInfo(f()), NamedTuple)
# how many entries of x should this return?

See also the last time I suggested it here #712 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions