使用 R 中的神经网络包进行神经网络训练时出错

问题描述 投票:0回答:1

菜鸟在这里。如果我没有清楚地说明问题,请提前道歉。

我正在尝试用男性和女性的假体重和身高训练一个神经网络,以获得仅基于公斤和厘米的性别概率,但我在 neuralnet() 或 train() 函数中遇到我无法解决的错误.大多数时候错误是:

Error in x[0, , drop = FALSE] : número incorreto de dimensiones

这是我的一次尝试的示例代码:

set.seed(123)
library(neuralnet)
library(caret)

# data1: training data
sex <- factor(sample(c("male", "female"), size = 1000, replace = TRUE))
height <- rnorm(1000, mean = c(1.8, 1.6)[sex])
weight <- rnorm(1000, mean = c(80, 60)[sex])
data1 <- data.frame(sex, height, weight)

# data2: population data
sex <- factor(sample(c("male", "female"), size = 10000, replace = TRUE))
height <- rnorm(1000, mean = c(1.81, 1.61)[sex])
weight <- rnorm(1000, mean = c(81, 61)[sex])
data2 <- data.frame(sex, height, weight)

# training 

net  <- neuralnet(sex ~ height + weight, 
                  data = data1, hidden = c(10, 10), 
                  linear.output = FALSE, 
                  threshold = 0.1, stepmax = 10000)

trained_net <- train(net, data, error.fun = "ce", stepmax = 1e+07)

# Prediction and results

predictions <- compute(trained_net, data2[, c("height", "weight")])
predictions_df <- data.frame(predictions$net.result)
colnames(predictions_df) <- c("probfemale")
result <- cbind(datos2, predictions_df)
                  result$prediccion_sex <- ifelse(resul$probfemale >= 0.5, "female", "male")
 

我知道男性和女性的体重和身高在现实世界中重叠得足够多,可以做出很好的预测,但这根本不是目标,而是根据任何两个数字变量获得对任何因素的一些预测,而不会停止由于尺寸不正确或缺乏收敛的致命错误。我现在的目标只是达成一些结果,不管它是否比政治家的承诺更不可靠。

r neural-network
1个回答
0
投票

您正在尝试组合两个包(框架)

caret
neuralnet
开箱即用。

数据创建:

set.seed(123)

# data1: training data
sex <- factor(sample(c("male", "female"), size = 1000, replace = TRUE))
height <- rnorm(1000, mean = c(1.8, 1.6)[sex])
weight <- rnorm(1000, mean = c(80, 60)[sex])
data1 <- data.frame(sex, height, weight)

# data2: population data
sex <- factor(sample(c("male", "female"), size = 10000, replace = TRUE))
height <- rnorm(1000, mean = c(1.81, 1.61)[sex])
weight <- rnorm(1000, mean = c(81, 61)[sex])
data2 <- data.frame(sex, height, weight)

选项 1 使用库
neuralnet


library(neuralnet)

# training using library: neuralnet

net  <- neuralnet(sex ~ height + weight, 
                  data = data1, hidden = c(10, 10), 
                  linear.output = FALSE, 
                  threshold = 0.1, stepmax = 10000)

# prediction using library: neuralnet
predictions_nn <- predict(net,data2[-1])
# convert to dataframe
predictions_nn <- as.data.frame(predictions_nn) 
# create sex factor from prediction probabilities
predictions_nn$sex <- factor(ifelse(predictions_nn[1] < predictions_nn[2],"male","female"))

> head(predictions_nn$sex)

[1] male   female female female female male  
Levels: female male

选项 2:使用
caret

library(caret)
# training nnet using caret
trained_net <- train(y=data1$sex,x=data1[-1] ,method = 'nnet',stepmax = 1e+05)

# prediction nnet using caret
predictions_nnet <- predict(trained_net,data2[-1])

> head(predictions_nnet)

[1] male   female female female female male  
Levels: female male

比较:

> all(predictions_nn$sex==predictions_nnet)
[1] TRUE

给出相同的预测。

© www.soinside.com 2019 - 2024. All rights reserved.