0

我正在尝试使用该neuralnet软件包在 R 中训练一个神经网络。我正在运行回归模型并尝试预测计数变量“Rented_Bike_Count”。我混合了分类变量和数值变量,并通过model.matrix.

我已将数据转换为 model.matrix 并删除了截距项。我读过与这个问题类似的问题,每个人都说要降低学习率。它似乎根本没有帮助,我不相信我需要让我的学习率小到1e-6.

还有什么问题?我怎样才能解决这个问题?我尝试使用threshold=0.5它似乎可以工作,但我真的不明白为什么。

代码:

library(caret)
library(neuralnet) 

sigmoid <-  function(x) 1 / (1+exp(-x)) 

# must make our factor variables in to a one-hot encoding (binary form)
X_train <- model.matrix(~., data = Train_set_standardized)[,-1] # remove intercept term

dimnames(X_train)
Train_nn_sigmoid <- neuralnet(Rented_Bike_Count~., 
                     data = X_train, 
                     hidden = 1, 
                     learningrate = 1e-6, 
                     act.fct = sigmoid,
                     linear.output = TRUE, # FALSE Means output node gets the activation function
                     threshold = 0.5,
                     err.fct = "sse") 

Error in if (reached.threshold < min.reached.threshold) { : 
  missing value where TRUE/FALSE needed

4

1 回答 1

0

很可能您没有缩放数据。使用示例:

library(mlbench)
data(BostonHousing)

X_train = model.matrix(~.,data=BostonHousing)[,-1]

m <- neuralnet(medv ~. , data = X_train, 
                      hidden = c(5,3), 
                      learningrate = 1e-6, 
                      linear.output = TRUE, 
                      threshold = 0.5,
                      act.fct=sigmoid,
                      err.fct = "sse")

Error in while (step < stepmax && reached.threshold > threshold) { : 
  missing value where TRUE/FALSE needed

请参阅有关缩放数据的这篇文章。如果您搜索,我相信还有其他帖子。如果你缩放它:

X_scaled = scale(X_train)
X_scaled = data.frame(X_scaled)

m <- neuralnet(medv ~. , data = X_scaled, 
                      hidden = c(5,3), 
                      learningrate = 1e-6, 
                      linear.output = TRUE, 
                      threshold = 0.5,
                      act.fct=sigmoid,
                      err.fct = "sse")

plot(predict(m,X_scaled),X_scaled$medv)

在此处输入图像描述

于 2020-11-09T23:27:48.193 回答