使用keras在R studio中建立分类模型

时间:2017-08-06 14:59:02

标签: r keras classification

我正在尝试通过R stdio中的keras张量流建立分类模型,但我收到的错误如下。请问有没有人知道?这是我第一次使用keras或深度学习。感谢

  > set.seed(10)
> ind <- sample(2, nrow(stdk), replace=TRUE, prob=c(0.80, 0.2))
> stdk.train <- stdk[ind==1, ]
> stdk.test <- stdk[ind==2, ]
> change.train <- stdk[ind==1, 5]
> change.test <- stdk[ind==2, 5]
> stdk.trainLabels <- to_categorical(change.train)
> stdk.testLabels <- to_categorical(change.test)
> 
> modelk <- keras_model_sequential()
> modelk %>% 
+   layer_dense(units = 23, activation = 'relu', input_shape = c(40)) %>% 
+   layer_dense(units = 9, activation = 'softmax')
> summary(modelk)
Model

    Layer (type)                                      Output Shape                                 Param #          
    ================================================================================================================
    dense_1 (Dense)                                   (None, 23)                                   943              
    ________________________________________________________________________________________________________________
    dense_2 (Dense)                                   (None, 9)                                    216

总参数:1,159     可训练的参数:1,159     不可训练的参数:0

  > get_layer(modelk, index = 1)
Dense
> modelk$layers
[[1]]
Dense

[[2]]
Dense

> modelk$inputs
[[1]]
Tensor("dense_1_input:0", shape=(?, 40), dtype=float32)

> modelk$outputs
[[1]]
Tensor("dense_2/Softmax:0", shape=(?, 9), dtype=float32)

> modelk %>% compile( loss = 'categorical_crossentropy',  optimizer = 'adam', metrics = c('accuracy')
+ )
> modelk %>% fit(
+   stdk.train, 
+   stdk.trainLabels, 
+   epochs = 200, 
+   batch_size = 5   )
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: No data provided for "dense_1_input". Need data for each key in: ['dense_1_input']
Detailed traceback: 
  File "C:\Users\A\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\models.py", line 844, in fit
    initial_epoch=initial_epoch)
  File "C:\Users\A\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1406, in fit
    batch_size=batch_size)
  File "C:\Users\A\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1300, in _standardize_user_data
    exception_prefix='model input')
  File "C:\Users\A\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 82, in _standardize_input_data
    '". Need data for each key in: ' + str(names))

1 个答案:

答案 0 :(得分:8)

在类似的情况下,我在ke中使用了Keras + tensorflow的keras库得到了同样的错误。我花了一天时间来解决我的问题,我认为你也有同样的错误。

当输入是数据框时,它无法识别输入中的列数。只需将输入的类型转换回矩阵,为形状提供正确的列数,现在它可以工作。