# Generative adversarial network (GAN) diagram in TikZ, explained

> How the generative adversarial network (GAN) diagram is drawn in TikZ: positioning, two labels per arrow, a switch made of edges, and two variations.

URL: https://www.labcanvas.com/tikz/examples/machine-learning/generative-adversarial-network

Updated 2026-11-22. Original figure: Generative adversarial network, © Petar Veličković, MIT licence (licence text: https://www.labcanvas.com/tikz-library/credits.html#petarv), from https://github.com/PetarV-/TikZ/blob/86aee049f5355d77c598b936399e42caf8941e60/Generative%20adversarial%20network/generative_adversarial_network.tex. Walkthrough and variations by LabCanvas; the variation code is CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/). Pictures typeset with TeX Live 2026 and PGF 3.1.12. Category: [TikZ neural network and machine learning examples](https://www.labcanvas.com/tikz/examples/machine-learning).

## Quick answer

This figure shows the idea of a GAN: noise z drawn from p θ (z) goes through the generator G to a fake sample, a real sample comes from p data (x), and a switch feeds one or the other to the discriminator D, which answers “real?”. It is about twenty lines of plain TikZ: circle nodes placed with the `positioning` library, arrows with one label above and one below, and a switch built from two dots, an open circle and a dashed curved `edge`.

## The figure and its code

![GAN diagram: on the bottom row z with an incoming arrow labelled p theta of z, an arrow labelled G of z and generator to the circle x fake; above it x real with an incoming arrow labelled p data of x; both feed small dots of a switch drawn with a dashed curve and an open circle, which leads to a circle x and an arrow labelled D of x and discriminator to the text real?](https://www.labcanvas.com/tikz-snippets/fig-generative-adversarial-network/original.webp)

Copyright (c) 2016 Petar Veličković, MIT licence (https://www.labcanvas.com/tikz-library/credits.html#petarv). The code below is exactly the library's file (pdfLaTeX); its first lines name the source and the licence, and the MIT licence asks that they stay with the code. Open it in the TikZ editor from the page: https://www.labcanvas.com/tikz/examples/machine-learning/generative-adversarial-network

```latex
% Generative adversarial network
% Source: https://github.com/PetarV-/TikZ/blob/86aee049f5355d77c598b936399e42caf8941e60/Generative%20adversarial%20network/generative_adversarial_network.tex
% Copyright (c) 2016 Petar Veličković. MIT licence: https://github.com/PetarV-/TikZ/blob/master/LICENSE
% The licence’s copyright and permission notice: https://www.labcanvas.com/tikz-library/credits.html
\documentclass[crop,tikz]{standalone}
\usepackage{tikz}

\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}

	\node[circle, draw, thick] (z) {$\vec{z}$};
	\node[circle, draw, thick, right=5em of z] (x) {$\vec{x}_{fake}$};
	\draw[-stealth, thick] (z) -- node[above] {$G(\vec{z})$} node[below] {generator} (x);
	\node[left=of z] (i) {};
	\draw[-stealth, thick] (i) -- node[above] {$p_\theta(\vec{z})$} (z);
	\node[above=of x, circle, draw, thick] (xt) {$\vec{x}_{real}$};
	\node[left=5em of xt] (it) {};
	\draw[-stealth, thick] (it) -- node[above] {$p_{data}(\vec{x})$} (xt);
	\node[circle, draw, thick, right=5em of x, yshift=2.5em] (D) {$\vec{x}$};
	\node[right=7em of D] (out) {real?};
	\draw[-stealth, thick] (D) -- node[above] {$D(\vec{x})$} node[below] {discriminator} (out);

	\node[right=2.5em of x, circle, fill, inner sep=0.15em] (pt1) {};
	\node[right=2.5em of xt, circle, fill, inner sep=0.15em] (pt2) {};

	\draw[dashed, thick] (pt1) edge[bend left] (pt2);

	\node[circle, draw, thick, fill=white, inner sep=0.15em] at ([xshift=-0.9em, yshift=4em]pt1.north) (pt3) {};

	\draw[-stealth, thick] (x) -- (pt1);
	\draw[-stealth, thick] (xt) -- (pt2);
	\draw[-stealth, thick] (pt3) -- (D);

\end{tikzpicture}
\end{document}
```

## A small preamble

*Lines 5–8 of the code.*

`\documentclass[crop,tikz]{standalone}` crops the page to the picture and loads TikZ (the `\usepackage{tikz}` on the next line is then redundant but harmless). The only library is `positioning`; the `-stealth` arrow tips used throughout are one of the old tips that need no library.

## The generator row

*Lines 13–17 of the code.*

z is a circle at the origin and the fake sample is placed `right=5em of z`, 5em between the two borders. `\draw[-stealth, thick] (z) -- node[above] {$G(\vec{z})$} node[below] {generator} (x);` puts two nodes on the same line: a node written inside a path operation sits at its middle, and `above` and `below` move each one off the line on its own side, so the formula and the name share the arrow.

The arrow coming in from the left starts at an empty node, `\node[left=of z] (i) {};`, one `node distance` (1 cm) to the left of z. An empty node still has its inner padding, so the line starts a few points from its centre; `\coordinate[left=of z] (i);` would mark the point exactly.

## The real data row and the discriminator

*Lines 18–23 of the code.*

The real sample goes `above=of x`, 1 cm above the fake one, with its own incoming arrow from an empty node `left=5em of xt`. The discriminator’s input is placed in two steps, `right=5em of x, yshift=2.5em`: positioning puts it 5em right of the fake sample, and the extra `yshift` lifts it towards the middle of the two rows. Its output arrow carries D(x) above and “discriminator” below, like the generator’s, and ends at a text node, `real?`, 7em to the right.

## A switch from dots, an open circle and a curved edge

*Lines 25–34 of the code.*

The switch has two contacts, `pt1` and `pt2`, each a tiny filled circle (`circle, fill, inner sep=0.15em` with no text) 2.5em to the right of a sample. `\draw[dashed, thick] (pt1) edge[bend left] (pt2);` draws the dashed curve between them: `edge` creates a separate path that is drawn after the main one (the main path here is only the point `(pt1)`, so it draws nothing), and `bend left` curves it to the left of its direction of travel, upward from the fake contact to the real one.

The open circle `pt3` is the switch’s common terminal. Its position is the north anchor of `pt1` with options written into the coordinate itself, `([xshift=-0.9em, yshift=4em]pt1.north)`, which shift only that point; `fill=white` hides anything under it. The last three arrows connect the samples to their contacts and the terminal to the discriminator.

## Techniques used

- [Placement with positioning](https://www.labcanvas.com/tikz/nodes): `right=5em of z`, `above=of x` and an extra `yshift` after positioning.
- [Two labels on one arrow](https://www.labcanvas.com/tikz/arrows): `node[above] {…} node[below] {…}` inside the same `--`.
- [Curved edges](https://www.labcanvas.com/tikz/graphs): `edge[bend left]` draws the dashed switch arc as its own path.
- [Shifted coordinates](https://www.labcanvas.com/tikz/coordinates): `([xshift=-0.9em, yshift=4em]pt1.north)` moves one point without a calc expression.
- [Neural network diagrams](https://www.labcanvas.com/tikz/neural-networks): generators, discriminators and how GAN figures are usually laid out.

## Variations

Our own code, CC0 1.0, each typeset with TeX Live 2026 in a standalone document with the preamble lines shown.

### The training loop with both updates

The figure shows the data flow; papers often also show how the two networks are trained. Here G and D are boxes, the loss sits below D, and two dashed red arrows show the updates: D is trained to tell real from fake, G to fool D. The discriminator is placed `right=14mm of $(xf)!0.5!(xr)$`, halfway between the two samples, and the two vertical arrows between D and the loss are offset with `([xshift=-4mm]D.south)` so that they run side by side.

![GAN training diagram: z into a blue Generator G box, out to x tilde; training data into x above it; both into an orange Discriminator D box with the output real or fake?; an arrow down from D to a loss box, and dashed red arrows from the loss up to D, labelled update D, and back to the generator, labelled update G (fool D).](https://www.labcanvas.com/tikz-snippets/fig-generative-adversarial-network/training-loop.webp)

*a GAN training loop: forward arrows in black, the updates of D and G as dashed red arrows*

Preamble: `\usepackage{tikz}`, `\usetikzlibrary{positioning, calc, arrows.meta}`

```latex
\begin{tikzpicture}[
    >={Stealth[length=2mm]}, thick, node distance=8mm and 10mm,
    net/.style={draw, rounded corners=3pt, minimum width=24mm, minimum height=10mm, align=center},
    data/.style={draw, circle, minimum size=9mm, inner sep=1pt},
    grad/.style={->, dashed, red!70!black},
  ]
  \node[data] (z) {$z$};
  \node[net, fill=blue!12, right=of z] (G) {Generator $G$};
  \node[data, right=of G] (xf) {$\tilde{x}$};
  \node[data, above=of xf] (xr) {$x$};
  \node[left=of xr, align=center, font=\small] (pd) {training\\data};
  \node[net, fill=orange!20, right=14mm of $(xf)!0.5!(xr)$] (D) {Discriminator $D$};
  \node[right=of D, align=left, font=\small] (out) {real\\or fake?};
  \node[net, fill=red!10, minimum width=14mm, below=12mm of D] (loss) {loss};
  % forward pass
  \draw[->] (z) -- (G);
  \draw[->] (G) -- (xf);
  \draw[->] (pd) -- (xr);
  \draw[->] (xr) -- (D);
  \draw[->] (xf) -- (D);
  \draw[->] (D) -- (out);
  \draw[->] ([xshift=-4mm]D.south) -- ([xshift=-4mm]loss.north);
  % the two updates, in opposite directions
  \draw[grad] ([xshift=4mm]loss.north) -- node[right, font=\small] {update $D$} ([xshift=4mm]D.south);
  \draw[grad] (loss.west) -| node[pos=0.25, below, font=\small] {update $G$ (fool $D$)} (G.south);
\end{tikzpicture}
```

### The switch as a reusable pic

Drawn with loose nodes, the switch cannot be reused. As a `pic` it becomes one command: `switch/.pic={…}` draws two contacts, a pivot and the lever, and names its points `(-a)`, `(-b)` and `(-p)`, which `\pic (s0) …` turns into `s0-a`, `s0-b`, `s0-p`. The pic’s argument says which contact the lever touches, so the same code shows the real and the fake case.

![Two copies of a small circuit: a circle x real above a circle x fake, each with an arrow to a dot, a lever from an open pivot circle to one of the dots and an arrow from the pivot to an orange box D; on the left the lever touches the real contact, captioned a real sample goes to D, on the right the fake one, captioned a fake sample goes to D.](https://www.labcanvas.com/tikz-snippets/fig-generative-adversarial-network/switch-pic.webp)

*a two-way switch defined once as a pic and used twice, set to the real and to the fake sample*

Preamble: `\usepackage{tikz}`, `\usetikzlibrary{arrows.meta}`

```latex
\begin{tikzpicture}[
    thick, >={Stealth[length=2mm]},
    % a two-way switch: contacts (-a) and (-b), pivot (-p);
    % the argument, 0 or 1, says which contact the lever touches
    switch/.pic={
      \fill (0,0) circle (1.6pt) coordinate (-a);
      \fill (0,1.6) circle (1.6pt) coordinate (-b);
      \coordinate (-p) at (0.9,0.8);
      \draw (-p) -- (0.07,{1.6*#1});
      \draw[fill=white] (-p) circle (1.6pt);
    },
    sample/.style={circle, draw, minimum size=10mm, inner sep=1pt},
  ]
  \foreach \k/\sel/\what in {0/1/real, 1/0/fake} {
    \begin{scope}[xshift={6cm*\k}]
      \node[sample] (r\k) at (0,1.6) {$\vec{x}_{\mathrm{real}}$};
      \node[sample] (f\k) at (0,0) {$\vec{x}_{\mathrm{fake}}$};
      \pic (s\k) at (1.5,0) {switch=\sel};
      \node[draw, rounded corners=2pt, fill=orange!20, minimum size=9mm] (d\k) at (4,0.8) {$D$};
      \draw[->, shorten >=2pt] (r\k) -- (s\k-b);
      \draw[->, shorten >=2pt] (f\k) -- (s\k-a);
      \draw[->, shorten <=2pt] (s\k-p) -- (d\k);
      \node[font=\small] at (2,2.6) {a \what\ sample goes to $D$};
    \end{scope}
  }
\end{tikzpicture}
```

## Frequently asked questions

### How do I draw a GAN in TikZ?

Place the generator, the samples and the discriminator as nodes with the positioning library and connect them with arrows; label an arrow on both sides with node[above] {…} node[below] {…}. The training-loop variation on this page adds the loss and the two updates.

### How do I put labels above and below the same arrow?

Write two nodes inside the line: \draw[->] (a) -- node[above] {$G(z)$} node[below] {generator} (b);. Both sit at the middle of the line, one on each side.

### How do I reuse a small drawing such as a switch several times?

Define it as a pic: \tikzset{switch/.pic={...}} and draw it with \pic (s) at (1,0) {switch};. Coordinates named (-a) inside the pic become s-a outside it.

## References

- [PetarV-/TikZ at commit 86aee04: Generative adversarial network/generative_adversarial_network.tex (MIT licence)](https://github.com/PetarV-/TikZ/blob/86aee049f5355d77c598b936399e42caf8941e60/Generative%20adversarial%20network/generative_adversarial_network.tex)
- [PGF/TikZ manual, §13.1 Overview (options for a single coordinate)](https://tikz.dev/tikz-coordinates#sec-13.1)
- [PGF/TikZ manual, §16.4.4 Defining Shorthands (shorten > and shorten <)](https://tikz.dev/tikz-arrows#sec-16.4.4)
- [PGF/TikZ manual, §17.5.3 Advanced Placement Options (positioning)](https://tikz.dev/tikz-shapes#sec-17.5.3)
- [PGF/TikZ manual, §17.8 Placing Nodes on a Line or Curve Explicitly](https://tikz.dev/tikz-shapes#sec-17.8)
- [PGF/TikZ manual, §17.12 Connecting Nodes: Using the Edge Operation](https://tikz.dev/tikz-shapes#sec-17.12)
- [PGF/TikZ manual, §18 Pics: Small Pictures on Paths](https://tikz.dev/tikz-pics)
- [PGF/TikZ manual, §74.3 To Path Library: Curves (bend left)](https://tikz.dev/library-edges#sec-74.3)

## TikZ guides for these techniques

- [TikZ neural network diagrams: perceptron, MLP, CNN, LSTM and transformer](https://www.labcanvas.com/tikz/neural-networks): Neural network diagrams in LaTeX with TikZ: perceptron, MLP from \foreach loops, labelled weights, CNN feature maps, RNN, LSTM, autoencoder, transformer.
- [TikZ nodes: shapes, anchors, positioning and labels](https://www.labcanvas.com/tikz/nodes): How TikZ nodes work: shapes, size (inner sep, minimum size), all anchors, the positioning library (right=of, node distance), labels, pins and scaling.
- [TikZ arrows: arrow tips, sizes, curves and labels](https://www.labcanvas.com/tikz/arrows): All TikZ arrow tips in one rendered table, arrows.meta sizing (length, width, scale, open, round), and curved, labelled, thick, block and squiggly arrows.
- [TikZ graphs: drawing networks of vertices and edges](https://www.labcanvas.com/tikz/graphs): Graph theory drawings in TikZ: vertices and edges by hand, directed and weighted graphs, loops, the \graph syntax, K_n and C_n, and automatic layouts.
- [TikZ coordinates, paths, \foreach, scopes and styles](https://www.labcanvas.com/tikz/coordinates): TikZ coordinates (polar, relative ++, named, calc, intersections), paths and controls, \foreach loops, scopes, \tikzset styles, decorations and grids.
