Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Fix structure for 2d CNN
train_array <- t(trainData$X)
dim(train_array) <- c(50, 50, nrow(trainData$X), 1)
# Reorder dimensions
train_array <- aperm(train_array, c(3,1,2,4))
test_array <- t(testData)
dim(test_array) <- c(50, 50, nrow(testData), 1)
# Reorder dimensions
test_array <- aperm(test_array, c(3,1,2,4))
# Check cat again
testCat <- train_array[2,,,]
image(t(apply(testCat, 2, rev)), col = gray.colors(12),
axes = F)
# Build CNN model
model <- keras_model_sequential()
model %>%
layer_conv_2d(kernel_size = c(3, 3), filter = 32,
activation = "relu", padding = "same",
input_shape = c(50, 50, 1),
data_format = "channels_last") %>%
layer_conv_2d(kernel_size = c(3, 3), filter = 32,
activation = "relu", padding = "valid") %>%
layer_max_pooling_2d(pool_size = 2) %>%
layer_dropout(rate = 0.25) %>%
layer_conv_2d(kernel_size = c(3, 3), filter = 64, strides = 2,
activation = "relu", padding = "same") %>%
layer_conv_2d(kernel_size = c(3, 3), filter = 64,
activation = "relu", padding = "valid") %>%
layer_max_pooling_2d(pool_size = 2) %>%
layer_dropout(rate = 0.25) %>%
layer_flatten() %>%
layer_dense(units = 50, activation = "relu") %>%
layer_dropout(rate = 0.25) %>%
layer_dense(units = 1, activation = "sigmoid")
summary(model)
model %>% compile(
loss = 'binary_crossentropy',
optimizer = "adam",
metrics = c('accuracy')
)
history <- model %>% fit(
x = train_array, y = as.numeric(trainData$y),
epochs = 30, batch_size = 100,
validation_split = 0.2
)
plot(history)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.