\savestack{\nnbias}{\input{modules/Module7/tikz_images/nn_bias.tex}}
\savestack{\nnweight}{\input{modules/Module7/tikz_images/nn_weight.tex}}

\begin{frame}
  \myheading{Module 4.7: Backpropagation: Computing Gradients w.r.t. Parameters}
\end{frame}

%Slide 36
\begin{frame}
  \begin{overlayarea}{\textwidth}{\textheight}
    \textbf{Quantities of interest (roadmap for the remaining part):}
    \begin{itemize}
      \justifying
      \item Gradient w.r.t. output units
      \item Gradient w.r.t. hidden units
      \item \alert<1->{Gradient w.r.t. weights and biases}
    \end{itemize}

    \begin{align*}
      \underbrace{\frac{\partial \mathscr{L}(\theta)}{\partial W_{111}}}_
      {\substack{\text{Talk to the}\\ \text{weight directly}}}
      =
      {\underbrace{\frac{\partial \mathscr{L}(\theta)}{\partial \hat{y}} \frac{\partial \hat{y}}{\partial a_{3}}}_
        {\substack{\text{Talk to the} \\ \text{output layer}}}} 
      {\underbrace{\frac{\partial a_{3}}{\partial h_{2}} \frac{\partial h_{2}}{\partial a_{2}}}_
        {\substack{\text{Talk to the} \\ \text{previous hidden} \\ \text{layer}}}
        \underbrace{\frac{\partial a_{2}}{\partial h_{1}} \frac{\partial h_{1}}{\partial a_{1}}}_
        {\substack{\text{Talk to the} \\ \text{previous} \\ \text{hidden layer}}}} 
      \alert<1->{\underbrace{\frac{\partial a_{1}}{\partial W_{111}}}_
        {\substack{\text{and now} \\ \text{talk to} \\ \text{the} \\ \text{weights}}}}
    \end{align*}

    \begin{itemize}
      \justifying
      \item<1-> Our focus is on \textit{Cross entropy loss} and \textit{Softmax} output.
    \end{itemize}
  \end{overlayarea}
\end{frame}

%Slide 37
\begin{frame}
  \begin{columns}
    \column{0.6\textwidth}
    \begin{overlayarea}{\textwidth}{\textheight}
      \justifying
      Recall that,
      \begin{align*}
        \mathbf{a_k}                                                                    & =  \mathbf{b_k}  + W_k \mathbf{h_{k-1}} \\
        % \visible<2->{a_{ki}                                                             & =  b_{ki} + W_{kij} h_{k-1,j} \\}
        \visible<2->{\frac{\partial a_{ki}}{\partial W_{kij}}                           & = h_{k-1,j} \\}
        \visible<3->{\frac{\partial\mathscr{L(\theta)}}{\partial W_{kij}} \visible<4->{ & = \frac{\partial\mathscr{L(\theta)}}{\partial a_{ki}} \frac{\partial a_{ki}}{\partial W_{kij}}}\\}
        \visible<5->{                                                                   & =\frac{\partial\mathscr{L(\theta)}}{\partial a_{ki}} h_{k-1,j}\\}
        \visible<6->{\nabla_{W_k} \mathscr{L(\theta)}                                   & =} \visible<7->{\begin{bmatrix}
            \frac{\partial\mathscr{L(\theta)}}{\partial W_{k11}} &
            \frac{\partial\mathscr{L(\theta)}}{\partial W_{k12}}
            & \dots &\dots &   \frac{\partial\mathscr{L(\theta)}}{\partial W_{k1n}} \\
            \dots  & \dots  & \dots  & \dots  & \dots                                                      \\
            \vdots & \vdots & \vdots & \vdots & \vdots                                                     \\
            \dots  & \dots  & \dots  & \dots  & \frac{\partial\mathscr{L(\theta)}}{\partial W_{knn}}
          \end{bmatrix}}
      \end{align*}
    \end{overlayarea}

    \column{0.4\textwidth}
    \begin{overlayarea}{\textwidth}{\textheight}
      \makebox[\textwidth][c]{\usebox{\nnweightcontent}}
    \end{overlayarea}
    % \include{nn_weight}
  \end{columns}
\end{frame}

\begin{frame}[b]
    \visible<1>{\tiny Intentionally left blank}
\end{frame}

