Skip to content

Networks

Flax modules used as policies and classifiers by the reinforcement learning and vision problems.

Neural network architectures for problems.

MLP

Bases: Module

MLP module.

A stack of dense layers. activation follows every layer except the last, whose output is passed through output_fn.

Attributes:

  • layer_sizes (Sequence[int]) –

    Number of units of every dense layer. The last entry is the output size.

  • kernel_init (Callable) –

    Initializer of the dense kernels.

  • bias_init (Callable) –

    Initializer of the dense biases.

  • activation (Callable) –

    Activation function applied after every layer except the last.

  • output_fn (Callable) –

    Function (x, key) -> output applied to the output of the last layer, e.g. identity_output_fn, categorical_output_fn or tanh_output_fn.

  • use_bias (bool) –

    If True, the dense layers have a bias.

  • layer_norm (bool) –

    If True, apply layer normalization to the result of output_fn.

__call__

__call__(x: Array, key: Array | None = None)

Forward pass of the MLP.

Parameters:

  • x (Array) –

    Input array; the dense layers act on its last axis.

  • key (Array | None, default: None ) –

    PRNG key passed to output_fn.

Returns:

  • –

    The result of output_fn.

CNN

Bases: Module

CNN module.

A stack of convolutional layers, each followed by activation, whose flattened output is passed to an MLP head.

Attributes:

  • num_filters (Sequence[int]) –

    Number of filters of every convolutional layer.

  • kernel_sizes (Sequence[tuple]) –

    Kernel size of every convolutional layer.

  • strides (Sequence[tuple]) –

    Strides of every convolutional layer.

  • mlp_layer_sizes (Sequence[int]) –

    Number of units of every dense layer of the MLP head. The last entry is the output size.

  • kernel_init (Callable) –

    Initializer of the convolutional and dense kernels.

  • bias_init (Callable) –

    Initializer of the convolutional and dense biases.

  • activation (Callable) –

    Activation function applied after every convolutional layer and inside the MLP head.

  • output_fn (Callable) –

    Function (x, key) -> output applied to the output of the last dense layer, e.g. identity_output_fn, categorical_output_fn or tanh_output_fn.

  • use_bias (bool) –

    If True, the convolutional and dense layers have a bias.

__call__

__call__(x: Array, key: Array | None = None)

Forward pass of the CNN.

Parameters:

  • x (Array) –

    Batch of inputs with a leading batch axis and a trailing channel axis.

  • key (Array | None, default: None ) –

    PRNG key passed to output_fn.

Returns:

  • –

    The result of output_fn, with one row per batch element.

identity_output_fn

identity_output_fn(x: Array, key: Array | None = None) -> jax.Array

Identity output function.

Parameters:

  • x (Array) –

    Output of the last network layer.

  • key (Array | None, default: None ) –

    Unused. Present so that all output functions share one signature.

Returns:

  • Array –

    x unchanged.

categorical_output_fn

categorical_output_fn(x: Array, key: Array) -> jax.Array

Categorical sample from logits.

Parameters:

  • x (Array) –

    Unnormalized logits. Categories are indexed along the last axis.

  • key (Array) –

    PRNG key.

Returns:

  • Array –

    The sampled category indices.

tanh_output_fn

tanh_output_fn(x: Array, key: Array | None = None) -> jax.Array

Tanh output function.

Parameters:

  • x (Array) –

    Output of the last network layer.

  • key (Array | None, default: None ) –

    Unused. Present so that all output functions share one signature.

Returns:

  • Array –

    tanh(x), computed elementwise.