使用Keras在R Studio中建立分类模型 [英] Bulding a classification model in R studio with keras
问题描述
我正在尝试通过R stdio中的keras张量流建立分类模型,但是下面出现错误。请问有人知道吗?这是我第一次使用keras或深度学习。谢谢
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
总参数:1,159
可训练参数:1,159
非可训练参数:0
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))
推荐答案
在类似情况下我遇到相同的错误用了keras + R中的tensorflow的keras库。我花了一天的时间来解决我的问题,我想您也是如此。
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.
这篇关于使用Keras在R Studio中建立分类模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!