About Documentation Tutorial

Documentation layers.hpp

Flint's C++ Deep Learning Framework

Layers in this architecture are graph nodes. Use the factory helpers in
FlintDL
(e.g.
conv2d
,
dense
,
maxpool2d
) to build a model quickly, or derive from
LayerGraph
to implement custom behavior.

src/dl/layers.hpp

Overview

Types and Functions
 • struct LayerGraph
  • virtual void forward() = 0
 • struct Variable : public LayerGraph
 • struct ConstantNode : public LayerGraph
 • struct InputNode : public LayerGraph
 • struct Relu : public LayerGraph
 • struct Dropout : public LayerGraph
 • struct Softmax : public LayerGraph
  • Softmax(int axis = -1) : LayerGraph(1), axis(axis)
 • struct Flatten : public LayerGraph
 • struct Add : public LayerGraph
 • struct Convolve : public LayerGraph
 • struct MaxPool : public LayerGraph
 • struct AvgPool : public LayerGraph
 • struct GlobalAvgPool : public LayerGraph
 • struct BatchNorm : public LayerGraph
 • struct Connected : public LayerGraph
 • namespace FlintDL
  • enum class PaddingMode
  • enum class InitializerKind
  • enum class ActivationKind
  • struct Conv2DOptions
  • struct DenseOptions
  • struct Pool2DOptions
  • inline LayerGraph *activation_layer(ActivationKind activation)
  • inline LayerGraph *apply_activation(LayerGraph *in, ActivationKind activation)
  • inline Convolve *conv2d(size_t filters, std::array<size_t, 2> kernel_size, size_t in_channels, const Conv2DOptions &options = Conv2DOptions())
  • inline Connected *dense(size_t in_units, size_t out_units, const DenseOptions &options = DenseOptions())
  • inline MaxPool *maxpool2d(std::array<size_t, 2> kernel_size, const Pool2DOptions &options = Pool2DOptions())
  • inline MaxPool *maxpool2d(std::array<size_t, 2> kernel_size, std::array<unsigned int, 2> stride)
  • inline AvgPool *avgpool2d(std::array<size_t, 2> kernel_size, const Pool2DOptions &options = Pool2DOptions())
  • inline AvgPool *avgpool2d(std::array<size_t, 2> kernel_size, std::array<unsigned int, 2> stride)
  • inline Flatten *flatten()
  • inline Relu *relu()
  • inline Softmax *softmax(int axis = -1)
  • inline Dropout *dropout(float probability)

struct LayerGraph 

Base class for all layer graph nodes.
virtual void forward() = 0

Computes the layer output in
output
by the output of the previous layers. The framework makes sure that the output of the incoming layers exists.
struct Variable : public LayerGraph 

Represents a learnable tensor
struct ConstantNode : public LayerGraph 

Represents a constant tensor
struct InputNode : public LayerGraph 

Input Layers
struct Relu : public LayerGraph 

Relu activation layer
struct Dropout : public LayerGraph 

Dropout layer (randomly sets entries of its input to 0 to avoid overfitting). The dropout probability is given by a second input (expects a 1D constant float).
struct Softmax : public LayerGraph 

Softmax classification layer
Softmax(int axis = -1) : LayerGraph(1), axis(axis) 

Initializes the SoftMax function with an optional axis parameter that describes the dimension of which the sum will be taken (may be negative in which case it will index from back, i.e. -1 means the last axis,
  • the one before the last etc.). Calculates
    exp(in)
    
/ sum(in, ax)
struct Flatten : public LayerGraph 

Flattens a dimension of its input
struct Add : public LayerGraph 

Sums to inputs
struct Convolve : public LayerGraph 

Convolution layer. Slides a set of filter kernels along the input and optionally adds a bias. The input has
[batch, channels, h, w, ...]
, the kernel weights have
[filters, channels, p, q, ...]
, the bias
[filters]
and the output has
[batch, filters, y, x, ...]
where
y
and
x
depend on
stride
and
padding
.
stride
defines the step size of the kernel window and
padding
adds a zero border around the spatial dimensions to control the output size and include the border elements.
struct MaxPool : public LayerGraph 

Max pooling layer. Reduces each spatial window to its maximum value. The window size is given by
kernel_shape
, the step size by
stride
. Padding works like for convolution: it adds a zero border around the spatial dimensions before pooling. Batch and channel dimensions are preserved.
struct AvgPool : public LayerGraph 