%Slide 38
\begin{frame}
  \begin{overlayarea}{\textwidth}{\textheight}
    \visible<1->{Lets take a simple example of a $W_k \in \mathbb{R}^{3 \times 3}$ and see what each entry looks like}
    \\~\\
    \visible<2->{
      $\nabla_{W_k}\mathscr{L}(\theta)= \begin{bmatrix}
          \frac{\partial \mathscr{L}(\theta)}{\partial W_{k11}} & \frac{\partial \mathscr{L}(\theta)}{\partial W_{k12}} & \frac{\partial \mathscr{L}(\theta)}{\partial W_{k13}} \\
          & & \\
          \frac{\partial \mathscr{L}(\theta)}{\partial W_{k21}} & \frac{\partial \mathscr{L}(\theta)}{\partial W_{k22}} & \frac{\partial \mathscr{L}(\theta)}{\partial W_{k23}} \\
          & & \\
          \frac{\partial \mathscr{L}(\theta)}{\partial W_{k31}} & \frac{\partial \mathscr{L}(\theta)}{\partial W_{k32}} & \frac{\partial \mathscr{L}(\theta)}{\partial W_{k33}} \\
        \end{bmatrix} \frac{\partial\mathscr{L(\theta)}}{\partial W_{kij}} \visible<2->{= \frac{\partial\mathscr{L(\theta)}}{\partial a_{ki}} \frac{\partial a_{ki}}{\partial W_{kij}}}$
    }
    \vspace{1cm}
    \visible<3->{
      $ \nabla_{W_k}\mathscr{L}(\theta) = \begin{bmatrix}
          \color<7->{brown} {\frac{\partial \mathscr{L}(\theta)} {\partial a_{k1}}} \color{black} \color<4->{blue}{h_{k-1,1}} \color{black}  & \color<7->{brown} {\frac{\partial \mathscr{L}(\theta)}{\partial a_{k1}}}  \color{black} \color<5->{red}{h_{k-1,2}} \color{black}  & \color<7->{brown} {\frac{\partial \mathscr{L}(\theta)}{\partial a_{k1}}} \color{black} \color<6->{orange}{h_{k-1,3}} \color{black}  \\
          & & \\
          \color<7->{magenta} {\frac{\partial \mathscr{L}(\theta)}{\partial a_{k2}}} \color{black} \color<4->{blue}{h_{k-1,1}} \color{black} & \color<7->{magenta} {\frac{\partial \mathscr{L}(\theta)}{\partial a_{k2}}} \color{black} \color<5->{red}{h_{k-1,2}} \color{black} & \color<7->{magenta} {\frac{\partial \mathscr{L}(\theta)}{\partial a_{k2}}} \color{black}\color<6->{orange}{h_{k-1,3}} \color{black} \\
           & & \\
          \frac{\partial \mathscr{L}(\theta)}{\partial a_{k3}} \color{black} \color<4->{blue}{h_{k-1,1}}  \color{black}                      & \frac{\partial \mathscr{L}(\theta)}{\partial a_{k3}} \color{black} \color<5->{red}{h_{k-1,2}} \color{black}                       & \frac{\partial \mathscr{L}(\theta)}{\partial a_{k3}} \color{black} \color<6->{orange}{h_{k-1,3}} \color{black}                      \\
        \end{bmatrix} = \visible<8->{\nabla_{a_k} \mathscr{L}(\theta) \cdot \mathbf{h_{k-1}}  ^T}$
    }

  \end{overlayarea}
\end{frame}

%Slide 39
\begin{frame}
  \begin{columns}
    \column{0.6\textwidth}
    \begin{overlayarea}{\textwidth}{\textheight}
      Finally, coming to the biases
        \begin{align*}
          \visible<2->{a_{ki}                                                & =  b_{ki} + \sum_j W_{kij} h_{k-1,j} \\}
          \visible<3->{\frac{\partial \mathscr{L}(\theta)} {\partial b_{ki}} & = \frac{\partial \mathscr{L}(\theta)}{\partial a_{ki}} \frac{\partial a_{ki}} {\partial b_{ki}} \\}
          \visible<4->{                                                      & = \frac{\partial \mathscr{L}(\theta)}{\partial {a_{ki}}}}
        \end{align*}
          \visible<5->{We can now write the gradient w.r.t. the vector $b_k$}
        \begin{align*}
          \visible<6->{\nabla_\mathbf{b_k}\mathscr{L}(\theta)                       & =
            \begin{bmatrix}
              \frac{\partial \mathscr{L}(\theta)}{a_{k1}} \\
              \frac{\partial \mathscr{L}(\theta)}{a_{k2}} \\
              \vdots                                       \\
              \frac{\partial \mathscr{L}(\theta)}{a_{kn}}
            \end{bmatrix}}
          \visible<7->{= \nabla_{{\mathbf{a_k}}}\mathscr{L}(\theta)}
        \end{align*}
    \end{overlayarea}

    \column{0.4\textwidth}
    \begin{overlayarea}{\textwidth}{\textheight}
      \makebox[\textwidth][c]{\usebox{\nnbiascontent}}
    \end{overlayarea}
    % \include{nn_bias}
  \end{columns}
\end{frame}