Dynamic Analysis and an Eigen Initializer for Recurrent Neural Networks
In recurrent neural networks, learning long-term dependency is the main
difficulty due to the vanishing and exploding gradient problem. Many
researchers are dedicated to solving this issue and they proposed many
algorithms. Although these algorithms have achieved great success,
understanding how the information decays remains an open problem. In this
paper, we study the dynamics of the hidden state in recurrent neural networks.
We propose a new perspective to analyze the hidden state space based on an
eigen decomposition of the weight matrix. We start the analysis by linear state
space model and explain the function of preserving information in activation
functions. We provide an explanation for long-term dependency based on the
eigen analysis. We also point out the different behavior of eigenvalues for
regression tasks and classification tasks. From the observations on
well-trained recurrent neural networks, we proposed a new initialization method
for recurrent neural networks, which improves consistently performance. It can
be applied to vanilla-RNN, LSTM, and GRU. We test on many datasets, such as
Tomita Grammars, pixel-by-pixel MNIST datasets, and machine translation
datasets (Multi30k). It outperforms the Xavier initializer and kaiming
initializer as well as other RNN-only initializers like IRNN and sp-RNN in
several tasks.
Ran Dou, Jose Principe
# Dynamic Analysis and an Eigen Initializer for Recurrent Neural Networks

###### Abstract

In recurrent neural networks, learning long-term dependency is the main difficulty due to the vanishing and exploding gradient problem. Many researchers are dedicated to solving this issue and they proposed many algorithms. Although these algorithms have achieved great success, understanding how the information decays remains an open problem. In this paper, we study the dynamics of the hidden state in recurrent neural networks. We propose a new perspective to analyze the hidden state space based on an eigen decomposition of the weight matrix. We start the analysis by linear state space model and explain the function of preserving information in activation functions. We provide an explanation for long-term dependency based on the eigen analysis. We also point out the different behavior of eigenvalues for regression tasks and classification tasks. From the observations on well-trained recurrent neural networks, we proposed a new initialization method for recurrent neural networks, which improves consistently the performance. It can be applied to vanilla-RNN, LSTM, and GRU. We test is on many datasets, such as Tomita Grammars, pixel-by-pixel MNIST dataset, and machine translation dataset (Multi30k). It outperforms Xavier initializer and kaiming initializer as well as other RNN-only initializers like IRNN and sp-RNN in several tasks.

 recurrent neural networks, initializer, eigendecomposition, state space model

## I Introduction

In a recurrent system, the hidden state is considered representative of the unknown state of the system that created the data. In general, a state space model approximates the unknown state \(h_{t}\) using the previous hidden state \(h_{t-1}\) and current sample \(x_{t}\). And the prediction \(y_{t}\) is observed by the hidden state.

\[h_{t}=H(x_{t},h_{t-1}) \tag{1}\]

\[y_{t}=G(h_{t}) \tag{2}\]

where \(H\) is the state transition function and \(G\) is the observation function. Extensive research has been studied on the design of state transition functions, such as the Kalman Filter family [27], echo state networks [13] and recurrent neural networks. The hidden states represent the memory of past information, however, information stored in hidden states is fading away through time. Models have difficulty learning the long-term dependencies.

Many mechanisms have been proposed to improve long-term memory. One is to use the gating mechanism [4, 10, 12] and these gated networks have been widely used in time series, reinforcement learning, and natural language processing. Generally, the output of a gated unit is limited from 0 to 1 by a sigmoid activation function. The forgetting gate is used to reset the hidden state memory and the writing gate decides what information should be stored in the hidden states. In LSTM [12], the forgetting and writing gates operate a cell state vector as the memory. And the GRU operates directly on the hidden state [4]. In order to learn the long-term dependency, a bias of 1 can be added to the forgetting gate for initialization [14].

