pub struct Classifier<C: Class> { /* private fields */ }
Expand description
A classifier.
Implementations§
Source§impl<C: Class> Classifier<C>
impl<C: Class> Classifier<C>
Sourcepub fn new(dev: &Device, config: ClassifierConfig) -> Result<Self>
pub fn new(dev: &Device, config: ClassifierConfig) -> Result<Self>
Create a new classifier.
§Example
use kalosm_learning::{Class, Classifier, ClassifierConfig};
#[derive(Debug, Clone, Copy, Class)]
enum MyClass {
Person,
Thing,
}
let dev = candle_core::Device::Cpu;
let classifier = Classifier::<MyClass>::new(&dev, ClassifierConfig::new()).unwrap();
Sourcepub fn config(&self) -> ClassifierConfig
pub fn config(&self) -> ClassifierConfig
Get the config of the classifier.
Sourcepub fn train(
&self,
m: &ClassificationDataset,
epochs: usize,
learning_rate: f64,
batch_size: usize,
progress: impl FnMut(ClassifierProgress),
) -> Result<f32>
pub fn train( &self, m: &ClassificationDataset, epochs: usize, learning_rate: f64, batch_size: usize, progress: impl FnMut(ClassifierProgress), ) -> Result<f32>
Train the model on the given dataset.
§Example
use kalosm_learning::{Class, ClassificationDatasetBuilder, Classifier, ClassifierConfig};
#[derive(Debug, Clone, Copy, Class)]
enum MyClass {
Person,
Thing,
}
let dev = candle_core::Device::Cpu;
let classifier = Classifier::<MyClass>::new(&dev, ClassifierConfig::new()).unwrap();
let mut dataset = ClassificationDatasetBuilder::new();
dataset.add(vec![1.0, 2.0, 3.0, 4.0], MyClass::Person);
dataset.add(vec![4.0, 3.0, 2.0, 1.0], MyClass::Thing);
classifier
.train(&dataset.build(&dev).unwrap(), 20, 0.05, 3, |_| {})
.unwrap();
Sourcepub fn save(&self, path: impl AsRef<Path>) -> Result<()>
pub fn save(&self, path: impl AsRef<Path>) -> Result<()>
Save the model to a safetensors file at the given path.
§Example
use kalosm_learning::{Class, Classifier, ClassifierConfig};
#[derive(Debug, Clone, Copy, Class)]
enum MyClass {
Person,
Thing,
}
let dev = candle_core::Device::Cpu;
let classifier = Classifier::<MyClass>::new(&dev, ClassifierConfig::new()).unwrap();
classifier.save("classifier.safetensors").unwrap();
Sourcepub fn load(
path: impl AsRef<Path>,
dev: &Device,
config: ClassifierConfig,
) -> Result<Self>
pub fn load( path: impl AsRef<Path>, dev: &Device, config: ClassifierConfig, ) -> Result<Self>
Load the model from a safetensors file at the given path.
§Example
use kalosm_learning::{Class, Classifier, ClassifierConfig};
#[derive(Debug, Clone, Copy, Class)]
enum MyClass {
Person,
Thing,
}
let dev = candle_core::Device::Cpu;
let classifier =
Classifier::<MyClass>::load("classifier.safetensors", &dev, ClassifierConfig::new())
.unwrap();
Sourcepub fn run(&self, input: &[f32]) -> Result<ClassifierOutput<C>>
pub fn run(&self, input: &[f32]) -> Result<ClassifierOutput<C>>
Run the model on the given input.
§Example
use kalosm_learning::{Class, Classifier, ClassifierConfig};
#[derive(Debug, Clone, Copy, Class)]
enum MyClass {
Person,
Thing,
}
let dev = candle_core::Device::Cpu;
let classifier = Classifier::<MyClass>::new(&dev, ClassifierConfig::new()).unwrap();
let result = classifier.run(&[1.0, 2.0, 3.0, 4.0]).unwrap();
println!("Result: {:?}", result);
Auto Trait Implementations§
impl<C> !Freeze for Classifier<C>
impl<C> !RefUnwindSafe for Classifier<C>
impl<C> Send for Classifier<C>where
C: Send,
impl<C> Sync for Classifier<C>where
C: Sync,
impl<C> Unpin for Classifier<C>where
C: Unpin,
impl<C> !UnwindSafe for Classifier<C>
Blanket Implementations§
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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