我有以下代码
public void alearningrate(){
//adopt learningrate
System.out.println(errorpositive+""+learningrate);
if ((errorpositive&&errorgradient>0)||!errorpositive&&errorgradient<0){
learningrate=learningrate*1.5;
}else{
learningrate=learningrate*0.75;
}
if (learningrate<0.0001){
learningrate=0.0001;
}
}
public void lastblame(double blame){
errorpositive=errorgradient>0;
errorgradient= Math.exp(-value)/Math.pow(1+Math.exp(-value), 2)*blame;
if (value==Double.NaN){
System.out.println("nan detected=?");
errorgradient=1;
}
for (int i=0; i<weight.length;i++){
weight[i]=weight[i]+learningrate*source[i].output()*errorgradient;
}
//alearningrate();
}
public void hiddenblame(double blame){
System.out.println(value);
errorpositive=errorgradient>0;
errorgradient=Math.exp(-value)/Math.pow(1+Math.exp(-value), 2)*blame;
if (value==Double.NaN){
System.out.println("nan detected=?");
errorgradient=1;
}
for (int i=0; i<weight.length;i++){
weight[i]=weight[i]+learningrate*source[i].output()*errorgradient;
}
System.out.println(errorgradient);
alearningrate();
}
但是,这会导致错误梯度迅速变为 NaN,但如果我决定删除 alearningrate(),则代码运行良好。我查看了我的函数 errorgradient 函数,它永远不会产生 nan,有人知道我为什么一直得到 NaN 吗?