locked
switch should be placed outside 'for' loop

    Question

  • I am continuing building the Markov Mixture model. I realized that for the start state, I want to keep a single Variable.Discrete variable that will be shared by all sequences. When I make this change, I get an error message:

    Compiling model...compilation failed.
    
    Unhandled Exception:
    MicrosoftResearch.Infer.CompilationFailedException: GateTransform failed with 1 error(s) and 0 warning(s):
    Error 0: 'switch(ZeroState)' should be placed outside 'for(N)' in
    if (ZeroState==K) {
    	States[N][0] = Factor.Discrete(CPTTrans[K]);
    }
    
    
      at MicrosoftResearch.Infer.ModelCompiler.GetTransformedDeclaration (MicrosoftResearch.Transforms.CodeModel.Interfaces.ITypeDeclaration itd, System.Reflection.MethodBase method, MicrosoftResearch.Transforms.AttributeRegistry`2[TObject,TAttribute] inputAttributes) [0x00155] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.ModelCompiler.CompileWithoutParams (MicrosoftResearch.Transforms.CodeModel.Interfaces.ITypeDeclaration itd, System.Reflection.MethodBase method, MicrosoftResearch.Transforms.AttributeRegistry`2[TObject,TAttribute] inputAttributes) [0x0000c] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.InferenceEngine.Compile () [0x000b5] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.InferenceEngine.BuildAndCompile (System.Boolean inferOnlySpecifiedVars, System.Collections.Generic.IEnumerable`1[T] vars) [0x000e1] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.InferenceEngine.GetCompiledInferenceAlgorithm (System.Boolean inferOnlySpecifiedVars, MicrosoftResearch.Infer.Models.IVariable var) [0x00017] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.InferenceEngine.InferAll (System.Boolean inferOnlySpecifiedVars, MicrosoftResearch.Infer.Models.IVariable var) [0x00000] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.InferenceEngine.Infer[TReturn] (MicrosoftResearch.Infer.Models.IVariable var) [0x00000] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MarkovMixtureModel.MarkovMixtureModel.InferPosteriors () [0x00001] in /Users/vlad/Projects/MarkovMixtureModel/MarkovMixtureModel/MarkovMixtureModel.cs:110 
      at MarkovMixtureModel.Program.TestMarkovMixtureModel () [0x00104] in /Users/vlad/Projects/MarkovMixtureModel/MarkovMixtureModel/Program.cs:51 
      at MarkovMixtureModel.Program.Main (System.String[] args) [0x00001] in /Users/vlad/Projects/MarkovMixtureModel/MarkovMixtureModel/Program.cs:15 
    [ERROR] FATAL UNHANDLED EXCEPTION: MicrosoftResearch.Infer.CompilationFailedException: GateTransform failed with 1 error(s) and 0 warning(s):
    Error 0: 'switch(ZeroState)' should be placed outside 'for(N)' in
    if (ZeroState==K) {
    	States[N][0] = Factor.Discrete(CPTTrans[K]);
    }
    
    
      at MicrosoftResearch.Infer.ModelCompiler.GetTransformedDeclaration (MicrosoftResearch.Transforms.CodeModel.Interfaces.ITypeDeclaration itd, System.Reflection.MethodBase method, MicrosoftResearch.Transforms.AttributeRegistry`2[TObject,TAttribute] inputAttributes) [0x00155] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.ModelCompiler.CompileWithoutParams (MicrosoftResearch.Transforms.CodeModel.Interfaces.ITypeDeclaration itd, System.Reflection.MethodBase method, MicrosoftResearch.Transforms.AttributeRegistry`2[TObject,TAttribute] inputAttributes) [0x0000c] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.InferenceEngine.Compile () [0x000b5] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.InferenceEngine.BuildAndCompile (System.Boolean inferOnlySpecifiedVars, System.Collections.Generic.IEnumerable`1[T] vars) [0x000e1] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.InferenceEngine.GetCompiledInferenceAlgorithm (System.Boolean inferOnlySpecifiedVars, MicrosoftResearch.Infer.Models.IVariable var) [0x00017] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.InferenceEngine.InferAll (System.Boolean inferOnlySpecifiedVars, MicrosoftResearch.Infer.Models.IVariable var) [0x00000] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MicrosoftResearch.Infer.InferenceEngine.Infer[TReturn] (MicrosoftResearch.Infer.Models.IVariable var) [0x00000] in <1c09b726d63a41118a8cecdae81e3110>:0 
      at MarkovMixtureModel.MarkovMixtureModel.InferPosteriors () [0x00001] in /Users/vlad/Projects/MarkovMixtureModel/MarkovMixtureModel/MarkovMixtureModel.cs:110 
      at MarkovMixtureModel.Program.TestMarkovMixtureModel () [0x00104] in /Users/vlad/Projects/MarkovMixtureModel/MarkovMixtureModel/Program.cs:51 
      at MarkovMixtureModel.Program.Main (System.String[] args) [0x00001] in /Users/vlad/Projects/MarkovMixtureModel/MarkovMixtureModel/Program.cs:15 
    

    My model currently is:

    // define primary model variables -- zero state
    ZeroState = Variable.Discrete(ProbInit).Named("ZeroState");
    
    // define primary model variables -- actual states
    States = Variable.Array(Variable.Array<int>(T), N).Named("States");
    
    using (Variable.ForEach(N))
    {
      using (var block = Variable.ForEach(T))
      {
        var t = block.Index;
        var previousState = States[N][t - 1];
    
        using (Variable.If(t == 0))
        {
           using (Variable.Switch(ZeroState))
             States[N][T].SetTo(Variable.Discrete(CPTTrans[ZeroState]));
        }
    
        using (Variable.If(t > 0))
        {
          using (Variable.Switch(previousState))
            States[N][T].SetTo(Variable.Discrete(CPTTrans[previousState]));
        }
      }
    }
    

    I have tried to put .ForEach(N) at relevant places, also the Variable.ForEach(N) blocks. To no avail. It looks like I am stuck.

    Tuesday, March 6, 2018 8:01 AM