Another mechanism for improving the long-term dependency is using external memory, such as the Neural Turing Machine (NTM [7]), Differentiable Neural Computer [8], End-to-end Network [23], Neural Stack and Neural Queue [9], RNN-EM [21], and Dynamic Memory Networks [15]. In these architectures, a recurrent network is used as the controller to interact with the external memory. At each time, the controller reads and writes from the external memory based on a rule such as the attention mechanism. Unlike overwriting new information into hidden states, the external memory stores all past information and provides a better long-term dependency.

Besides the two mechanisms mentioned above, there are other studies trying to improve the performance of RNNs. And most of them are focusing on manipulating the hidden state, such as AntisymmetricRNN [3], Noisy RNN [17], PF-RNN [19], SRNN [22], TP-RNN [18], uRNN [2] and SR-RNN [26]. These methods show that well-designed hidden states are capable of handling longer memory. Other methods are focusing more on the learning algorithms for RNNs. For example, [1] shows that with a sufficiently large architecture, the stochastic gradient descent can provide a linear convergence rate. [28] proposed a stochastic bilevel optimization for RNN to prevent vanishing and exploding gradients. And [20] proposed to use the stochastic Riemannian coordinate descent for RNN training.

There is also research studying the effect of different
initialization methods for RNNs. In gradient descent methods, a proper initialization for weights is of vital importance. If the weights are initialized with small values, the gradients will vanish and if the weights are initialized with large values, the gradients will explode. And the rules of thumb for weight initialization are to set the mean of activations close to zero and the variance of activations consistent with each other. Followed by this idea, Xavier initializer [6] and kaiming initializer [11] are proposed and frequently used in nowadays research. Unlike feedforward neural networks, recurrent neural networks share weights recurrently, which provides a different dynamic for the hidden states. IRNN [16] shows that RNNs can be simply initialized by identity matrices and [24] proposed the np-RNN afterward. However, both methods are designed for vanilla RNNs and can not be applied in general applications such as LSTM and GRU.

In this paper, we start by analyzing a linear state space model in terms of eigendecomposition and extrapolate it to nonlinear cases. We conclude that in RNNs, the long-term dependency is improved by increasing eigenvalues. Based on our observation, we propose a new initializer for recurrent layers. Unlike IRNN and np-RNN, which are only designed for vanilla-RNN, our initializer can be widely used for different types of RNNs, like LSTM and GRU.

## II Eigen Decomposition for State Transition Function

Given a linear state space model, the state transition function and observation function can be written as,

\[h_{t}=W_{h}h_{t-1}+W_{x}x_{t} \tag{3}\]

\[y_{t}=W_{y}h_{t} \tag{4}\]

where \(h_{t}\) is the state vector with the size of \(n\). For simplicity, denote,

\[x^{\prime}_{t}=W_{x}x_{t} \tag{5}\]

For the state transition matrix \(W_{h}\), let \(\Lambda=[\lambda_{1},\lambda_{2},...,\lambda_{n}]\) and \(U=[u_{1},u_{2},...,u_{n}]\) be the eigenvalues and eigenvectors,

\[W_{h}=U\Lambda\bar{U}^{T} \tag{6}\]

Note here \([u_{1},u_{2},...,u_{n}]\) are the orthonormal basis, and \(\Lambda\) and \(U\) may have complex values. When the eigenvalues are real values, after each update, the hidden state is enlarged along the direction of eigenvectors by the corresponding eigenvalues. And when the eigenvalues are complex numbers, they show up in conjugate pairs and the hidden state also rotates in the plane formed by the corresponding conjugate eigenvectors.

Consider only the hidden state with zero inputs, we can decompose \(h_{t-1}\) by the eigenvectors,

\[h_{t-1}=\sum_{i=1}^{n}\alpha^{i}_{t-1}u_{i} \tag{7}\]

where \([\alpha^{1}_{t-1},\alpha^{2}_{t-1},...,\alpha^{n}_{t-1}]\) are the coefficients for the corresponding eigenvectors. And,

