0

I am trying to train a neural net on a dataset. Everything works. There is no issue with the code if I specifiy 70% or 50% percent of the data as training and the rest as testing. But as I specify 55% and 45% for training and testing, the kernel gets stuck and gives the following error:

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")

Here is the code that I have written so far:

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) 

Here is a link to the: Dataset

Saad Zaheer
  • 171
  • 7

1 Answers1

0

By adding just act.fct = "tanh" parameter in the model, everything runs smoothly. Here is the working version:

    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) 
Saad Zaheer
  • 171
  • 7