SEQUENCE-LEVEL FEATURES: HOW GRU AND LSTM CELLS CAPTURE N-GRAMS

1 INTRODUCTION . Long Short-term Memory ( LSTM ) ( Hochreiter & Schmidhuber , 1997 ) and Gated Recurrent Unit ( GRU ) ( Chung et al. , 2014 ) are widely used and investigated for tasks that involve sequential data . They are generally believed to be capable of capturing long-range dependencies while being able to alleviate gradient vanishing or explosion issues ( Hochreiter & Schmidhuber , 1997 ; Karpathy et al. , 2015 ; Sutskever et al. , 2014 ) . While such models were empirically shown to be successful across a range of tasks , certain fundamental questions such as “ what essential features are GRU or LSTM cells able to capture ? ” have not yet been fully addressed . Lacking answers to them may limit our ability in designing better architectures . One obstacle can be attributed to the non-linear activations used in the cells that prevent us from obtaining explicit closed-form expressions for hidden states . A possible solution is to expand the non-linear functions using the Taylor series ( Arfken & Mullin , 1985 ) and represent hidden states with explicit input terms . Literally , each hidden state can be viewed as the combination of constituent terms capturing features of different levels of complexity . However , there is a prohibitively large number of polynomial terms involved and they can be difficult to manage . But it is possible that certain terms are more significant than others . Through a series of mathematical transformation , we found there were sequence-level representations in a form of matrix-vector multiplications among the expanded and unrolled hidden states of the GRU/LSTM cell . Such representations could represent sequence-level features that could theoretically be sensitive to the order of tokens and able to differ from the token-level features of its tokens as well as the sequence-level features of its sub-sequences , thus making it able to represent N -grams . We assessed the significance of such sequence-level representations on sentiment analysis and language modeling tasks . We observed that the sequence-level representations derived from a GRU or LSTM cell were able to reflect desired properties on sentiment analysis tasks . Furthermore , in both the sentiment analysis and language modeling tasks , we replaced the GRU or LSTM cell with corresponding sequence-level representations ( along with token-level representations ) directly during training , and found that such models behaved similarly to the standard GRU or LSTM based models . This indicated that the sequence-level features might be significant for GRU or LSTM cells . 2 RELATED WORK . There have been plenty of prior works aiming to explain the behaviors of RNNs along with the variants . Early efforts were focused on exploring the empirical behaviors of recurrent neural networks ( RNNs ) . Li et al . ( 2015 ) proposed a visualization approach to analyze intermediate representations of the LSTM-based models where certain interesting patterns could be observed . However , it might not be easy to extend to models with high-dimension representations . Greff et al . ( 2016 ) explored the performances of LSTM variants on representative tasks such as speech recognition , handwriting recognition , and argued that none of the proposed variants could significantly improve upon the standard LSTM architecture . Karpathy et al . ( 2015 ) studied the existence of interpretable cells that could capture long-range dependencies such as line lengths , quotes and brackets . However , those works did not involve the internal mechanism of GRUs or LSTMs . Melis et al . ( 2020 ) and Krause et al . ( 2017 ) found that creating richer interaction between contexts and inputs on top of standard LSTMs could result in improvements . Their efforts actually pointed out the significance of rich interactions between inputs and contexts for LSTMs , but did not study what possible features such interactions could result in for good performances . Arras et al . ( 2017 ) applied an extended technique Layer-wise Relevance Propagation ( LRP ) to a bidirectional LSTM for sentiment analysis and produced reliable explanations of which words are responsible for attributing sentiment in individual text . Murdoch et al . ( 2018 ) leverage contextual decomposition methods to conduct analysis on the interactions of terms for LSTMS , which could produce importance scores for words , phrases and word interactions . A RNN unrolling technique was proposed by Sherstinsky ( 2018 ) based on signal processing concepts , transforming the RNN into the “ Vanilla LSTM ” network through a series of logical arguments , and Kanai et al . ( 2017 ) discussed the conditions that could prevent gradient explosions by looking into the dynamics of GRUs . Merrill et al . ( 2020 ) examined the properties of saturated RNNs and linked the update behaviors to weighted finite-state machines . Their ideas gave inspirations to explore internal behaviors of LSTM or GRU cells further . In this work , we sought to explore and study such significant underlying features . 3 MODEL DEFINITIONS . Vanilla RNN The representation of a vanilla RNN cell can be written as : ht = tanh ( Wixt +Whht−1 ) , ( 1 ) where ht ∈ Rd , xt ∈ Rdx are the hidden state and input at time step t respectively , ht−1 is the hidden state of the layer at time ( t− 1 ) or the initial hidden state . Wi andWh are weight matrices . Bias is suppressed here as well . GRU The representation of a GRU cell can be written as 1 : rt = σ ( Wirxt +Whrht−1 ) , zt = σ ( Wizxt +Whzht−1 ) , nt = tanh ( Winxt + rt Whnht−1 ) , ht = ( 1− zt ) nt + zt ht−1 , ( 2 ) where ht ∈ Rd , xt ∈ Rdx are the hidden state and input at time step t respectively , ht−1 is the hidden state of the layer at time ( t − 1 ) or the initial hidden state . rt ∈ Rd , zt ∈ Rd , nt ∈ Rd are the reset , update , and the new gates respectively . W ? ? refers to a weight matrix . σ is the elementwise sigmoid function , and is the element-wise Hadamard product . LSTM The representation of an LSTM cell can be written as : it = σ ( Wiixt +Whiht−1 ) , ft = σ ( Wifxt +Whfht−1 ) , ot = σ ( Wioxt +Whoht−1 ) , c′t = tanh ( Wicxt +Whcht−1 ) , ct = ft ct−1 + it c′t , ht = ot tanh ( ct ) , ( 3 ) 1For brevity , we suppressed the bias for both GRU and LSTM cells here . where ht ∈ Rd , xt ∈ Rdx are the hidden state and input at time step t respectively , it , ft , ot ∈ Rd are the input gate , forget gate , output gate respectively . c′t ∈ Rd is the new memory , ct is the final memory . W ? ? refers to a weight matrix . 4 UNROLLING RNNS . Using the Taylor series , the activations tanh ( x ) and σ ( x ) can be expanded ( at 0 ) as : tanh ( x ) = x+O ( x3 ) ( |x| < π 2 ) , σ ( x ) = 1 2 + 1 4 x+O ( x3 ) ( |x| < π ) ( 4 ) In this work , we do not seek to approximate the GRU or LSTM cells precisely , but to explore what features the cells could capture . 4.1 VANILLA RNN . We can expand the vanilla RNN hidden state using the Taylor series as : ht = x n t +Whht−1 +O ( x n t +Whht−1 ) 3 , ( 5 ) where xnt =Wixt . Let us unroll it as : ht = x n t + t−1∑ i=1 W t−ih x n i + r ( x1 , x2 , ... , xt ) , ( 6 ) where r is the unrolled representation produced by higher-order terms . It can be seen that the vanilla RNN cell can capture the input information at each time step . 4.2 GRU . Let us write xrt = Wirxt , x z t = Wizxt , x n t = Winxt , h r t−1 = Whrht−1 , h z t−1 = Whzht−1 , hnt−1 =Whnht−1 . Plugging Equation 4 in Equation 2 , we can expand the hidden state at time step t , then combine like terms with respect to the order of ht−1 and represent them as : ht = 1 2 xnt − 1 4 xnt xzt︸ ︷︷ ︸ zeroth−order + 1 2 ht−1 + 1 4 hnt−1 + 1 4 xzt ht−1 − 1 4 xnt hzt−1 + 1 8 ( xrt − xzt ) hnt−1 − 1 16 xrt xzt hnt−1︸ ︷︷ ︸ first−order + 1 4 hzt−1 ht−1 + 1 8 ( hrt−1 − hzt−1 ) hnt−1 − 1 16 xrt hzt−1 hnt−1 − 1 16 xzt hrt−1 hnt−1︸ ︷︷ ︸ second−order − 1 16 hzt−1 hrt−1 hnt−1︸ ︷︷ ︸ third−order +ξ ( xt , ht−1 ) , ( 7 ) where ξ ( xt , ht−1 ) refers to the higher-order terms of xt , ht−1 as well as their interactions . We will focus on the terms involving zeroth-order and first-order terms of ht−1 and explore the features they can possibly result in . Then the hidden state at time step t can be written as : ht = 1 2 xnt − 1 4 xnt xzt + 1 2 ht−1 + 1 4 hnt−1 + 1 4 xzt ht−1 − 1 4 xnt hzt−1 + 1 8 ( xrt − xzt − 1 2 xzt xrt ) hnt−1 + ξ′ ( xt , ht−1 ) , ( 8 ) where ξ′ ( xt , ht−1 ) refers to the higher-order terms of ht−1 plus ξ ( xt , ht−1 ) . Note that the Hadamard products can be transformed into matrix-vector multiplications ( a b = diag ( a ) b ) and we can obtain the following : ht = 1 2 xnt − 1 4 xnt xzt + [ 1 2 I + 1 4 Whn + 1 4 diag ( xzt ) − 1 4 diag ( xnt ) Whz + 1 8 diag ( xrt − xzt − 1 2 xzt xrt ) Whn ] ht−1 + ξ′ ( xt , ht−1 ) . ( 9 ) For brevity , let us define two functions of xt : g ( xt ) = 1 2 xnt − 1 4 xnt xzt , A ( xt ) = 1 2 I + 1 4 Whn + 1 4 diag ( xzt ) − 1 4 diag ( xnt ) Whz + 1 8 diag ( xrt − xzt − 1 2 xzt xrt ) Whn . ( 10 ) Both g ( xt ) andA ( xt ) are only functions of xt . Then we can rewrite Equation 9 as : ht = g ( xt ) +A ( xt ) ht−1 + ξ ′ ( xt , ht−1 ) . ( 11 ) Throughout all the previous time steps ( assuming the initial state are 0s ) , the hidden state at time step t can be finally unrolled as : ht = g ( xt ) + t−1∑ i=1 A ( xt ) A ( xt−1 ) ... A ( xi+1 ) ︸ ︷︷ ︸ M ( i+1 ) : t g ( xi ) + g ( x1 , x2 , ... , xt ) = g ( xt ) + t−1∑ i=1 M ( i+1 ) : tg ( xi ) ︸ ︷︷ ︸ Φi : t + g ( x1 , x2 , ... , xt ) , ( 12 ) where M ( i+1 ) : t = ∏i+1 k=tA ( xk ) ∈ Rd×d is the matrix-matrix product from time step t to i + 1 , g ( x1 , x2 , ... , xt ) are the unrolled representations from the higher-order terms in Equation 11 . The function g ( xt ) solely encodes current input , namely token-level features and thus we call it tokenlevel representation . The matrix-vector product Φi : t = M ( i+1 ) : tg ( xi ) encodes the tokens starting from time step i and ending at time step t. If the matrices are different and not diagonal , any change of the order will result in a different product . Therefore , Φi : t is able to capture the feature of the token sequence between time step i and t in an order-sensitive manner . We call it a sequence-level representation . Such representations are calculated sequentially from the left to the right through a sequence of vector/matrix multiplications , leading to features reminiscent of the classical N -grams commonly used in natural language processing ( NLP ) . Let us use ĥt to denote the first two terms in Equation 12 as : ĥt = g ( xt ) + t−1∑ i=1 Φi : t. ( 13 ) ĥt can be called as N -gram representations ( N ≥ 1 ) . At time step t , it is able to encode current token input and all the token sequences starting from time step i ∈ { 1 , 2 , ... , t − 1 } and ending at time step t given an instance . In other words , it is a linear combination of current token-level input feature ( can be understood as the unigram feature ) and sequence-level features of all the possible N -grams ending at time step t. Bidirectional GRUs would be able to capture sequence-level features from both directions . If we make a comparison with the unrolled vanilla RNN cell as discussed above , we can see that the sequence-level representation A ( xt ) A ( xt−1 ) ... A ( xi+1 ) g ( xi ) is more expressive than W t−ih x n i ( i = 1 , ... , t − 1 ) when capturing the sequence level features . Specifically , the sequence level representation in GRU explicitly models interactions among input tokens , while capturing the useful order information conveyed by them . This may also be a reason why gating mechanism can bring in improved effectiveness over vanilla RNNs apart from alleviating the gradient vanishing or explosion problems .

Summary: This paper attempts to add a contribution on understanding how gated recurrent neural networks like GRUs and LSTMs can learn the representation of n-grams. The authors expand the sigmoid function and the hyperbolic tangent function using Taylor series to obtain approximated closed-form mathematical expression of hidden representation when using the GRU or the LSTM as the update rules. The approximated hidden representation of the GRU and the LSTM update rules can be separated into two terms, (1) the current token-level input feature and (2) the sequence-level feature, which is a weighted sum of all previous tokens. As the hidden representation consists of two feature terms, one can take each feature (either token-level or sequence-level) separately for a downstream task, e.g., evaluate how good when sequence-level feature is used for predicting polarity score in sentiment analysis.