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§
Sourcefn layer_type(&self) -> &str
fn layer_type(&self) -> &str
Forward pass through the layer. Get the layer type name for serialization.
fn forward( &self, inputs: &dyn ArrayProtocol, ) -> Result<Box<dyn ArrayProtocol>, OperationError>
Sourcefn parameters(&self) -> Vec<Box<dyn ArrayProtocol>>
fn parameters(&self) -> Vec<Box<dyn ArrayProtocol>>
Get the layer’s parameters.
Sourcefn parameters_mut(&mut self) -> Vec<&mut Box<dyn ArrayProtocol>>
fn parameters_mut(&mut self) -> Vec<&mut Box<dyn ArrayProtocol>>
Get mutable references to the layer’s parameters.
Sourcefn update_parameter(
&mut self,
name: &str,
value: Box<dyn ArrayProtocol>,
) -> Result<(), OperationError>
fn update_parameter( &mut self, name: &str, value: Box<dyn ArrayProtocol>, ) -> Result<(), OperationError>
Update a specific parameter by name
Sourcefn parameter_names(&self) -> Vec<String>
fn parameter_names(&self) -> Vec<String>
Get parameter names
Sourcefn is_training(&self) -> bool
fn is_training(&self) -> bool
Check if the layer is in training mode.
Provided Methods§
Sourcefn backward(
&self,
_input: &dyn ArrayProtocol,
_grad_output: &dyn ArrayProtocol,
) -> Result<LayerGrad, OperationError>
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".