r - 如果我训练/测试分成 55% 和 45%,内核就会卡住
问题描述
我正在尝试在数据集上训练神经网络。一切正常。如果我将 70% 或 50% 的数据指定为训练,而将其余数据指定为测试,则代码没有问题。但是当我指定 55% 和 45% 用于训练和测试时,内核卡住并给出以下错误:
Error in plot.nn(nnet, rep = "best"): weights were not calculated
Traceback:
1. plot(nnet, rep = "best")
2. plot(nnet, rep = "best")
3. plot.nn(nnet, rep = "best")
4. stop("weights were not calculated")
这是我到目前为止编写的代码:
library(neuralnet)
Main <- read.table("miRNAs200TypesofCancerData.txt", header = TRUE,stringsAsFactors = T ) # reading the dataset
for(i in 1:ncol(Main)){
Main[is.na(Main[,i]), i] <- mean(Main[,i], na.rm = TRUE)
}
set.seed(123)
# in the following line, if you replace p=0.55 by p=0.5, no problem is reported and everything works smoothly
indexes = createDataPartition(Main$Type, p=0.55, list = F)
# Creating test and train sets.
train = Main[indexes, ]
test = Main[-indexes, ]
xtest = test[, -1]
ytest = test[, 1]
nnet = neuralnet(Type~., train, hidden = 5, linear.output = FALSE)
# Plotting
plot(nnet, rep = "best")
# Predictions
ypred = neuralnet::compute(nnet, xtest)
yhat = ypred$net.result
yhat=data.frame("yhat"=ifelse(max.col(yhat[ ,1:4])==1, "Mesenchymal",
ifelse(max.col(yhat[ ,1:4])==2, "Proneural",
ifelse(max.col(yhat[ ,1:4])==3, "Classical","Neural"))))
# Confusion matrix
cm = confusionMatrix(as.factor(yhat$yhat),as.factor(ytest))
print(cm)
这是一个链接:数据集
解决方案
通过在模型中添加act.fct = "tanh"
参数,一切运行顺利。这是工作版本:
library(neuralnet)
Main <- read.table("miRNAs200TypesofCancerData.txt", header = TRUE,stringsAsFactors = T ) # reading the dataset
for(i in 1:ncol(Main)){
Main[is.na(Main[,i]), i] <- mean(Main[,i], na.rm = TRUE)
}
set.seed(123)
# in the following line, if you replace p=0.55 by p=0.5, no problem is reported and everything works smoothly
indexes = createDataPartition(Main$Type, p=0.55, list = F)
# Creating test and train sets.
train = Main[indexes, ]
test = Main[-indexes, ]
xtest = test[, -1]
ytest = test[, 1]
nnet = neuralnet(Type~., train, hidden = 5, act.fct="tanh", linear.output = FALSE)
# Plotting
plot(nnet, rep = "best")
# Predictions
ypred = neuralnet::compute(nnet, xtest)
yhat = ypred$net.result
yhat=data.frame("yhat"=ifelse(max.col(yhat[ ,1:4])==1, "Mesenchymal",
ifelse(max.col(yhat[ ,1:4])==2, "Proneural",
ifelse(max.col(yhat[ ,1:4])==3, "Classical","Neural"))))
# Confusion matrix
cm = confusionMatrix(as.factor(yhat$yhat),as.factor(ytest))
print(cm)
推荐阅读
- laravel - 从表中选择(laravel) - where 和 orWhere
- reactjs - 如何将多个参数值作为请求从 axios 传递给 laravel
- c# - 如何根据其他单元格 ID 填充 datagridview 单元格?
- android - OkHttp 错误:流已重置:INTERNAL_ERROR
- javascript - useEffect 如何维护状态
- gns3 - 如何配置交换机帧中继
- angular - 查询参数未显示正确参数
- android - 如何在我的颤振应用程序中添加启动画面?
- r - Cox-Snell 残差:较长的物体长度不是较短物体长度的倍数
- python - 尝试在 Python 3.8 上安装 kivy 时出错