pub struct OnlineLearner {
pub algorithm: OnlineAlgorithm,
pub weights: Vec<f64>,
pub bias: f64,
pub dims: usize,
pub loss_fn: OlLossFunction,
pub velocity: Vec<f64>,
/* private fields */
}Expand description
Online / incremental learner supporting Perceptron, Passive-Aggressive, and SGD-with-Momentum algorithms.
The learner maintains a weight vector w ∈ ℝᵈ and a scalar bias, updated
sample-by-sample via the selected OnlineAlgorithm.
Fields§
§algorithm: OnlineAlgorithmThe update algorithm in use.
weights: Vec<f64>Current weight vector.
bias: f64Scalar bias term.
dims: usizeDimensionality (number of features).
loss_fn: OlLossFunctionLoss function for computing per-sample losses.
velocity: Vec<f64>Velocity buffer for SGD-with-Momentum (zero for other algorithms).
Implementations§
Source§impl OnlineLearner
impl OnlineLearner
Sourcepub fn new(
algorithm: OnlineAlgorithm,
dims: usize,
loss_fn: OlLossFunction,
) -> Self
pub fn new( algorithm: OnlineAlgorithm, dims: usize, loss_fn: OlLossFunction, ) -> Self
Create a new OnlineLearner with zero-initialised weights.
§Arguments
algorithm— update rule to apply on eachupdate()call.dims— feature dimensionality; all input vectors must have exactlydimselements.loss_fn— loss function used for reporting and SGD gradient computation.
§Panics
Does not panic; returns a well-formed OnlineLearner even for dims == 0.
Sourcepub fn predict(&self, features: &[f64]) -> Result<f64, LearnerError>
pub fn predict(&self, features: &[f64]) -> Result<f64, LearnerError>
Compute the raw decision score: dot(weights, features) + bias.
§Errors
Returns LearnerError::EmptyInput if features is empty when
dims > 0, or LearnerError::DimensionMismatch if
features.len() != dims.
Sourcepub fn predict_class(&mut self, features: &[f64]) -> Result<i32, LearnerError>
pub fn predict_class(&mut self, features: &[f64]) -> Result<i32, LearnerError>
Sourcepub fn classify(&self, features: &[f64]) -> Result<i32, LearnerError>
pub fn classify(&self, features: &[f64]) -> Result<i32, LearnerError>
A non-mutating variant of predict_class that
does not update internal prediction statistics.
Useful for evaluation loops where you want to call accuracy() later
without double-counting.
Sourcepub fn loss(&self, score: f64, label: f64) -> f64
pub fn loss(&self, score: f64, label: f64) -> f64
Compute the loss for a given (score, label) pair using the learner’s
configured OlLossFunction.
| Loss | Formula |
|---|---|
| Hinge | max(0, 1 − label · score) |
| SquaredHinge | max(0, 1 − label · score)² |
| LogLoss | ln(1 + exp(−label · score)) (stable) |
Sourcepub fn update(&mut self, sample: &TrainingSample) -> Result<f64, LearnerError>
pub fn update(&mut self, sample: &TrainingSample) -> Result<f64, LearnerError>
Perform a single online update for sample and return the pre-update
loss.
§Errors
LearnerError::EmptyInput—sample.featuresis empty butdims > 0.LearnerError::DimensionMismatch— feature length ≠dims.LearnerError::InvalidLabel— label is not ±1.0 for Perceptron or Passive-Aggressive (binary classifiers).
Sourcepub fn batch_update(
&mut self,
samples: &[TrainingSample],
) -> Result<Vec<f64>, LearnerError>
pub fn batch_update( &mut self, samples: &[TrainingSample], ) -> Result<Vec<f64>, LearnerError>
Sourcepub fn accuracy(&self, samples: &[TrainingSample]) -> Result<f64, LearnerError>
pub fn accuracy(&self, samples: &[TrainingSample]) -> Result<f64, LearnerError>
Compute the fraction of samples correctly classified without updating
weights.
Classification is performed via classify so the
internal total_predictions counter is not incremented.
§Errors
LearnerError::EmptyInput—samplesis empty.- Propagates dimension/label errors from
classify.
Sourcepub fn stats(&self) -> OnlineLearnerStats
pub fn stats(&self) -> OnlineLearnerStats
Snapshot current training statistics.
Sourcepub fn average_loss(
&self,
samples: &[TrainingSample],
) -> Result<f64, LearnerError>
pub fn average_loss( &self, samples: &[TrainingSample], ) -> Result<f64, LearnerError>
Compute the average loss over a slice of samples without updating weights.
§Errors
Returns LearnerError::EmptyInput if samples is empty, or
propagates dimension errors.
Sourcepub fn evaluate_losses(
&self,
samples: &[TrainingSample],
) -> Result<Vec<f64>, LearnerError>
pub fn evaluate_losses( &self, samples: &[TrainingSample], ) -> Result<Vec<f64>, LearnerError>
Compute per-sample losses over samples without updating weights.
§Errors
Returns LearnerError::EmptyInput if samples is empty.
Sourcepub fn record_prediction(&mut self, was_correct: bool)
pub fn record_prediction(&mut self, was_correct: bool)
Record a correct/incorrect prediction result into the running stats.
This is used internally when predict_class is called. Exposed
publicly for external evaluation loops that use classify() and wish
to manually feed outcomes back.
Trait Implementations§
Source§impl Clone for OnlineLearner
impl Clone for OnlineLearner
Source§fn clone(&self) -> OnlineLearner
fn clone(&self) -> OnlineLearner
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 OnlineLearner
impl RefUnwindSafe for OnlineLearner
impl Send for OnlineLearner
impl Sync for OnlineLearner
impl Unpin for OnlineLearner
impl UnsafeUnpin for OnlineLearner
impl UnwindSafe for OnlineLearner
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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