Title: h-DETACH: MODIFYING THE LSTM GRADIENT TO-WARDS BETTER OPTIMIZATION

Abstract: Recurrent neural networks are known for their notorious exploding and vanishing gradient problem (EVGP). This problem becomes more evident in tasks where the information needed to correctly solve them exist over long time scales, because EVGP prevents important gradient components from being back-propagated adequately over a large number of steps. We introduce a simple stochastic algorithm (h-detach) that is specific to LSTM optimization and targeted towards addressing this problem. Specifically, we show that when the LSTM weights are large, the gradient components through the linear path (cell state) in the LSTM computational graph get suppressed. Based on the hypothesis that these components carry information about long term dependencies (which we show empirically), their suppression can prevent LSTMs from capturing them. Our algorithm 1 prevents gradients flowing through this path from getting suppressed, thus allowing the LSTM to capture such dependencies better. We show significant improvements over vanilla LSTM gradient based training in terms of convergence speed, robustness to seed and learning rate, and generalization using our modification of LSTM gradient on various benchmark datasets.

1. INTRODUCTION:

Recurrent Neural Networks (RNNs) (Rumelhart et al. (1986) ; Elman (1990) ) are a class of neural network architectures used for modeling sequential data. Compared to feed-forward networks, the loss landscape of recurrent neural networks are much harder to optimize. Among others, this difficulty may be attributed to the exploding and vanishing gradient problem (Hochreiter, 1991; Bengio et al., 1994; Pascanu et al., 2013) which is more severe for recurrent networks and arises due to the highly ill-conditioned nature of their loss surface. This problem becomes more evident in tasks where training data has dependencies that exist over long time scales.

Due to the aforementioned optimization difficulty, variants of RNN architectures have been proposed that aim at addressing these problems. The most popular among such architectures that are used in a wide number of applications include long short term memory (LSTM, Hochreiter & Schmidhuber (1997) ) and gated recurrent unit (GRU, Chung et al. (2014) ) networks, which is a variant of LSTM with forget gates (Gers et al., 1999) . These architectures mitigate such difficulties by introducing a linear temporal path that allows gradients to flow more freely across time steps. Arjovsky et al. (2016) on the other hand try to address this problem by parameterizing a recurrent neural network to have unitary transition matrices based on the idea that unitary matrices have unit singular values which prevents gradients from exploding/vanishing. Among the aforementioned RNN architectures, LSTMs are arguably most widely used (for instance they have more representational power compared with GRUs (Weiss et al., 2018) ) and it remains a hard problem to optimize them on tasks that involve long term dependencies. Examples of such tasks are copying problem (Bengio et al., 1994; Pascanu et al., 2013) , and sequential MNIST (Le Figure 1 : The computational graph of a typical LSTM. Here we have omitted the inputs x i for convenience. The top horizontal path through the cell state units c t s is the linear temporal path which allows gradients to flow more freely over long durations. The dotted blue crosses along the computational paths denote the stochastic process of blocking the flow of gradients though the h t states (see Eq 2) during the back-propagation phase of LSTM. We call this approach h-detach. et al., 2015) , which are designed in such a way that the only way to produce the correct output is for the model to retain information over long time scales.

The goal of this paper is to introduce a simple trick that is specific to LSTM optimization and improves its training on tasks that involve long term dependencies. To achieve this goal, we write out the full back-propagation gradient equation for LSTM parameters and split the composition of this gradient into its components resulting from different paths in the unrolled network. We then show that when LSTM weights are large in magnitude, the gradients through the linear temporal path (cell state) get suppressed (recall that this path was designed to allow smooth gradient flow over many time steps). We show empirical evidence that this path carries information about long term dependencies (see section 3.5) and hence gradients from this path getting suppressed is problematic for such tasks. To fix this problem, we introduce a simple stochastic algorithm that in expectation scales the individual gradient components, which prevents the gradients through the linear temporal path from being suppressed. In essence, the algorithm stochastically prevents gradient from flowing through the h-state of the LSTM (see figure 1 ), hence we call it h-detach. Using this method, we show improvements in convergence/generalization over vanilla LSTM optimization on the copying task, transfer copying task, sequential and permuted MNIST, and image captioning.

2. PROPOSED METHOD: h-DETACH:

We begin by reviewing the LSTM roll-out equations. We then derive the LSTM back-propagation equations and by studying its decomposition, identify the aforementioned problem. Based on this analysis we propose a simple stochastic algorithm to fix this problem.

2.1. LONG SHORT TERM MEMORY NETWORKS:

LSTM is a variant of traditional RNNs that was designed with the goal of improving the flow of gradients over many time steps. The roll-out equations of an LSTM are as follows,

EQUATION

where denotes point-wise product and the gates f t , i t , o t and g t are defined as,

EQUATION

Here c t and h t are the cell state and hidden state respectively. Usually a transformation φ(h T ) is used as the output at time step t (Eg. next word prediction in language model) based on which we can compute the loss t := (φ(h t )) for that time step.

An important feature of the LSTM architecture is the linear recursive relation between the cell states c t as shown in Eq. 1. This linear path allows gradients to flow easily over long time scales. This however is one of the components in the full composition of the LSTM gradient. As we will show next, the remaining components that are a result of the other paths in the LSTM computational graph are polynomial in the weight matrices W gh , W f h , W ih , W oh whose order grows with the number of time steps. These terms cause an imbalance in the order of magnitude of gradients from different paths, thereby suppressing gradients from linear paths of LSTM computational graph in cases where the weight matrices are large.

