Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

AI · Generative Models

Numbers In, Images Out.

A generative model — a GAN, a VAE, the decoder of a diffusion model — is a function that takes a short vector of numbers, the latent code z, and turns it into a full image. The astonishing part isn’t that it can produce an image from numbers; it’s that the space of those numbers is organized. Training doesn’t just teach the model to memorize outputs — it arranges the latent space so that its geometry means something. Three properties fall out, and they’re what make generative models feel magical. First, continuity: codes that are close together produce images that look similar, so there are no jarring jumps — the space is smooth. Second, interpolation: walk in a straight line from one code to another and the image morphs smoothly between the two, passing through plausible in-between images the model was never explicitly shown. Third, semantic directions: particular directions in the space correspond to meaningful attributes — add a certain vector and a face gains a smile, or an object rotates — so you can steer a single property while leaving the rest intact. Below is a small, hand-built generator so you can see these properties directly: move its latent code, interpolate between two points, and push along a semantic direction.

latent code z → image · continuity (near = similar) · interpolation morphs · directions = attributes
latent code (z)

The input vector to a generator. Every point in this space decodes to an image.

continuity

Nearby latent codes produce similar images — the space is smooth, with no sudden jumps.

interpolation

Walking a straight line between two codes morphs one image smoothly into the other.

semantic direction

A direction in latent space that changes one meaningful attribute (e.g. expression) while leaving others fixed.

generator.js — a code becomes a picture
Ready
On the left is a 2-D latent space; on the right is what a toy generator decodes from the current code. Move the code with the arrows and watch the image change smoothly (continuity). Interpolate A→B to morph between two codes. Then walk the expression direction to steer one attribute.
-0.6
z₁ · expression
-0.6
z₂ · energy
sleepy frown
decoded

How it works

Why should a pile of numbers end up meaningfully organized? Because of the pressure training puts on the generator. A generator is trained to map latent codes drawn from a simple, smooth distribution (usually a Gaussian) onto the complicated distribution of real images, and to fool a discriminator (in a GAN) or reconstruct data (in a VAE). To succeed with a continuous input distribution, the mapping it learns must itself be continuous — small changes in z can’t cause wild jumps, or the model couldn’t consistently produce realistic images across the whole input distribution. That continuity is exactly what makes interpolation work: the straight-line path between two codes is made of nearby points, each decoding to a plausible image, so you glide smoothly from one output to the other through valid in-betweens the model synthesizes on the fly (this is the famous “latent walk,” and it’s strong evidence a model has learned genuine structure rather than memorized samples). Semantic directions emerge because the model discovers that varying certain factors of the data — pose, lighting, the presence of a smile — is an efficient way to explain the variation in real images, so those factors get encoded as (often roughly linear) directions in latent space; you can find a “smile vector” by averaging the codes of smiling faces and subtracting the codes of non-smiling ones, then add it to any code to add a smile. Real generators differ from this toy in scale and messiness — the space is hundreds of dimensions, directions are entangled rather than perfectly clean, and techniques like StyleGAN’s intermediate latent space or diffusion’s noise schedule exist partly to make the geometry more controllable — but the three properties you can feel here (continuity, interpolation, steerable directions) are precisely why we say a generative model has learned a representation, not just a lookup table. It’s also the foundation of practical image editing: to change one attribute of a generated (or inverted real) image, you find its code and move along the corresponding direction.

1

A code decodes to an image

The generator is a function from a latent vector z to an image. Every point in the latent space corresponds to some output — the space is the model’s vocabulary of things it can make.

2

Continuity: near codes, similar images

Nudge z a little and the image changes a little. The learned mapping is smooth, because the generator was trained to map a continuous input distribution onto realistic images — there are no sudden jumps.

3

Interpolation morphs between codes

Walk a straight line from code A to code B and the image morphs smoothly through plausible in-betweens the model synthesizes on the fly — evidence it learned real structure, not a lookup table of memorized samples.

✓

Directions carry meaning

Specific directions correspond to attributes: move along the “expression” axis and only the expression changes. Find such a direction (e.g. average smiling minus non-smiling codes) and you can steer one property of any image — the basis of latent-space editing.

