<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2026-05-17T04:28:44+00:00</updated><id>/feed.xml</id><title type="html">Thanasis Taprantzis</title><subtitle>Hey there, I&apos;m Thanasis and this is my blog! I write about stuff I find interesting.</subtitle><entry><title type="html">The Vanishing and Exploding Gradients Problem</title><link href="/jekyll/update/2026/04/04/exploding-gradients.html" rel="alternate" type="text/html" title="The Vanishing and Exploding Gradients Problem" /><published>2026-04-04T15:00:01+00:00</published><updated>2026-04-04T15:00:01+00:00</updated><id>/jekyll/update/2026/04/04/exploding-gradients</id><content type="html" xml:base="/jekyll/update/2026/04/04/exploding-gradients.html"><![CDATA[<p>While studying neural networks you might often hear of the vanishing/exploding gradients problem as a usual issue when trying to train deep multi-layered models. “The model’s gradient gets exponentially large or small the more layers you add”.</p>

<p>But … why? Intuitively, one can probably see on a very high level why that might happen. However, what is the mathematical reason this happens, what is the proof that explains why this occurs? Let us explore this problem at a fundamental level and understand what the problem is and why it happens at its core.</p>

<h3 id="recurrent-neural-networks-fundamentals">Recurrent Neural Networks Fundamentals</h3>

<p>We will begin with a brief but necessary review of what a recurrent neural network is and how we can formulate it formally.</p>

<p>A recurrent neural network is a neural network <em>whose output will constitute part of its next input</em>.</p>

<p>It’s important to note that the input depends both on the previous output and any new input the model might receive at any particular loop. Thus, RNNs function as neural networks that can “remember” information from past computations. The output of previous computations is simply propagated <em>directly</em> to newer states.</p>

<p>Take a look at a snapshot of the RNN flow at some timestamp $t$.
<img src="/assets/RNN_flow.png" alt="The flow of an RNN" /></p>

<p>At each state we have a brand new input $u_t$, the previous output $x_{t-1}$ as additional input, output $x_t$, and a loss $\mathcal{E}_t$.</p>

<p>Unlike a traditional Neural Network, the loss is not simply defined on the final output since an RNN computes multiple outputs in a sequence. Each output produced demands a new loss computation $\mathcal{E}_t$</p>

<p><strong>Why are we focusing on RNNs?</strong> They are a generalised version of multi-layered networks. They can have arbitrary depth due to their recursive nature making them ideal to demonstrate vanishing/exploding gradients.</p>

<p>Our findings will be applicable to any multi-layered network with the same foundational structure.</p>

<h3 id="a-mathematical-formulation-of-rnn">A Mathematical Formulation of RNN</h3>
<p>Let us describe the RNN in algebraic terms. At each timestep $t$:</p>

\[x_t = F(x_{t-1}, u_t, \theta) \tag{1}\]

<p>where $\theta$ are the model parameters - usually the weights $W$ of the model.</p>

<p>This formulation is intentionally broad and non-specific as to what the function $F$ actually looks like. In our case, for an RNN we will be looking at the following implementation:</p>

\[\begin{align}
x_t &amp;= W
    \begin{bmatrix}
      \sigma(x_{t-1}) \\
      u_t \\
      1\\
    \end{bmatrix}\\
  &amp;= W_{rec}\sigma(x_{t-1}) + W_{in} u_t  + b\tag{2}
\end{align}\]

<p>where $W_{rec}$ (recurrent) are the weights that are multiplied with the previous output, $W_{in}$ is multiplied with the new input, $\sigma$ is an element-wise function (such as ReLU) to introduce non-linearity, and $b$ is the bias.</p>

<p><strong>NOTE:</strong> This is one of the multiple potential implementations of an RNN. Here we only have one layer $W$ per timestep but we could have multiple. Also, $\sigma$ could be applied in a different way: $x_t = \sigma(W_{rec}x_{t-1} + W_{in} u_t + b)$.</p>

<p>We will stick with eq. (2) but note that implementation details can change, however the essence of the problem is the same. Eq. (2) is a “vanilla” implementation of RNN, and all findings can be applied to different implementations of RNN models.</p>

<h3 id="defining-the-loss">Defining the Loss</h3>

<p>With RNNs we are optimising for some type of error/loss. The specific loss function we utilize is arbitrary for this proof. For instance, it could be the difference of the output vector $x_t$ and some target $y_t$ squared (<a href="https://en.wikipedia.org/wiki/Mean_squared_error">MSE</a>). All we should care about is that our loss is a function of $x_t$, i.e. $\mathcal{E_t} = \mathcal{L}(x_t)$.</p>

<p>However, we can measure the loss of each output we produce at any timestep $t$. The question naturally arises: <em>what is the final metric for which we are optimising for if we have multiple losses?</em></p>

<p>Here we could come up with all sorts of schemes. For instance, we could weigh the loss computations of the later stages higher than previous ones. To keep things simple, let us assume that we are simply considering all stages of the RNN to have an equal share of impact. We will assume that the final metric we are optimising for is the sum of all losses:</p>

\[\mathcal{E} = \sum_{t} \mathcal{E_t} = \sum_t \mathcal{L(x_t)} \tag{3}\]

<h3 id="optimising-for-the-loss">Optimising for the Loss</h3>

<p>Now for the part we have building up to: let us optimise for the loss as defined above. We are trying to minimise the loss by altering the parameters of the model ($\theta$).</p>

<p><strong>Remember, vanishing/exploding gradients affects our ability to optimise the loss.</strong> Here is where we start to show this.</p>

\[\begin{aligned}
  \frac{\partial\mathcal{E}}{\mathcal{\partial\theta}} &amp;= \sum_t \frac{\partial\mathcal{E_t}}{\partial \mathcal \theta}
\end{aligned} \tag{4}\]

<p>Let us focus w.l.o.g. on some timestep $t$:</p>

\[\begin{aligned}
  \frac{\partial\mathcal{E_t}}{\mathcal{\partial\theta}} &amp;= \frac{\partial\mathcal{E_t}}{\partial x_t} \frac{\partial x_t}{\mathcal{\partial\theta}}
\end{aligned}\tag{5}\]

<p>Let us pause here. We have two components.</p>

<p>First, $\frac{\partial\mathcal{E_t}}{\partial x_t}$ remains as is. The loss is a direct function of $x_t$. We cannot simplify further.</p>

<p>$\frac{\partial x_t}{\mathcal{\partial\theta}}$ is more involved. By looking at eq. (2) $x_t$ is a function of $W$ and $x_{t-1}$ (which is is also in turn implicitly a function of $W$). These are the two variables we care about since we are optimising for $\theta$ ($W$). $u_t$ and $b$ are irrelevant as they are not a function of $\theta$($W$).</p>

<p>Remember from multivariable calculus that if $x=g(t)$, $y=h(t)$, and $z = f(x, y)$, then $\frac{\partial z}{\partial t} = \frac{\partial z}{\partial x} \frac{\partial x}{\partial t} + \frac{\partial z}{\partial y} \frac{\partial y}{\partial t}$.</p>

<p>To make our case easier to read, let us make an auxiliary function $g(W) = W$. So:</p>

\[\begin{align}
x_t &amp;= g(W)
    \begin{bmatrix}
      \sigma(x_{t-1}(W)) \\
      u_t \\
      1\\
    \end{bmatrix}\tag{6}
\end{align}\]

<p>Now it becomes easier to see that we can do the following:</p>

\[\begin{align}
  \frac{\partial x_t}{\mathcal{\partial\theta}} = \frac{\partial x_t}{\mathcal{\partial g}} \frac{\partial g}{\mathcal{\partial\theta}} + \frac{\partial x_t}{\mathcal{\partial x_{t-1}}} \frac{\partial x_{t-1}}{\mathcal{\partial\theta}}
\end{align}\]

<p>which can be simplified to 
\(\begin{align}
  \boxed{\frac{\partial x_t}{\mathcal{\partial\theta}} = \frac{\partial x_t}{\mathcal{\partial g}} + \frac{\partial x_t}{\mathcal{\partial x_{t-1}}} \frac{\partial x_{t-1}}{\mathcal{\partial\theta}}}
\end{align}\tag{7}\)
Now we can expand:
\(\begin{align}
  \Rightarrow \frac{\partial\mathcal{E_t}}{\mathcal{\partial\theta}}
  = \frac{\partial\mathcal{E_t}}{\partial x_t} \frac{\partial x_t}{\mathcal{\partial\theta}}
  &amp;= \frac{\partial\mathcal{E_t}}{\partial x_t} [\frac{\partial x_t}{\mathcal{\partial g}} + \frac{\partial x_t}{\mathcal{\partial x_{t-1}}} \frac{\partial x_{t-1}}{\mathcal{\partial\theta}}]\\
  &amp;\boxed{= \frac{\partial\mathcal{E_t}}{\partial x_t}\frac{\partial x_t}{\mathcal{\partial g}} + \frac{\partial\mathcal{E_t}}{\partial x_t}\frac{\partial x_t}{\mathcal{\partial x_{t-1}}} \frac{\partial x_{t-1}}{\mathcal{\partial\theta}}}\\
\end{align}\tag{8.1}\)</p>

<p>Notice that the last term $\frac{\partial x_{t-1}}{\mathcal{\partial\theta}}$ can be recursively substituted using eq (7) which will result in an equation with $\frac{\partial x_{t-2}}{\mathcal{\partial\theta}}$ which will be recursively substituted, etc. till we reach $x_1$.</p>

<p>Thus, intuitively we should see the following pattern occurring in eq (8.1):
\(\begin{align}
  \Rightarrow \frac{\partial\mathcal{E_t}}{\mathcal{\partial\theta}}
  &amp;= \frac{\partial\mathcal{E_t}}{\partial x_t}\frac{\partial x_t}{\mathcal{\partial g}} + \frac{\partial\mathcal{E_t}}{\partial x_t}\boxed{\frac{\partial x_t}{\mathcal{\partial x_{t-1}}}} \frac{\partial x_{t-1}}{\mathcal{\partial\theta}}\\
  &amp;= \frac{\partial\mathcal{E_t}}{\partial x_t}\frac{\partial x_t}{\mathcal{\partial g}} + \frac{\partial\mathcal{E_t}}{\partial x_t}\boxed{\frac{\partial x_t}{\mathcal{\partial x_{t-1}}}} \frac{\partial x_{t-1}}{\partial g} +\frac{\partial\mathcal{E_t}}{\partial x_t}\boxed{\frac{\partial x_t}{\mathcal{\partial x_{t-1}}} \frac{\partial x_{t-1}}{\partial x_{t-2}}}\frac{\partial x_{t-2}}{\partial \theta}\\
  &amp;= \frac{\partial\mathcal{E_t}}{\partial x_t}\frac{\partial x_t}{\mathcal{\partial g}} + \frac{\partial\mathcal{E_t}}{\partial x_t}\boxed{\frac{\partial x_t}{\mathcal{\partial x_{t-1}}}} \frac{\partial x_{t-1}}{\partial g} + \frac{\partial\mathcal{E_t}}{\partial x_t}\boxed{\frac{\partial x_t}{\mathcal{\partial x_{t-1}}} \frac{\partial x_{t-1}}{\partial x_{t-2}}}\frac{\partial x_{t-2}}{\partial g} + \frac{\partial\mathcal{E_t}}{\partial x_t}\boxed{\frac{\partial x_t}{\mathcal{\partial x_{t-1}}} \frac{\partial x_{t-1}}{\partial x_{t-2}}\frac{\partial x_{t-2}}{\partial x_{t-3}}}\frac{\partial x_{t-3}}{\partial \theta}\\
  &amp;= ...
\end{align}\tag{8.2}\)</p>

<p>The emerging structure is pretty clear and we can formulate it <em>inefficiently</em> as follows:</p>

\[\begin{align}
  \Rightarrow \frac{\partial\mathcal{E_t}}{\mathcal{\partial\theta}}
  &amp;= \sum_{k=1}^t [\frac{\partial \mathcal{E}_t}{\partial x_t} \prod_{j=k+1}^t [\frac{\partial x_j}{\partial x_{j-1}}]\frac{\partial x_k}{\partial g}]
\end{align}\tag{9}\]

<p>This can be simplified further. Notice that when taking the derivative of $x_t$ in terms of $x_k$ such that $k&lt;t$ there is a <em>linear and predictable relationship between each timestep</em>.</p>

<p>That is, $x_t$ is a function of $x_{t-1}$ which is a function of $x_{t-2}$ which is a function of $x_{t-3}$ … until $x_k$. <strong>There are no other paths from $x_t$ to $x_k$.</strong> I.e. $x_t$ is not a function of both $x_{t-1}$ and $x_{t-2}$ or any other $x_j$ for some $j\neq t-1$.</p>

<p>Thus:
\(\begin{align}
  \frac{\partial x_t}{\partial x_k} &amp;= \frac{\partial x_t}{\partial x_{t-1}} \frac{\partial x_{t-1}}{\partial x_k}\\
  &amp;= \frac{\partial x_t}{\partial x_{t-1}} \frac{\partial x_{t-1}}{\partial x_{t-2}} \frac{\partial x_{t-2}}{\partial x_k}\\
  &amp;= ...\\
  &amp;= \prod_{j=k+1}^t \frac{\partial x_{j}}{\partial x_{j-1}}
\end{align}\tag{10}\)</p>

<p>So, eq (9) gets further simplified to:
\(\begin{align}
  \Rightarrow \frac{\partial\mathcal{E_t}}{\mathcal{\partial\theta}}
  &amp;= \sum_{k=1}^t [\frac{\partial \mathcal{E}_t}{\partial x_t} \frac{\partial x_t}{\partial x_{k}}\frac{\partial x_k}{\partial g}]
\end{align}\tag{11}\)</p>

<p><strong>TL;DR, we derived a simple formula for optimising the loss at a timstep $t$.</strong></p>

<h4 id="solving-the-gradients">Solving the Gradients</h4>

<p>With the simplified formulation of what our loss looks like at timestep $t$, we will go a step further and start computing the gradients.</p>

<p>We will cheat here and focus directly on $\frac{\partial x_t}{\partial x_k}$. We already showed that $\frac{\partial x_t}{\partial x_k} = \prod_{j=k+1}^t \frac{\partial x_{j}}{\partial x_{j-1}}$.</p>

<p>So let us solve $\frac{\partial x_{j}}{\partial x_{j-1}}$:</p>

\[\begin{align}
  \frac{\partial x_{j}}{\partial x_{j-1}} &amp;= \frac{\partial [W
    \begin{bmatrix}
      \sigma(x_{j-1}) \\
        u_t \\
        1\\
      \end{bmatrix}
    ]
    }{\partial x_{j-1}} 
    =  \frac{\partial [W_{rec}\sigma(x_{j-1})]
    }{\partial x_{j-1}}\\
\end{align}\]

<p>Most resources skip this final calculation but we will go through step by step how to compute this to understand the derivation fully.</p>

<p>As a refresher, take a look at <a href="https://en.wikipedia.org/wiki/Matrix_calculus#Vector-by-vector">vector by vector partial derivatives</a>. We will use numerator layout notation but it does not really matter in this case.</p>

<p>Let us denote $x_j^i$ as the <strong>$i$-th element</strong> of output vector $x$ at timestep $j$:</p>

\[\begin{align}
 \frac{\partial x_{j}}{\partial x_{j-1}} &amp;=
  \frac{\partial [W_{rec}\sigma(x_{j-1})]}{\partial x_{j-1}}\\
    &amp;= \frac{\partial \begin{bmatrix}
      W_{rec (1,1)}\sigma(x_{j-1}^1) + W_{rec (1,2)}\sigma(x_{j-1}^2) + \dots \\
      W_{rec (2,1)}\sigma(x_{j-1}^1) + W_{rec (2,2)}\sigma(x_{j-1}^2) + \dots \\
      \vdots\\
    \end{bmatrix}
    }{\partial x_{j-1}}\\
    &amp;= \begin{bmatrix}
      \frac{\partial [W_{rec (1,1)}\sigma(x_{j-1}^1) + W_{rec (1,2)}\sigma(x_{j-1}^2) + \dots]}{\partial x_{j-1}^1} &amp; \frac{\partial [W_{rec (1,1)}\sigma(x_{j-1}^1) + W_{rec (1,2)}\sigma(x_{j-1}^2) + \dots]}{\partial x_{j-1}^2}  &amp; \dots\\
      \frac{\partial [W_{rec (2,1)}\sigma(x_{j-1}^1) + W_{rec (2,2)}\sigma(x_{j-1}^2) + \dots]}{\partial x_{j-1}^1} &amp; \frac{\partial [W_{rec (2,1)}\sigma(x_{j-1}^1) + W_{rec (2,2)}\sigma(x_{j-1}^2) + \dots]}{\partial x_{j-1}^2}  &amp; \\
      \vdots &amp;  &amp; \ddots\\
    \end{bmatrix}\\
    &amp;= \begin{bmatrix}
      W_{rec(1, 1)}\sigma'(x_{j-1}^1) &amp; W_{rec(1, 2)}\sigma'(x_{j-1}^2) &amp; \dots\\
      W_{rec(2, 1)}\sigma'(x_{j-1}^1) &amp; W_{rec(2, 2)}\sigma'(x_{j-1}^2) &amp; \\
      \vdots &amp;  &amp; \ddots\\
    \end{bmatrix}\\
    &amp;= \begin{bmatrix}
      W_{rec(1, 1)} &amp; W_{rec(1, 2)} &amp; \dots\\
      W_{rec(2, 1)} &amp; W_{rec(2, 2)} &amp; \\
      \vdots &amp;  &amp; \ddots \\
    \end{bmatrix}
    \begin{bmatrix}
      \sigma'(x_{j-1}^1) &amp; 0 &amp; \dots\\
      0 &amp; \sigma'(x_{j-1}^2) &amp; \\
      \vdots &amp;  &amp; \ddots \\
    \end{bmatrix}\\
    &amp;= \boxed{W_{rec} diag(\sigma'(x_{j-1}))}\tag{12}
\end{align}\]

<p>Going back to the beginning of this paragraph, we can now show the following:
\(\begin{align}
    \frac{\partial x_t}{\partial x_k} = \prod_{j=k+1}^t \frac{\partial x_{j}}{\partial x_{j-1}} = \prod_{j=k+1}^t [W_{rec} diag(\sigma'(x_{j-1}))]
\end{align}\tag{13}\)</p>

<p>This equation is <strong>crucial</strong>. We just showed that when optimising for the loss of an RNN we will inevitably have to deal with a gradient computation which involves multiplying $W_{rec}$ repeatedly with itself $t-k$ times. This intuitively means that the deeper the network the more times we will have to multiply with $W_{rec}$ and the more amplified the effect of the matrix will be. Let’s make this concrete.</p>

<h3 id="vanishingexploding-the-gradients">Vanishing/Exploding the Gradients</h3>

<p><em>“To understand this phenomenon we need to look at the form of each temporal component, and in particular at the matrix factors $\frac{\partial x_t}{\partial x_k}$ […] that take the form of a product of $t − k$ Jacobian matrices. In the same way a product of $t − k$ real numbers can shrink to zero or explode to infinity, so does this product of matrices (along some direction v)”</em> (<a href="https://proceedings.mlr.press/v28/pascanu13.pdf">Pascanu et al</a>).</p>

<h4 id="a-linear-algebra-precursor">A Linear Algebra Precursor</h4>

<p>Before we step into the final part, let us introduce/recap a few concepts and formulas that will be important.</p>

<h5 id="singular-values"><strong>Singular values</strong></h5>

<p>A singular value of a matrix $\sigma(A)$ geometrically expresses the <em>stretching factor in any direction</em>. This is different than the eigenvalues of a matrix which only express the stretching factor along the direction of a matrix’s eigenvectors (which do not necessarily span the matrix’s space).</p>

<p>By extension, $\sigma_{max}(A)$ geometrically expresses the <strong>(absolute) maximum stretching factor in any direction</strong>.</p>

<p>Formally:</p>

\[\sigma_{max}(A) = \sqrt{\lambda_{max}(A^*A)}\]

<h5 id="spectral-norm-2-norm-of-a-matrix"><strong>Spectral Norm (2-Norm) of a Matrix</strong></h5>

<p>The spectral norm of a matrix is just the max singular value:</p>

\[\|A\|_2 = \sigma_{max}(A) = \sqrt{\lambda_{max}(A^*A)}\]

<h5 id="spectral-radius"><strong>Spectral Radius</strong></h5>

<p>The spectral radius of a matrix is a fancy term for the maximum (absolute) eigenvalue:</p>

\[\rho(A) = \max \{\vert\lambda_1\vert, \dots, \vert\lambda_n\vert\}\]

<h5 id="spectral-norm--spectral-radius"><strong>Spectral Norm + Spectral Radius</strong></h5>

\[\|A\|_2 = \sigma_{max}(A) = \sqrt{\lambda_{max}(A^*A)} = \sqrt{\rho(A^*A)}\]

<p>We are allowed to do this because all the eigenvalues of \(A^*A\) are non-negative. This is because \(A^*A\) is a Hermitian Positive Semi-Definite Matrix:</p>

<ol>
  <li>Hermitian: \((A^*A)^* = A^*A\)</li>
  <li>PSD: \(x^* A^*A x = (Ax)^*(Ax) = \sum\limits_{i} {(Ax)_i ^2} \geq 0\)</li>
</ol>

<h4 id="vanishing-gradients">Vanishing Gradients</h4>

<p>Review equations (11) and (13). They have led us to this recursive multiplication of $W_{rec}$. Attempting to visualise how the vectors shrink and expand due to this product is difficult.</p>

<p>Instead we will focus on the “upper bound” of what this product produces: <em>the (absolute) maximum stretching factor</em>. The sign of the stretch does not matter. What matters is the magnitude of the stretch.</p>

<p>Consulting the Linear Algebra Precursor, we will go ahead and prove a <em>sufficient condition</em> for vanishing gradients:</p>

<p>Let us define an upper bound for $diag(\sigma’(x_{j-1}))$. Assume:</p>

\[\|diag(\sigma'(x_{j-1}))\|_2 &lt; \gamma,\quad \text{ for some }\gamma&gt;0\]

<p><strong>If \(\boxed{\sigma_{max}(W_{rec}) &lt; \frac{1}{\gamma}}\), then long term components vanish (as $t \rightarrow \infty$)</strong>:</p>

\[\begin{align}
  \|\frac{\partial x_{j}}{\partial x_{j-1}}\|_2 &amp;= \|W_{rec}diag(\sigma'(x_{j-1}))\|_2 &lt; \|W_{rec}\|_2 \|diag(\sigma'(x_{j-1}))\|_2\\
  \Rightarrow &amp;\|W_{rec}\|_2 \|diag(\sigma'(x_{j-1}))\|_2 = \sigma_{max}(W_{rec}) * \sigma_{max}(diag(\sigma'(x_{j-1}))) &lt; \frac{1}{\gamma} \gamma = 1\\
  \Rightarrow &amp;\boxed{\|\frac{\partial x_{j}}{\partial x_{j-1}}\|_2 &lt; 1}\tag{14}
\end{align}\]

<p>Let us define an upper bound $\eta$:</p>

\[\|\frac{\partial x_{j}}{\partial x_{j-1}}\|_2 \leq \eta &lt; 1\]

<p>Working our way upwards, let’s revisit eq. (11) and (13):</p>

\[\begin{align}
  \Rightarrow \|\frac{\partial\mathcal{E_t}}{\mathcal{\partial\theta}}\|_2
  &amp;= \|\sum_{k=1}^t [\frac{\partial \mathcal{E}_t}{\partial x_t} \frac{\partial x_t}{\partial x_{k}}\frac{\partial x_k}{\partial g}]\|_2\\
  &amp;=\|\sum_{k=1}^t [\frac{\partial \mathcal{E}_t}{\partial x_t} [\prod_{j=k+1}^t \frac{\partial x_{j}}{\partial x_{j-1}}] \frac{\partial x_k}{\partial g}]\|_2\\
  &amp;\leq \sum_{k=1}^t [\|\frac{\partial \mathcal{E}_t}{\partial x_t} \frac{\partial x_k}{\partial g}\|_2[\prod_{j=k+1}^t \|\frac{\partial x_{j}}{\partial x_{j-1}}\|_2]]\\
  &amp;\leq \sum_{k=1}^t [\|\frac{\partial \mathcal{E}_t}{\partial x_t} \frac{\partial x_k}{\partial g}\|_2 * \eta^{t-k}]\\
\end{align}\tag{15}\]

<p>Since $\eta &lt; 1$, as $t-k$ increases,  the terms tend to go to 0. This means that events further in the past stop contributing to the future.</p>

<p>Thus, under the requirement that $\sigma_{max}(W_{rec}) &lt; \frac{1}{\gamma}$, the model becomes unable to consider events in the past and bases its predictions on recent events. The vanishing gradients make the model “forget” past events easily (ie. past events do not contribute to future outcomes).</p>

<h4 id="exploding-gradients">Exploding Gradients</h4>

<p>Just as gradients can vanish due to the repeated multiplication of $W_{rec}$, they can similary “explode”. We will skip the full proof as it is almost identical to the vanishing gradients one we showed above.</p>

<p>However, notice how we used the following inequality to formulate our proof above:</p>

\[\begin{align}
  \|W_{rec}diag(\sigma'(x_{j-1}))\|_2 &lt; \|W_{rec}\|_2 \|diag(\sigma'(x_{j-1}))\|_2\\
\end{align}\]

<p>The direction of the inequality prohibits us for using it to prove the inverse of the vanishing gradients. Thus, we cannot form a sufficient condition but only a necessary one:</p>

<p><strong>For the exploding gradients problem to appear we need: \(\boxed{\sigma_{max}(W_{rec}) &gt; \frac{1}{\gamma}}\), with \(\|diag(\sigma'(x_{j-1}))\|_2 &gt; \gamma\). Long term components will then be able to explode (as $t \rightarrow \infty$)</strong>.</p>

<h3 id="notes">Notes</h3>

<p>This article was heavily based on the paper by <a href="https://doi.org/10.48550/arxiv.1211.5063">Pascanu et al.</a> which explores the problem of vanishing gradients at great technical detail.</p>

<p>I tried keeping the notation on par with their paper as well as other sources to maintain consistency.</p>

<p>On a minor note, the paper on equation (5) has a minor algebraic error on $W^T_{rec}diag(\sigma’(x_{i-1}))$. Tracing the equation that leads to this result (as we showed in this article) actually leads to $W_{rec}diag(\sigma’(x_{i-1}))$. Even if we decide to not use numerator layout notation, the outcome is still different. However, this does not cause any fundamental error in the findings of their paper.</p>

<h3 id="references">References</h3>
<ul>
  <li>Pascanu, R., Mikolov, T., &amp; Bengio, Y. (2012). On the difficulty of training Recurrent Neural Networks. arXiv. <a href="https://doi.org/10.48550/arxiv.1211.5063">https://doi.org/10.48550/arxiv.1211.5063</a></li>
  <li>“Matrix Norm.” Wikipedia, Wikimedia Foundation, 25 Apr. 2026, <a href="https://en.wikipedia.org/wiki/Matrix_norm">Matrix_norm</a></li>
  <li>“Vanishing Gradient Problem.” Wikipedia, Wikimedia Foundation, 30 Sept. 2025, <a href="https://en.wikipedia.org/wiki/Vanishing_gradient_problem">Vanishing_gradient_problem</a>.</li>
</ul>]]></content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html"><![CDATA[While studying neural networks you might often hear of the vanishing/exploding gradients problem as a usual issue when trying to train deep multi-layered models. “The model’s gradient gets exponentially large or small the more layers you add”.]]></summary></entry></feed>