Skip to main content

MultiClass

Struct MultiClass 

Source
pub struct MultiClass { /* private fields */ }
Expand description

Multi-class Tsetlin Machine using one-vs-all strategy.

Each class has its own set of clauses. Prediction is the class with the highest vote sum.

§Example

use tsetlin_rs::{Config, MultiClass};

let config = Config::builder().clauses(100).features(4).build().unwrap();
let mut tm = MultiClass::new(config, 3, 50);

// Train on data where label is class index (0, 1, or 2)
let x = vec![vec![1, 1, 0, 0], vec![0, 0, 1, 1]];
let y = vec![0, 1];
tm.fit(&x, &y, 100, 42);

Implementations§

Source§

impl MultiClass

Source

pub fn new(config: Config, n_classes: usize, threshold: i32) -> Self

Creates multi-class machine with given number of classes.

§Arguments
  • config - Machine configuration (clauses, features, etc.)
  • n_classes - Number of output classes
  • threshold - Vote threshold for training
Source

pub fn n_classes(&self) -> usize

Returns the number of classes.

Source

pub fn class_votes(&self, x: &[u8]) -> Vec<f32>

Computes vote sums for each class.

Source

pub fn predict(&self, x: &[u8]) -> usize

Predicts class with highest vote sum.

Returns 0 if votes are empty or contain NaN.

Source

pub fn train_one<R: Rng>(&mut self, x: &[u8], y: usize, rng: &mut R)

Trains on a single example.

Source

pub fn fit(&mut self, x: &[Vec<u8>], y: &[usize], epochs: usize, seed: u64)

Trains for given number of epochs.

§Arguments
  • x - Training inputs (binary features)
  • y - Class labels (0 to n_classes-1)
  • epochs - Number of training epochs
  • seed - Random seed for reproducibility
Source

pub fn evaluate(&self, x: &[Vec<u8>], y: &[usize]) -> f32

Evaluates classification accuracy on test data.

Returns fraction of correct predictions (0.0 to 1.0).

Source

pub fn quick( n_clauses: usize, n_features: usize, n_classes: usize, threshold: i32, ) -> Self

Quick constructor with sensible defaults.

§Panics

Panics if n_clauses is odd or zero, or n_features is zero.

Source

pub fn partial_fit(&mut self, x: &[u8], y: usize, seed: u64)

Trains on a single sample (online/incremental learning).

§Arguments
  • x - Single input sample (binary features)
  • y - Class label (0 to n_classes-1)
  • seed - Random seed for this update
Source

pub fn partial_fit_batch(&mut self, xs: &[Vec<u8>], ys: &[usize], seed: u64)

Trains on a mini-batch of samples (online/incremental learning).

§Arguments
  • xs - Batch of input samples
  • ys - Batch of class labels
  • seed - Random seed for this batch

Trait Implementations§

Source§

impl Clone for MultiClass

Source§

fn clone(&self) -> MultiClass

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MultiClass

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl TsetlinModel<Vec<u8>, usize> for MultiClass

Source§

fn fit(&mut self, x: &[Vec<u8>], y: &[usize], epochs: usize, seed: u64)

Trains the model on labeled data. Read more
Source§

fn predict(&self, x: &Vec<u8>) -> usize

Predicts label for a single sample.
Source§

fn evaluate(&self, x: &[Vec<u8>], y: &[usize]) -> f32

Evaluates model accuracy/performance on test data. Read more
Source§

fn predict_batch(&self, xs: &[X]) -> Vec<Y>

Batch prediction for multiple samples.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V