\[h_{t}=\sum_{i=1}^{n}\alpha^{i}_{t}u_{i}=\sum_{i=1}^{n}\alpha^{i}_{t-1}\lambda _{i}u_{i} \tag{8}\]

After \(t^{\prime}\) steps,

\[h_{t+t^{\prime}}=\sum_{i=1}^{n}\alpha^{i}_{t-1}\lambda^{i}_{t}u_{i} \tag{9}\]

It is obvious that the system is stable only when \(|\lambda_{i}|\leq 1\). And as time goes on, the information of hidden states is decaying at different speeds along different eigenvectors.

Now take the input \(x^{\prime}_{t}\) into consideration. Same as the hidden state, we can decompose the \(x^{\prime}_{t}\) by the eigenvectors \(u_{i}\) and its coefficients \(a^{i}_{t}\).

\[x^{\prime}_{t}=\sum_{i=1}^{n}a^{i}_{t}u_{i} \tag{10}\]

Set the initial hidden state to zero vector, then we have,

\[h_{1}=x^{\prime}_{1}=\sum_{i=1}^{n}a^{i}_{1}u_{i} \tag{11}\]

Compute \(h_{t}\) recursively,

\[h_{t}=\sum_{i=1}^{n}\sum_{j=1}^{t}a^{i}_{j}\lambda^{j-1}_{i}u_{i} \tag{12}\]

Recall (8), then we have,

\[\alpha^{i}_{t}=\sum_{j=1}^{t}a^{i}_{j}\lambda^{j-1}_{i} \tag{13}\]

\[\alpha^{i}_{t}=\lambda_{i}\alpha^{i}_{t-1}+a^{i}_{t} \tag{14}\]

Therefore, a linear state space model equals a set of first-order IIR filters in complex space along the direction of eigenvectors.

In nonlinear recurrent systems, for the weight matrix of the hidden state, we can still use the same analysis of eigendecomposition as above. However, equation (12) breaks due to the nonlinear activation function, causing it difficult to analyze nonlinear cases.

## III Conjectures for Nonlinear Case

The activation functions are used to introduce non-linearity in recurrent neural networks. Here, we can rethink it from the view of eigenvalues and eigenvectors of the weight matrix in equation (9). However, the issue is that the information decay of a hidden state is determined by the eigenvalues. A system with small eigenvalues loses information very fast. And in a stable linear system, the eigenvalue norms are strictly limited by 1, which provides no long-term dependency. However, using nonlinear activation functions restricts the outputs either from 0 to 1 (\(sigmoid\)) or from -1 to 1 (\(tanh\)). It prevents the hidden states from exploding when the eigenvalues are greater than 1. As for the \(ReLU\) activation, there are cases where eigenvalues are negative numbers, meaning the hidden states flip in the opposite direction. And \(ReLU\) prevents the exploding by directly cutting the negative parts. Hereby, we make the following conjecture:

**Conjecture 1**: _A nonlinear recurrent system improves the long-term dependency by increasing the eigenvalues of the
weighting matrix (\(>1\)). The larger the eigenvalues are, the better long-term dependency is provided.

To test this conjecture, we perform two experiments on linear-RNN, tanh-RNN, and relu-RNN and compare the eigenvalue norms. We first train the three models on a regression task using the Mackey glass dataset. We use the MSE as the loss function and Adam optimizer with a learning rate of 0.01. We set the size of the hidden state to 8. Since the new hidden state is given by both the previous hidden state and input sample, the eigenvalue norms should be much smaller than 1. From Table I we can see both tanh-RNN and relu-RNN have larger eigenvalue norms than linear-RNN.

