/// <summary> /// Automatic differentiation using reverse accumulation /// </summary> /// <param name="cost"></param> public void Differentiate(Op <T> cost) { if (!this._derivativeComputed) { this.Cost = cost; cost.Derivate = this._cns.Const(ConvNetSharp <T> .One, "1"); //this._func.Derivate = cost; var differentiateVisitor = new DifferentiateVisitor <T>(); cost.Accept(differentiateVisitor); this._derivativeComputed = true; } }
/// <summary> /// Automatic differentiation using reverse accumulation /// </summary> /// <param name="cost"></param> /// <param name="gradient">1 will be used as gradient if not specify</param> public void Differentiate(Op <T> cost, Op <T> gradient = null) { if (!this._derivativeComputed) { var visitor = new OpVisitor <T>(op => { op.Derivate = null; }); cost.Accept(visitor); this.Cost = cost; cost.Derivate = gradient ?? ConvNetSharp <T> .One; var differentiateVisitor = new DifferentiateVisitor <T>(); cost.Accept(differentiateVisitor); this._derivativeComputed = true; } }