Generator
z (vector) → image
Continuity
near codes → similar images
Interpolation
straight line morphs
Direction
a steerable attribute

The code

# A generator is just a function z -> image image = G(z) # z is a short latent vector # Continuity: nearby z -> similar images (smooth mapping) # Interpolation: walk a straight line between two codes for t in linspace(0, 1, 30): z_t = (1 - t) * z_A + t * z_B # morphs A into B, frame by frame show(G(z_t)) # Semantic direction: find an attribute vector, then steer it smile_dir = mean(z_smiling) - mean(z_not_smiling) edited = G(z + alpha * smile_dir) # add a smile, keep the rest

The picture: one straight line, three images

AB +expression frownneutralsmile wide-eyedcalmsleepy G t 0 to 0.32sleepy frown, eyes 5.8-10.4 t 0.36 to 0.64calm neutral, eyes 10.9-15.1 t 0.68 to 1wide-eyed smile, eyes 15.6-20.2 eye height in px, 29 frames
The interpolation path cuts diagonally through the toy’s nine mood regions, so the face changes on both attributes at once. The expression walk is horizontal, so it changes only one. The labels jump at the grid lines, but the drawing does not: between frames 9 and 10 the eyes grow from 10.4 to 10.9 px, the same small step as everywhere else on the path.

Worked example: the A→B walk, frame by frame

The lab’s Interpolate A→B button computes z = (1 − t)·A + t·B for t = i/28, i = 0 … 28. Here is what its generator decodes at selected frames, then what Walk +expression does from the starting code (−0.6, −0.6).

Movetz = (z₁, z₂)Eye heightDecoded
frame 00(−0.80, −0.80)5.8sleepy frown
frame 70.25(−0.40, −0.40)9.4sleepy frown
frame 90.321(−0.29, −0.29)10.4sleepy frown
frame 100.357(−0.23, −0.23)10.9calm neutral
frame 140.5(0, 0)13.0calm neutral
frame 190.679(0.29, 0.29)15.6wide-eyed smile
frame 281(0.80, 0.80)20.2wide-eyed smile
walk ×1—(−0.25, −0.60)7.6sleepy neutral
walk ×3—(0.45, −0.60)7.6sleepy smile
walk ×5—(1.00, −0.60)7.6clamped at the edge

Two readings. On the diagonal, both coordinates move together, so expression and energy change in lockstep: 10 frames of sleepy frown, 9 calm neutral, 10 wide-eyed smile. On the walk, z₂ never moves, so the eye height stays at 7.6 while only the expression changes. That is the whole idea of a semantic direction. The fifth click would take z₁ to 1.15, but the lab clamps it to 1. A real latent space has no such edge, which is why walking too far is a real failure (see the mistakes below).

Cost and trade-offs: what each latent operation costs

Here Time is the compute, Space is what you have to store. d is the latent size: 2 in this toy, 512 for StyleGAN’s z.

CaseTimeSpaceWhy
Decode one codeone forward pass of Gd numbers per imageA 512-number code stands in for a whole image. Generation is cheap once training is done.
Interpolate, N framesN forward passesjust the two end codesThe lab draws 29 frames from 2 codes. Slerp costs one extra angle per frame, nothing more.
Find a direction (mean difference)one code per labelled example, then an averageone d-vectorCheap, but it inherits every correlation in your labels: if smiling faces are also younger, so is the direction.
Find a code for a real image (GAN)an optimisation loop: many forward and backward passesd numbersA GAN has no encoder, so it has to search. A VAE gets the code in one encoder pass.
Edit with a directionone forward pass per strength αnothing newThe cheap step. Quality depends entirely on how clean the direction is.

The hidden cost is geometry, not compute. A 512-dimensional Gaussian code almost always has length close to √512 ≈ 22.6. The straight-line midpoint between two such codes has length about 16.0 (71% of that), a region the generator rarely saw in training. Spherical interpolation (slerp) follows the arc instead and keeps the midpoint at about 22.6. In 2-D, as in this lab, the difference is too small to notice.

