#r #neural-network #train-test-split
#r #нейронная сеть #train-test-split
Вопрос:
Я пытаюсь обучить нейронную сеть на наборе данных. Все работает. С кодом нет проблем, если я указываю 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)
Вот ссылка на: Dataset
Ответ №1:
Добавив только 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)