Bulding a classification model in R studio with keras

痴心易碎 提交于 2020-01-04 02:34:19

问题


I am trying to build a classification model through keras tensor flow in R stdio but I am getting an error below. Pls does anyone have a clue? this is my first time using keras or deep learning. Thanks

  > 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

Total params: 1,159 Trainable params: 1,159 Non-trainable params: 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:


I was getting the same error in a similar situation with the keras library for keras + tensorflow in R. It took me a day to solve my problem, I think you have the same.

It does not recognize the number of columns in your input, when that input is a data frame. Just cast the type of the input back to a matrix, give the shape the correct number of columns and now it works.



来源:https://stackoverflow.com/questions/45533252/bulding-a-classification-model-in-r-studio-with-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!