You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
F# builders for simpler stochastic block syntax (DSL)
I would like to share some helpers used in my F# scripts, either as a
suggestion for Infer.NET FSharpWrapper.fs or just a tip for F# folks.
The below F# module defines block builders for stochastic If, IfNot, Case, Switch, ForEach, Repeat.
The module
[<AutoOpen>]moduleInferNetBlocksopenMicrosoft.ML.Probabilistic.Models/// The base builder literally wrapping a code block./// Derived builders have custom overrides, e.g. Run.typeBlockBuilder()=member__.Delay(f)=
f
member__.Run(f)=
f ()member__.Zero()=()member__.Return(x)=
x
member__.Combine(a,b)=
b a
member__.For(sequence,body)=for item in sequence do
body item
member__.While(guard,body)=while guard ()do
body ()member__.TryWith(body,handler)=try body ()with exn -> handler exn
member__.TryFinally(body,compensation)=try body ()finally compensation ()member__.Using(disposable:#System.IDisposable,body)=try body disposable
finallyifnot(isNull disposable)then disposable.Dispose ()/// Creates a stochastic if statement when the variable is true./// Example: If variable { ... }typeIf(variable)=inherit BlockBuilder ()member__.Run(f)=use __ = Variable.If variable
f ()/// Creates a stochastic if statement when the variable is false./// Example: IfNot variable { ... }typeIfNot(variable)=inherit BlockBuilder ()member__.Run(f)=use __ = Variable.IfNot variable
f ()/// Creates a stochastic case statement when the integer variable has the specified value./// Example: Case (variable, value) { ... }typeCase(variable,value)=inherit BlockBuilder ()member__.Run(f)=use __ = Variable.Case (variable, value)
f ()/// Creates a stochastic switch statement using the specified condition variable./// Example: Switch variable { ... }typeSwitch(variable)=inherit BlockBuilder ()member__.Run(f)=use __ = Variable.Switch variable
f ()/// Creates a 'for each' block./// Example: ForEach range { ... }typeForEach(range)=inherit BlockBuilder ()member__.Run(f)=use __ = Variable.ForEach range
f ()/// Creates a 'repeat' block./// Example: Repeat count { ... }typeRepeat(count)=inherit BlockBuilder ()member__.Run(f)=use __ = Variable.Repeat count
f ()
Examples with stochastic blocks
From my version of causality example (ForEach, If, IfNot):
// Loop over the data points
ForEach N {
If doB.[N]{
B.[N]<- Variable.Bernoulli probBIntervention
}}// First model: A causes B
If AcausesB {
ForEach N {
A.[N]<- Variable.Bernoulli 0.5
IfNot doB.[N]{
B.[N]<- Variable.NotEqual (A.[N], Variable.Bernoulli q)}}}// Second model: B causes A
IfNot AcausesB {
ForEach N {
IfNot doB.[N]{
B.[N]<- Variable.Bernoulli 0.5}
A.[N]<- Variable.NotEqual (B.[N], Variable.Bernoulli q)}}
From my version of Gaussian mixture example (ForEach, Switch):
F# builders for simpler stochastic block syntax (DSL)
I would like to share some helpers used in my F# scripts, either as a
suggestion for Infer.NET FSharpWrapper.fs or just a tip for F# folks.
The below F# module defines block builders for stochastic
If
,IfNot
,Case
,Switch
,ForEach
,Repeat
.The module
Examples with stochastic blocks
From my version of causality example (
ForEach
,If
,IfNot
):From my version of Gaussian mixture example (
ForEach
,Switch
):From my version of "A Murder Mystery" (
Case
):The text was updated successfully, but these errors were encountered: