Struct finalfusion::embeddings::Embeddings
source · pub struct Embeddings<V, S> { /* private fields */ }
Expand description
Word embeddings.
This data structure stores word embeddings (also known as word vectors) and provides some useful methods on the embeddings, such as similarity and analogy queries.
Implementations§
source§impl<V, S> Embeddings<V, S>where
V: Vocab,
S: Storage,
impl<V, S> Embeddings<V, S>where V: Vocab, S: Storage,
source§impl<V, S> Embeddings<V, S>
impl<V, S> Embeddings<V, S>
sourcepub fn into_parts(self) -> (Option<Metadata>, V, S, Option<NdNorms>)
pub fn into_parts(self) -> (Option<Metadata>, V, S, Option<NdNorms>)
Decompose embeddings in its vocabulary, storage, and optionally norms.
sourcepub fn metadata_mut(&mut self) -> Option<&mut Metadata>
pub fn metadata_mut(&mut self) -> Option<&mut Metadata>
Get metadata mutably.
source§impl<V, S> Embeddings<V, S>where
V: Vocab,
S: Storage,
impl<V, S> Embeddings<V, S>where V: Vocab, S: Storage,
sourcepub fn embedding(&self, word: &str) -> Option<CowArray<'_, f32, Ix1>>
pub fn embedding(&self, word: &str) -> Option<CowArray<'_, f32, Ix1>>
Get the embedding of a word.
sourcepub fn embedding_into(&self, word: &str, target: ArrayViewMut1<'_, f32>) -> bool
pub fn embedding_into(&self, word: &str, target: ArrayViewMut1<'_, f32>) -> bool
Realize the embedding of a word into the given vector.
This variant of embedding
realizes the embedding into the
given vector. This makes it possible to look up embeddings
without any additional allocations. This method returns
false
and does not modify the vector if no embedding could
be found.
Panics when then the vector does not have the same dimensionality as the word embeddings.
sourcepub fn embedding_batch(
&self,
words: &[impl AsRef<str>]
) -> (Array2<f32>, Vec<bool>)
pub fn embedding_batch( &self, words: &[impl AsRef<str>] ) -> (Array2<f32>, Vec<bool>)
Get a batch of embeddings.
The embeddings of all words
are computed and returned. This method also
return a Vec
indicating for each word if an embedding could be found.
sourcepub fn embedding_batch_into(
&self,
words: &[impl AsRef<str>],
output: ArrayViewMut2<'_, f32>
) -> Vec<bool>
pub fn embedding_batch_into( &self, words: &[impl AsRef<str>], output: ArrayViewMut2<'_, f32> ) -> Vec<bool>
Get a batch of embeddings.
The embeddings of all words
are computed and written to output
. A Vec
is
returned that indicates for each word if an embedding could be found.
This method panics when output
does not have the correct shape.
sourcepub fn embedding_with_norm(&self, word: &str) -> Option<EmbeddingWithNorm<'_>>
pub fn embedding_with_norm(&self, word: &str) -> Option<EmbeddingWithNorm<'_>>
Get the embedding and original norm of a word.
Returns for a word:
- The word embedding.
- The norm of the embedding before normalization to a unit vector.
The original embedding can be reconstructed by multiplying all embedding components by the original norm.
If the model does not have associated norms, 1 will be returned as the norm for vocabulary words.
sourcepub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter(&self) -> Iter<'_> ⓘ
Get an iterator over pairs of words and the corresponding embeddings.
sourcepub fn iter_with_norms(&self) -> IterWithNorms<'_> ⓘ
pub fn iter_with_norms(&self) -> IterWithNorms<'_> ⓘ
Get an iterator over triples of words, embeddings, and norms.
Returns an iterator that returns triples of:
- A word.
- Its word embedding.
- The original norm of the embedding before normalization to a unit vector.
The original embedding can be reconstructed by multiplying all embedding components by the original norm.
If the model does not have associated norms, the norm is always 1.
source§impl<I, S> Embeddings<SubwordVocab<I>, S>where
I: BucketIndexer,
S: Storage + CloneFromMapping,
impl<I, S> Embeddings<SubwordVocab<I>, S>where I: BucketIndexer, S: Storage + CloneFromMapping,
sourcepub fn to_explicit(&self) -> Result<Embeddings<ExplicitSubwordVocab, S::Result>>
pub fn to_explicit(&self) -> Result<Embeddings<ExplicitSubwordVocab, S::Result>>
Convert to explicitly indexed subword Embeddings.
source§impl<S> Embeddings<VocabWrap, S>where
S: Storage + CloneFromMapping,
impl<S> Embeddings<VocabWrap, S>where S: Storage + CloneFromMapping,
sourcepub fn try_to_explicit(
&self
) -> Result<Embeddings<ExplicitSubwordVocab, S::Result>>
pub fn try_to_explicit( &self ) -> Result<Embeddings<ExplicitSubwordVocab, S::Result>>
Try to convert to explicitly indexed subword embeddings.
Conversion fails if the wrapped vocabulary is SimpleVocab
, FloretSubwordVocab
or
already an ExplicitSubwordVocab
.
Trait Implementations§
source§impl<V, S> Analogy for Embeddings<V, S>where
V: Vocab,
S: StorageView,
impl<V, S> Analogy for Embeddings<V, S>where V: Vocab, S: StorageView,
source§impl<V: Clone, S: Clone> Clone for Embeddings<V, S>
impl<V: Clone, S: Clone> Clone for Embeddings<V, S>
source§fn clone(&self) -> Embeddings<V, S>
fn clone(&self) -> Embeddings<V, S>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more