pub struct TextCnnLite { /* private fields */ }Expand description
Lightweight text-CNN classifier.
Extracts n-gram features (bag-of-n-grams) for each filter size and then trains a multinomial logistic regression head using mini-batch stochastic gradient descent.
This is intentionally a “lite” variant — it approximates a CNN’s 1-D convolution by computing frequency counts of character/word n-grams and using the max-pooled (most-frequent) value as the feature. A proper convolutional network would require a full neural-network framework.
§Example
use scirs2_text::text_classification::TextCnnLite;
let mut clf = TextCnnLite::new(vec![2, 3], 8);
let texts = &["good movie fun", "bad film boring", "great show entertaining", "terrible awful waste"];
let labels = &["pos", "neg", "pos", "neg"];
clf.fit(texts, labels, 20).unwrap();Implementations§
Source§impl TextCnnLite
impl TextCnnLite
Sourcepub fn new(filter_sizes: Vec<usize>, n_filters: usize) -> Self
pub fn new(filter_sizes: Vec<usize>, n_filters: usize) -> Self
Create a new TextCnnLite.
§Parameters
filter_sizes: n-gram window sizes, e.g.[2, 3, 4].n_filters: number of top n-gram features to keep per filter size.
Sourcepub fn fit(
&mut self,
texts: &[&str],
labels: &[&str],
epochs: usize,
) -> Result<()>
pub fn fit( &mut self, texts: &[&str], labels: &[&str], epochs: usize, ) -> Result<()>
Train the classifier.
Internally:
- Build the n-gram vocabulary from all documents.
- Vectorise each document into a frequency vector.
- Train multinomial logistic regression with SGD.
§Errors
Returns TextError::InvalidInput when texts and labels lengths differ
or when the corpus is empty.
Auto Trait Implementations§
impl Freeze for TextCnnLite
impl RefUnwindSafe for TextCnnLite
impl Send for TextCnnLite
impl Sync for TextCnnLite
impl Unpin for TextCnnLite
impl UnsafeUnpin for TextCnnLite
impl UnwindSafe for TextCnnLite
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> 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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.