pub struct TsetlinMachine { /* private fields */ }Expand description
Implementations§
Source§impl TsetlinMachine
impl TsetlinMachine
Sourcepub fn new(config: Config, threshold: i32) -> Self
pub fn new(config: Config, threshold: i32) -> Self
§Overview
Creates new machine. Half clauses +1 polarity, half -1.
Sourcepub fn with_advanced(
config: Config,
threshold: i32,
advanced: AdvancedOptions,
) -> Self
pub fn with_advanced( config: Config, threshold: i32, advanced: AdvancedOptions, ) -> Self
§Overview
Creates machine with advanced options.
Sourcepub fn threshold(&self) -> f32
pub fn threshold(&self) -> f32
Returns current threshold value (may differ from base if adaptive).
Sourcepub fn threshold_base(&self) -> f32
pub fn threshold_base(&self) -> f32
§Overview
Base threshold (initial value).
Sourcepub fn reset_threshold(&mut self)
pub fn reset_threshold(&mut self)
§Overview
Resets threshold to base value.
Sourcepub fn predict_batch(&self, xs: &[Vec<u8>]) -> Vec<u8> ⓘ
pub fn predict_batch(&self, xs: &[Vec<u8>]) -> Vec<u8> ⓘ
§Overview
Batch prediction for multiple samples.
Sourcepub fn train_one<R: Rng>(&mut self, x: &[u8], y: u8, rng: &mut R)
pub fn train_one<R: Rng>(&mut self, x: &[u8], y: u8, rng: &mut R)
§Overview
Trains on single example with tracking.
Sourcepub fn update_weights(&mut self)
pub fn update_weights(&mut self)
§Overview
Updates clause weights. Call at end of epoch.
Sourcepub fn prune_dead_clauses(&mut self)
pub fn prune_dead_clauses(&mut self)
§Overview
Prunes dead clauses (low activation or weight).
Sourcepub fn reset_activations(&mut self)
pub fn reset_activations(&mut self)
§Overview
Resets activation counters. Call at start of epoch.
Sourcepub fn fit(&mut self, x: &[Vec<u8>], y: &[u8], epochs: usize, seed: u64)
pub fn fit(&mut self, x: &[Vec<u8>], y: &[u8], epochs: usize, seed: u64)
Simple training for given epochs.
§Arguments
x- Training inputs (binary features)y- Binary labels (0 or 1)epochs- Number of training epochsseed- Random seed for reproducibility
Sourcepub fn fit_with_options(
&mut self,
x: &[Vec<u8>],
y: &[u8],
opts: FitOptions,
) -> FitResult
pub fn fit_with_options( &mut self, x: &[Vec<u8>], y: &[u8], opts: FitOptions, ) -> FitResult
Sourcepub fn evaluate(&self, x: &[Vec<u8>], y: &[u8]) -> f32
pub fn evaluate(&self, x: &[Vec<u8>], y: &[u8]) -> f32
Evaluates accuracy on test data.
Returns fraction of correct predictions (0.0 to 1.0).
Sourcepub fn clause_weights(&self) -> Vec<f32>
pub fn clause_weights(&self) -> Vec<f32>
§Overview
Returns clause weights for inspection.
Sourcepub fn clause_activations(&self) -> Vec<u32>
pub fn clause_activations(&self) -> Vec<u32>
Returns clause activation counts.
Sourcepub fn quick(n_clauses: usize, n_features: usize, threshold: i32) -> Self
pub fn quick(n_clauses: usize, n_features: usize, threshold: i32) -> Self
Quick constructor with sensible defaults.
Equivalent to
Config::builder().clauses(n_clauses).features(n_features).build()
followed by TsetlinMachine::new(config, threshold).
§Panics
Panics if n_clauses is odd or zero, or n_features is zero.
Sourcepub fn to_sparse(&self) -> SparseTsetlinMachine
pub fn to_sparse(&self) -> SparseTsetlinMachine
Converts to sparse representation for memory-efficient inference.
Call after fit() to reduce memory by 5-100x depending on clause
sparsity. The sparse model supports prediction but not training.
§Memory Savings
| Features | Dense | Sparse (typical) | Reduction |
|---|---|---|---|
| 100 | 40 KB | 2 KB | 20x |
| 1000 | 400 KB | 8 KB | 50x |
| 10000 | 4 MB | 20 KB | 200x |
§Example
use tsetlin_rs::{Config, TsetlinMachine};
let config = Config::builder().clauses(20).features(100).build().unwrap();
let mut tm = TsetlinMachine::new(config, 10);
let x: Vec<Vec<u8>> = (0..100).map(|i| vec![(i % 2) as u8; 100]).collect();
let y: Vec<u8> = (0..100).map(|i| (i % 2) as u8).collect();
tm.fit(&x, &y, 100, 42);
// Convert to sparse
let sparse = tm.to_sparse();
// Verify same predictions
for xi in &x {
assert_eq!(tm.predict(xi), sparse.predict(xi));
}
// Check compression
let stats = sparse.memory_stats();
println!("Compression: {}x", stats.compression_ratio(100));Sourcepub fn partial_fit(&mut self, x: &[u8], y: u8, seed: u64)
pub fn partial_fit(&mut self, x: &[u8], y: u8, seed: u64)
Trains on a single sample (online/incremental learning).
Unlike fit(), this processes one sample without requiring the full
dataset in memory. Useful for streaming data or real-time learning.
§Arguments
x- Single input sample (binary features)y- Binary label (0 or 1)seed- Random seed for this update
§Example
use tsetlin_rs::{Config, TsetlinMachine};
let config = Config::builder().clauses(20).features(2).build().unwrap();
let mut tm = TsetlinMachine::new(config, 10);
// Stream samples one at a time
tm.partial_fit(&[0, 1], 1, 42);
tm.partial_fit(&[1, 0], 1, 43);
tm.partial_fit(&[0, 0], 0, 44);Sourcepub fn partial_fit_batch(
&mut self,
xs: &[Vec<u8>],
ys: &[u8],
seed: u64,
update_weights: bool,
)
pub fn partial_fit_batch( &mut self, xs: &[Vec<u8>], ys: &[u8], seed: u64, update_weights: bool, )
Trains on a mini-batch of samples (online/incremental learning).
Processes multiple samples in sequence without requiring the full dataset. Optionally updates weights after the batch.
§Arguments
xs- Batch of input samplesys- Batch of binary labelsseed- Random seed for this batchupdate_weights- Whether to update clause weights after batch
§Example
use tsetlin_rs::{Config, TsetlinMachine};
let config = Config::builder().clauses(20).features(2).build().unwrap();
let mut tm = TsetlinMachine::new(config, 10);
// Process mini-batches
let batch_x = vec![vec![0, 1], vec![1, 0]];
let batch_y = vec![1, 1];
tm.partial_fit_batch(&batch_x, &batch_y, 42, true);Trait Implementations§
Source§impl Clone for TsetlinMachine
impl Clone for TsetlinMachine
Source§fn clone(&self) -> TsetlinMachine
fn clone(&self) -> TsetlinMachine
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more