-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathregular-vs-bayes-nn.typ
More file actions
145 lines (136 loc) · 3.81 KB
/
Copy pathregular-vs-bayes-nn.typ
File metadata and controls
145 lines (136 loc) · 3.81 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#import "@preview/cetz:0.5.2": canvas, draw
#import "@preview/cetz-plot:0.1.4": plot
#import draw: circle, content, group, line, translate
#set page(width: auto, height: auto, margin: 8pt, fill: none)
#canvas({
let spacing = (layer: 3.5, node: 1.5)
let arrow-style = (
mark: (end: "stealth", scale: 0.7),
stroke: gray + 0.7pt,
fill: gray,
)
let neuron(pos, fill: white, label: none, name: none) = {
content(
pos,
if label != none { $#label$ },
frame: "circle",
fill: fill,
stroke: none,
radius: 0.4,
padding: 3pt,
name: name,
)
}
// unit shift vector along start->end, scaled by dist
let line-shift(start, end, dist) = {
let dx = end.at(0) - start.at(0)
let dy = end.at(1) - start.at(1)
let len = calc.sqrt(dx * dx + dy * dy)
(x: dist * dx / len, y: dist * dy / len)
}
let weight-label(start, end, ii, jj, offset: 0) = {
let mid-x = (start.at(0) + end.at(0)) / 2
let mid-y = (start.at(1) + end.at(1)) / 2
let shift = if offset != 0 {
let s = line-shift(start, end, offset * 0.4)
(s.x, s.y)
} else { (0, 0) }
content(
(mid-x + shift.at(0), mid-y + shift.at(1)),
[#calc.round(0.35 * ii - jj * 0.15, digits: 2)],
frame: "rect",
fill: white,
stroke: none,
padding: 1.5pt,
)
}
let gaussian(start, end, offset: 0, shift: 0) = {
let width = 0.6
let height = 0.25
let x-mid = (start.at(0) + end.at(0)) / 2
let y-mid = (start.at(1) + end.at(1)) / 2
let mu = offset * 0.15
let s = if shift != 0 { line-shift(start, end, shift * 0.4) } else {
(x: 0, y: 0)
}
group({
translate((x-mid - width / 2 + s.x, y-mid - height / 2 + s.y))
plot.plot(size: (width, height), axis-style: none, {
plot.add(
style: (stroke: orange + 1pt, fill: orange.lighten(80%)),
domain: (-1, 1),
samples: 50,
x => {
let variance = 0.3 + calc.abs(offset) * 0.1
let peak = 0.8 + calc.rem(calc.abs(offset), 0.4)
peak * calc.exp(-5 * calc.pow(x - mu, 2) / variance)
},
)
})
})
}
// 2-4-1 network; decorate-ih/decorate-ho draw the per-edge annotation (weight or distribution)
let draw-network(name, x0, decorate-ih, decorate-ho) = group(name: name, {
for ii in range(2) {
neuron(
(x0, (ii + 1) * spacing.node + 1),
fill: rgb("#90EE90"),
label: "i" + str(ii + 1),
name: "ii" + str(ii + 1),
)
}
for ii in range(4) {
neuron(
(x0 + spacing.layer, (ii + 1) * spacing.node),
fill: rgb("#ADD8E6"),
label: "h" + str(ii + 1),
name: "h" + str(ii + 1),
)
}
neuron(
(x0 + 2 * spacing.layer, 2.5 * spacing.node),
fill: rgb("#FFB6C6"),
label: "o",
name: "o",
)
for ii in range(2) {
for jj in range(4) {
line("ii" + str(ii + 1), "h" + str(jj + 1), ..arrow-style)
decorate-ih(
(x0, (ii + 1) * spacing.node + 1),
(x0 + spacing.layer, (jj + 1) * spacing.node),
ii,
jj,
)
}
}
for ii in range(4) {
line("h" + str(ii + 1), "o", ..arrow-style)
decorate-ho(
(x0 + spacing.layer, (ii + 1) * spacing.node),
(x0 + 2 * spacing.layer, 2.5 * spacing.node),
ii,
)
}
})
draw-network(
"regular",
0,
(start, end, ii, jj) => weight-label(
start,
end,
ii + 1,
jj + 1,
offset: if ii == 0 { 1.5 } else { -1 },
),
(start, end, ii) => weight-label(start, end, ii + 1, 1),
)
draw-network(
"bayes",
3 * spacing.layer,
(start, end, ii, jj) => gaussian(start, end, offset: ii - jj, shift: if ii == 0 { 1.5 } else {
-1
}),
(start, end, ii) => gaussian(start, end, offset: ii),
)
})