Как я должен установить input_shape на CNN?(в R-Keras) - PullRequest
0 голосов
/ 09 мая 2019

Использование набора данных

Строка: 10000

столбец: 24

целевой столб = 24-й

Я пытаюсь написать код CNN, используя R-Keras.

Но я не знаю, как установить input_shape.

df <- read.csv("C:/~~.csv")

set.seed(1)
sample_idx <- sample(1:nrow(df), nrow(df)*0.7)

training <- df[sample_idx, ]
testing <- df[-sample_idx, ]

x_train <- training[,1:23]
y_train <- training[,24]
x_test <- testing[,1:23]
y_test <- testing[,24]

#Rescale
x_train <- x_train/255
x_test <- x_test/255

#Change dataset to matrix
x_train <- data.matrix(x_train)
x_test <- data.matrix(x_test)

#One hot encoding
y_train <- to_categorical(y_train)
y_test <- to_categorical(y_test)

# Build a CNN model to improve the accuracy
# Data preprocessing
x_train_cnn <- array_reshape(data.matrix(training[,1:23]),c(nrow    (training), 23, 1))/255
# I don't know this part. How do I set shape?

# Data partition
y_train_cnn <- data.matrix(training[,24])
y_train_cnn <- to_categorical(y_train_cnn)

#Build a CNN model to improve the accuracy
model_cnn_1 <- keras_model_sequential() %>%
layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = "relu",
            input_shape = c(nrow(training), 23, 1)) %>% 
# I don't know also. How do I set input_shape?

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.5) %>%
layer_dense(units = 64, activation = "relu") %>%
layer_dense(units = 10, activation = "softmax")

# Compile the model
model_cnn_1 %>% compile(
optimizer = 'rmsprop',
loss = 'categorical_crossentropy',
metrics = 'accuracy')

# Train the model
history <- model_cnn_1 %>% fit(
x_train_cnn, y_train_cnn,
epochs = 20,
batch_size = 32,
validation_split = 0.2)

#Train model
history <- model %>%
fit(x_train, y_train,
epochs = 50, batch_size = 32,
validation_split = 0.2)`

Ошибка:

Ошибка в py_call_impl (вызываемая, точка $ args,dots $ keyword): ValueError: Ошибка при проверке ввода: ожидалось, что conv2d_28_input будет иметь 4 измерения, но получит массив с формой (7000, 23, 1)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...