Realize a LaTeX Pseudo-code Block Using LaTeX algorithm and algpseudocode Package

Jun. 02, 2024

Here is a LaTeX pseudo-code block example by LaTeX algorithm and algpseudocode package from the paper, Wasserstein Generative Adversarial Networks12:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
\documentclass[a4paper,11pt]{article}
%\documentclass[a4paper,twocolumn]{article}
\usepackage{amsmath,amsfonts}
\usepackage{algorithm,algpseudocode}

\newcommand\PP{\mathbb{P}}

\begin{document}
\begin{algorithm}[t!]
\caption{WGAN, our proposed algorithm. All experiments in the paper
used the default values $\alpha = 0.00005$, $c = 0.01$, $m=64$, $n_{\text{critic}}=5$.}\label{algo::wgan}
	\begin{algorithmic}[1]
	 \Require: $\alpha$, the learning rate. $c$, the clipping parameter. $m$, the batch size. $n_{\text{critic}}$, the number of iterations of the critic per generator iteration.
	 \Require: $w_0$, initial critic parameters. $\theta_0$, initial generator's parameters.
	 \While{$\theta$ has not converged}
	   \For{$t = 0, ..., n_{\text{critic}}$}
	     \State Sample $\{x^{(i)}\}_{i=1}^m \sim \PP_r$ a batch from the real data.
	     \State Sample $\{z^{(i)}\}_{i=1}^m \sim p(z)$ a batch of prior samples.
	     \State $g_w \gets \nabla_w \left[\frac{1}{m}\sum_{i=1}^m f_w(x^{(i)}) - \frac{1}{m} \sum_{i=1}^m f_w(g_\theta(z^{(i)})) \right]$
	     \State $w \gets w + \alpha \cdot \text{RMSProp}(w, g_w) $
	     \State $w \gets \text{clip}(w, -c, c) $
	   \EndFor
	   \State Sample $\{z^{(i)}\}_{i=1}^m \sim p(z)$ a batch of prior samples.
	   \State $g_\theta \gets -\nabla_\theta \frac{1}{m} \sum_{i=1}^m f_w(g_\theta(z^{(i)}))$ 
	   \State $\theta \gets \theta - \alpha \cdot \text{RMSProp}(\theta, g_\theta)$
	 \EndWhile
	\end{algorithmic}
\end{algorithm}
\end{document}

image-20250109154531810

where the option [1] of algorithmic environment is to display the line numbers3. If we delete it, so we have:

1
2
3
4
5
% ...
	\begin{algorithmic}
		% ...
	\end{algorithmic}
% ...

image-20250109155117500

and if we use other certain number, like [7] for example, instead, we have:

1
2
3
4
5
% ...
	\begin{algorithmic}[7]
		% ...
	\end{algorithmic}
% ...

image-20250109155311059


References

  1. Wasserstein Generative Adversarial Networks˄

  2. Arjovsky, Martin, Soumith Chintala, and Léon Bottou. “Wasserstein generative adversarial networks.” International conference on machine learning. PMLR, 2017, https://arxiv.org/abs/1701.07875˄

  3. Algorithms˄