0 | module Control.Monad.Distribution
7 | import public Data.Tensor
8 | import Data.Container.Additive
20 | record Dist (name : AxisName) (i : Nat) where
23 | logits : Tensor [name ~~> i] Double
27 | uniform : {name : AxisName} -> {i : Nat} ->
28 | (isSucc : IsSucc i) => Dist name i
29 | uniform = MkDist (fill 0)
34 | diracDelta : {name : AxisName} -> {i : Nat} ->
36 | (j : Fin i) -> Dist name i
37 | diracDelta @{ItIsSucc {n}} j
38 | = MkDist (># (insertAt j 0 (replicate n minusInfinity)))
44 | Dist : AxisName -> Nat -> Cont
45 | Dist name n = Const2 (Dist name n) (Fin n)
56 | Simplex : AxisName -> Nat -> AddCont
57 | Simplex name n = Const2 (Dist name n)
58 | (
Tensor [name ~~> n] Double ** numIsMonoid)
62 | {axisName : AxisName} -> {i : Nat} -> Show (Dist axisName i) where
63 | show (MkDist xs) = show (softargmaxImpl xs)
69 | ProbabilisticChoice : {n : Nat} -> (distName : AxisName) ->
70 | (branches : Vect n AddCont) -> AddCont
71 | ProbabilisticChoice distName branches
72 | = Simplex distName n >*< (Vect n >-+@ Coproduct branches)
76 | ChoiceMade : {n : Nat} -> (distName : AxisName) ->
77 | (branches : Vect n AddCont) -> AddCont
78 | ChoiceMade distName branches = Simplex distName n >*< Coproduct branches
84 | resolveByLabel : {distName : AxisName} ->
85 | {branches : Vect n AddCont} ->
86 | ProbabilisticChoice distName branches >*< ChoiceMade distName branches
87 | =%+> ChoiceMade distName branches >*< ChoiceMade distName branches
88 | resolveByLabel = !%+ \((dist, ex), y@(distTrue, (
iTrue ** _)
)) =>
89 | (
((dist, index ex iTrue), y) ** \((d', g'), yGrad) =>
90 | ((d', MkBag [(
iTrue ** g')
]), yGrad))