2017-01-02 89 views
1

我想創建一個數據框,將呈現不同的種子數量和深度學習方法的準確性。我創建了包含兩個循環的代碼(見下文),但我得到一個錯誤,我如何創建此循環正確如何基於h2o包env創建一個循環。正確

Error in .h2o.doSafeREST(h2oRestApiVersion = h2oRestApiVersion, urlSuffix = page, : 
ERROR MESSAGE: 

Can only append one column 

附件是我的代碼:

attach(iris) 
train<-iris 
test<-iris 
invisible(capture.output(h2o.init(nthreads = -1))) # initalising with all cpu cores 
trainHex <- as.h2o(train[1:200,]) 
testHex <- as.h2o(test) 
x_names <- colnames(trainHex[1:4]) 
SEED<-c(123456789,12345678,1234567) 
method<-c("Rectifier", "Tanh", "TanhWithDropout", "RectifierWithDropout", "Maxout", "MaxoutWithDropout") 
Res<-data.frame() 

for(i in 1:6){ 
    for(j in 1:3){ 

     system.time(ann <- h2o.deeplearning(
      reproducible = TRUE, 
      seed = SEED[j], 
      x = x_names, 
      y = "Species", 
      training_frame = trainHex,epochs = 50, 
      standardize = TRUE, 
      nesterov_accelerated_gradient = T, # for speed 
      activation = method[i] 
     )) 
     #ann 
     testHex$h20<-ifelse(predict(ann,newdata = testHex)>0.5,1,0) 
     testHex<-as.data.frame(testHex) 
     s<-xtabs(~Species +h20,data=testHex) 
     accuracy<-sum(diag(s))/sum(s) 
     tmp<-data.frame(seed=SEED[j],method=method[i],result=accuracy) 
     Res<-rbind(Res,tmp) 

    } 
} 
Error in .h2o.doSafeREST(h2oRestApiVersion = h2oRestApiVersion, urlSuffix = page, : 


ERROR MESSAGE: 

Can only append one column 

回答

1

你正在做多項分類;即預測將是三個類別中的一個。因此h2o.predict()將返回4列:

> predict(ann,newdata = testHex) 
    |========================================================================================================================================================| 100% 
    predict setosa versicolor virginica 
1 setosa 0.9999930 7.032604e-06 1.891484e-30 
2 setosa 0.9998726 1.274161e-04 2.791200e-28 
3 setosa 0.9999923 7.679687e-06 1.101218e-29 
4 setosa 0.9999838 1.619749e-05 1.593254e-28 
5 setosa 0.9999978 2.150244e-06 7.174795e-31 
6 setosa 0.9999932 6.844831e-06 5.511857e-29 

[150 rows x 4 columns] 

我不能完全肯定自己在做什麼,但鑑於這讓預測:

p = predict(ann,newdata = testHex) 

你可以這樣做是爲了得到一個1正確的答案,0個錯誤:

p$predict == testHex$Species 

或者,做客戶端:

p = as.data.frame(predict(ann,newdata = testHex)) 
p$predict == iris$Species 

更一般地,h2o.grid()更適合用於試驗替代參數。我認爲這可能是更接近你的意圖:

parts = h2o.splitFrame(as.h2o(iris), 0.8, seed=123) 
trainHex = parts[[1]] 
testHex = parts[[2]] 

g = h2o.grid("deeplearning", 
hyper_params = list(
    seed = c(123456789,12345678,1234567), 
    activation = c("Rectifier", "Tanh", "TanhWithDropout", "RectifierWithDropout", "Maxout", "MaxoutWithDropout") 
    ), 
reproducible = TRUE, 
x = 1:4, 
y = 5, 
training_frame = trainHex, 
validation_frame = testHex, 
epochs = 1 
) 
g #Output the grid 

(。我已經設置紀元1只是爲了得到它來快速完成,如果你想設置爲50)

我用splitFrame()使用80%作爲訓練數據,20%作爲測試數據;通過將測試數據分配給validation_frame,網格會自動爲我們評分那些看不見的數據。