2.2. BACK-PROPAGATION EQUATIONS FOR LSTM:

In this section we derive the back-propagation equations for LSTM network and by studying its composition, we identify a problem in this composition. The back-propagation equation of an LSTM can be written in the following form.

Theorem 1 Fix w to be an element of the matrix

W gh , W f h , W ih , W oh ,W gx , W f x , W ix or W ox . Define, A t =   F t 0 n diag(k t ) Ft 0 n diag( kt ) 0 n 0 n Id n   B t =   0 n ψ t 0 n 0 n ψt 0 n 0 n 0 n 0 n   z t =   dct dw dht dw 1 n   (7) Then z t = (A t + B t )z t-1 .

In other words,

z t = (A t + B t )(A t-1 + B t-1 ) . . . (A 2 + B 2 )z 1 (8)

where all the symbols used to define A t and B t are defined in notation 1 in appendix.

To avoid unnecessary details, we use a compressed definitions of A t and B t in the above statement and write the detailed definitions of the symbols that constitute them in notation 1 in appendix. Nonetheless, we now provide some intuitive properties of the matrices A t and B t .

The matrix A t contains components of parameter's full gradient that arise due to the cell state (linear temporal path) described in Eq. (1) (top most horizontal path in figure 1 ). 8) because the product becomes polynomial in the weights which can become unbounded for large weights very quickly as the number of time steps grow.

Thus based on the above analysis, we identify the following problem with the LSTM gradient: when the LSTM weights are large, the gradient component through the cell state paths (A t ) get suppressed compared to the gradient components through the other paths (B t ) due to an imbalance in gradient component magnitudes. We recall that the linear recursion in the cell state path was introduced in the LSTM architecture (Hochreiter & Schmidhuber, 1997) as an important feature to allow gradients to flow smoothly through time. As we show in our ablation studies (section 3.5), this path carries information about long term dependencies in the data. Hence it is problematic if the gradient components from this path get suppressed.

2.3. h-DETACH:

We now propose a simple fix to the above problem. Our goal is to manipulate the gradient components such that the components through the cell state path (A t ) do not get suppressed when the components through the remaining paths (B t ) are very large (described in the section 2.2). Thus it would be helpful to multiply B t by a positive number less than 1 to dampen its magnitude. In Algorithm 1 we propose a simple trick that achieves this goal. A diagrammatic form of algorithm 1 is shown in Figure 1 . In simple words, our algorithm essentially blocks gradients from flowing through each of the h t states independently with a probability 1 -p, where p ∈ [0, 1] is a tunable hyper-parameter. Note the subtle detail in Algorithm 1 (line 9) that the loss t at any time step t is a function of h t which is not detached.

Algorithm 1 Forward Pass of h-detach Algorithm

1: INPUT: {x t } T t=1 , h 0 , c 0 , p 2: = 0 3: for 1 ≤ t ≤ T do 4: if bernoulli(p)==1 then 5: ht-1 ← stop-gradient(h t-1 ) 6: else 7: ht-1 ← h t-1 8: h t , c t ← LSTM(x t , ht-1 , c t-1 ) (Eq. 1-6) 9:

t ← loss(φ(h t ))

10:

← + t 11: return

We now show that the gradient of the loss function resulting from the LSTM forward pass shown in algorithm 1 has the property that the gradient components arising from B t get dampened.

Theorem 2 Let z t = [ dct dw T ; dht dw T ; 1 T n ]

T and zt be the analogue of z t when applying h-detach with probability 1 -p during back-propagation. Then,

zt = (A t + ξ t B t )(A t-1 + ξ t-1 B t-1 ) . . . (A 2 + ξ 2 B 2 )z 1

where ξ t , ξ t-1 , . . . , ξ 2 are i.i.d. Bernoulli random variables with probability p of being 1, and w, A t and B t and are same as defined in theorem 1.

The above theorem shows that by stochastically blocking gradients from flowing through the h t states of an LSTM with probability 1 -p, we stochastically drop the B t term in the gradient components. The corollary below shows that in expectation, this results in dampening the B t term compared to the original LSTM gradient.

Corollary 1 E ξ2,...,ξt [z t ] = (A t + pB t )(A t-1 + pB t-1 ) . . . (A 2 + pB 2 )z 1

Finally, we note that when training LSTMs with h-detach, we reduce the amount of computation needed. This is simply because by stochastically blocking the gradient from flowing through the h t hidden states of LSTM, less computation needs to be done during back-propagation through time (BPTT).

3. EXPERIMENTS:



3.1. COPYING TASK:

This task requires the recurrent network to memorize the network inputs provided at the first few time steps and output them in the same order after a large time delay. Thus the only way to solve this task is for the network to capture the long term dependency between inputs and targets which requires gradient components carrying this information to flow through many time steps.

We follow the copying task setup identical to Arjovsky et al. (2016) (described in appendix). Using their data generation process, we sample 100,000 training input-target sequence pairs and 5,000 validation pairs. We use cross-entropy as our loss to train an LSTM with hidden state size 128 for a maximum of 500-600 epochs. We use the ADAM optimizer with batch-size 100, learning rate 0.001 and clip the gradient norms to 1. We see that for T = 100, even the baseline LSTM is able to reach ∼ 100% accuracy for most seeds and the only difference we see between vanilla LSTM and LSTM with h-detach is in terms of convergence. T = 300 is a more interesting case because it involves longer term dependencies. In this case we find that h-detach leads to faster convergence and achieves ∼ 100% validation accuracy while being more robust to the choice of seed.

