Skip to main content

Layer

Trait Layer 

Source
pub trait Layer: Send + Sync {
    // Required methods
    fn layer_type(&self) -> &str;
    fn forward(
        &self,
        inputs: &dyn ArrayProtocol,
    ) -> Result<Box<dyn ArrayProtocol>, OperationError>;
    fn parameters(&self) -> Vec<Box<dyn ArrayProtocol>>;
    fn parameters_mut(&mut self) -> Vec<&mut Box<dyn ArrayProtocol>>;
    fn update_parameter(
        &mut self,
        name: &str,
        value: Box<dyn ArrayProtocol>,
    ) -> Result<(), OperationError>;
    fn parameter_names(&self) -> Vec<String>;
    fn train(&mut self);
    fn eval(&mut self);
    fn is_training(&self) -> bool;
    fn name(&self) -> &str;

    // Provided method
    fn backward(
        &self,
        _input: &dyn ArrayProtocol,
        _grad_output: &dyn ArrayProtocol,
    ) -> Result<LayerGrad, OperationError> { ... }
}
Expand description

Trait for neural network layers.

Required Methods§

Source

fn layer_type(&self) -> &str

Forward pass through the layer. Get the layer type name for serialization.

Source

fn forward( &self, inputs: &dyn ArrayProtocol, ) -> Result<Box<dyn ArrayProtocol>, OperationError>

Source

fn parameters(&self) -> Vec<Box<dyn ArrayProtocol>>

Get the layer’s parameters.

Source

fn parameters_mut(&mut self) -> Vec<&mut Box<dyn ArrayProtocol>>

Get mutable references to the layer’s parameters.

Source

fn update_parameter( &mut self, name: &str, value: Box<dyn ArrayProtocol>, ) -> Result<(), OperationError>

Update a specific parameter by name

Source

fn parameter_names(&self) -> Vec<String>

Get parameter names

Source

fn train(&mut self)

Set the layer to training mode.

Source

fn eval(&mut self)

Set the layer to evaluation mode.

Source

fn is_training(&self) -> bool

Check if the layer is in training mode.

Source

fn name(&self) -> &str

Get the layer’s name.

Provided Methods§

Source

fn backward( &self, _input: &dyn ArrayProtocol, _grad_output: &dyn ArrayProtocol, ) -> Result<LayerGrad, OperationError>

Backward pass through the layer: given the layer’s original input (as seen by the preceding forward call) and grad_output (the gradient of the loss with respect to this layer’s output), compute the gradient with respect to the input and every parameter.

forward does not cache any intermediate state, so implementations that need it (e.g. a pre-activation value) recompute it from input — cheap relative to the training pipeline’s own forward pass, and it keeps Layer object-safe and side-effect-free.

The default returns OperationError::NotImplemented so that existing third-party Layer implementations continue to compile; every layer defined in this module overrides it with a real implementation (or, where forward itself is already a documented simplification, an explicit and equally honest error).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§