locked
Variable.Sum -> Provided array of length 12 when length 7 was expected for variable 'vdouble__5' RRS feed

  • Question

  • Hi

    I have a piece of code as below. I have to compute two weighted sums, and the sum of these two would be a new variable which I need - I call it totalSum in my code.

    When I run it, the model compiles successfully but then I get this error:

    Provided array of length 12 when length 7 was expected for variable 'vdouble__5'

    Can you give me any clue why this happens?

    PertData variable has 12 elements. I don't understand what can be wrong with this. The result of Variable.Sum is just a single variable. I should be able to add these two weighted sums...

    Thanks a lot. I appreciate any help.

    for (int layer = 0; layer < NumLayers.ObservedValue - 1; layer++ )
                {
                    using (ForEachBlock firstBlock = Variable.ForEach(geneRange))
                    {
                        Variable<double> selfEffect = -(alpha[geneRange] * timeSeriesData[layer][geneRange]);
                        VariableArray<double> weightSumArray = Variable.Array<double>(geneWeightRange);//.Named("weightSum");
                        genesubarray = Variable.Subarray(timeSeriesData[layer], indicesArray[geneRange]);
                        weightSumArray[geneWeightRange] = w[geneRange][geneWeightRange] * genesubarray[geneWeightRange];//genesT1[geneRange2]
                        // new code for perturbation effect
                        perturbEffectSubarray = Variable.Subarray(pertData, pertIndices[geneRange]);
                        VariableArray<double> perturbationEffect = Variable.Array<double>(perturbRange);//.Named("perturbationEffect"+geneRange); 
                        perturbationEffect[perturbRange] = genePertW[geneRange][perturbRange] * perturbEffectSubarray[perturbRange];
                        Variable<double> totalSum = Variable.Sum(perturbationEffect) + Variable.Sum(weightSumArray); //!!!! error in this line
                        // end of perturbation effect
                        Variable<double> logisticEffect = Variable.New<double>().Named("logisticEffect"+layer);
                        using (Variable.If(totlSum > 0.5)) { logisticEffect.SetTo(Variable.GaussianFromMeanAndPrecision(1, 1)); }
                        using (Variable.IfNot(totalSum > 0.5)) logisticEffect.ObservedValue = 0;   //.SetTo(Variable.Constant<double>(0));//
                        Variable<double> othersEffect = beta[geneRange] * (logisticEffect);
                        
                        timeSeriesData[layer+1][geneRange] = selfEffect + othersEffect;
                    }
                }//for each time layer
    Tuesday, August 5, 2014 11:00 PM

Answers

  • This means that one of your observed values is the wrong length.  If you name your array variables, then the error message will tell you which array is the wrong size.
    • Marked as answer by RazinR Sunday, August 10, 2014 6:02 PM
    Wednesday, August 6, 2014 1:40 PM
    Owner
  • When you say 'compiles' you are presumably meaning the C# compiler not the model compiler.

    The message you are seeing looks like a model compiler message, so it will occur when the model compiles the first time you do inference, not when the model is being built in the modelling API - e.g. at the line you identify. So it seems unlikely that this is to do with the Sum line.

    Can you give names to all the variables  (with .Named()) including the Variables formed by each Sum, so we can see which one variable is the offending one? A small running test program with dummy data would help.

    Thanks

    John

    • Marked as answer by RazinR Friday, August 8, 2014 10:13 AM
    Wednesday, August 6, 2014 1:46 PM
    Owner
  • Thanks a lot John :-)

    Oh I should have wrote the same test program myself. Excuse me for being careless.

    My problem is solved.

    The reason that I get this error after getting "Compiling model... done." message, was that the wrong index problem happened in data observation. So the model in fact didn't have any problem, but the size of an array in model didn't match the size of the array that I observed for that matrix. I had a two dimensional matrix. I changed my code with like this:

    Range pertRange = new Range(perturbations.Length);

    to:

    Range pertRange = new Range(perturbations[0].Length);

    And now it is working.

    Naming of variables absolutely helped. Thanks a lot :) I'm so glad the model is working now! :-)



    • Marked as answer by RazinR Friday, August 8, 2014 10:12 AM
    • Edited by RazinR Friday, August 8, 2014 10:14 AM
    Thursday, August 7, 2014 10:41 AM

All replies

  • This means that one of your observed values is the wrong length.  If you name your array variables, then the error message will tell you which array is the wrong size.
    • Marked as answer by RazinR Sunday, August 10, 2014 6:02 PM
    Wednesday, August 6, 2014 1:40 PM
    Owner
  • When you say 'compiles' you are presumably meaning the C# compiler not the model compiler.

    The message you are seeing looks like a model compiler message, so it will occur when the model compiles the first time you do inference, not when the model is being built in the modelling API - e.g. at the line you identify. So it seems unlikely that this is to do with the Sum line.

    Can you give names to all the variables  (with .Named()) including the Variables formed by each Sum, so we can see which one variable is the offending one? A small running test program with dummy data would help.

    Thanks

    John

    • Marked as answer by RazinR Friday, August 8, 2014 10:13 AM
    Wednesday, August 6, 2014 1:46 PM
    Owner
  • Thanks for replying my question.

    I tried to name the variable but it said I have the same name for several variable. I think I should have named each element of arrays separately. I will do that. By compiling I mean this message: "Compiling model... done." which appears when I debug the project.

    Any way I have two Array.Sum over two different arrays with different lengths. and I try to add these sums into one variable, for example if my variable arrays are a and b I have:

    Variable<double> totalsum = Variable.sum(a) + Variable.sum(b);

    If I use just one of these sums, everything is just fine and I get the result. But now summing these two Variable.sum() is problematic. I don't understand why. since each of these sums gives a single double variable. I don't understand when it says they have different sizes.

    I don't have access to my code right now. But I check this as soon as possible and will post the result here.

    Thanks a lot :)


    • Edited by RazinR Wednesday, August 6, 2014 7:39 PM
    Wednesday, August 6, 2014 3:00 PM
  • You have a .NET loop over variables - so each time you go through the loop you are adding a different variable. So you must provide a different name for each loop index - for example:

    .Named("weight" + layer);

    The following program works for me:

    Range i = new Range(2);
    Range j = new Range(3);
    var a = Variable.Array<double>(i);
    var b = Variable.Array<double>(j);
    a[i] = Variable.GaussianFromMeanAndPrecision(0, 1).ForEach(i);
    b[j] = Variable.GaussianFromMeanAndPrecision(2, 3).ForEach(j);
    var c = Variable.Sum(a) + Variable.Sum(b);
    var engine = new InferenceEngine();
    Console.WriteLine(engine.Infer(c));

    Thursday, August 7, 2014 9:01 AM
    Owner
  • Thanks a lot John :-)

    Oh I should have wrote the same test program myself. Excuse me for being careless.

    My problem is solved.

    The reason that I get this error after getting "Compiling model... done." message, was that the wrong index problem happened in data observation. So the model in fact didn't have any problem, but the size of an array in model didn't match the size of the array that I observed for that matrix. I had a two dimensional matrix. I changed my code with like this:

    Range pertRange = new Range(perturbations.Length);

    to:

    Range pertRange = new Range(perturbations[0].Length);

    And now it is working.

    Naming of variables absolutely helped. Thanks a lot :) I'm so glad the model is working now! :-)



    • Marked as answer by RazinR Friday, August 8, 2014 10:12 AM
    • Edited by RazinR Friday, August 8, 2014 10:14 AM
    Thursday, August 7, 2014 10:41 AM