Common mistakes

  • Linear interpolation in a high-dimensional Gaussian space

    The midpoint of a straight line is about 29% shorter than a typical code (16.0 against 22.6 at d = 512). The middle frames can look washed out or average. Use slerp, or interpolate in a space that was not sampled as a Gaussian, such as StyleGAN’s w.

  • Assuming a direction is one coordinate

    In the toy, expression is z₁ by construction. In a trained model no single coordinate means “smile”. A direction is a vector that mixes all d coordinates, and you have to find it: by mean difference, a linear classifier on codes, or PCA.

  • Walking too far along a direction

    The lab clamps z to [−1, 1], so you cannot fall off. A real model has no clamp: a large α pushes the code outside the region training covered and the output degrades into artefacts. Edit with small, tested strengths.

  • Reading a smooth walk as proof

    Smooth interpolation is good evidence of learned structure, but it is not a full test. Papers also show each generated image next to its nearest training images, to check the model is not copying.

  • Moving codes between models or spaces

    A code means something only to the generator it was found for. A direction found in StyleGAN’s w space does nothing sensible in z, and a new checkpoint can rearrange the whole space.

Where this shows up: real-world models and interviews

  • Vector arithmetic on faces

    The DCGAN paper showed that averaged codes add up the way words do in word2vec: smiling woman − neutral woman + neutral man gives a smiling man. It is the same subtraction as the page’s smile_dir.

  • StyleGAN editing

    StyleGAN sends z through a mapping network to an intermediate w, where attributes are less entangled. Tools such as InterFaceGAN (linear classifiers on codes) and GANSpace (PCA on codes) find editing directions there.

  • Latent diffusion

    Stable Diffusion denoises in a VAE’s compressed latent space (a 512×512 image becomes a 64×64×4 latent) rather than in pixels. The same idea, a small structured code decoded into an image, makes it affordable.

  • Interview angles

    Expect: why interpolation works (a continuous map from a continuous input), why slerp instead of lerp, what disentanglement means, and how you would edit a real photo (invert it to a code, then move along a direction).

Quick check

1. What does the latent code (z) represent for a generative model?

2. What does it mean that latent space is continuous, and why does it matter?

3. What is a semantic direction in latent space?

FAQ

What is latent space in a generative model?

The space of input vectors (latent codes) a generative model decodes into outputs like images. Rather than storing pictures, the model learns a function turning a short vector into a full image, and the set of all those vectors is the latent space. Training organizes it: nearby codes make similar images (continuity), straight-line paths morph smoothly (interpolation), and particular directions mean attributes (semantic directions). So it acts as a compact, structured representation of everything the model can generate.

Why does interpolating between two latent codes create a smooth morph?

Because the generator’s mapping from code to image is continuous. Walking a straight line from A to B passes through codes near their neighbors, and continuity means each decodes to a similar image — so outputs change gradually, gliding from A’s image to B’s through plausible intermediates the model synthesizes on the fly. This smooth interpolation is often taken as evidence the model learned real structure (a representation) rather than memorizing training examples, since the in-between images are coherent things it was never shown.

How do you find and use a semantic direction (like a "smile vector")?

Collect latent codes for examples with an attribute and codes for examples without it, then take the difference of their averages: mean(smiling) − mean(not-smiling) gives an approximate "smile direction." Adding a scaled version to any code makes the image smile more; subtracting it, less — ideally without changing other features. In practice directions are entangled (pushing "smile" may nudge age), so techniques like StyleGAN’s style space or unsupervised direction discovery aim for cleaner, disentangled directions. This underlies latent-space image editing: find an image’s code and move along the attribute direction.

What is the difference between how GANs, VAEs, and diffusion models use latent space?

All decode from a latent representation, but differently. A GAN maps random latents to images by fooling a discriminator — smooth space, great for interpolation, but finding the code for a real image needs a separate inversion step. A VAE learns an encoder and decoder together, so it can map images to codes and back, with a latent regularized to be smooth (often at some cost to sharpness). Diffusion models generate by denoising random noise, so the "latent" is the noise plus trajectory, and control comes from steering the denoising (and, in latent diffusion, a learned compressed space). The shared idea is a structured, navigable representation.

Keep going

Finished this one? 0 / 61 Labs done
NextOut of the Static: Diffusion Next on the “Structure, and making new things” ladder · rung 6 of 6

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Labs