We also train the three networks on the sequential MNIST classification task, which requires a higher demand for long-term dependency. We use the softmax output and cross-entropy as the loss function. Since models are only trained with fixed length sequences and there is no limitation on the values of the outputs (before the softmax), the restriction for stability in the networks is looser. And the eigenvalue norms can be slightly larger than 1 in linear RNN. Besides, in most classification cases, the models extract features from the input and keep information. Larger eigenvalue norms are expected. We set the size of the hidden state to 150 and use Adam with a learning rate of 0.0001. We only show the largest 10th unique eigenvalue norms (only keep one for conjugate eigenvalues) in Table II.

In a stable linear system, the eigenvalue norms cannot be larger than 1, meaning that the information can hardly be preserved in the hidden states. Therefore, the hidden states tend to shrink to zero when propagating forward through time. And the hidden states have a higher density distribution around the origin. It causes discriminating information much more difficult. However, with the nonlinear activation functions, the eigenvalue norms can be greater than 1. Here the space of hidden states is a hypercube limited by the activation functions. These eigenvalues keep pushing the hidden states to the surface of the hypercube. Therefore, the hidden states can explore more space in the hypercube and have a better distribution for discriminant features. In conclusion, we have the following conjecture:

_Conjecture 2:_ For a recurrent system with long-term dependency, the hidden states are capable of exploring the space away from the origin and avoiding collapsing.

From the tables above, we notice that the classification task has much better long-term dependency than regression. Therefore, we validate this conjecture using the MNIST dataset and compare the hidden states of linear-RNN, tanh-RNN, LSTM and GRU. We first train the four networks until the best performance is achieved. Then, we collect all the hidden states along the trajectories. For visualization, we use the principal component analysis (PCA) and plot the scatters on the first two principal components (Figure 1). We can see that the plots are consistent with our conjecture. Besides, LSTM and GRU show special structures in the distribution which requires future explanation.

## IV Eigen Initializer

In gradient descent methods, a proper initialization for the weights not only helps with the convergence but also provides better local optimal solutions. To study the relation between eigenvalues and different initialization methods, we calculate the eigenvalue norms for different initializers (Table III). We create a weighting matrix with a size \(8\times 8\) and compare the default uniform initializer (in Pytorch) with sp-RNN, Xavier initializer, and kaiming initializer. We generate each initializer 500 times and calculate the ordered mean values. The default initializer of Pytorch is initialized by a uniform distribution with a standard deviation given by,

\[stdv=1/sqrt(n) \tag{15}\]

where \(n\) is the size of input features.

Fig. 1: Distribution of Hidden States
From table III, we notice that compared with other initializers, the default initializer provides small eigenvalues. At the beginning of training, if the eigenvalue norms are small, the information of input samples decays very fast. And the model can hardly learn from previous samples. Other initializers provide larger eigenvalues, helping the model to learn from more samples. From the perspective of eigenvalues, we can also explain why other initializers help with the convergence rate.

Therefore, we can help models learn better long-term dependency with a better initialization on eigenvalues. First, we set all the eigenvalue norms to \(\lambda\in(0,1)\). We want the model can learn more from long-term dependency and let the hidden states store more information in the beginning. Therefore, we set \(\lambda\) to 0.95 for all experiments.

\[W_{0}=diag(\lambda) \tag{16}\]

where \(w_{1}\) is a diagonal matrix with the size of \(n\times n\), and \(n\) is the size of hidden states.

In hidden state space, the transition function cannot only magnify the hidden states but should also rotate the hidden states by a certain angle along several directions. Therefore, we mimic this rotation by decomposing the process into \(n-1\) step. And at step \(i\), we randomly sample an angle \(\theta_{i}\) from a uniform distribution \([0,2\pi]\). Then, we perform the rotation in the space of \(i\)th and (\(i+1\))th dimension.

\[W_{i}=\left[\begin{array}{ccccc}1&&\cdots&&0\\ &\ddots&&&&&\\ \vdots&&\cos\theta_{i}&-\sin\theta_{i}&&\vdots\\ &&&\ddots&\\ 0&&\cdots&&1\end{array}\right] \tag{17}\]