Average pooling layer. Reduces each spatial window to its average value. The window size is given by
kernel_shape
, the step size by
stride
, and padding behaves like in convolution. Batch and channel dimensions are preserved.
struct GlobalAvgPool : public LayerGraph 

Global average pooling layer. Averages over all spatial dimensions so each channel is reduced to a single value per batch. The output keeps the batch and channel dimensions and sets all spatial dimensions to 1.
struct BatchNorm : public LayerGraph 

Batch normalization layer. Normalizes the input per channel and applies a learnable scale (gamma) and bias (beta). If running mean and variance are provided they are updated with momentum
alpha
when training, otherwise they are computed on the fly.
struct Connected : public LayerGraph 

Fully connected (dense) layer.
namespace FlintDL 

High level layer factories and configuration helpers.
enum class PaddingMode 

Padding for convolution and pooling.
  • Valid
    uses no padding (only fully covered windows are used)
  • Same
    adds padding so the output keeps its spatial size (for odd kernels)
  • Explicit
    uses the
    padding
    values from the options
enum class InitializerKind 

Weight initializer kind for trainable weights.
enum class ActivationKind 

Activation layer kind for builder helpers.
struct Conv2DOptions 

Options for a 2D convolution layer.
  • stride
    step size of the kernel window
  • padding_mode
    controls how padding is generated
  • padding
    is used only if
    padding_mode
    is
    Explicit
  • use_bias
    adds a trainable bias per filter
  • bias_init
    initializes the bias if enabled
  • kernel_initializer
    selects the weight initializer
  • uniform_min
    /
    uniform_max
    are used for the uniform initializer
  • kernel_constant
    is used for constant initialization
struct DenseOptions 

Options for a dense layer.
  • use_bias
    adds a trainable bias
  • bias_init
    initializes the bias if enabled
  • transpose_input
    transposes the input before multiplication
  • transpose_kernel
    transposes the kernel weights before multiplication
  • kernel_initializer
    selects the weight initializer
struct Pool2DOptions 

Options for pooling layers.
  • stride
    step size of the pooling window (0 means kernel size)
  • padding_mode
    controls how padding is generated
  • padding
    is used only if
    padding_mode
    is
    Explicit
inline LayerGraph *activation_layer(ActivationKind activation) 

Constructs an Activation Layer from an
ActivationKind
(None, Relu, or Softmax).
inline LayerGraph *apply_activation(LayerGraph *in, ActivationKind activation) 

Chains a newly constructed activation layer (from a
ActivationKind
) to its input node (i.e. input -> activation) and returns the activation layer.
inline Convolve *conv2d(size_t filters, std::array<size_t, 2> kernel_size,
						size_t in_channels,
						const Conv2DOptions &options = Conv2DOptions()) 

Constructs a 2D convolution layer and initializes its weights.
  • filters
    number of filter kernels
  • kernel_size
    shape of filter kernels
  • in_channels
    number of channels of the input image (e.g., 3 for RGB)
  • options
    control stride, padding, bias and initialization
inline Connected *dense(size_t in_units, size_t out_units,
						const DenseOptions &options = DenseOptions()) 

Constructs a fully connected layer and initializes its weights.
  • in_units
    number of input units
  • out_units
    number of output units
  • options
    control bias, transpositions and initialization
inline MaxPool *maxpool2d(std::array<size_t, 2> kernel_size,
						  const Pool2DOptions &options = Pool2DOptions()) 

Constructs a max pooling layer.
  • kernel_size
    pooling window size
  • options
    control stride and padding
inline MaxPool *maxpool2d(std::array<size_t, 2> kernel_size,
						  std::array<unsigned int, 2> stride) 

Constructs a max pooling layer with explicit stride.
inline AvgPool *avgpool2d(std::array<size_t, 2> kernel_size,
						  const Pool2DOptions &options = Pool2DOptions()) 

Constructs an average pooling layer.
  • kernel_size
    pooling window size
  • options
    control stride and padding
inline AvgPool *avgpool2d(std::array<size_t, 2> kernel_size,
						  std::array<unsigned int, 2> stride) 

Constructs an average pooling layer with explicit stride.
inline Flatten *flatten() 

Constructs a flatten layer.
inline Relu *relu() 

Constructs a ReLU activation layer.
inline Softmax *softmax(int axis = -1) 

Constructs a softmax activation layer.
inline Dropout *dropout(float probability) 

Constructs a dropout layer with the given probability.