Figure 2 shows the validation accuracy plots for copying task training for T = 100 (top row) and T = 300 (bottom row) without h-detach (left), and with h-detach (middle and right). Each plot contains runs from the same algorithm with multiple seeds to show a healthy sample of variations using these algorithms. For T = 100 time delay, we see both vanilla LSTM and LSTM with hdetach converge to 100% accuracy. For time delay 100 and the training setting used, vanilla LSTM is known to converge to optimal validation performance (for instance, see Arjovsky et al. (2016) ). Nonetheless, we note that h-detach converges faster in this setting. A more interesting case is when time decay is set to 300 because it requires capturing longer term dependencies. In this case, we find that LSTM training without h-detach achieves a validation accuracy of ∼ 82% at best while a number of other seeds converge to much worse performance. On the other hand, we find that using h-detach with detach probabilities 0.25 and 0.5 achieves the best performance of 100% and converging quickly while being reasonably robust to the choice of seed.

3.2. TRANSFER COPYING TASK:

Having shown the benefit of h-detach in terms of training dynamics, we now extend the challenge of the copying task by evaluating how well an LSTM trained on data with a certain time delay generalizes when a larger time delay is used during inference. This task is referred as the transfer copying task (Hochreiter & Schmidhuber, 1997) . Specifically, we train the LSTM architecture on copying task with delay T = 100 without h-detach and with h-detach with probability 0.25 and 0.5. We then evaluate the accuracy of the trained model for each setting for various values of T > 100.

The results are shown in table 1 . We find that the function learned by LSTM when trained with h-detach generalize significantly better on longer time delays during inference compared with the LSTM trained without h-detach.

3.3. SEQUENTIAL MNIST:

This task is a sequential version of the MNIST classification task (LeCun & Cortes, 2010) . In this task, an image is fed into the LSTM one pixel per time step and the goal is to predict the label after the last pixel is fed. We consider two versions of the task: one is which the pixels are read in order (from left to right and top to bottom), and one where all the pixels are permuted in a random but fixed order. We call the second version the permuted MNIST task or pMNIST in short. The setup used for this experiment is as follows. We use 50000 images for training, 10000 for validation and 10000 for testing. We use the ADAM optimizer with different learning rates-0.001,0.0005 and 0.0001, and a fixed batch size of 100. We train for 200 epochs and pick our final model based on the best validation score. We use an LSTM with 100 hidden units. For h-detach, we do a hyperparameter search on the detach probability in {0.1, 0.25, 0.4, 0.5}. For both pixel by pixel MNIST and pMNIST, we found the detach hyper-parameter of 0.25 to perform best on the validation set for both MNIST and pMNIST.

On the sequential MNIST task, both vanilla LSTM and training with h-detach give an accuracy of 98.5%. Here, we note that the convergence of our method is much faster and is more robust to the different learning rates of the ADAM optimizer as seen in Figure 3 . Refer to appendix (figure 6 ) for experiments with multiple seeds that shows the robustness of our method to initialization.

In Table 3 : Test performance on image captioning task on MS COCO dataset using metrics BLEU 1 to 4, METEOR, and CIDEr (higher values are better for all metrics). We re-implement both Show&Tell (Vinyals et al., 2015) and Soft Attention (Xu et al., 2015) and train the LSTM in these models with and without h-detach. 

3.4. IMAGE CAPTIONING:

We now evaluate h-detach on an image captioning task which involves using an RNN for generating captions for images. We use the Microsoft COCO dataset (Lin et al., 2014) which contains 82,783 training images and 40,504 validation images. Since this dataset does not have a standard split for training, validation and test, we follow the setting in Karpathy & Fei-Fei (2015) which suggests a split of 80,000 training images and 5,000 images each for validation and test set.

We use two models to test our approach-the Show&Tell encoder-decoder model (Vinyals et al., 2015) which does not employ any attention mechanism, and the 'Show, Attend and Tell' model (Xu et al., 2015) , which uses soft attention. For feature extraction, we use the 2048-dimensional last layer feature vector of a residual network (Resnet He et al. ( 2015)) with 152 layers which was pretrained on ImageNet for image classification. We use an LSTM with 512 hidden units for caption generation. We train both the Resnet and LSTM models using the ADAM optimizer (Kingma & Ba, 2014) with a learning rate of 10 -4 and leave the rest of the hyper-parameters as suggested in their paper. We also perform a small hyperparameter search where we find the optimial value of the h-detach parameter. We considered values in the set {0.1, 0.25, 0.4, 0.5} and pick the optimal value based on the best validation score. Similar to Serdyuk et al. (2018) , we early stop based on the validation CIDEr scores and report BLEU-1 to BLEU-4, CIDEr, and Meteor scores.

The results are presented in table 3 . Training the LSTM with h-detach outperforms the baseline LSTM by a good margin for all the metrics and produces the best BLEU-1 to BLEU-3 scores among all the compared methods. Even for the other metrics, except for the results reported by Lu et al. (2017) , we beat all the other methods reported. We emphasize that compared to all the other reported methods, h-detach is extremely simple to implement and does not add any computational overhead (in fact reduces computation).

