0 | module Control.Monad.Distribution
7 | import public Data.Tensor
8 | import Data.Container.Additive
21 | record Dist (a : Axis) where
24 | logits : Tensor [a] Double
29 | uniform : {a : Axis} -> (s : a.cont.Shp) -> Dist a
30 | uniform s = MkDist (fillAt (shapeToRank1Shape s) 0)
35 | diracDelta : {a : Axis} -> IsDecidable a.cont =>
36 | (s : a.cont.Shp) -> a.cont.Pos s -> Dist a
37 | diracDelta s p = MkDist $
fillAt (shapeToRank1Shape s) minusInfinity // [p] .~ 0
42 | uniform : {a : Axis} -> IsNaperian a => Dist a
43 | uniform @{MkIsNaperian _ _} = uniform ()
46 | diracDelta : {a : Axis} -> IsNaperian a => DecEq (Log a) => Log a -> Dist a
47 | diracDelta @{MkIsNaperian _ _} p = diracDelta () p
54 | Dist a = (d : Dist a) !> a.cont.Pos d.logits.extractShapeRank1
57 | uniformLens : {a : Axis} -> a.cont =%> Cont.Dist a
58 | uniformLens = !% \s => (
uniform s ** id)
70 | Simplex : Axis -> AddCont
71 | Simplex a = MkAddCont
73 | (
Tensor [a.name ~> (a.cont) `At` d.logits.extractShapeRank1] Double ** numIsMonoid)
)
78 | AllDisplay2D [a] Double =>
79 | IsFoldable a.cont =>
80 | AllAlgebra [a] Double =>
81 | TensorCubEvidence [a] =>
82 | Display2D (Dist a) where
83 | display2D (MkDist xs) = display2D (softargmaxImpl xs)
87 | AllDisplay2D [a] Double =>
88 | IsFoldable a.cont =>
89 | AllAlgebra [a] Double =>
90 | TensorCubEvidence [a] =>
92 | show = showViaDisplay2D
99 | ProbabilisticChoice : (a : Axis) -> a.cont.filledWith AddCont -> AddCont
100 | ProbabilisticChoice a branches
101 | = Simplex (a `At` (shapeExt branches)) >*< Section (index branches)
107 | pendingChoice : {a : Axis} ->
108 | (branches : a.cont.filledWith AddCont) ->
109 | ProbabilisticChoice a branches =%+> Dist a >-+@ AddContDPair (index branches)
110 | pendingChoice branches =
111 | let Choice : AddCont
112 | Choice = Simplex (a `At` (shapeExt branches))
113 | in (id {c = Choice} >*< graph) %+>> distribute
114 | {c = Simplex (a `At` (shapeExt branches)),
115 | g = AddContDPair (index branches)} (\d => !% \_ =>
116 | (
MkDist (MkT ((shapeExt branches <| const ()) <| \pp =>
117 | index (GetT d.logits) (
fst pp ** ())
)) ** id)
)