pub trait Layer<F: Float + Debug + ScalarOperand>: Send + Sync {
Show 17 methods
// Required methods
fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>>;
fn backward(
&self,
input: &Array<F, IxDyn>,
grad_output: &Array<F, IxDyn>,
) -> Result<Array<F, IxDyn>>;
fn update(&mut self, learningrate: F) -> Result<()>;
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
// Provided methods
fn params(&self) -> Vec<Array<F, IxDyn>> ⓘ { ... }
fn gradients(&self) -> Vec<Array<F, IxDyn>> ⓘ { ... }
fn set_gradients(&mut self, _gradients: &[Array<F, IxDyn>]) -> Result<()> { ... }
fn set_params(&mut self, _params: &[Array<F, IxDyn>]) -> Result<()> { ... }
fn set_training(&mut self, _training: bool) { ... }
fn is_training(&self) -> bool { ... }
fn layer_type(&self) -> &str { ... }
fn parameter_count(&self) -> usize { ... }
fn layer_description(&self) -> String { ... }
fn inputshape(&self) -> Option<Vec<usize>> { ... }
fn outputshape(&self) -> Option<Vec<usize>> { ... }
fn name(&self) -> Option<&str> { ... }
}
Expand description
Base trait for neural network layers
This trait defines the core interface that all neural network layers must implement. It supports forward propagation, backpropagation, parameter management, and training/evaluation mode switching.
Required Methods§
Sourcefn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>>
fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>>
Forward pass of the layer
Computes the output of the layer given an input tensor.
Sourcefn backward(
&self,
input: &Array<F, IxDyn>,
grad_output: &Array<F, IxDyn>,
) -> Result<Array<F, IxDyn>>
fn backward( &self, input: &Array<F, IxDyn>, grad_output: &Array<F, IxDyn>, ) -> Result<Array<F, IxDyn>>
Backward pass of the layer to compute gradients
Computes gradients with respect to the layer’s input, which is needed for backpropagation.
Sourcefn update(&mut self, learningrate: F) -> Result<()>
fn update(&mut self, learningrate: F) -> Result<()>
Update the layer parameters with the given learning rate
Sourcefn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
Get the layer as a mutable dyn Any for downcasting
Provided Methods§
Sourcefn set_gradients(&mut self, _gradients: &[Array<F, IxDyn>]) -> Result<()>
fn set_gradients(&mut self, _gradients: &[Array<F, IxDyn>]) -> Result<()>
Set the gradients of the layer parameters
Sourcefn set_params(&mut self, _params: &[Array<F, IxDyn>]) -> Result<()>
fn set_params(&mut self, _params: &[Array<F, IxDyn>]) -> Result<()>
Set the parameters of the layer
Sourcefn set_training(&mut self, _training: bool)
fn set_training(&mut self, _training: bool)
Set the layer to training mode (true) or evaluation mode (false)
Sourcefn is_training(&self) -> bool
fn is_training(&self) -> bool
Get the current training mode
Sourcefn layer_type(&self) -> &str
fn layer_type(&self) -> &str
Get the type of the layer (e.g., “Dense”, “Conv2D”)
Sourcefn parameter_count(&self) -> usize
fn parameter_count(&self) -> usize
Get the number of trainable parameters in this layer
Sourcefn layer_description(&self) -> String
fn layer_description(&self) -> String
Get a detailed description of this layer
Sourcefn inputshape(&self) -> Option<Vec<usize>>
fn inputshape(&self) -> Option<Vec<usize>>
Get the input shape if known
Sourcefn outputshape(&self) -> Option<Vec<usize>>
fn outputshape(&self) -> Option<Vec<usize>>
Get the output shape if known