为CNN keras模型EMNIST数据集重塑x

时间:2019-06-15 12:37:18

标签: r keras

我尝试在r:基于emnist-byclass数据集(62 category =(a-z);(A-Z);(0-9))上的6字母预测单词模型训练中使用keras构建模型。

我尝试通过conv_d2和max_poolimg使用CNN,但我的数据不合适。输入形状为784,无法正常工作。

如何更改我的x火车,使其适合我? 阅读有关重塑功能的信息,但没有成功。

试图仅使用密集层和辍学的其他模型,但准确性较低。

data <- list()
data <- EMNIST_read() # read emnist-myclass data

train_x <- data$x_train
train_y <- data$y_train
test_x <- data$x_test
test_y <- data$y_test

# dim(train_x)
[1] 697932    784
# > dim(train_y)
[1] 697932     62

#converting the target variable to once hot encoded vectors using keras inbuilt function
train_y <- to_categorical(train_y,62)
test_y <- to_categorical(test_y,62)

#defining a keras sequential model
model <- keras_model_sequential()


model %>%
  layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = 'relu') %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
  layer_conv_2d(filters = 64, kernel_size = c(3,3), activation = 'relu') %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
  layer_conv_2d(filters = 64, kernel_size = c(3,3), activation = 'relu') %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
  layer_conv_2d(filters = 64, kernel_size = c(3,3), activation = 'relu') %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
  layer_conv_2d(filters = 64, kernel_size = c(3,3), activation = 'relu') %>%
  layer_flatten() %>%
  layer_dropout(rate = 0.4) %>%
  layer_dense(units = 128, activation = 'relu') %>%
  layer_dense(units = 62, activation = 'softmax')

model %>% compile(
  optimizer = 'adam',
  loss = 'categorical_crossentropy',
  metrics = c('accuracy') 
)

#fitting the model on the training dataset
model %>% fit(train_x, train_y, epochs = 10, batch_size = 256, validation_split = 0.1, 
              callbacks = callback_early_stopping(monitor = "val_acc", patience = 6, mode = "min"))

遇到此错误:

 Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 784]

0 个答案:

没有答案