/// Parses with cache when available.
///
/// This skips BiLSTMs forward computation when their output vectors have been computed.
/// Otherwise this computes BiLSTMs output vectors and holds them as cache.
Parses with cache when available.
This skips BiLSTMs forward computation when their output vectors have been computed.
Otherwise this computes BiLSTMs output vectors and holds them as cache.
Parses with cache when available . This skips BiLSTMs forward computation when their output vectors have been computed . Otherwise this computes BiLSTMs output vectors and holds them as cache .
pub fn parse_with_cache<WordIDs: AsRef<[u32]>, PostagIDs: AsRef<[u32]>>(
        &mut self,
        words: &[WordIDs],
        postags: &[PostagIDs],
    ) -> Vec<models::ParserOutput> {
        if self.cache.is_none() {
            self.cache = Some(self.encode(&words, &postags, false));
        }
        let hs = self.cache.as_ref().unwrap().clone();
        let mut states: Vec<(usize, State)> = hs
            .iter()
            .map(|h| State::new(h.shape().at(0) as u32))
            .enumerate()
            .collect();
        let mut target_states: Vec<(usize, *mut State)> = states
            .iter_mut()
            .map(|&mut (index, ref mut state)| (index, state as *mut _))
            .collect();
        while target_states.len() > 0 {
            let features: Vec<_> = target_states
                .iter()
                .map(|&(_index, state)| {
                    let feature = Feature::extract(unsafe { &*state });
                    [feature]
                })
                .collect();
            let ys = self.decode(&features, &hs, false);
            let num_actions = ys.shape().at(0) as usize;
            let action_scores = ys.to_vector();
            for i in (0..target_states.len()).rev() {
                let state: &mut State = unsafe { &mut *target_states.as_mut_slice()[i].1 };
                let mut best_action = None;
                let mut best_score = F32_NEG_INFINITY;
                for action in 0..num_actions {
                    let score = action_scores[i * num_actions + action];
                    if score > best_score && ArcStandard::is_allowed(action as u32, state) {
                        best_action = Some(action as u32);
                        best_score = score;
                    }
                }
                ArcStandard::apply(best_action.unwrap(), state).unwrap();
                if ArcStandard::is_terminal(state) {
                    target_states.remove(i);
                }
            }
        }
        let outputs = states
            .into_iter()
            .map(|(_index, state)| state.into_arcs())
            .collect();
        outputs
    }
pub fn parse_with_cache < WordIDs : AsRef < [ u32 ] > , PostagIDs : AsRef < [ u32 ] > > ( & mut self , words : & [ WordIDs ] , postags : & [ PostagIDs ] , ) -> Vec < models :: ParserOutput > { if self . cache . is_none ( ) { self . cache = Some ( self . encode ( & words , & postags , false ) ) ; } let hs = self . cache . as_ref ( ) . unwrap ( ) . clone ( ) ; let mut states : Vec < ( usize , State ) > = hs . iter ( ) . map ( | h | State :: new ( h . shape ( ) . at ( 0 ) as u32 ) ) . enumerate ( ) . collect ( ) ; let mut target_states : Vec < ( usize , * mut State ) > = states . iter_mut ( ) . map ( | & mut ( index , ref mut state ) | ( index , state as * mut _ ) ) . collect ( ) ; while target_states . len ( ) > 0 { let features : Vec < _ > = target_states . iter ( ) . map ( | & ( _index , state ) | { let feature = Feature :: extract ( unsafe { & * state } ) ; [ feature ] } ) . collect ( ) ; let ys = self . decode ( & features , & hs , false ) ; let num_actions = ys . shape ( ) . at ( 0 ) as usize ; let action_scores = ys . to_vector ( ) ; for i in ( 0 .. target_states . len ( ) ) . rev ( ) { let state : & mut State = unsafe { & mut * target_states . as_mut_slice ( ) [ i ] . 1 } ; let mut best_action = None ; let mut best_score = F32_NEG_INFINITY ; for action in 0 .. num_actions { let score = action_scores [ i * num_actions + action ] ; if score > best_score && ArcStandard :: is_allowed ( action as u32 , state ) { best_action = Some ( action as u32 ) ; best_score = score ; } } ArcStandard :: apply ( best_action . unwrap ( ) , state ) . unwrap ( ) ; if ArcStandard :: is_terminal ( state ) { target_states . remove ( i ) ; } } } let outputs = states . into_iter ( ) . map ( | ( _index , state ) | state . into_arcs ( ) ) . collect ( ) ; outputs }
Parses with cache when available.
Parses with cache when available .
&mut self
words
&[WordIDs]
postags
&[PostagIDs]