CONFUSION MATRIX,R,

时间:2017-06-02 11:23:19

标签: r loops confusion-matrix

我在下面的代码中无需帮助。我必须设置一个循环来训练一个神经网络模型在TRAINING数据上每次使用不同数量的纪元,从5开始并加3直到我达到20.然后我必须计算一个折线图,显示不同数字的精度时代。我还必须保持所有参数如图所示。大部分代码都是由我们的教师提供的。我添加了epochs= c(5,8,11,14,17,20)来创建一个时期列表和error.rate = vector(),我打算将每个循环的精度存储到一个向量中。我想要的准确度来自混淆矩阵,可从公式

中找到

h2o.hit_ratio_table(<model>,train = TRUE)[1,2]

我面临的问题是我试图创建一个循环。我试图从每个循环得到结果。我将循环的第一部分标记为X,试图将其放入向量中,以便将每个循环的精确度放入向量error.rate=h2o.hit_ratio_table(x,train=TRUE)[1,2])

但是,它会出错。

> Error in is(object, "H2OModelMetrics") : object 'X' not found In
> addition: Warning messages: 1: In 1:epochs : numerical expression has
> 6 elements: only the first used

此外,当我删除error.rate=......部分时,该函数运行正常,但无法找到准确度的值。

我是R的菜鸟,所以我会非常感激你的帮助。

s <- proc.time()
 epochs= c(5,8,11,14,17,20)
 error.rate = vector()

 for (epoch in 1:epochs){#set up loop to go around 6 times
 X=h2o.deeplearning(x = 2:785,  # column numbers for predictors
               y = 1,   # column number for label
               training_frame = train_h2o, # data in H2O format
               activation = "RectifierWithDropout", # mathematical   activation function
               input_dropout_ratio = 0.2, # % of inputs dropout, because some inputs might not matter.
               hidden_dropout_ratios = c(0.25,0.25,0.25,0.25), # % for nodes dropout, because maybe we don't need full connections. Improves generalisability
               balance_classes = T, # over/under samples so that all classes are similar size.
               hidden = c(50,50,50,50), # two layers of 100 nodes
               momentum_stable = 0.99,
               nesterov_accelerated_gradient = T,
               error.rate=h2o.hit_ratio_table(x,train=TRUE)[1,2])
proc.time() - s}

1 个答案:

答案 0 :(得分:0)

你在做for(epoch in 1:epochs)。这里的'epoch'术语会改变每个循环(通常你在循环中使用它但我看不到它?)。 1:epochs无法按照您的想法运作。它采用了纪元(5)的第一个元素,基本上说for(epoch in 1:5),其中纪元是1,然后是2,......然后是5.你想要像for(epoch in epochs)这样的东西,如果你想要一个序列从1:你的代码中的每个纪元,你应该在循环中写它。

此外,每次循环时都会重写x。你应该初始化它并在每个循环中保存它的子集:

epochs= c(5,8,11,14,17,20)
 x <- list() # save as list #option 1
 y <- list() # for an option 2
 for (epoch in epochs){ #set up loop to go around 6 times
   X[[epoch]] = h2o.deeplearning(... )
   # or NOW you can somehow use 1:epoch where each loop epoch changes
 }

但我真的会专注于在你的帖子中看到你的for循环中没有使用你的纪元。也许找出你想用它的地方......

相关问题