Skip to main content

Trainer

Struct Trainer 

Source
pub struct Trainer {
    pub history: Vec<EpochStats>,
    /* private fields */
}
Expand description

Manages the full training pipeline.

Fields§

§history: Vec<EpochStats>

Loss history per epoch (populated during train).

Implementations§

Source§

impl Trainer

Source

pub fn new(config: Config) -> Self

Create a new trainer with the given configuration.

use word2vec::{Config, Trainer};
let trainer = Trainer::new(Config::default());
assert!(trainer.history.is_empty());
Source

pub fn train(&mut self, corpus: &[String]) -> Result<Embeddings>

Train on a corpus of sentences.

Returns Embeddings which wraps the trained model and vocabulary for inference queries.

§Errors

Returns Word2VecError if the config is invalid or the corpus is too small to train.

§Example
use word2vec::{Config, ModelType, Trainer};

let corpus = vec![
    "paris is the capital of france".to_string(),
    "berlin is the capital of germany".to_string(),
    "tokyo is the capital of japan".to_string(),
];

let mut trainer = Trainer::new(Config {
    epochs: 3,
    embedding_dim: 50,
    ..Config::default()
});

let embeddings = trainer.train(&corpus).unwrap();
println!("Vocab size: {}", embeddings.vocab_size());
Source

pub fn save_history(&self, path: impl AsRef<Path>) -> Result<()>

Save training history as JSON.

use word2vec::{Config, Trainer};
let mut trainer = Trainer::new(Config::default());
// trainer.train(...);
// trainer.save_history("history.json").unwrap();

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> 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, 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.
§

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

§

fn vzip(self) -> V