R 神经网络对线性函数 f(x)=19x 采取大量步骤



所以这是场景:-

我运行了以下代码来创建神经网络(在 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 参数并增加步长。但真正的问题是 - 为什么仅仅为了这样的简单线性函数,它就需要这么多步骤?

我相信,这里最重要的事情是你需要扩展你的数据。 随着值变大,神经网络需要覆盖更大范围的结果(这可以使用更复杂的技术来处理,例如不同形式的momentum但这不是这里的重点)。 如果您只是像这样扩展数据:

maxs <- apply(data, 2, max) 
mins <- apply(data, 2, min)
raw.sc <- scale(data, center = mins, scale = maxs - mins)
scaled <- as.data.frame(raw.sc)

您现在可以更快地创建 NN。 注意 - 线性函数不需要多个层。 在这里,我用单层、单节点网络来演示这一点。 这对于"深度学习"来说并不是一个真正的问题。

set.seed(123)
# Create NN
fit<-neuralnet(response~attribute,data=scaled,hidden = c(1),threshold=0.01);

# make sure to use normalized input and de-normalize the output
pr.nn <- compute(fit, scaled$attribute)
pr.nn_ <- pr.nn$net.result*attr(raw.sc, 'scaled:scale')[2] + attr(raw.sc, 'scaled:center')[2]

在这种情况下,它以 1349 步收敛。 然后计算一些指标,如 MSE。

# MSE
sum((data$response - pr.nn_)^2)/nrow(data)

最新更新