Handle fixing and conditioning together

Aperta
#1,012 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

  • #1468 di @yebai — chiusa senza merge
  • #1501 di @yebai — chiusa senza merge

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
38/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
julia
Ambito
compilers

Direzione di ricerca

Inizia dalle righe 448-461 di src/compiler.jl e segui la logica getconditioned/getfixed per le operazioni annidate di condition e fix. Esamina il test di regressione previsto in test/submodel.jl e la rappresentazione esistente di CondFixContext. Il lavoro è completato quando l'operazione di conditioning o fixing più esterna ha la precedenza in modo coerente per tutte e quattro le coppie di operazioni, con test che coprono questo comportamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement

Consider:

using DynamicPPL, Distributions

for op1 in [condition, fix]
    for op2 in [condition, fix]
        @model g() = x ~ Normal()
        g_op = op1(g(), (; x = 1.0))
        @model f() = a ~ to_submodel(g_op)
        f_op = op2(f(), (@varname(a.x) => 2.0))
        println("$op1 $op2 => $(f_op())")
    end
end

The result (last tested on DynamicPPL 0.41):

condition condition => 2.0
condition fix => 2.0
fix condition => 1.0
fix fix => 2.0

For fixfix and concon, there's no issue because the values are merged in a way that respects the outer. (it's somewhere in getconditioned / getfixed I think)

For confix and fixcon, unfortunately, the logic in the codebase says that fixing always takes precedence over conditioning, because fix is in the first branch and condition is in the last branch

https://github.com/TuringLang/DynamicPPL.jl/blob/0b7213f071cb27ca80ee8b6d4fee2bae8fdec693/src/compiler.jl#L448-L461

I discovered while writing tests for nested condition/fix. This was the test I had intended to add to test/submodel.jl.

    @testset "precedence for conditioning/fixing the same variable twice" begin
        # Check that the outermost conditioning takes precedence. I'm not sure
        # if there is a strong a priori reason for this, but it is currently
        # the case and thus this test just ensures that the behaviour doesn't
        # suddenly change without it being a conscious decision to do so.
        @testset "$op1,$op2" for op1 in [condition, fix], op2 in [condition, fix]
            @model function f()
                x ~ Normal()
            end
            fcond = op1(f(), (; x=1.0)) # this is ignored
            @model function g()
                return a ~ to_submodel(fcond)
            end
            gcond = op2(g(), (@varname(a.x) => 2.0)) # this takes precedence
            @test gcond() == 2.0
        end
    end

How to solve?

Right now, conditioning and fixing go via what's now called CondFixContext. However, the conditioned values and fixed values are still stored in separate 'layers' of this.

The way to prevent this would be to 'flatten' the layers such that conditioned variables and fixed variables are part of the same VarNamedTuple. They would be held inside a VNT together with a flag that indicates whether it's conditioned or fixed. Thus instead of having a data structure that holds two VNTs, one conditioned and one fixed, we would only have one VNT that holds both.

We could then use templated_setindex_no_overwrite!! or something similar to issue an error when attempting to fix a variable that is already conditioned.

Lingua principale
Julia
Stelle
291
Fork
43
Merge medio
1g 23h
PR unite (30g)
15

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TuringLang/DynamicPPL.jl

Tutte le issue di TuringLang/DynamicPPL.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.