Skip to main content

SmallTsetlinMachine

Struct SmallTsetlinMachine 

Source
pub struct SmallTsetlinMachine<const N: usize, const C: usize> { /* private fields */ }
Expand description

Binary classification Tsetlin Machine with compile-time known dimensions.

Stack-allocated with zero heap allocations. All loops unrolled at compile time.

§Type Parameters

  • N - Number of features (compile-time constant)
  • C - Number of clauses (compile-time constant, must be even)

§Performance

Up to 3x faster than dynamic TsetlinMachine for small dimensions due to:

  • No heap allocations
  • Full loop unrolling
  • Better cache locality

§Example

use tsetlin_rs::SmallTsetlinMachine;

// XOR with 2 features and 20 clauses
let mut tm: SmallTsetlinMachine<2, 20> = SmallTsetlinMachine::new(100, 15);

let x = [[0, 0], [0, 1], [1, 0], [1, 1]];
let y = [0u8, 1, 1, 0];

tm.fit(&x, &y, 200, 42);
assert!(tm.evaluate(&x, &y) >= 0.75);

Implementations§

Source§

impl<const N: usize, const C: usize> SmallTsetlinMachine<N, C>

Source

pub fn new(n_states: i16, threshold: i32) -> Self

Creates new machine with given states and threshold.

Half clauses get +1 polarity, half get -1.

§Panics

Debug-asserts that C is even.

Source

pub fn with_s(n_states: i16, threshold: i32, s: f32) -> Self

Creates machine with custom specificity parameter.

Source

pub const fn n_features(&self) -> usize

Returns the number of features (compile-time constant).

Source

pub const fn n_clauses(&self) -> usize

Returns the number of clauses (compile-time constant).

Source

pub fn threshold(&self) -> f32

Returns current threshold.

Source

pub const fn clauses(&self) -> &[SmallClause<N>; C]

Returns read-only access to clauses.

Source

pub fn sum_votes(&self, x: &[u8; N]) -> i32

Sum of clause votes for input x.

Source

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

Predicts class (0 or 1).

Source

pub fn train_one(&mut self, x: &[u8; N], y: u8, rng: &mut impl Rng)

Trains on single example.

Source

pub fn fit(&mut self, x: &[[u8; N]], y: &[u8], epochs: usize, seed: u64)

Simple training for given epochs.

Source

pub fn evaluate(&self, x: &[[u8; N]], y: &[u8]) -> f32

Evaluates accuracy on test data.

Trait Implementations§

Source§

impl<const N: usize, const C: usize> Clone for SmallTsetlinMachine<N, C>

Source§

fn clone(&self) -> SmallTsetlinMachine<N, C>

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<const N: usize, const C: usize> Debug for SmallTsetlinMachine<N, C>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const N: usize, const C: usize> Freeze for SmallTsetlinMachine<N, C>

§

impl<const N: usize, const C: usize> RefUnwindSafe for SmallTsetlinMachine<N, C>

§

impl<const N: usize, const C: usize> Send for SmallTsetlinMachine<N, C>

§

impl<const N: usize, const C: usize> Sync for SmallTsetlinMachine<N, C>

§

impl<const N: usize, const C: usize> Unpin for SmallTsetlinMachine<N, C>

§

impl<const N: usize, const C: usize> UnwindSafe for SmallTsetlinMachine<N, C>

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