首页 > 解决方案 > 如果我训练/测试分成 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) 

这是一个链接:数据集

标签: rneural-networktrain-test-split

解决方案


通过在模型中添加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) 

推荐阅读