locked
GetCompiledInferenceAlgorithm always returns the same object? RRS feed

  • Question

  • I would like to do 

    • To run multi-threaded inference. Including the compiled algorithm directly allows different instances of the compiled algorithm to be used in each thread separately without recompilation.

    I am wondering if I can achieve that using GetCompiledInferenceAlgorithm, since I found that it is tedious to compile a model.cs first, and then reference it to create a number of instances of the compiled algorithm.

    However, on my experiment, it seems GetCompiledInferenceAlgorithm always returns the same instance, which means I am not able to use that for muti threading?

    E.g.

        let m = Variable.New<double>()
        let mu = Variable.GaussianFromMeanAndVariance(m, 2.0)
        let a = Variable.GaussianFromMeanAndVariance(mu, 1.0)
        m.ObservedValue <- 2.0

        for i in 1..10 do 
            let ca = engine.GetCompiledInferenceAlgorithm(a)
            ca.SetObservedValue(m.NameInGeneratedCode, 1.0)
            ca.Execute(50)
            printfn "%A" (ca.Marginal(a.NameInGeneratedCode))

        let ca = engine.GetCompiledInferenceAlgorithm(a)
        let ca2 = engine.GetCompiledInferenceAlgorithm(a)
        ca.SetObservedValue(m.NameInGeneratedCode, 1.0)
        ca2.SetObservedValue(m.NameInGeneratedCode, 3.0)
        ca.Execute(50)
        printfn "%A" (ca.Marginal(a.NameInGeneratedCode))   

    Setting the observed value for a variable in ca2 actually changes the observedValue in ca.

    • Edited by colinfang Monday, November 10, 2014 6:41 PM
    Monday, November 10, 2014 6:32 PM

Answers

  • You do not have to name variables to use Model.cs.  Also, if you want another instance of something you can always use the Activator class in .NET.
    • Marked as answer by colinfang Tuesday, November 11, 2014 4:38 PM
    Tuesday, November 11, 2014 3:43 PM
    Owner

All replies

  • Yes, it always returns the same instance.
    Tuesday, November 11, 2014 2:33 PM
    Owner
  • If I want to do multithreading, do I have to  take the generated output code Model.cs so that I can get multiple instance of the algorithm?


    which also means I have to name all variables?
    • Edited by colinfang Tuesday, November 11, 2014 3:31 PM
    Tuesday, November 11, 2014 3:12 PM
  • You do not have to name variables to use Model.cs.  Also, if you want another instance of something you can always use the Activator class in .NET.
    • Marked as answer by colinfang Tuesday, November 11, 2014 4:38 PM
    Tuesday, November 11, 2014 3:43 PM
    Owner
  • ok, i see. I will use GetCompiledInferenceAlgorithm(...).GetType() together with Activator.GetInstance(...) then cast back to  IGeneratedAlgorithm
    Tuesday, November 11, 2014 4:38 PM