Generative adversarial network (GAN) diagram in TikZ, explained
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 pdata(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.
Contents
The figure and its code

% 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
right=5em of z,above=of xand an extrayshiftafter positioning. - Two labels on one arrow
node[above] {…} node[below] {…}inside the same--. - Curved edges
edge[bend left]draws the dashed switch arc as its own path. - Shifted coordinates
([xshift=-0.9em, yshift=4em]pt1.north)moves one point without a calc expression. - Neural network diagrams generators, discriminators and how GAN figures are usually laid out.
Variations
Our own code, written for this page and typeset with TeX Live 2026 like the figure above. Each one is a complete example: copy it, or open it in the TikZ editor and change it. This code is CC0 1.0, free to use without conditions.
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.

\usepackage{tikz} \usetikzlibrary{positioning, calc, arrows.meta}\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}
Full document
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc, arrows.meta}
\begin{document}
\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}
\end{document}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.

\usepackage{tikz} \usetikzlibrary{arrows.meta}\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}
Full document
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\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}
\end{document}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)github.com
- PGF/TikZ manual, §13.1 Overview (options for a single coordinate)tikz.dev
- PGF/TikZ manual, §16.4.4 Defining Shorthands (shorten > and shorten <)tikz.dev
- PGF/TikZ manual, §17.5.3 Advanced Placement Options (positioning)tikz.dev
- PGF/TikZ manual, §17.8 Placing Nodes on a Line or Curve Explicitlytikz.dev
- PGF/TikZ manual, §17.12 Connecting Nodes: Using the Edge Operationtikz.dev
- PGF/TikZ manual, §18 Pics: Small Pictures on Pathstikz.dev
- PGF/TikZ manual, §74.3 To Path Library: Curves (bend left)tikz.dev
The walkthrough was checked against these sources and the pictures are the code typeset with TeX Live 2026. The original figure is © Petar Veličković, under the MIT licence; the variations on this page are ours, CC0 1.0.
Change this figure in your browserOpen the figure or any variation in the free TikZ editor: it is typeset with TeX Live as you type, with errors pointed at their line.
Open the figure in the TikZ editor