create_architecture: Create a CNN Architecture

View source: R/cnn.R

create_architectureR Documentation

Create a CNN Architecture

Description

This function constructs a citoarchitecture object that defines the architecture of a Convolutional Neural Network (CNN). The citoarchitecture object can be used by the cnn function to specify the structure of the network, including layer types, parameters, and default values.

Usage

create_architecture(
  ...,
  default_n_neurons = 10,
  default_n_kernels = 10,
  default_kernel_size = list(conv = 3, maxPool = 2, avgPool = 2),
  default_stride = list(conv = 1, maxPool = NULL, avgPool = NULL),
  default_padding = list(conv = 0, maxPool = 0, avgPool = 0),
  default_dilation = list(conv = 1, maxPool = 1),
  default_bias = list(conv = TRUE, linear = TRUE),
  default_activation = list(conv = "relu", linear = "relu"),
  default_normalization = list(conv = FALSE, linear = FALSE),
  default_dropout = list(conv = 0, linear = 0)
)

Arguments

...

Objects of class citolayer created by linear, conv, maxPool, avgPool, or transfer. These layers define the architecture of the CNN.

default_n_neurons

(integer) Default number of neurons in a linear layer. Default is 10.

default_n_kernels

(integer) Default number of kernels in a convolutional layer. Default is 10.

default_kernel_size

(integer or tuple) Default size of kernels in convolutional and pooling layers. Can be a single integer or a tuple if sizes differ across dimensions. Default is list(conv = 3, maxPool = 2, avgPool = 2).

default_stride

(integer or tuple) Default stride of kernels in convolutional and pooling layers. Can be a single integer, a tuple if strides differ across dimensions, or NULL to use the kernel size. Default is list(conv = 1, maxPool = NULL, avgPool = NULL).

default_padding

(integer or tuple) Default zero-padding added to both sides of the input. Can be a single integer or a tuple if padding differs across dimensions. Default is list(conv = 0, maxPool = 0, avgPool = 0).

default_dilation

(integer or tuple) Default dilation of kernels in convolutional and max pooling layers. Can be a single integer or a tuple if dilation differs across dimensions. Default is list(conv = 1, maxPool = 1).

default_bias

(boolean) Default value indicating if a learnable bias should be added to neurons of linear layers and kernels of convolutional layers. Default is list(conv = TRUE, linear = TRUE).

default_activation

(character) Default activation function used after linear and convolutional layers. Supported activation functions include "relu", "leaky_relu", "tanh", "elu", "rrelu", "prelu", "softplus", "celu", "selu", "gelu", "relu6", "sigmoid", "softsign", "hardtanh", "tanhshrink", "softshrink", "hardshrink", "log_sigmoid". Default is list(conv = "relu", linear = "relu").

default_normalization

(boolean) Default value indicating if batch normalization should be applied after linear and convolutional layers. Default is list(conv = FALSE, linear = FALSE).

default_dropout

(numeric) Default dropout rate for linear and convolutional layers. Set to 0 for no dropout. Default is list(conv = 0.0, linear = 0.0).

Details

This function creates a citoarchitecture object that outlines the CNN's architecture based on the provided layers and default parameters. The final architecture consists of layers in the order they are provided. Any unspecified parameters in the citolayer objects are filled with the provided default values for their respective layer types. Defaults can be specified for each layer type individually or for all layers at once.

Value

An S3 object of class "citoarchitecture" that encapsulates the architecture of the CNN.

Author(s)

Armin Schenk

See Also

cnn, linear, conv, maxPool, avgPool, transfer, print.citoarchitecture, plot.citoarchitecture

Examples


if(torch::torch_is_installed()){
library(cito)

# Convolutional layers with different n_kernels and kernel_sizes
c1 <- conv(n_kernels = 8, kernel_size = 5)
c2 <- conv(n_kernels = 16, kernel_size = 3)

# Linear layer
l <- linear(n_neurons = 100)

# MaxPooling layer
mP <- maxPool(kernel_size = 2)

# Create the architecture by using the created layers
# Change the defaults with which the not assigned layer parameters will be filled e.g.
# change default dropout to different values for linear and convolutional layer
# only change the default normalization for linear layers
# change default activation of both linear and convolutional layers to 'selu'
architecture <- create_architecture(c1, c1, mP, c2, c2, mP, l,
                                    default_dropout = list(linear=0.6, conv=0.4),
                                    default_normalization = list(linear=TRUE),
                                    default_activation = "selu")

# See how the finished CNN would look like for specific input and output shapes
print(architecture, c(3,128,128), 10)

# To use predefined architectures  use the transfer() layer
alexnet <- transfer("alexnet")

# No other linear layers are used after the transfer layer:
# The cnn() function will only replace the last linear layer of the architecture
# to match the output dimensions of the data
architecture <- create_architecture(alexnet)
print(architecture, c(3,128,128), 10)

# Some linear layers are used after the transfer layer:
# The cnn() function will replace the whole "classifier" part of the architecture
# with the specified linear layers + an output layer that matches the output dimensions
architecture <- create_architecture(alexnet, linear(300), linear(100))
print(architecture, c(3,128,128), 10)
}


citoverse/cito documentation built on Jan. 16, 2025, 11:49 p.m.