【问题标题】:kernel gets stuck if I train/test split by 55% and 45%如果我训练/测试分成 55% 和 45%,内核就会卡住
【发布时间】:2021-03-28 21:04:12
【问题描述】:

我正在尝试在数据集上训练神经网络。一切正常。如果我将 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

【问题讨论】:

    标签: r neural-network train-test-split


    【解决方案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) 
    

    【讨论】:

      猜你喜欢
      • 2016-03-22
      • 2019-04-11
      • 2011-10-14
      • 2016-04-04
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      相关资源
      最近更新 更多