无法使用Keras进行预测 - ValueError:没有为“dense_39_input”

时间:2017-08-29 10:00:11

标签: r keras

我正在尝试使用Keras预测类,其中包含我从CSV加载的一些数据。 训练工作正常,但在尝试使用predict_classes时,我遇到了这个错误

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: No data provided for "dense_39_input". Need data for each key in: ['dense_39_input']  

我不知道如何解决这个问题。显然X的格式错误,但我尝试以各种方式转换它并没有任何帮助。

这些是输入csv文件中的前五行

-0.27453,-0.46843,-0.42289,-0.34303,-0.36671,-0.16860,0.03683,0.10808,0.26768,0.25572,0.53928,0.27146,0.19920,0.12027,0.05847,0.14058,0.20341,0.21308,0.35766,0.35199,0.00015
-0.29815,-0.34678,-0.49428,-0.44181,-0.45124,-0.44862,-0.26333,-0.04132,0.02974,0.20265,0.03516,0.30401,0.18651,0.12860,0.08156,0.03689,0.11063,0.17845,0.18046,0.33268,0.00061
0.18402,-0.06009,-0.17883,-0.32844,-0.38632,-0.37212,-0.36844,-0.18262,-0.00003,0.08355,-0.20665,-0.10170,0.18719,0.07661,0.05608,0.02057,-0.02999,0.09360,0.14104,0.12479,0.00042
0.28664,0.31157,0.09778,-0.04142,-0.23107,-0.27664,-0.25168,-0.25491,-0.11681,0.06230,-0.03656,-0.12935,-0.15293,0.07433,0.00843,-0.00593,-0.03014,-0.04157,0.06251,0.07761,0.00041
0.04764,0.22211,0.26461,0.07411,-0.03068,-0.22023,-0.21605,-0.21464,-0.24856,-0.11535,-0.12799,-0.01001,-0.05935,-0.17199,0.00122,-0.04570,-0.04941,-0.04192,-0.06015,-0.00324,0.00036

这就是代码。通常我运行TestOOS()来运行我的测试,然后调用预测函数。 (在调试此问题时,只需使用1个纪元)

library('caret')
library(keras)

neural.train = function(model,XY) 
{
  XY <- as.matrix(XY)
  X <- XY[,-ncol(XY)]
  Y <- XY[,ncol(XY)]
  Y <- ifelse(Y > 0,1,0)

  newModel <- keras_model_sequential() 
  newModel %>% 
    layer_dense(units = 20, activation = 'relu', input_shape = c(20)) %>% 
    layer_dense(units = 15, activation = 'relu') %>% 
    layer_dense(units = 10, activation = 'relu') %>% 
    layer_dropout(rate = 0.2) %>% 
    layer_dense(units = 5, activation = 'relu') %>% 
    layer_dropout(rate = 0.2) %>% 
    layer_dense(units = 1) %>% 
    layer_activation('sigmoid')

  newModel %>% compile( 
    optimizer = optimizer_sgd(lr = 0.01, momentum = 0, decay = 0, nesterov = FALSE),
    loss = 'binary_crossentropy',
    metrics = c('accuracy')
  )

  newModel %>% fit(X, Y, epochs=1, batch_size=300, validation_split = 0.2)
  Models[[model]] <<- newModel
}

neural.predict = function(model,x_pred) 
{
  if(is.vector(x_pred)) x_pred <- t(x_pred)
  classes <- Models[[model]] %>% predict_classes(x_pred)
  return(classes)
}

neural.save = function(name)
{
  save_model_hdf5(Models, name + '.h5')
}

neural.init = function()
{
  set.seed(365)
  Models <<- vector("list")
}

TestOOS = function() 
{
  neural.init()
  #XY <<- read.csv('Y:/Zorro/Data/OvernightEffectsDeepLearnUSDJPY_S.csv',header = F)
  XY <<- read.csv('Y:/Zorro/Data/DeepSignalsEURUSD_L.csv',header = F)
  splits <- nrow(XY)*0.8
  XY.tr <<- head(XY,splits);
  XY.ts <<- tail(XY,-splits)
  neural.train(2,XY.tr)
  X <<- XY.ts[,-ncol(XY.ts)]
  Y <<- XY.ts[,ncol(XY.ts)]
  Y.ob <<- ifelse(Y > 0,1,0)
  Y <<- neural.predict(2,X)
  Y.pr <<- ifelse(Y > 0.5,1,0)
  confusionMatrix(Y.pr,Y.ob)
}

0 个答案:

没有答案