递归函数中的基础情况未能停止递归 TypeScript

我正在用TypeScript开发一个机器学习算法,并有一个部分导数方法,旨在复制以下内容:enter image description here这是我用来实现的递归方法:

private pd(a : Activation, w : Weight, t : number) : number { //Takes partial derivative of activation with respect to weight        return sigDeriv(this.sums[t][a.l][a.j].val)*(a.l == w.l && a.j == w.j ?            this.activations[t][a.l - 1][w.k].val            : sumFunc(this.activations[t][a.l - 1].length, 1,                async k => await this.weights[a.l][a.j][k].val*this.pd(this.activations[t][a.l - 1][k], w, t)            )        );    }

问题是,即使达到了基础条件(a.l == w.l && a.j == w.j),函数仍然继续执行,并最终到达输入层(那里没有权重),导致错误。这为什么会发生?我该如何修复?当我运行函数并记录基础情况的值时,它会在适当的时候返回true,但函数会继续执行,导致错误。


回答:

我发现需要将我的基础条件改得更通用。虽然第一个条件在找到被区分的权重乘以的激活时停止了函数,但函数继续尝试区分其余的激活,超出了权重对函数有影响的范围。基本上,我最终得到了∂/∂w(aw + a2w2 + ...),当区分该项时,pd会返回乘以w的激活,但会继续对其他项进行递归,而这些项不再受w的影响,因此永远不会达到基础条件。因此,解决方案是在函数到达权重所在层时返回a的值,因为上述导数计算结果为(a + 0 + ...)

private pd(a : Activation, w : Weight, t : number) : number { //Takes partial derivative of activation with respect to weight    return sigDeriv(this.sums[t][a.l][a.j].val)*this.pdSum(a, w, t);}private pdSum(a : Activation, w : Weight, t : number) : number { //Handles sum statement in recursive derivative definition    if(a.l == w.l) return this.activations[t][a.l - 1][w.k].val; //This line solves the problem    return sumFunc(this.activations[t][a.l - 1].length, 1,        async k => await this.weights[a.l][a.j][k].val*this.pd(this.activations[t][a.l - 1][k], w, t)    );}

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注