Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/02/19 07:44:35 (5 years ago)
Author:
gkronber
Message:

#2925: fixed a bug in AutoDiff code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Problem.cs

    r16999 r17001  
    238238      InitAllParameters();
    239239
     240      // TODO: alglib gradient check fails for integration autodiff
     241      // TODO: implement CVODES solver and use gradient check in alglib to verify
    240242      // TODO: optimization of starting values for latent variables in CVODES solver
    241243      // TODO: allow to specify the name for the time variable in the dataset and allow variable step-sizes
     
    446448            alglib.minlmsetbc(state, lowerBounds, upperBounds);
    447449#if DEBUG
    448             //alglib.minlmsetgradientcheck(state, 1.0e-7);
     450            alglib.minlmsetgradientcheck(state, 1.0e-7);
    449451#endif
    450452            alglib.minlmoptimize(state, EvaluateObjectiveVector, EvaluateObjectiveVectorAndJacobian, null, myState);
     
    16671669        for (int i = 1; i < node.SubtreeCount; i++) {
    16681670          InterpretRec(node.GetSubtree(i), nodeValues, out g, out dg);
    1669           f = f * g;
     1671          var newF = f * g;
    16701672          df = df.Scale(g).Add(dg.Scale(f));  // f'*g + f*g'
     1673
     1674          f = newF;
    16711675        }
    16721676        z = f;
     
    17351739        InterpretRec(node.GetSubtree(0), nodeValues, out f, out df);
    17361740        z = Math.Exp(f);
    1737         dz = df.Scale(Math.Exp(f));
     1741        dz = df.Scale(z);
    17381742      } else if (node.Symbol is Logarithm) {
    17391743        Assert(node.SubtreeCount == 1);
     
    21482152      public Tuple<double, Vector> VariableNodeValueAndGradient(VariableTreeNode node) {
    21492153        // (f*g)' = (f'*g)+(g'*f)       
    2150         var g = node2val[node];
    2151         var f = variableValues[node.VariableName];
     2154        var w = node2val[node];
     2155        var x = variableValues[node.VariableName];
    21522156
    21532157        return Tuple.Create(
    2154           g.Item1 * f.Item1,
    2155           Vector.CreateNew(f.Item2).Scale(g.Item1).Add(Vector.CreateNew(g.Item2).Scale(f.Item1)));
     2158          w.Item1 * x.Item1,
     2159          x.Item2 == Vector.Zero ? Vector.CreateNew(w.Item2).Scale(x.Item1) :
     2160          Vector.CreateNew(w.Item2).Scale(x.Item1).Add(Vector.CreateNew(x.Item2).Scale(w.Item1)));
    21562161      }
    21572162
     
    21712176          }
    21722177        } else {
     2178          throw new NotImplementedException();
    21732179          // variable gradient means we want to calculate the gradient over the target variables instead of parameters
    21742180          for (int paramIdx = 0; paramIdx < leafNodes.Length; paramIdx++) {
Note: See TracChangeset for help on using the changeset viewer.