The “layer_conv_2d” Function in R

  • Package: keras

  • Purpose: Perform 2D convolution on input data.

  • General class: Layer

  • Required argument(s):

    • filters: Number of output filters in the convolution.

    • kernel_size: Dimensions of the convolution window.

  • Notable optional arguments:

    • strides: Stride length of the convolution.

    • padding: Padding mode (‘valid’ or ‘same’).

    • activation: Activation function to use.

    • input_shape: Shape of the input data (required for the first layer in the model).

  • Example:

  • # Load the required library
    library(keras)

    # Define a simple neural network model with a 2D convolutional layer
    model <- keras_model_sequential() %>%
    layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = 'relu', input_shape = c(28, 28, 1)) %>%
    layer_max_pooling_2d(pool_size = c(2, 2)) %>%
    layer_flatten() %>%
    layer_dense(units = 128, activation = 'relu') %>%
    layer_dense(units = 10, activation = 'softmax')

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

    # Summary of the model
    summary(model)

  • In this example, the layer_conv_2d function from the keras package is used to add a 2D convolutional layer to the neural network model. The convolutional layer has 32 filters, each of size 3x3, and uses the ReLU activation function. The input_shape argument specifies the shape of the input data, which is required for the first layer in the model. The model continues with a max pooling layer, a flatten layer, and two dense layers to perform classification.

Previous
Previous

The “layer_max_pooling_2d” Function in R

Next
Next

The “layer_flatten” Function in R