/// Computes feature representations
///
/// This flattens the slice of features and returns a varible whose shape is ([dim], n) where
/// n is the number of `Feature` objects.
///
/// # Implementation notes
///
/// This first gathers ids for lookup without duplication, and then maps the ids to vectors and
/// retrieves representation.
Computes feature representations
This flattens the slice of features and returns a varible whose shape is ([dim], n) where
n is the number of `Feature` objects.
Computes feature representations This flattens the slice of features and returns a varible whose shape is ( [ dim ] n ) where n is the number of ` Feature ` objects .
fn lookup_features<V: Variable, FSeq: AsRef<[Feature]>>(
        &mut self,
        xs: &[FSeq],
        train: bool,
    ) -> V {
        let num_samples = xs.iter().fold(0, |sum, x| sum + x.as_ref().len());
        let mut word_ids: Vec<u32> = Vec::with_capacity(1024);
        let mut postag_ids: Vec<u32> = Vec::with_capacity(64);
        let mut label_ids: Vec<u32> = Vec::with_capacity(64);
        let mut word_indices: Vec<usize> = Vec::with_capacity(num_samples * NUM_CM14_WORD_FEATURES);
        let mut postag_indices: Vec<usize> =
            Vec::with_capacity(num_samples * NUM_CM14_POSTAG_FEATURES);
        let mut label_indices: Vec<usize> =
            Vec::with_capacity(num_samples * NUM_CM14_LABEL_FEATURES);
        for features in xs {
            for feature in features.as_ref() {
                word_indices.extend(feature.words.iter().map(|id| or_insert(&mut word_ids, *id)));
                postag_indices.extend(
                    feature
                        .postags
                        .iter()
                        .map(|id| or_insert(&mut postag_ids, *id)),
                );
                label_indices.extend(
                    feature
                        .labels
                        .iter()
                        .map(|id| or_insert(&mut label_ids, *id)),
                );
            }
        }
        let xs_words = F::batch::split(
            F::dropout(
                self.word_embed.lookup::<V>(&word_ids),
                self.dropout_rate,
                train,
            ),
            word_ids.len() as u32,
        );
        let xs_postags = F::batch::split(
            F::dropout(
                self.postag_embed.lookup::<V>(&postag_ids),
                self.dropout_rate,
                train,
            ),
            postag_ids.len() as u32,
        );
        let xs_labels = F::batch::split(
            F::dropout(
                self.label_embed.lookup::<V>(&label_ids),
                self.dropout_rate,
                train,
            ),
            label_ids.len() as u32,
        );
        let xs_features: Vec<V> = (0..num_samples)
            .map(|i| {
                let w_iter = word_indices
                    [(i * NUM_CM14_WORD_FEATURES)..((i + 1) * NUM_CM14_WORD_FEATURES)]
                    .iter()
                    .map(|idx| &xs_words[*idx]);
                let p_iter = postag_indices
                    [(i * NUM_CM14_POSTAG_FEATURES)..((i + 1) * NUM_CM14_POSTAG_FEATURES)]
                    .iter()
                    .map(|idx| &xs_postags[*idx]);
                let l_iter = label_indices
                    [(i * NUM_CM14_LABEL_FEATURES)..((i + 1) * NUM_CM14_LABEL_FEATURES)]
                    .iter()
                    .map(|idx| &xs_labels[*idx]);
                F::concat(w_iter.chain(p_iter).chain(l_iter).collect::<Vec<&V>>(), 0)
            })
            .collect();
        F::batch::concat(xs_features)
    }
fn lookup_features < V : Variable , FSeq : AsRef < [ Feature ] > > ( & mut self , xs : & [ FSeq ] , train : bool , ) -> V { let num_samples = xs . iter ( ) . fold ( 0 , | sum , x | sum + x . as_ref ( ) . len ( ) ) ; let mut word_ids : Vec < u32 > = Vec :: with_capacity ( 1024 ) ; let mut postag_ids : Vec < u32 > = Vec :: with_capacity ( 64 ) ; let mut label_ids : Vec < u32 > = Vec :: with_capacity ( 64 ) ; let mut word_indices : Vec < usize > = Vec :: with_capacity ( num_samples * NUM_CM14_WORD_FEATURES ) ; let mut postag_indices : Vec < usize > = Vec :: with_capacity ( num_samples * NUM_CM14_POSTAG_FEATURES ) ; let mut label_indices : Vec < usize > = Vec :: with_capacity ( num_samples * NUM_CM14_LABEL_FEATURES ) ; for features in xs { for feature in features . as_ref ( ) { word_indices . extend ( feature . words . iter ( ) . map ( | id | or_insert ( & mut word_ids , * id ) ) ) ; postag_indices . extend ( feature . postags . iter ( ) . map ( | id | or_insert ( & mut postag_ids , * id ) ) , ) ; label_indices . extend ( feature . labels . iter ( ) . map ( | id | or_insert ( & mut label_ids , * id ) ) , ) ; } } let xs_words = F :: batch :: split ( F :: dropout ( self . word_embed . lookup :: < V > ( & word_ids ) , self . dropout_rate , train , ) , word_ids . len ( ) as u32 , ) ; let xs_postags = F :: batch :: split ( F :: dropout ( self . postag_embed . lookup :: < V > ( & postag_ids ) , self . dropout_rate , train , ) , postag_ids . len ( ) as u32 , ) ; let xs_labels = F :: batch :: split ( F :: dropout ( self . label_embed . lookup :: < V > ( & label_ids ) , self . dropout_rate , train , ) , label_ids . len ( ) as u32 , ) ; let xs_features : Vec < V > = ( 0 .. num_samples ) . map ( | i | { let w_iter = word_indices [ ( i * NUM_CM14_WORD_FEATURES ) .. ( ( i + 1 ) * NUM_CM14_WORD_FEATURES ) ] . iter ( ) . map ( | idx | & xs_words [ * idx ] ) ; let p_iter = postag_indices [ ( i * NUM_CM14_POSTAG_FEATURES ) .. ( ( i + 1 ) * NUM_CM14_POSTAG_FEATURES ) ] . iter ( ) . map ( | idx | & xs_postags [ * idx ] ) ; let l_iter = label_indices [ ( i * NUM_CM14_LABEL_FEATURES ) .. ( ( i + 1 ) * NUM_CM14_LABEL_FEATURES ) ] . iter ( ) . map ( | idx | & xs_labels [ * idx ] ) ; F :: concat ( w_iter . chain ( p_iter ) . chain ( l_iter ) . collect :: < Vec < & V > > ( ) , 0 ) } ) . collect ( ) ; F :: batch :: concat ( xs_features ) }
Computes feature representations
This flattens the slice of features and returns a varible whose shape is ([dim], n) where
n is the number of `Feature` objects.
Computes feature representations This flattens the slice of features and returns a varible whose shape is ( [ dim ] n ) where n is the number of ` Feature ` objects .
&mut self
xs
&[FSeq]
train
bool
implementation notes
This first gathers ids for lookup without duplication, and then maps the ids to vectors and
retrieves representation.
This first gathers ids for lookup without duplication and then maps the ids to vectors and retrieves representation .