\begin{frame}
	\myheading{Module 20.5 : Training RBMs using Contrastive Divergence }
\end{frame}

\begin{frame}
	\begin{itemize}
		\item<1-> In practice, Gibbs Sampling can be very inefficient because for every step of stochastic gradient descent we need to run the Markov chain for many many steps and then compute the expectation using the samples drawn from this chain
		\item<2-> We will now see a more efficient algorithm called k-contrastive divergence which is used in practice for training RBMs
	\end{itemize}
\end{frame}

\begin{frame}
	\begin{columns}
		\begin{column}{0.5\textwidth}
			\begin{align*}
				%\mathbb{E}_{p(H|V)}[v_j h_i] &= \bm{h}_{i}v_{j}\\
				\mathbb{E}_{p(H|V)}[v_j h_i] &= \sigma(\bm{w}_{i}\bm{v} + c_{i})v_{j}\\
				\mathbb{E}_{p(V,H)} [v_j h_i] &= \sum_{\bm{v}}p(\bm{v})\sigma(\bm{w}_{i}\bm{v} + c_{i})v_{j}
			\end{align*}
		\end{column}
		\begin{column}{0.5\textwidth}
			\small{\begin{itemize}
							\item<1->Just to reiterate, our goal is to compute the two expectations efficiently
							\item<2-> We already have a simplified formula for the first expectation
							\item<3-> Furthermore, note that the first expectation depends only on the seen training example ($\bm{v}$)
							\item<4-> The second expectation depends on the samples drawn from the Markov chain $(v_1, v_2, ..., v_n)$
							\item<5-> The first expectation thus depends on the empirical samples, whereas the second expectation depends on the model samples (because the samples are generated based on $P(V|H)$ and $P(H|V)$ output by the model)
						\end{itemize}}
		\end{column}
	\end{columns}
\end{frame}

\begin{frame}
    \resizebox{400pt}{120pt}{
	    \input{modules/Module2/tikz_images/cd}
	}
	\begin{itemize}
		\item<1-> Contrastive divergence uses the following idea
		\item<2-> Instead of starting the Markov Chain at a random point $(V = \bm{v}^0)$, start from $\bm{v}^{(t)}$ where $\bm{v}^{(t)}$ is the current training instance
		\item<3-> Run Gibbs Sampling for $k$ steps and denote the sample at the $k^{th}$ step by $\tilde{\bm{v}}$
		\item<4-> Replace the expectation by a point estimate
	\end{itemize}
	\begin{align*}
	    \onslide<9->{\mathbb{E}_{p(V,H)} [v_j h_i] \onslide<10->{= \sum_{\bm{v}}p(\bm{v})\sigma(\bm{w}_{i}\bm{v} + c_{i})v_{j}}}
	    \onslide<11->{\approx \sigma(\bm{w}_{i}\bm{\tilde{v}} + c_{i})\tilde{v}_{j}}
	\end{align*}
\end{frame}

\begin{frame}
	\begin{itemize}
		\item<1-> Over time as our model becomes better and better $\tilde{\bm{v}}$ should start looking more and more like our training (empirical) samples
		\item<2-> Once that starts happening what will happen to the gradient ?
		\item<3-> We consider the derivative w.r.t $w_{ij}$ again
		    \begin{align*}
		        \onslide<4->{\frac{\partial \mathscr{L}(\theta)}{\partial {w}_{ij}} &= \sigma(\bm{w}_{i}\bm{v} + c_{i})v_j - \sum_{\bm{v}}p(\bm{v})\sigma(\sum_{j=1}^{m}\bm{w}_{i}\bm{v} + c_{i})v_{j}}
		    \end{align*}
		\item<5-> We have two summations here
		\item<6-> The first term can be thought of as summation over a single point $v$ from training example
		\item<7-> Similarly, for the second term, the summation over $\bm{\tilde{v}}$ is being replaced by a point estimate computed from the model sample
		\item<8-> As training progresses and $\tilde{\bm{v}}$ (model sample) starts looking more and more like our training (empirical) samples, the difference between the two terms will be small and the parameters of the model will stabilize (convergence)

	\end{itemize}
