pub struct MlpClassifier {
pub w1: Vec<f32>,
pub b1: Vec<f32>,
pub w2: Vec<f32>,
pub b2: Vec<f32>,
/* private fields */
}Expand description
A small two-layer MLP classifier: input → linear → ReLU → linear → logits.
Weights are stored row-major:
w1has shape[input_dim, hidden_dim]w2has shape[hidden_dim, output_dim]
Fields§
§w1: Vec<f32>Input-to-hidden weights, shape [input_dim, hidden_dim].
b1: Vec<f32>Hidden bias, length hidden_dim.
w2: Vec<f32>Hidden-to-output weights, shape [hidden_dim, output_dim].
b2: Vec<f32>Output bias, length output_dim.
Implementations§
Source§impl MlpClassifier
impl MlpClassifier
Sourcepub fn new(
input_dim: usize,
hidden_dim: usize,
output_dim: usize,
seed: u32,
) -> Self
pub fn new( input_dim: usize, hidden_dim: usize, output_dim: usize, seed: u32, ) -> Self
Create a fresh classifier with Xavier-like random weights.
Sourcepub fn forward(&self, x: &[f32]) -> MlpForward
pub fn forward(&self, x: &[f32]) -> MlpForward
Forward pass for a single input vector.
Hidden state (post-ReLU) for a single input vector, without the hidden-to-output projection. Useful for FFN-head diagnostics and confidence-gated routing where logits may not be needed.
Sourcepub fn probabilities(&self, x: &[f32]) -> Vec<f32>
pub fn probabilities(&self, x: &[f32]) -> Vec<f32>
Class probabilities for a single input vector.
Sourcepub fn loss(&self, x: &[f32], target: usize) -> f32
pub fn loss(&self, x: &[f32], target: usize) -> f32
Cross-entropy loss for a single input/target pair.
Sourcepub fn backward(&self, x: &[f32], target: usize) -> MlpGradients
pub fn backward(&self, x: &[f32], target: usize) -> MlpGradients
Back-propagation for a single input/target pair.
Sourcepub fn apply_sgd(&mut self, grad: &MlpGradients, lr: f32)
pub fn apply_sgd(&mut self, grad: &MlpGradients, lr: f32)
Apply a single SGD step using the supplied gradients.
Sourcepub fn forward_batch(&self, x: &[f32], batch_size: usize) -> MlpForwardBatch
pub fn forward_batch(&self, x: &[f32], batch_size: usize) -> MlpForwardBatch
Forward pass for a batch of inputs.
x is a row-major flat buffer of shape [batch_size, input_dim].
Sourcepub fn loss_batch(&self, x: &[f32], targets: &[usize], batch_size: usize) -> f32
pub fn loss_batch(&self, x: &[f32], targets: &[usize], batch_size: usize) -> f32
Cross-entropy loss averaged over a batch.
Sourcepub fn backward_batch(
&self,
x: &[f32],
targets: &[usize],
batch_size: usize,
) -> (MlpGradients, Vec<f32>, f32)
pub fn backward_batch( &self, x: &[f32], targets: &[usize], batch_size: usize, ) -> (MlpGradients, Vec<f32>, f32)
Back-propagation for a batch of input/target pairs.
Returns gradients averaged over the batch, the input gradient
dx as a row-major [batch_size, input_dim] buffer, and the average
cross-entropy loss. A single SGD/Adam step with the returned gradients
is equivalent to one step per mini-batch (not one step per example).
Sourcepub fn flatten_params(&self) -> Vec<f32>
pub fn flatten_params(&self) -> Vec<f32>
Flatten all learnable parameters into one vector, ordered
[w1, b1, w2, b2].
Sourcepub fn flatten_grad(&self, grad: &MlpGradients) -> Vec<f32>
pub fn flatten_grad(&self, grad: &MlpGradients) -> Vec<f32>
Flatten the gradients for a single example in the same order as
Self::flatten_params.
Sourcepub fn load_flat_params(&mut self, params: &[f32])
pub fn load_flat_params(&mut self, params: &[f32])
Restore parameters from a flat vector produced by Self::flatten_params.
Trait Implementations§
Source§impl Clone for MlpClassifier
impl Clone for MlpClassifier
Source§fn clone(&self) -> MlpClassifier
fn clone(&self) -> MlpClassifier
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for MlpClassifier
impl RefUnwindSafe for MlpClassifier
impl Send for MlpClassifier
impl Sync for MlpClassifier
impl Unpin for MlpClassifier
impl UnsafeUnpin for MlpClassifier
impl UnwindSafe for MlpClassifier
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more