File size: 3,431 Bytes
4f6545d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// =============================================================================
// Autoencoder Architecture Diagram
// =============================================================================
// DESIGN PATTERNS IN THIS FILE:
//
// 1. HELPER FUNCTIONS: The diagram defines two reusable functions:
//    - `neuron(pos, fill, text)`: Draws a single node
//    - `connect-layers(start-pos, start-count, end-pos, end-count)`:
//      Connects all neurons between two layers using nested loops
//
// 2. LAYER CONFIGURATION TUPLE: All layer properties are stored in a tuple:
//    `(x-pos, neuron-count, fill-color, label-prefix, label-superscript, y-offset)`
//    This is a powerful pattern for configurable diagrams.
//
// 3. DRAW ORDER MATTERS: Connections are drawn BEFORE nodes.
//    This ensures lines appear behind circles, not on top.
//
// 4. VISUAL HIERARCHY:
//    - Input layer: yellow (#f6db71) - distinct from hidden
//    - Hidden layers: gray (#eee) - neutral
//    - Latent layer: positioned at center
//    - Output layer: purple (#cecef9) - distinct from input
//
// 5. LABEL PLACEMENT: Uses layer index to place labels:
//    `content((layers.at(0).at(0), 4), [Input Layer])`
//
// WHY THIS APPROACH WORKS:
// - Helper functions eliminate copy-paste
// - Single source of truth for layer configuration
// - Color coding helps readers understand data flow
// - The latent bottleneck (3 neurons) naturally emphasizes compression

#import "@preview/cetz:0.4.2": canvas, draw
#import draw: content, line

#set page(width: auto, height: auto, margin: 8pt)

#let neuron(pos, fill: white, text: none) = {
  draw.content(pos, text, frame: "circle", fill: fill, stroke: 0.5pt, padding: 1pt)
}

#let connect-layers(start-pos, start-count, end-pos, end-count) = {
  let start-y = start-count / 2 * 0.8
  let end-y = end-count / 2 * 0.8

  for ii in range(start-count) {
    for jj in range(end-count) {
      let start = (start-pos, start-y - ii * 0.8)
      let end = (end-pos, end-y - jj * 0.8)
      draw.line(start, end, stroke: rgb("#aaa") + .5pt)
    }
  }
}

#canvas({
  // Define layer configurations
  let layers = (
    // (x-pos, neuron-count, fill-color, label-prefix, label-superscript, y-offset)
    (0, 8, rgb("#f6db71"), "x", none, 3.2), // Input layer
    (2, 5, rgb("#eee"), "h", "1", 2), // First hidden layer
    (4, 3, rgb("#eee"), "z", none, 1.2), // Latent layer
    (6, 5, rgb("#eee"), "h", "2", 2), // Third hidden layer
    (8, 8, rgb("#cecef9"), "hat(x)", none, 3.2), // Output layer
  )

  // Draw connections first (so they appear behind nodes)
  for idx in range(layers.len() - 1) {
    let (x1, n1, ..) = layers.at(idx)
    let (x2, n2, ..) = layers.at(idx + 1)
    connect-layers(x1, n1, x2, n2)
  }

  // Layer labels
  content((layers.at(0).at(0), 4), align(center)[Input Layer])
  content((layers.at(2).at(0), 2.2), align(center)[Latent\ Representation])
  content((layers.at(-1).at(0), 4), align(center)[Output Layer])

  // Draw all layers
  for (x, count, fill, prefix, sup, y-offset) in layers {
    for idx in range(count) {
      let y-pos = y-offset - idx * 0.8
      let label = if sup != none {
        $prefix^sup_idx$
      } else if prefix == "hat(x)" {
        $hat(x)_idx$
      } else {
        $prefix_idx$
      }
      neuron((x, y-pos), fill: fill, text: label)
    }
  }
})