3.5. ABLATION STUDIES:

In this section, we first study the effect of removing gradient clipping in the LSTM training and compare how the training of vanilla LSTM and our method get affected. Getting rid of gradient clipping would be insightful because it would confirm our claim that stochastically blocking gradients through the hidden states h t of the LSTM prevent the growth of gradient magnitude. We train both models on pixel by pixel MNIST using ADAM without any gradient clipping. The validation accuracy curves are reported in figure 4 for two different learning rates. We notice that removing gradient clipping causes the Vanilla LSTM training to become extremely unstable. h-detach on the using LSTM such that gradient is stochastically blocked through the cell state (the probability of detaching the cell state in this experiment is mentioned in sub-titles.). Blocking gradients from flowing through the cell state path of LSTM (c-detach) leads to significantly worse performance compared even to vanilla LSTM on tasks that requires long term dependencies. This suggests that the cell state path carry information about long term dependencies.

other hand seems robust to removing gradient clipping for both the learning rates used. Additional experiments with multiple seeds and learning rates can be found in figure 8 in appendix.

Second, we conduct experiments where we stochastically block gradients from flowing through the cell state c t instead of the hidden state h t and observe how the LSTM behaves in such a scenario. We refer detaching the cell state as c-detach. The goal of this experiment is to corroborate our hypothesis that the gradients through the cell state path carry information about long term dependencies. Figure 5 shows the effect of c-detach (with probabilities shown) on copying task and pixel by pixel MNIST task. We notice in the copying task for T = 100, learning becomes very slow (figure 5 (a)) and does not converge even after 500 epochs, whereas when not detaching the cell state, even the Vanilla LSTM converges in around 150 epochs for most cases for T=100 as shown in the experiments in section 3.1. For pixel by pixel MNIST (which involves 784 time steps), there is a much larger detrimental effect on learning as we find that none of the seeds cross 60% accuracy at the end of training (Figure 5 (b) ). This experiment corroborates our hypothesis that gradients through the cell state contain important components of the gradient signal as blocking them worsens the performance of these models when compared to Vanilla LSTM.

4. RELATED WORK:

Capturing long term dependencies in data using recurrent neural networks has been long known to be a hard problem (Hochreiter, 1991; Bengio et al., 1993) . Therefore, there has been a considerable amount of work on addressing this issue. Prior to the invention of the LSTM architecture (Hochreiter & Schmidhuber, 1997) , another class of architectures called NARX (nonlinear autoregressive models with exogenous) recurrent networks (Lin et al., 1996) was popular for tasks involving long term dependencies. More recently gated recurrent unit (GRU) networks (Chung et al., 2014) was proposed that adapts some favorable properties of LSTM while requiring fewer parameters. Other recent recurrent architecture designs that are aimed at preventing EVGP can be found in Zhang et al. (2018) , Jose et al. (2017) and Li et al. (2018) . Work has also been done towards better optimization for such tasks (Martens & Sutskever, 2011; Kingma & Ba, 2014) . Since vanishing and exploding gradient problems (Hochreiter, 1991; Bengio et al., 1994) also hinder this goal, gradient clipping methods have been proposed to alleviate this problem (Tomas, 2012; Pascanu et al., 2013 ). Yet another line of work focuses on making use of unitary transition matrices in order to avoid loss of information as hidden states evolve over time. Le et al. (2015) propose to initialize recurrent networks with unitary weights while Arjovsky et al. (2016) propose a new network parameterization that ensures that the state transition matrix remains unitary. Extensions of the unitary RNNs have been in Wisdom et al. (2016) , Mhammedi et al. (2016) and Jing et al. (2016) . Very recently, Ke et al. (2018) propose to learn an attention mechanism over past hidden states and sparsely back-propagate through paths with high attention weights in order to capture long term dependencies. Trinh et al. (2018) propose to add an unsupervised auxiliary loss to the original objective that is designed to encourage the network to capture such dependencies. We point out that our proposal in this paper is orthogonal to a number of the aforementioned papers and may even be applied in conjunction to some of them. Further, our method is specific to LSTM optimization and reduces computation relative to the vanilla LSTM optimization which is in stark contrast to most of the aforementioned approaches which increase the amount of computation needed for training.

5. DISCUSSION AND FUTURE WORK:

In section 3.5 we showed that LSTMs trained with h-detach are stable even without gradient clipping. We caution that while this is true, in general the gradient magnitude depends on the value of detaching probability used in h-detach. Hence for the general case, we do not recommend removing gradient clipping.

When training stacked LSTMs, there are two ways in which h-detach can be used: 1) detaching the hidden state of all LSTMs simultaneously for a given time step t depending on the stochastic variable ξ t ) stochastically detaching the hidden state of each LSTM separately. We leave this for future work.

h-detach stochastically blocks the gradient from flowing through the hidden states of LSTM. In corollary 1, we showed that in expectation, this is equivalent to dampening the gradient components from paths other than the cell state path. We especially chose this strategy because of its ease of implementation in current auto-differentiation libraries. Another approach to dampen these gradient components would be to directly multiply these components with a dampening factor. This feature is currently unavailable in these libraries but may be an interesting direction to look into. A downside of using this strategy though is that it will not reduce the amount of computation similar to h-detach (although it will not increase the amount of computation compared with vanilla LSTM either). Regularizing the recurrent weight matrices to have small norm can also potentially prevent the gradient components from the cell state path from being suppressed but it may also restrict the representational power of the model.

