# Contrib¶

## Modules¶

### Layers¶

class Residual : public fl::Container

A module for creating a generic Residual block as given by He et al (2015) and Kumar et al (2015).

Example:

auto res = Residual();
res.add(Conv2D(30, 50, 9, 7, 2, 3, 3, 2));
// Add a shortcut from the input to the block to the third layer
// Add a shortcut from the second layer to the output
// Scale the inputs to the third layer by some constant

// Create a model
Sequential model;
// ...
// Add our residual block as needed
model.add(Pool2D(2, 3, 1, 1, 1, 1, PoolingMode::MAX));
// ...


Public Functions

void addScale(int beforeLayer, float scale)

Adds a scaling factor to all residual connections connecting to a layer given by some index index.

Given some scale $$\alpha$$, the input to beforeLayer becomes $$(x + f(x)) * \alpha$$.

Parameters
• [in] beforeLayer: the index of the layer to which to scale the input and residual connection output.

• [in] scale: the value by which to scale the sum of the previous layer and output of the residual connection.

void addShortcut(int fromLayer, int toLayer)

Adds a shortcut between two layers.

Parameters
• [in] fromLayer: the layer index from which the skip connection will originate; must be in the range $$[0, N_{layers} - 1]$$. If the index 0 is used, the input to the shortcut will be equal to the input to the residual block.

• [in] toLayer: the layer index to which the skip connection outputs a tensor; must be in the range $$[1, N_{layers} + 1]$$. If the index $$N_{layers} + 1$$ is used, the output of the shortcut will be added to the output of the entire residual block.

template<typename T>
void addShortcut(int fromLayer, int toLayer, const T &module)

See Residual::addShortcut.

template<typename T>
void addShortcut(int fromLayer, int toLayer, std::shared_ptr<T> module)

Adds a shortcut connection between two layers such that tensors passed through the connection are forwarded through a passed module before being added to the resultant module’s input.

Can be used to reshape the output of input module to match the input dimensions for the output module.

Parameters
• [in] fromLayer: the layer index from which the shortcut connection will originate; must be in the range $$[0, N_{layers} - 1]$$. If the index 0 is used, the input to the shortcut will be equal to the input to the residual block.

• [in] toLayer: the layer index to which the shortcut connection outputs a tensor; must be in the range $$[1, N_{layers} + 1]$$. If the index $$N_{layers} + 1$$ is used, the output of the shortcut will be added to the output of the entire residual block.

• [in] module: a specified module through which the input to the shortcut connection will be forwarded before being added to the input to the destination module.

std::vector<Variable> forward(const std::vector<Variable> &inputs)

Performs forward computation for the module, given some inputs.

Return

a vector of Variable tensors containing the result of the forward computation

Parameters
• inputs: the values to compute forward computation for the module.

std::string prettyString() const

Generates a stringified representation of the module.

Return

a string containing the module label