Then, the final initializer is,

\[W=\prod_{i=0}^{n-1}W_{i} \tag{18}\]

## V Experiments and Results

### _Tomita Grammar_

The Tomita Grammar [25] is a set of strings with alphabet \(\{0,1\}\) following predefined rules. It contains 7 different languages and has been widely studied for deterministic finite automata (DFA) extraction. Here, we use the Tomita Grammar to test our initialization method. We compare the learning curves on tanh-RNN, LSTM and GRU with uniform initialization (Pytorch), Xavier initializer and kaiming initializer. We also compare IRNN, sp-RNN for tanh-RNN, which are both RNN-only initializers. We show the comparison of learning curves on grammar 4. To show stability, we train each method 20 times and plot the average learning curve. We use Adam as the optimizer and uniformly set the learning rate to 0.001 with no weighting decay.

Figure 2 shows that in tanh-RNN, our initialization method provides a smoother learning curve. This is because our initialization method provides a better long-term dependency at the beginning and networks can benefit from this initialization. And in LSTM and GRU, our method shows slight improvement compared with other methods. Note that the Kaiming method takes advantage of the nonsaturating nonlin

Fig. 2: Average Learning Curves on Tomita Grammar 4
earity (ReLU) and is therefore not comparable to the others.

### _MNIST Classification_

We also test our initialization method on MNIST classification. In the experiment, we use the sequential MNIST by scanlines. Each time 28 pixels from the same row are presented as the input and there are 28 inputs in total. This is a simple task and we only care about the convergence of the learning curves. We set the same comparison as in section V-A. We set the hidden states for all networks to 150 and use cross-entropy as the loss function. We use Adam as the optimizer with a learning rate of 0.0001. The result is shown in Figure 3. Our method provides both a good convergence rate and better local optima for all networks. Still, we only have a significant improvement on the convergence rate on tanh-RNN. But in LSTM and GRU, we also benefit from learning the long-term dependency.

### _Multi30k Machine Translation_

The Multi30k [5] is a multi-model dataset that has been widely used in machine translation and image description. Each sample contains an image and a pair of descriptions in German and English. In this paper, we only take the sentences and perform a machine translation task from German to English. In the experiment, we choose the encoder-decoder architecture for sequence-to-sequence prediction. Each encoder and decoder contains an embedding layer and a recurrent layer (either tanh-RNN, LSTM or GRU). For simplicity, we only use one layer of each recurrent layer. For data prepossessing, we use spaCy to extract tokens from the sentences, which is an industrial package for natural language processing. Then, we replace all tokens that appear only once with the unknown token [UNK]. We also use the [PAD] token for padding the sequences. We uniformly set the embedding size to 512 and the hidden size to 256. We compare our methods with other initialization methods on tanh-RNN, LSTM and GRU. We use the cross-entropy as the loss function with a label smooth of 0.1. We also use Adam as the optimizer with a learning rate of 0.1. The learning curves are shown in Figure 4. We overtrain the LSTM and GRU cases to display the full convergence of the other methods.

In the tanh-RNN, our method outperforms default uniform, IRNN and sp-RNN. It also provides a better and more stable solution compared with all other initialization methods. In LSTM, our methods provide the best convergence rate as the default uniform initialization and in GRU, our performance is pretty close to all other methods, except Xavier initializers fail to provide a good convergence.

## VI Conclusion

In this paper, we analyze the dynamics of hidden states in recurrent systems. We study the relationship between eigenvalues and long-term dependency in linear recurrent models and testify to the conjectures for nonlinear systems. We also propose a new eigen initializer for all kinds of recurrent neural networks. We test is on multiple datasets. Our results show that our initialization methods can not only provide a good convergence rate but can also provide a better local optimal solution. In some experiments, we outperform Xavier initializer and kaiming initializer. We provide a new alternative initializer for training recurrent neural networks and an intuitive explanation of why it works.