Given the superficial similarity of h-detach with dropout, we outline the difference between the two methods. Dropout randomly masks the hidden units of a network during the forward pass (and can be seen as a variant of the stochastic delta rule (Hanson, 1990) ). Therefore, a common view of dropout is training an ensemble of networks (Warde-Farley et al., 2013) . On the other hand, our method does not mask the hidden units during the forward pass. It instead randomly blocks the gradient component through the h-states of the LSTM only during the backward pass and does not change the output of the network during forward pass. More specifically, our theoretical analysis shows the precise behavior of our method: the effect of h-detach is that it changes the update direction used for descent which prevents the gradients through the cell state path from being suppressed.

We would also like to point out that even though we show improvements on the image captioning task, it does not fit the profile of a task involving long term dependencies that we focus on. We believe the reason why our method leads to improvements on this task is that the gradient components from the cell state path are important for this task and our theoretical analysis shows that h-detach prevents these components from getting suppressed compared with the gradient components from the other paths. On the same note, we also tried our method on language modeling tasks but did not notice any benefit.

6. CONCLUSION:

We proposed a simple stochastic algorithm called h-detach aimed at improving LSTM performance on tasks that involve long term dependencies. We provided a theoretical understanding of the method using a novel analysis of the back-propagation equations of the LSTM architecture. We note that our method reduces the amount of computation needed during training compared to vanilla LSTM training. Finally, we empirically showed that h-detach is robust to initialization, makes the convergence of LSTM faster, and/or improves generalization compared to vanilla LSTM (and other existing methods) on various benchmark datasets. 

APPENDIX A ADDITIONAL INFORMATION:

Copying Experiment setup -We define 10 tokens, {a i } 9 i=0 . The input to the LSTM is a sequence of length T + 20 formed using one of the ten tokens at each time step. Input for the first 10 time steps are sampled i.i.d. (uniformly) from {a i } 7 i=0 . The next T -1 entries are set to a 8 , which constitutes a delay. The next single entry is a 9 , which represents a delimiter, which should indicate to the algorithm that it is now required to reproduce the initial 10 input tokens as output. The remaining 10 input entries are set to a 8 . The target sequence consists of T + 10 repeated entries of a 8 , followed by the first 10 entries of the input sequence in exactly the same order. 

o t = σ W o [h t-1 , x t ] T + b o i t = σ W i [h t-1 , x t ] T + b i g t = tanh W g [h t-1 , x t ] T + b g f t = σ W f [h t-1 , x t ] T + b f h t = o t tanh(c t ) c t = f t c t-1 + i t g t

Here denotes the element-wise product, also called the Hadamard product. σ denotes the sigmoid activation function.

W o = [W oh ; W ox ]. W i = [W ih ; W ix ]. W g = [W gh ; W gx ]. W f = [W f h ; W f x ]. Notation 1 ∆ c t = diag[o t (1 -tanh 2 (c t ))] ∆ o t = diag[o t (1 -o t ) tanh(c t )] ∆ f t = diag[f t (1 -f t ) c t-1 ] ∆ i t = diag[i t (1 -i t ) g t ] ∆ g t = diag[(1 -g 2 t ) i t ]

For any ∈ {f, g, o, i}, define E (w) to be a matrix of size dim(h t ) × dim([h t ; x t ]). We set all the elements of this matrix to 0s if if w is not an element of W . Further, if w = (W ) kl , then (E (w)) kl = 1 and (E (w)) k l = 0 for all (k , l ) = (k, l).

ψ t = ∆ f t W f h + ∆ g t W gh + ∆ i t W ih ψt = ∆ o t W oh + ∆ c t ψ t k t = ∆ f t E f (w) + ∆ g t E g (w) + ∆ i t E i (w) • [h t-1 , x t ] T kt = ∆ o t E o (w) • [h t-1 , x t ] T + ∆ c t k t F t = diag(f t ) Ft = ∆ c t diag(f t )

Lemma 1 Let us assume w is an entry of the matrix W f , W i , W g or W o , then

df t dw = diag[f t (1 -f t )] • W f h • dh t-1 dw + E f (w) • [h t-1 , x t ] T do t dw = diag[o t (1 -o t )] • W oh • dh t-1 dw + E o (w) • [h t-1 , x t ] T di t dw = diag[i t (1 -i t )] • W ih • dh t-1 dw + E i (w) • [h t-1 , x t ] T dg t dw = diag[(1 -g 2 t )] • W gh • dh t-1 dw + E g (w) • [h t-1 , x t ] T

Proof By chain rule of total differentiation,

df t dw = ∂f t ∂w + ∂f t ∂h t-1 dh t-1 dw We note that, ∂f t ∂w = diag[f t (1 -f t )] • E f (w) • [h t-1 , x t ] T and, ∂f t ∂h t-1 = diag[f t (1 -f t )] • W f h • dh t-1 dw

which proves the claim for dft dw . The derivation for dot dw , dit dw , dgt dw are similar. Now let us establish recursive formulas for dht dw and dct dw , using the above formulas

Corollary 1 Considering the above notations, we have

dh t dw = ∆ o t W oh • dh t-1 dw + ∆ c t • dc t dw + ∆ o t E o (w) • [h t-1 , x t ] T

