首页 > 解决方案 > 如何防止神经网络/插入符号/R中的“算法不收敛”错误?

问题描述

我正在尝试使用train函数训练神经网络并neuralnet作为我的method参数来预测时间表。

我也在扩展我的训练数据集。

即使我为 my 尝试了不同learningrate的 s、stepmaxes 和thresholds neuralnet,但每次我尝试使用train函数训练网络时,其中一个 k-folds 碰巧每次都失败了

1:算法在 stepmax 内的 1 次重复中没有收敛。2:Fold05.Rep1 的预测失败:layer1=8, layer2=0, layer3=0 cbind(1, pred) %*% weights[[num_hidden_​​layers + 1]] 中的错误:需要数字/复杂矩阵/向量参数

我猜这是因为权重是随机的,所以每次我碰巧得到一些不会收敛的权重。

有没有办法阻止这种情况?也许试图重新训练使用不同权重失败的特定折叠?

这是我的代码:

library(caret)
library(neuralnet)

# Create the dataset
tt = data.frame(multiplier = rep(1:10, times = 10), multiplicand = rep(1:10, each = 10))
tt = cbind(tt, data.frame(product = tt$multiplier * tt$multiplicand))

# Splitting 
indexes = createDataPartition(tt$product,
                              times = 1,
                              p = 0.7,
                              list = FALSE)
tt.train = tt[indexes,]
tt.test = tt[-indexes,]

# Pre-process

preProc <- preProcess(tt, method = c('center', 'scale'))
tt.preProcessed <- predict(preProc, tt)
tt.preProcessed.train <- tt.preProcessed[indexes,]
tt.preProcessed.test <- tt.preProcessed[-indexes,]

# Train

train.control <- trainControl(method = "repeatedcv",
                              number = 10,
                              repeats = 3)

tune.grid <- expand.grid(layer1 = 8,
                         layer2 = 0,
                         layer3 = 0)

tt.cv <- train(product ~ .,
               data = tt.preProcessed.train,
               method = 'neuralnet',
               tuneGrid = tune.grid,
               trControl = train.control,
               linear.output = TRUE,
               algorithm = 'backprop',
               learningrate = 0.01,
               stepmax = 500000,
               lifesign = 'minimal',
               threshold = 0.01)

标签: rmachine-learningneural-networkr-caret

解决方案


推荐阅读