所以这是场景: -
我运行了以下代码来创建神经网络(使用 R 中的神经网络包)来近似函数 f(x)=x^2:-
set.seed(2016);
rm(list=ls());
# Prepare Training Data
attribute<-as.data.frame(sample(seq(-2,2,length=50), 50 , replace=FALSE ),ncol=1);
response<-attribute^2;
data <- cbind(attribute,response);
colnames(data)<- c("attribute","response");
# Create DNN
fit<-neuralnet(response~attribute,data=data,hidden = c(3,3),threshold=0.01);
fit$result.matrix;
这工作正常并在 3191 步中收敛。现在我对代码做了一个小改动——我改变了被逼近的函数。我使用了一个非常简单的线性函数,而不是二次函数f(x)=2x
。这也很好,然后我调整了 x 的系数并进行了多次运行,例如
f(x) = 2x
f(x) = 3x
.
.
f(x) = 19x
到目前为止,它运行良好。但我注意到的一件事是,收敛所需的步骤数从 2 倍急剧增加到 19 倍。例如 19x 的步数是惊人的84099。奇怪的是,DNN 只对线性函数采取了如此多的步骤来收敛,而对于二次函数 f(x)=x^2,它只需要 3191 步。
因此,当我将函数更改为 f(x)=20x 时,它可能需要更多步骤,因此我收到以下警告:-
> set.seed(2016);
> rm(list=ls());
> # Prepare Training Data
> attribute<-as.data.frame(sample(seq(-2,2,length=50), 50 , replace=FALSE ),ncol=1);
> response<-attribute*20;
> data <- cbind(attribute,response);
> colnames(data)<- c("attribute","response");
>
> # Create DNN
> fit<-neuralnet(response~attribute,data=data,hidden = c(3,3),threshold=0.01);
Warning message:
algorithm did not converge in 1 of 1 repetition(s) within the stepmax
> fit$result.matrix;
所以我想我可以调整默认的 stepmax 参数并增加步数。但真正的问题是——为什么仅仅为了这样一个简单的线性函数就需要这么多步骤?