Proof Recall that h t = o t tanh(c t ), and thus

dh t dw = do t dw tanh(c t ) + o t (1 -tanh 2 (c t )) dc t dw

Using the previous Lemma as well as the above notation, we get

dh t dw = diag[o t (1-o t )]• W oh • dh t-1 dw + E o (w) • [h t-1 , x t ] T tanh(c t )+o t (1-tanh 2 (c t )) dc t dw = ∆ o t W oh • dh t-1 dw + ∆ o t E o (w) • [h t-1 , x t ] T + o t (1 -tanh 2 (c t )) dc t dw = ∆ c t dc t dw + ∆ o t W oh • dh t-1 dw + ∆ o t E o (w) • [h t-1 , x t ] T

Corollary 2 Considering the above notations, we have

dc t dw = F t dc t-1 dw + ψ t • dh t-1 dw + k t

Proof Recall that c t = f t c t-1 + i t g t , and thus

dc t dw = df t dw c t-1 + f t dc t-1 dw + dg t dw i t + g t di t dw

Using the previous Lemma as well as the above notation, we get

dc t dw = diag[f t (1 -f t )] • W f h • dh t-1 dw + E f (w) • [h t-1 , x t ] T c t-1 + f t dc t-1 dw +diag[(1 -g 2 t )] • W gh • dh t-1 dw + E g (w) • [h t-1 , x t ] T i t +diag[i t (1 -i t )] • W ih • dh t-1 dw + E i (w) • [h t-1 , x t ] T g t = ∆ f t W f h • dh t-1 dw + ∆ f t E f (w) • [h t-1 , x t ] T + f t dc t-1 dw +∆ g t W gh • dh t-1 dw + ∆ g t E g (w) • [h t-1 , x t ] T +∆ i t W ih • dh t-1 dw + ∆ i t E i (w) • [h t-1 , x t ] T = F t dc t-1 dw + ∆ f t W f h + ∆ g t W gh + ∆ i t W ih • dh t-1 dw + ∆ f t E g (w) + ∆ g t E g (w) + ∆ i t E g (w) • [h t-1 , x t ] T = F t dc t-1 dw + ψ t • dh t-1 dw + k t

Let us now combine corollary 1 and 2 to get a recursive expression of dht dw in terms of It suffices to take the expectation both sides, and use independence of ξ t 's.

:

Our code is available at https://github.com/bhargav104/h-detach.

Figures and Tables:

Figure2: Validation accuracy curves during training on copying task using vanilla LSTM (left) and LSTM with h-detach with probability 0.25 (middle) and 0.5 (right). Top row is delay T = 100 and bottom row is delay T = 300. Each plot contains multiple runs with different seeds. We see that for T = 100, even the baseline LSTM is able to reach ∼ 100% accuracy for most seeds and the only difference we see between vanilla LSTM and LSTM with h-detach is in terms of convergence. T = 300 is a more interesting case because it involves longer term dependencies. In this case we find that h-detach leads to faster convergence and achieves ∼ 100% validation accuracy while being more robust to the choice of seed.

Figure 3: Validation accuracy curves of LSTM training on pixel by pixel MNIST. Each plot shows LSTM training with and without h-detach for different values of learning rate. We find that hdetach is both more robust to different learing rates and converges faster compared to vanilla LSTM training. Refer to the Fig. 6 in appendix for validation curves on multiple seeds.

Figure 4: The effect of removing gradient clipping from vanilla LSTM training vs. LSTM trained with h-detach on pixel by pixel MNIST dataset. Refer to Fig. 8 in appendix for experiments with multiple seeds.

Figure 6: Validation accuracy curves on pixel by pixel MNIST dataset with vanilla LSTM training and LSTM training with h-detach with various values of learning rate and initialization seeds.

Figure 7: Validation accuracy curves on pMNIST dataset with vanilla LSTM training and LSTM training with h-detach.

Figure 8: The effect of removing gradient clipping during optimization. Validation accuracy curves on pixel by pixel MNIST dataset with vanilla LSTM training and LSTM training with h-detach with various values of learning rate and initialization seeds. LSTM training using h-detach is both significantly more stable and robust to initialization when removing gradient clipping compared with vanilla LSTM training.

Thus the terms in A t are a function of the LSTM gates and hidden and cell states. Note that all the gates and hidden states h t are bounded by definition because they are a result of sigmoid or tanh activation functions. The cell state c t on the other hand evolves through a linear recursive equation shown in Eq. (1). Thus it can grow at each time step by at most ±1 (element-wise) and its value is bounded by the number of time steps t. Thus given a finite number of time steps and finite initialization of c 0 , the values in matrix A t are bounded.The matrix B t on the other hand contains components of parameter's full gradient that arise due to the remaining paths. The elements of B t are a linear function of the weights W gh , W f h , W ih , W oh . Thus the magnitude of elements in B t can become very large irrespective of the number of time steps if the weights are very large. This problem becomes worse when we multiply B t s in Eq. (

Table content: <table/>

Accuracy on transfer copying task. We find that the generalization of LSTMs trained with h-detach is significantly better compared with vanilla LSTM training when tested on time delays longer that what the model is trained on (T = 100).