Answers

  •             using (Variable.Switch(ZeroState))
                using (Variable.ForEach(N))
                {
                    using (var block = Variable.ForEach(T))
                    {
                        var t = block.Index;
    
                        using (Variable.If(t == 0))
                        {
                            States[N][T].SetTo(Variable.Discrete(CPTTrans[ZeroState]));
                        }
                    }
                }
    
                using (Variable.ForEach(N))
                {
                    using (var block = Variable.ForEach(T))
                    {
                        var t = block.Index;
                        var previousState = States[N][t - 1];
    
                        using (Variable.If(t > 0))
                        {
                            using (Variable.Switch(previousState))
                                States[N][T].SetTo(Variable.Discrete(CPTTrans[previousState]));
                        }
                    }
                }
    

    • Marked as answer by usptact Tuesday, March 6, 2018 5:45 PM
    Tuesday, March 6, 2018 2:47 PM
    Owner

All replies

  •             using (Variable.Switch(ZeroState))
                using (Variable.ForEach(N))
                {
                    using (var block = Variable.ForEach(T))
                    {
                        var t = block.Index;
    
                        using (Variable.If(t == 0))
                        {
                            States[N][T].SetTo(Variable.Discrete(CPTTrans[ZeroState]));
                        }
                    }
                }
    
                using (Variable.ForEach(N))
                {
                    using (var block = Variable.ForEach(T))
                    {
                        var t = block.Index;
                        var previousState = States[N][t - 1];
    
                        using (Variable.If(t > 0))
                        {
                            using (Variable.Switch(previousState))
                                States[N][T].SetTo(Variable.Discrete(CPTTrans[previousState]));
                        }
                    }
                }
    

    • Marked as answer by usptact Tuesday, March 6, 2018 5:45 PM
    Tuesday, March 6, 2018 2:47 PM
    Owner
  • Thank you, Tom! I found the issue with my model. The initial states are modeled using "ProbInit", the transition matrix is not used. I was not careful enough.

    The model now looks like this:

    using (Variable.ForEach(N))
    {
      using (var block = Variable.ForEach(T))
      {
        var t = block.Index;
    
        using (Variable.If(t == 0))
          States[N][T].SetTo(Variable.Discrete(ProbInit));
    
        var previousState = States[N][t - 1];
    
        using (Variable.If(t > 0))
          using (Variable.Switch(previousState))
            States[N][T].SetTo(Variable.Discrete(CPTTrans[previousState]));
      }
    }

    All model parameters are getting recovered successfully. Now to the next step - add clusters.

    Tuesday, March 6, 2018 5:12 PM