Algorithm in Beamer

\documentclass{beamer}
\mode<presentation>
{
\usetheme{Warsaw}
\usecolortheme{beaver}
}
\usepackage{algpseudocode}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{alphabeta}
\usepackage[table,xcdraw]{xcolor}
\usepackage{booktabs}
\begin{document}

\begin{frame}
\begin{algorithm}[H]
\begin{algorithmic}[1]
\State\textbf{Input: }full information of the state ($s_t$), priority vector for each task request based on the formula  (3.19), (3.20), (3.21), (3.22), (3.23), discount factor $\gamma$, learning rate $\alpha$
\State\textbf{Initialize} two neural networks as Q-networks $Q_\pi$ and $Q_{\pi}^{-}$ with random weight (and bias) $\theta$ and $\theta^-$, replay memory M
\For {episode = 1 to $e$}
\State initialize the first state $s_0$
\For{ $t=1$ to $T$ }
\State Gather the state $s_t$ from the environment
\If{ random value $\textless$  $\epsilon$ }
\State select a random action $a_t$
\Else
\State select $a_t = argmax Q(s_t,a\vert \theta)$
\EndIf
\State Execute action $a_t$, received reward $r_t$ and next state $s_{t+1}$
\State Store ($s_t, a_t, r_t, s_{t+1}$ into replay memory
\For{ $i=1$ to $b$ }
\If{ $s_{t+1}$ = terminal }
\State $y_i = r_i$
\Else
\State $y_i = r_i + \gamma \times max_a Q^-(s_{t+1}, a\vert \theta)$
\State $q_i = Q_\pi (s_i, a_i\vert \theta)$
\EndIf
\EndFor
\State $\theta = \theta - \alpha\Delta_\theta \sum\limits_{i=1}^{b} \frac{(q_i – y_i)^2}{b}$ \Comment{Gradient descent}
\State Every K episode, copy $\theta$ to $\theta^-$  $\backslash\backslash$ \Comment{Update target network}
\EndFor
\EndFor
\end{algorithmic}
\end{algorithm}
\end{frame}

\end{document}


But it returns error

How can I fix it?

Load some package which defines the algorithm environment:

\documentclass{beamer}
\mode<presentation>
{
\usetheme{Warsaw}
\usecolortheme{beaver}
}
\usepackage{algorithm}
\usepackage{algpseudocode}
%\usepackage{amsmath}
%\usepackage{amssymb}
\usepackage{alphabeta}
%\usepackage[table,xcdraw]{xcolor}
\usepackage{booktabs}

\begin{document}

\begin{frame}
\begin{algorithm}[H]
\tiny
\begin{algorithmic}[1]
\State\textbf{Input: }full information of the state ($s_t$), priority vector for each task request based on the formula  (3.19), (3.20), (3.21), (3.22), (3.23), discount factor $\gamma$, learning rate $\alpha$
\State\textbf{Initialize} two neural networks as Q-networks $Q_\pi$ and $Q_{\pi}^{-}$ with random weight (and bias) $\theta$ and $\theta^-$, replay memory M
\For {episode = 1 to $e$}
\State initialize the first state $s_0$
\For{ $t=1$ to $T$ }
\State Gather the state $s_t$ from the environment
\If{ random value $\textless$  $\epsilon$ }
\State select a random action $a_t$
\Else
\State select $a_t = argmax Q(s_t,a\vert \theta)$
\EndIf
\State Execute action $a_t$, received reward $r_t$ and next state $s_{t+1}$
\State Store ($s_t, a_t, r_t, s_{t+1}$ into replay memory
\For{ $i=1$ to $b$ }
\If{ $s_{t+1}$ = terminal }
\State $y_i = r_i$
\Else
\State $y_i = r_i + \gamma \times max_a Q^-(s_{t+1}, a\vert \theta)$
\State $q_i = Q_\pi (s_i, a_i\vert \theta)$
\EndIf
\EndFor
\State $\theta = \theta - \alpha\Delta_\theta \sum\limits_{i=1}^{b} \frac{(q_i – y_i)^2}{b}$ \Comment{Gradient descent}
\State Every K episode, copy $\theta$ to $\theta^-$  $\backslash\backslash$ \Comment{Update target network}
\EndFor
\EndFor
\end{algorithmic}
\end{algorithm}
\end{frame}

\end{document}


Or drop the environment. As beamer does not have a floating mechanism, there aren't a lot of advantages in having it:

\documentclass{beamer}
\mode<presentation>
{
\usetheme{Warsaw}
\usecolortheme{beaver}
}
\usepackage{algorithm}
\usepackage{algpseudocode}
%\usepackage{amsmath}
%\usepackage{amssymb}
\usepackage{alphabeta}
%\usepackage[table,xcdraw]{xcolor}
\usepackage{booktabs}
\usepackage{caption}

\begin{document}

\begin{frame}[allowframebreaks]
\begin{algorithmic}[1]
\State\textbf{Input: }full information of the state ($s_t$), priority vector for each task request based on the formula  (3.19), (3.20), (3.21), (3.22), (3.23), discount factor $\gamma$, learning rate $\alpha$
\State\textbf{Initialize} two neural networks as Q-networks $Q_\pi$ and $Q_{\pi}^{-}$ with random weight (and bias) $\theta$ and $\theta^-$, replay memory M
\For {episode = 1 to $e$}
\State initialize the first state $s_0$
\For{ $t=1$ to $T$ }
\State Gather the state $s_t$ from the environment
\If{ random value $\textless$  $\epsilon$ }
\State select a random action $a_t$
\Else
\State select $a_t = argmax Q(s_t,a\vert \theta)$
\EndIf
\State Execute action $a_t$, received reward $r_t$ and next state $s_{t+1}$
\State Store ($s_t, a_t, r_t, s_{t+1}$ into replay memory
\For{ $i=1$ to $b$ }
\If{ $s_{t+1}$ = terminal }
\State $y_i = r_i$
\Else
\State $y_i = r_i + \gamma \times max_a Q^-(s_{t+1}, a\vert \theta)$
\State $q_i = Q_\pi (s_i, a_i\vert \theta)$
\EndIf
\EndFor
\State $\theta = \theta - \alpha\Delta_\theta \sum\limits_{i=1}^{b} \frac{(q_i – y_i)^2}{b}$ \Comment{Gradient descent}
\State Every K episode, copy $\theta$ to $\theta^-$  $\backslash\backslash$ \Comment{Update target network}
\EndFor
\EndFor
\end{algorithmic}
\end{frame}

\end{document}


• The Coolest answer out there. Thanks bro.😍😍 Commented Jun 25 at 13:00
• @MacTavish You're welcome! Commented Jun 25 at 13:05
• It'll be easier for the audience if you split these two slides into separate algorithms that fit in a slide each.
– lhf
Commented Jun 25 at 22:10