Table content: <table><tr><td>T</td><td colspan="3">VanillaLSTM h-detach 0.5 h-detach 0.25</td></tr><tr><td>200</td><td>64.85</td><td>74.79</td><td>90.72</td></tr><tr><td>400</td><td>48.17</td><td>54.91</td><td>77.76</td></tr><tr><td>500</td><td>43.03</td><td>52.43</td><td>74.68</td></tr><tr><td>1000</td><td>28.82</td><td>43.54</td><td>63.19</td></tr><tr><td>2000</td><td>19.48</td><td>34.34</td><td>51.83</td></tr><tr><td>5000</td><td>14.58</td><td>24.55</td><td>42.35</td></tr></table>

the pMNIST task, we find that training LSTM with h-detach gives a test accuracy of 92.3% which is an improvement over the regular LSTM training which reaches an accuracy of 91.1%. A detailed comparison of test performance with existing algorithms is shown in table 2.

Table content: <table><tr><td>Method</td><td colspan="2">MNIST pMNIST</td></tr><tr><td>Vanilla LSTM</td><td>98.5</td><td>91.1</td></tr><tr><td>SAB (Ke et al., 2018)</td><td>-</td><td>94.2</td></tr><tr><td>iRNN Le et al. (2015)</td><td>97.0</td><td>82.0</td></tr><tr><td>uRNN (Arjovsky et al., 2016)</td><td>95.1</td><td>91.4</td></tr><tr><td>Zoneout (Krueger et al., 2016)</td><td>-</td><td>93.1</td></tr><tr><td>IndRNN (Li et al., 2018)</td><td>99</td><td>96</td></tr><tr><td>h-detach (ours)</td><td>98.5</td><td>92.3</td></tr></table>

Fix w to be an element of the matrix W gh , W f h , W ih or W oh . Define, In other words,z t = (A t + B t )(A t-1 + B t-1 ) . . . (A 2 + B 2 )z 1where all the symbols used to define A t and B t are defined in notation 1.Applying this formula recursively proves the claim.Note: Since A t has 0 n 's in the second column of the block matrix representation, it ignores the contribution of z t coming from h t-1 , whereas B t (having non-zero block matrices only in the second column of the block matrix representation) only takes into account the contribution coming from h t-1 . Hence A t captures the contribution of the gradient coming from the cell state c t-1 .C DERIVATION OF BACK-PROPAGATION EQUATION FOR LSTM WITHh-DETACH T n ] T and zt be the analogue of z t when applying h-detach with probability p during back-propagation. Then,zt = (A t + ξ t B t )(A t-1 + ξ t-1 B t-1 ) . . . (A 2 + ξ 2 B 2 )z 1where ξ t , ξ t-1 , . . . , ξ 2 are i.i.d. Bernoulli random variables with probability p of being 1, A t and B t and are same as defined in theorem 1.Iterating this formula gives,zt = (A t + ξ t B t )(A t-1 + ξ t-1 B t-1 ) . . . (A 3 + ξ 3 B 3 )z 2Corollary 4 E[z t ] = (A t + pB t )(A t-1 + pB t-1 ) . . . (A 3 + pB 3 )z 2

