【发布时间】:2017-06-02 11:23:19
【问题描述】:
下面的代码我需要一点帮助。我必须设置一个循环,通过从 5 开始并添加 3 直到达到 20,每次使用不同数量的 epoch 训练训练数据上的神经网络模型。然后我必须计算一个折线图,显示不同数字的准确性的时代。我还必须保持所有参数与所示相同。大部分代码是我们的讲师提供的。我添加了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}
【问题讨论】:
-
你正在做'for(epoch in 1:epochs)'。在这里,'epoch' 术语会改变每个循环(通常你在循环中使用它,但我没有看到它?)。 '1:epochs' 不会像你认为的那样工作。它采用 epochs (5) 的第一个元素,基本上说“for(epoch in 1:5)”,其中 epoch 是 1,然后是 2,……然后是 5。你想要类似“for(epoch in epochs)”的东西' 如果您确实想要代码中从 1:each 开始的序列,您应该在循环中编写它。此外,'x' 和你的 error.rate 结果在每次循环时都会被重写。您应该改为运行 'error.rate[epoch] ='。
-
谢谢埃文,这非常有用....这就是为什么我无法让循环运行
-
请允许我将其复制到答案中,以便您可以标记此线程已完成。干杯。
-
谢谢埃文,你真的很有帮助!!!!!
标签: r loops confusion-matrix