R tryCatch neuralnet,意外输出

时间:2016-11-26 18:05:41

标签: r neural-network try-catch

我为Kaggle建立一个神经网络," Ghost,Ghoul,Goblin"挑战。

我正在对我的(训练)数据进行X次采样(为了说明目的,X = 6)将其分成训练和测试集。然后,我在每个X(6)数据集上运行我的神经网络并记录准确性。我这样做,所以我可以比较不同网络之间的准确性(一层3,4,5隐藏,两层3 + 3,4 + 3等)

我的神经网络部分是

set.seed(26)
mysamples <- sapply(1:iterations, function(j) {
  temp <- sample(1:371, size = insamplesize, replace = F)
})

nn3results <- data.frame(matrix(0, ncol = iterations, nrow = 1))

myseed <- 0
for (nnloop in 1:iterations){
  myseed = myseed + 1
  set.seed(myseed)
  nn_idx <- mysamples[,nnloop]
  nn_rep_train <- mydata[nn_idx,]
  nn_rep_test <- mydata[-nn_idx,]
  nn_rep <- neuralnet(Ghost+Ghoul+Goblin ~ bone_N + rot_N + hair_N + soul_N, 
                      data=nn_rep_train, hidden=c(4))
  mypredict <- compute(nn_rep, nn_rep_test[,2:5])$net.result
  idx <- apply(mypredict, c(1), maxidx)
  nn_rep_test$Pred <- as.factor(c('Ghost', 'Ghoul', 'Goblin')[idx])
  print(paste("sim",nnloop,"=",round(sum(diag(table(nn_rep_test$type, nn_rep_test$Pred)))
          /outsamplesize,5),"%","myseed = ",myseed))
  nn3results[1,nnloop] <- sum(diag(table(nn_rep_test$type, nn_rep_test$Pred)))
}

完全按照我的预期执行,直到迭代5,其中神经网络没有收敛如下

[1] "sim 1 = 0.74194 % myseed =  1"
[1] "sim 2 = 0.73118 % myseed =  2"
[1] "sim 3 = 0.75269 % myseed =  3"
[1] "sim 4 = 0.74194 % myseed =  4"
Error in nrow[w] * ncol[w] : non-numeric argument to binary operator
In addition: Warning messages:
1: algorithm did not converge in 1 of 1 repetition(s) within the stepmax 
2: In is.na(weights) :
  is.na() applied to non-(list or vector) of type 'NULL'

所以不要担心。我修改了我的代码并将其放在tryCatch内,如下所示。

myseed <- 0
for (nnloop in 1:iterations){
  myseed = myseed + 1
  set.seed(myseed)
  nn_idx <- mysamples[,nnloop]
  nn_rep_train <- mydata[nn_idx,]
  nn_rep_test <- mydata[-nn_idx,]
  tryCatch({
        nn_rep <- neuralnet(Ghost+Ghoul+Goblin ~ bone_N + rot_N + hair_N + soul_N, 
            data=nn_rep_train, hidden=c(4))
        },
       error = function(e){nn3results[1,nnloop] <- -1},
       warning = function(w){nn3results[1,nnloop] <- -1}, 
       finally={
          mypredict <- compute(nn_rep, nn_rep_test[,2:5])$net.result
          idx <- apply(mypredict, c(1), maxidx)
          nn_rep_test$Pred <- as.factor(c('Ghost', 'Ghoul', 'Goblin')[idx])
          print(paste("sim",nnloop,"=",round(sum(diag(
                table(nn_rep_test$type, nn_rep_test$Pred)))
                /outsamplesize,5),"%","myseed = ",myseed))
          nn3results[1,nnloop] <- sum(diag(table(nn_rep_test$type, nn_rep_test$Pred)))
  })
}
nn3results

现在出乎意料的结果。由于模拟5返回错误,我希望相应的nn3results由于-1而具有error = function(e){nn3results[1,nnloop] <- -1}的值,但现在整个代码似乎都可以工作,即使对于模拟5,当之前它没&#39;吨。

[1] "sim 1 = 0.74194 % myseed =  1"
[1] "sim 2 = 0.73118 % myseed =  2"
[1] "sim 3 = 0.75269 % myseed =  3"
[1] "sim 4 = 0.74194 % myseed =  4"
[1] "sim 5 = 0.70968 % myseed =  5"
[1] "sim 6 = 0.75269 % myseed =  6"
> nn3results
  X1 X2 X3 X4 X5 X6
1 69 68 70 69 66 70

种子是一样的。结果1-4是相同的。为什么模拟5现在可以工作而不返回-1?

除此之外,任何(建设性的)代码建议总是受到赞赏。

1 个答案:

答案 0 :(得分:1)

当出现错误并且没有错误时,始终会执行finally语句中的代码。因此,在这种情况下,您使用上一步(模型4)中的模型计算预测。您应该将评估模型的代码放在tryCatch块中。

finally语句通常用于确保清理事物。例如,关闭文件。无论是否出现错误都应该发生这种情况。

相关问题