\end{frame}

% \begin{frame}
% 	\begin{figure}
% 		\includegraphics[scale=0.35]{images/cd.jpg}
% 		\caption{Contrastive divergence}
% 	\end{figure}
% \end{frame}

\begin{frame}
	\small{
		\onslide<1->{\begin{algorithm}[H]
						\onslide<2->{\textbf{Input:}} \onslide<3->{RBM $(V_1, ... , V_m, H_1 ,..., H_n)$, training batch $D$}\\
						\onslide<4->{\textbf{Output:}} \onslide<5->{Learned Parameters $\mathbf{W}, \bm{b}, \bm{c}$} \\
						\onslide<6->{init $\mathbf{W} = \bm{b} = \bm{c} = 0$} \\
						\onslide<7->{\ForAll{$\bm{v} \in D$}{
										\onslide<8->{Initialize $\bm{v}^{(0)} \leftarrow \bm{v}$ } \\
										\onslide<9->{\For{$t=0,...,k$}{
																		\onslide<10->{\For{$i=1,...,n$} {\onslide<11->{sample $h_{i}^{(t)} \sim\ p(h_{i}|\bm{v}^{(t)})$}}}
																		\onslide<12->{\For{$j=1,...,m$} {\onslide<13->{sample $v_{j}^{(t+1)} \sim\ p(v_{j}|\bm{h}^{(t)})$}} }
																	} }
										\onslide<14->{$\mathbf{W} \leftarrow \mathbf{W} + \eta\only<14>{\nabla_{\mathbf{W}}\mathscr{L}(\theta)}\onslide<15->{[\sigma(\mathbf{W}\bm{v}_{d} + \bm{c})\bm{v}_{d}^{T} - \sigma(\mathbf{W}\bm{\tilde{v}} + \bm{c})\bm{\tilde{v}}]}$} \\
										%\onslide<14->{$\nabla_{\mathbf{W}}\mathscr{L}(\theta) \leftarrow \onslide<15->{\nabla_{\mathbf{W}}\mathscr{L}(\theta) + \sigma(\mathbf{W}\bm{v}_{d} + \bm{c})\bm{v}_{d}^{T} - \frac{1}{r}\sum_{t=k+1}^{k+r}\sigma(\mathbf{W}\bm{v}^{(t)} + \bm{c})\bm{v}^{(t)T}$}} \\
										\onslide<16->{$\bm{b} \leftarrow \bm{b} + \eta \only<16>{\nabla_{\bm{b}}\mathscr{L}(\theta)} \onslide<17->{[\bm{v} - \bm{\tilde{v}}]}$} \\
										%\onslide<16->{$\nabla_{\bm{b}}\mathscr{L}(\theta) \leftarrow \onslide<17->{\nabla_{\bm{b}}\mathscr{L}(\theta) + \bm{v}_{d} - \frac{1}{r}\sum_{t=k+1}^{k+r}\bm{v}^{(t)}}$} \\
										\onslide<18->{$\bm{c} \leftarrow \bm{c} + \eta \only<18>{\nabla_{\bm{c}}\mathscr{L}(\theta)}\onslide<19->{[\sigma(\mathbf{W}\bm{v} + \bm{c}) - \sigma(\mathbf{W}\bm{\tilde{v}} + \bm{c})]}$}
										%\onslide<18->{$\nabla_{\bm{c}}\mathscr{L}(\theta) \leftarrow \onslide<19->{\nabla_{\bm{c}}\mathscr{L}(\theta) + \sigma(\mathbf{W}\bm{v}_{d} + \bm{c}) - \frac{1}{r}\sum_{t=k+1}^{k+r}\sigma(\mathbf{W}\bm{v}^{(t)} + \bm{c})}$}
									}}
						\caption*{k-step Contrastive Divergence}
					\end{algorithm}}
		}
\end{frame}

\begin{frame}
	\input{modules/Module2/tikz_images/cd1}
	\begin{itemize}
		\item<1->In practice, $k=1$ also works well
		\item<2-> The higher the value of $k$, the less biased the estimate of the gradient will be.
	\end{itemize}
\end{frame}
