'R neuralnet package: Can't train neural network
I'm trying to use the neuralnet
package to train a model on this data set. However, I'm getting the following error which I can't understand:
Error: the error derivative contains a NA; varify that the derivative function does not divide by 0 (e.g. cross entropy)
This is my code:
library(neuralnet)
library(tidyverse)
framingham <- read_csv('https://courses.edx.org/assets/courseware/v1/7022cf016eefb6d3747447589423dab0/asset-v1:MITx+15.071x+3T2019+type@asset+block/framingham.csv',
col_types = cols(.default = 'i',sysBP = 'n', diaBP = 'n', BMI = 'n' ))
# Split data
set.seed(123); train_idx <- sample(nrow(framingham), 2/3 * nrow(framingham))
framingham_train <- framingham[train_idx, ]
framingham_test <- framingham[-train_idx, ]
# Binary classification
nn <- neuralnet(formula = TenYearCHD ~ ., data = framingham_train,
hidden=c(3,2),
act.fct = "tanh",
stepmax = 1e8,
err.fct = 'ce',
linear.output = TRUE)
I've tried changing the error function and other details, but nothing seem to work.
Solution 1:[1]
There are some columns with NAs:
summary(framingham)
male age education currentSmoker
Min. :0.0000 Min. :32.00 Min. :1.000 Min. :0.0000
1st Qu.:0.0000 1st Qu.:42.00 1st Qu.:1.000 1st Qu.:0.0000
Median :0.0000 Median :49.00 Median :2.000 Median :0.0000
Mean :0.4292 Mean :49.58 Mean :1.979 Mean :0.4941
3rd Qu.:1.0000 3rd Qu.:56.00 3rd Qu.:3.000 3rd Qu.:1.0000
Max. :1.0000 Max. :70.00 Max. :4.000 Max. :1.0000
NA's :105
cigsPerDay BPMeds prevalentStroke prevalentHyp
Min. : 0.000 Min. :0.00000 Min. :0.000000 Min. :0.0000
1st Qu.: 0.000 1st Qu.:0.00000 1st Qu.:0.000000 1st Qu.:0.0000
Median : 0.000 Median :0.00000 Median :0.000000 Median :0.0000
Mean : 9.006 Mean :0.02962 Mean :0.005896 Mean :0.3106
3rd Qu.:20.000 3rd Qu.:0.00000 3rd Qu.:0.000000 3rd Qu.:1.0000
Max. :70.000 Max. :1.00000 Max. :1.000000 Max. :1.0000
NA's :29 NA's :53
If you subset for rows with no NAs, it should work:
set.seed(123)
framingham = framingham[complete.cases(framingham),]
train_idx <- sample(nrow(framingham), 2/3 * nrow(framingham))
framingham_train <- framingham[train_idx, ]
framingham_test <- framingham[-train_idx, ]
Also, I think you cannot use tanh
for activation with cross-entropy, so something below will work with logistic
as activation function:
nn <- neuralnet(formula = TenYearCHD ~ ., act.fct="logistic",rep = 3,
data = framingham_train,hidden=c(3,2), err.fct = 'ce',linear.output=FALSE)
Solution 2:[2]
I also had this problem, but the right solution is to choose the right variables to enter the model. So you should not enter less important variables into the model
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | StupidWolf |
Solution 2 | Meysam |