pub struct Model {
pub vocab_size: usize,
pub embedding_dim: usize,
pub input_weights: Vec<f32>,
pub output_weights: Vec<f32>,
}Expand description
Core weight matrices for Word2Vec.
Both matrices are row-major flat Vec<f32> for cache efficiency.
Row i starts at byte offset i * embedding_dim * 4.
Fields§
§vocab_size: usize§embedding_dim: usize§input_weights: Vec<f32>§output_weights: Vec<f32>Implementations§
Source§impl Model
impl Model
Sourcepub fn new(vocab_size: usize, embedding_dim: usize, seed: u64) -> Self
pub fn new(vocab_size: usize, embedding_dim: usize, seed: u64) -> Self
Initialise with Xavier uniform weights.
use word2vec::model::Model;
let m = Model::new(100, 50, 42);
assert_eq!(m.input_weights.len(), 100 * 50);Sourcepub fn input_vec(&self, idx: usize) -> &[f32]
pub fn input_vec(&self, idx: usize) -> &[f32]
Get the embedding vector for word at idx (slice into input_weights).
Sourcepub fn input_vec_mut(&mut self, idx: usize) -> &mut [f32]
pub fn input_vec_mut(&mut self, idx: usize) -> &mut [f32]
Mutable access to input embedding.
Sourcepub fn output_vec_mut(&mut self, idx: usize) -> &mut [f32]
pub fn output_vec_mut(&mut self, idx: usize) -> &mut [f32]
Mutable access to output embedding.
Sourcepub fn skipgram_update(
&mut self,
center: usize,
context: usize,
negatives: &[usize],
lr: f32,
) -> f32
pub fn skipgram_update( &mut self, center: usize, context: usize, negatives: &[usize], lr: f32, ) -> f32
Skip-gram update: given center word, update for one context word
and n_neg negative samples.
Returns the binary cross-entropy loss contribution.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Model
impl<'de> Deserialize<'de> for Model
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for Model
impl RefUnwindSafe for Model
impl Send for Model
impl Sync for Model
impl Unpin for Model
impl UnsafeUnpin for Model
impl UnwindSafe for Model
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