使用数据集
行:10000
列:24
目标克隆= 24日
我正在尝试使用R-Keras编写CNN代码。
但是我不知道如何设置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(可调用,dots $ args,dots $ keywords)错误: ValueError:检查输入时出错:预期conv2d_28_input具有4维,但数组的形状为(7000,23,1)