Table content: <table><tr><td colspan="8">Theorem 1 A t = Then, Theorem 2 Let z t = [ dct   F t 0 n diag(k t ) Ft 0 n diag( kt ) 0 n 0 n Id n   B t = z t = (A t + B t )z t-1   0 n ψ t 0 n 0 n ψt 0 n 0 n 0 n 0 n dw T ; dht dw T ; 1 Proof Replacing ∂ ∂ht-1 by ξ t ∂ ∂ht-1 in lemma 1 and therefore in Corollaries 2 and 3, we get the   z t =   dct dw dht dw 1 n   (9)</td></tr><tr><td colspan="4">following analogous equations</td><td/><td/><td/></tr><tr><td/><td/><td/><td>dc t dw</td><td colspan="2">= F t</td><td colspan="2">dc t-1 dw</td><td>+ ξ t ψ t •</td><td>dh t-1 dw</td><td>+ k t</td></tr><tr><td>and</td><td/><td/><td/><td/><td/><td/></tr><tr><td colspan="4">Proof By Corollary 2, we get</td><td/><td/><td/></tr><tr><td colspan="8">dc t dw dh t dw Similarly as in the proof of previous theorem, we can rewrite = F t dc t-1 dw + ψ t • dh t-1 = Ft dc t-1 dw + ξ t ψt • dh t-1 dw + k t + kt dw</td></tr><tr><td colspan="7">dct-1 dw Similarly by Corollary 3, we get = F t dh t dc t-1 dw dw = Ft = Ft dc t-1 dw dc t dw Id n  = [F 0 n 0 n  • zt-1 =</td><td>+ ψ t • dc t-1 dw + ψt •   F t 0 n diag(k t ) dh t-1 dw + diag(k t )1 n + ψt • dh t-1 dw + kt dh t-1 dw + diag( kt )1 n 0 n 0 n Id n    Ft 0 n diag( kt )  + ξ t</td><td> 0 n ψ t 0 n 0 n 0 n 0 n  0 n ψt 0 n</td><td>dht-1 dw and     • zt-1</td></tr><tr><td colspan="8">Corollary 3 Considering the above notations, we have</td></tr><tr><td/><td/><td/><td/><td/><td colspan="3">= (A</td></tr><tr><td colspan="2">Thus we have</td><td/><td colspan="2">dh t dw</td><td colspan="2">= Ft</td><td>dc t-1 dw</td><td>+ ψt •</td><td>dh t-1 dw</td><td>+ kt</td></tr><tr><td colspan="6">Proof From Corollary 1, we know that</td><td/></tr><tr><td/><td>dh t dw</td><td colspan="6">= ∆ o t W oh •  F t ψ t diag(k t ) dh t-1 dw + ∆ c t •  z t =  Ft ψt diag( kt )  • z t-1 = (A t + B t ) • z t-1 dc t dw + ∆ o t E o (w) • [h t-1 , x t ] T</td><td>(10)</td></tr><tr><td colspan="3">Using Corollary 2, we get</td><td colspan="3">0 n 0 n</td><td/><td>Id n</td></tr><tr><td>dh t dw</td><td>= ∆ o t W oh •</td><td>dh t-1 dw</td><td colspan="5">+ ∆ c t • F t t E = Ft dc t-1 dw + ψ t • dh t-1 dw + k t + ∆ o dc t-1 dw + ψt • dh t-1 dw + kt</td></tr></table>

Peer review (iclr2019):
 The results are intriguing. However, similar methods like BN-LSTM [3] and Variational RNNs [4] achieve arguably the same with very similar mechanisms. We do not think they can be considered as orthogonal. This should be addressed by the authors. Also, hard long-term experiments like sequentially predicting pixels (like through MDLSTM-based PixelRNN) or language modelling should be favoured over short sentence image captions. It is possible that we will improve our ratings once our concerns are addressed. Paper Summary: The authors claim that the gradient along the computational path that goes through the cell state (the linear temporal path or A gradient) of an LSTM carries information about long-term dependencies. Those gradients can be corrupted by the gradient of all other computational paths (i.e. the B gradient). They claim that this makes it hard to learn long-term dependencies and has, therefore, significant negative effects on the convergence speed, training stability, and generalisation performance. They propose a method called h-detach and run experiments on the delayed copy task, sequential MNIST, permuted sequential MNIST (pMNIST), and caption generation on the MS COCO dataset. All show either somewhat improved performance or much more stable learning curves. At every step, h-detach randomly drops all gradients that flow through the h of the standard LSTM, the B gradients, and only keeps the ones from the linear temporal path, the A gradients. Experiments also suggest that the A gradients carry more long-term information than B gradients and that LSTMs with h-detach do not need gradient clipping for successful training. Positive: The paper is written clearly. It is well structured and well motivated. H-detach is simple, effective, and somewhat novel (see below). Experiments indicate that its main benefit is training stability as well as minor performance improvements. Negative: We are not sure how significant these results are for the following reasons: - MS COCO image caption generation is the only more challenging dataset, but it seems a bit misplaced as it has very short sentences, while the authors motivate their work through a focus on long-term dependencies. Why not apply h-detach to a language model such as [1] with official online implementations, e.g., [2]. A setting with PixelRNN [6] based on MD-LSTM [7] would also be a great testbed for h-detach. - The purpose of h-detach is to scale down the B gradients. However, methods which apply e.g. BatchNorm to the hidden state learn a scale parameter which could be learned by the network explicitly. For the backward pass, this has the effect of scaling down the B gradient. Consider e.g. [3] which also achieves similar training stability on sequential MNIST and pMNIST with little overhead. - Another very related method is [4] which properly applies a random dropout mask over the recurrent inputs that is shared across timesteps of an RNN. We think that h-detach is essentially achieving the same in a similar way. Problems with Introduction and Related Work Section: - The vanishing gradient problem was first described by Hochreiter in 1991 [5] (not by Bengio in 1994). - Intro mentions GRU as if it was separate from LSTM. Clarify that GRU is essentially a variant of vanilla LSTM with forget gates [8]. Since one gate is missing, GRU is less powerful than the original LSTM [9]. [1] Zaremba et al. "Recurrent neural network regularization." arXiv:1409.2329 (2014). [2] https://www.tensorflow.org/tutorials/sequences/recurrent [3] Cooijmans et al. "Recurrent batch normalization." arXiv:1603.09025 (2016). [4] Gal et al. "A theoretically grounded application of dropout in recurrent neural networks." NIPS 2016. [5] Hochreiter, Sepp. "Untersuchungen zu dynamischen neuronalen Netzen." Diploma thesis, TUM (1991) [6] Oord et al. "Pixel recurrent neural networks." arXiv preprint arXiv:1601.06759 (2016). [7] Graves et al. "Multi-Dimensional Recurrent Neural Networks" arXiv preprint arXiv:0705.2011 (2011). [8] Gers et al. “Learning to Forget: Continual Prediction with LSTM.“ Neural Computation, 12(10):2451-2471, 2000. [9] Weiss et al. On the Practical Computational Power of Finite Precision RNNs for Language Recognition. arXiv:1805.04908. Comments after rebuttal: The paper has clearly improved. It leaves a few questions open though. For example, it is surprising that h-detach doesn*t work on language modelling since Dropout-LSTM and BN-LSTM clearly improve over vanilla LSTM in this case (if not every case). In the new version, the authors only reference it in one or two sentences but don*t discuss this in detail. When dropout is mentioned, one should also mention that dropout is a variant of the old stochastic delta rule: Hanson, S. J. (1990). A Stochastic Version of the Delta Rule, PHYSICA D,42, 265-272. See also arXiv:1808.03578 Nevertheless, we now think that this is a very interesting LSTM regularization paper that people who study this field should probably know. We are increasing the score by 2 points!