-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformers.html
More file actions
1308 lines (1181 loc) · 90.9 KB
/
transformers.html
File metadata and controls
1308 lines (1181 loc) · 90.9 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<title>D-Lab - Understanding Transformer Attention</title>
<style>
:root{
--bg:#ffffff;
--panel:#f8f9fa;
--muted:#6c757d;
--text:#212529;
--blue:#0d6efd;
--indigo:#6610f2;
--purple:#6610f2;
--emerald:#198754;
--orange:#fd7e14;
--border:#dee2e6;
--red:#dc3545;
}
*{box-sizing:border-box}
body {
margin:0;
font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
background:var(--bg);
color:var(--text);
line-height: 1.5;
}
.wrap{max-width:1200px;margin:32px auto;padding:0 20px}
.course-header{
text-align:center;
margin-bottom:20px;
padding:16px;
background:var(--panel);
border-radius:12px;
border:1px solid var(--border)
}
.course-logo{width:180px;height:auto;margin-bottom:8px}
.course-title{
font-size:1.2em;
color:var(--blue);
margin:0;
font-weight:600
}
h1{font-size:32px;margin:0 0 12px; text-align: center;}
p.lead{color:var(--muted);margin:0 0 24px; font-size: 18px; text-align: center;}
.intro{
background:var(--panel);
border:1px solid var(--border);
border-radius:16px;
padding:24px;
margin-bottom: 24px;
box-shadow:0 2px 8px rgba(0,0,0,.1);
}
.intro h2{margin:0 0 12px; color:var(--blue);}
.intro p{margin:12px 0; font-size: 16px;}
.stage {
border:1px solid var(--border);
border-radius:16px;
padding:24px;
background:var(--bg);
margin-bottom:24px;
box-shadow:0 2px 8px rgba(0,0,0,.1);
}
.stage h2{margin:0 0 12px; font-size:20px; color:var(--blue);}
.step-number{
display: inline-flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
border-radius: 50%;
background: var(--blue);
color: white;
font-weight: bold;
font-size: 12px;
margin-right: 8px;
}
.explanation {
font-size:15px;
color:var(--text);
margin-bottom:16px;
background:rgba(13,110,253,.08);
padding:12px 16px;
border-radius:12px;
border-left: 4px solid var(--blue);
}
.goal{
background:rgba(255,179,102,.1);
border:1px solid rgba(255,179,102,.3);
border-radius:12px;
padding:16px;
margin:16px 0;
}
.goal h3{margin:0 0 8px; color:var(--orange);}
.chip{
display:inline-flex;
gap:6px;
align-items:center;
padding:8px 12px;
border-radius:999px;
border:1px solid var(--border);
background:var(--bg);
font-size:14px;
margin:4px 8px 4px 0;
font-weight: 500;
transition: all 0.3s;
}
.chip.word-clickable{
cursor: pointer;
}
.chip.word-clickable:hover, .chip[onclick]:hover{
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.chip.selected {
background: var(--blue);
color: white;
border-color: var(--blue);
}
.chip.query{ background:rgba(239,68,68,.15); border-color:rgba(239,68,68,.4); color:var(--red) }
.chip.key{ background:rgba(34,197,94,.15); border-color:rgba(34,197,94,.4); color:var(--emerald) }
.chip.value{ background:rgba(59,130,246,.15); border-color:rgba(59,130,246,.4); color:var(--blue) }
.attention-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
margin: 20px 0;
max-width: 600px;
}
.attention-cell {
aspect-ratio: 1;
display: flex;
align-items: center;
justify-content: center;
border-radius: 8px;
font-size: 14px;
font-weight: bold;
transition: all 0.3s;
cursor: pointer;
}
.btn{
display:inline-flex;
align-items:center;
gap:8px;
padding:10px 14px;
border-radius:12px;
border:1px solid var(--border);
background:var(--bg);
color:var(--text);
cursor:pointer;
font-size: 14px;
transition: all 0.3s;
margin: 10px 5px;
}
.btn:hover{
background:var(--panel);
transform: translateY(-1px);
}
.btn:disabled{
background: var(--panel);
opacity: 0.5;
cursor: not-allowed;
transform: none;
}
.btn-primary{
background:var(--blue);
color:white;
border-color:var(--blue);
}
.btn-primary:hover{
background:#0b5ed7;
border-color:#0b5ed7;
}
.matrix-toggle.active{
background:var(--blue);
color:white;
border-color:var(--blue);
}
.matrix-toggle.active:hover{
background:#0b5ed7;
border-color:#0b5ed7;
}
.matrix-demo{
transition: opacity 0.3s ease;
}
.card{
background:var(--panel);
border:1px solid var(--border);
border-radius:16px;
padding:18px;
box-shadow:0 2px 8px rgba(0,0,0,.1);
margin: 20px 0;
}
.arrow {
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 15px solid #667eea;
margin: 10px auto;
}
.tag{
font-size:14px;
color:var(--muted);
padding: 8px 12px;
background: var(--panel);
border-radius: 8px;
margin: 5px;
display: inline-block;
font-family: ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas;
}
.tag b{color:var(--text)}
.highlight {
animation: pulse 1s infinite;
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
.progress-bar {
width: 100%;
height: 8px;
background: #e0e0e0;
border-radius: 4px;
overflow: hidden;
margin: 20px 0;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #667eea, #764ba2);
transition: width 0.5s ease;
}
.hidden {
display: none;
}
.fade-in {
animation: fadeIn 0.5s ease;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
</style>
</head>
<body>
<div class="wrap">
<div class="course-header">
<img src="img/dlab-bubble-logo-2025.png" alt="D-Lab Logo" class="course-logo">
</div>
<h1>Understanding Transformer Attention</h1>
<p class="lead">Interactive tutorial on how attention mechanisms work in transformers</p>
<div class="progress-bar">
<div class="progress-fill" id="progress" style="width: 20%"></div>
</div>
<!-- Introduction -->
<div class="intro">
<h2>What are we learning?</h2>
<p>Transformers revolutionized AI by introducing the <strong>attention mechanism</strong>. Unlike earlier models that process words sequentially, transformers can look at all words simultaneously and decide which ones are most relevant for understanding each word.</p>
<div class="goal">
<h3>Learning Objective: Self-Attention</h3>
<p>Understand how transformers use Query, Key, and Value matrices to compute attention scores, allowing the model to focus on relevant parts of the input when processing each word.</p>
</div>
</div>
<!-- Step 1: Introduction -->
<div class="stage">
<h2><span class="step-number">1</span>What is Attention?</h2>
<div class="explanation">
<strong>Simple Definition:</strong> Attention is a mechanism that helps the model decide which words to "pay attention to" when understanding each word in a sentence.
</div>
<div class="card">
<p>Let's work with this sentence that has clear relationships:</p>
<div id="sentence">
<span class="chip" style="cursor: default;">The</span>
<span class="chip" style="cursor: default;">black</span>
<span class="chip" style="cursor: default;">cat</span>
<span class="chip" style="cursor: default;">ate</span>
<span class="chip" style="cursor: default;">fish</span>
</div>
<p style="color: var(--muted); margin-top: 20px; font-size: 14px;">
<strong>Think about it:</strong> To understand "cat" fully, the model needs to know it's a "black cat" (not just any cat) that "ate fish" (not sleeping or playing). This is why attention matters - words get meaning from their context!
</p>
<button class="btn btn-primary" onclick="showStep1b()" style="margin-top: 15px;">Let's Learn How! →</button>
</div>
</div>
<!-- Step 2: What are Transformers? -->
<div class="stage hidden" id="step1b">
<h2><span class="step-number">2</span>What are Transformers?</h2>
<div class="explanation">
<strong>The Big Picture:</strong> Transformers are a type of neural network architecture that revolutionized AI by processing all words in a sentence simultaneously, rather than one by one like older models. Previous models like RNNs (Recurrent Neural Networks) and LSTMs (Long Short-Term Memory networks) had to read text sequentially, word by word, which made them slower and less effective at understanding long-range relationships.
</div>
<div class="card">
<div class="goal">
<h3>How Transformers Differ from Earlier Models</h3>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 15px;">
<div style="padding: 15px; background: rgba(239,68,68,.1); border-radius: 8px; border-left: 4px solid var(--red);">
<h4 style="color: var(--red); margin: 0 0 8px;">Old Way (RNNs/LSTMs)</h4>
<p style="margin: 0; font-size: 14px;">Process words sequentially: "The" → "black" → "cat" → "ate" → "fish"</p>
<p style="margin: 8px 0 0; font-size: 13px; color: var(--muted);">Slow, struggles with long sequences</p>
</div>
<div style="padding: 15px; background: rgba(25,135,84,.1); border-radius: 8px; border-left: 4px solid var(--emerald);">
<h4 style="color: var(--emerald); margin: 0 0 8px;">Transformer Way</h4>
<p style="margin: 0; font-size: 14px;">Process all words at once using <strong>self-attention</strong></p>
<p style="margin: 8px 0 0; font-size: 13px; color: var(--muted);">Fast, handles long sequences well</p>
</div>
</div>
</div>
<div style="margin-top: 20px;">
<h4 style="color: var(--blue); margin-bottom: 10px;">The Key Innovation: Self-Attention</h4>
<p>Instead of processing words in order, transformers ask: <em>"For each word, which other words in the sentence should I pay attention to?"</em></p>
<!-- Attention Mode Toggle -->
<div style="display: flex; justify-content: center; gap: 8px; margin: 15px 0;">
<button class="btn matrix-toggle active" data-mode="encoder" onclick="showAttentionMode('encoder')">
<span style="color: var(--emerald);">●</span> Encoder (Bidirectional)
</button>
<button class="btn matrix-toggle" data-mode="decoder" onclick="showAttentionMode('decoder')">
<span style="color: var(--orange);">●</span> Decoder (Causal)
</button>
</div>
<!-- Encoder Attention visualization (default) -->
<div id="attention-encoder" class="attention-mode-demo">
<div style="background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 20px; margin: 20px 0;">
<p style="text-align: center; margin-bottom: 15px; font-size: 14px;"><strong>Attention Matrix (Bidirectional)</strong></p>
<p style="text-align: center; margin-bottom: 20px; font-size: 12px; color: var(--muted);">Each row shows how that word attends to all other words. Darker = stronger attention.</p>
<div style="display: flex; justify-content: center;">
<div style="display: grid; grid-template-columns: 60px repeat(5, 65px); gap: 4px; font-size: 12px;">
<!-- Header row -->
<div></div>
<div style="text-align: center; font-weight: bold; padding: 8px;">the</div>
<div style="text-align: center; font-weight: bold; padding: 8px;">black</div>
<div style="text-align: center; font-weight: bold; padding: 8px;">cat</div>
<div style="text-align: center; font-weight: bold; padding: 8px;">ate</div>
<div style="text-align: center; font-weight: bold; padding: 8px;">fish</div>
<!-- The row -->
<div style="font-weight: bold; display: flex; align-items: center; padding: 0 8px;">the</div>
<div style="background: rgba(13,110,253,0.3); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.4);">0.30</div>
<div style="background: rgba(13,110,253,0.1); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.2);">0.10</div>
<div style="background: rgba(13,110,253,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.2);">0.15</div>
<div style="background: rgba(13,110,253,0.35); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.4);">0.35</div>
<div style="background: rgba(13,110,253,0.1); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.2);">0.10</div>
<!-- Black row -->
<div style="font-weight: bold; display: flex; align-items: center; padding: 0 8px;">black</div>
<div style="background: rgba(13,110,253,0.1); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.2);">0.10</div>
<div style="background: rgba(13,110,253,0.3); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.4);">0.30</div>
<div style="background: rgba(13,110,253,0.5); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.6); font-weight: bold;">0.50</div>
<div style="background: rgba(13,110,253,0.05); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.15);">0.05</div>
<div style="background: rgba(13,110,253,0.05); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.15);">0.05</div>
<!-- Cat row (highlighted) -->
<div style="font-weight: bold; display: flex; align-items: center; padding: 0 8px; background: rgba(255,193,7,0.2); border-radius: 4px;">cat</div>
<div style="background: rgba(13,110,253,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 2px solid rgba(255,193,7,0.5);">0.10</div>
<div style="background: rgba(13,110,253,0.7); padding: 12px; text-align: center; border-radius: 4px; border: 2px solid rgba(255,193,7,0.5); font-weight: bold; color: white;">0.45</div>
<div style="background: rgba(13,110,253,0.05); padding: 12px; text-align: center; border-radius: 4px; border: 2px solid rgba(255,193,7,0.5);">0.00</div>
<div style="background: rgba(13,110,253,0.5); padding: 12px; text-align: center; border-radius: 4px; border: 2px solid rgba(255,193,7,0.5); font-weight: bold;">0.30</div>
<div style="background: rgba(13,110,253,0.25); padding: 12px; text-align: center; border-radius: 4px; border: 2px solid rgba(255,193,7,0.5);">0.15</div>
<!-- Ate row -->
<div style="font-weight: bold; display: flex; align-items: center; padding: 0 8px;">ate</div>
<div style="background: rgba(13,110,253,0.1); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.2);">0.10</div>
<div style="background: rgba(13,110,253,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.2);">0.15</div>
<div style="background: rgba(13,110,253,0.4); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.5);">0.40</div>
<div style="background: rgba(13,110,253,0.2); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.3);">0.20</div>
<div style="background: rgba(13,110,253,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.2);">0.15</div>
<!-- Fish row -->
<div style="font-weight: bold; display: flex; align-items: center; padding: 0 8px;">fish</div>
<div style="background: rgba(13,110,253,0.05); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.15);">0.05</div>
<div style="background: rgba(13,110,253,0.1); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.2);">0.10</div>
<div style="background: rgba(13,110,253,0.2); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.3);">0.20</div>
<div style="background: rgba(13,110,253,0.5); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.6); font-weight: bold;">0.50</div>
<div style="background: rgba(13,110,253,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.2);">0.15</div>
</div>
</div>
<div style="text-align: center; margin-top: 15px; padding: 10px; background: rgba(255,193,7,0.1); border-radius: 6px; font-size: 12px;">
<strong>Focus on "cat" row:</strong> Can attend to ALL words (including future words "ate" and "fish")
</div>
<div style="background: rgba(25,135,84,.08); border: 1px solid rgba(25,135,84,.3); border-radius: 8px; padding: 12px; margin-top: 15px;">
<p style="margin: 0; font-size: 13px;"><strong style="color: var(--emerald);">✓ Bidirectional Attention:</strong> Each word sees the entire sentence. Perfect for understanding context when you have the full input (like BERT, for classification, understanding).</p>
</div>
</div>
</div>
<!-- Decoder Attention visualization (hidden) -->
<div id="attention-decoder" class="attention-mode-demo" style="display: none;">
<div style="background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 20px; margin: 20px 0;">
<p style="text-align: center; margin-bottom: 15px; font-size: 14px;"><strong>Attention Matrix (Causal/Masked)</strong></p>
<p style="text-align: center; margin-bottom: 20px; font-size: 12px; color: var(--muted);">Each row shows how that word attends ONLY to itself and previous words. Future positions are masked (⨯).</p>
<div style="display: flex; justify-content: center;">
<div style="display: grid; grid-template-columns: 60px repeat(5, 65px); gap: 4px; font-size: 12px;">
<!-- Header row -->
<div></div>
<div style="text-align: center; font-weight: bold; padding: 8px;">the</div>
<div style="text-align: center; font-weight: bold; padding: 8px;">black</div>
<div style="text-align: center; font-weight: bold; padding: 8px;">cat</div>
<div style="text-align: center; font-weight: bold; padding: 8px;">ate</div>
<div style="text-align: center; font-weight: bold; padding: 8px;">fish</div>
<!-- The row -->
<div style="font-weight: bold; display: flex; align-items: center; padding: 0 8px;">the</div>
<div style="background: rgba(13,110,253,0.9); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.6); font-weight: bold; color: white;">1.00</div>
<div style="background: rgba(200,200,200,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(150,150,150,0.2); color: var(--muted);">⨯</div>
<div style="background: rgba(200,200,200,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(150,150,150,0.2); color: var(--muted);">⨯</div>
<div style="background: rgba(200,200,200,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(150,150,150,0.2); color: var(--muted);">⨯</div>
<div style="background: rgba(200,200,200,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(150,150,150,0.2); color: var(--muted);">⨯</div>
<!-- Black row -->
<div style="font-weight: bold; display: flex; align-items: center; padding: 0 8px;">black</div>
<div style="background: rgba(13,110,253,0.25); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.3);">0.20</div>
<div style="background: rgba(13,110,253,0.8); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.6); font-weight: bold; color: white;">0.80</div>
<div style="background: rgba(200,200,200,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(150,150,150,0.2); color: var(--muted);">⨯</div>
<div style="background: rgba(200,200,200,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(150,150,150,0.2); color: var(--muted);">⨯</div>
<div style="background: rgba(200,200,200,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(150,150,150,0.2); color: var(--muted);">⨯</div>
<!-- Cat row (highlighted) -->
<div style="font-weight: bold; display: flex; align-items: center; padding: 0 8px; background: rgba(255,193,7,0.2); border-radius: 4px;">cat</div>
<div style="background: rgba(13,110,253,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 2px solid rgba(255,193,7,0.5);">0.10</div>
<div style="background: rgba(13,110,253,0.9); padding: 12px; text-align: center; border-radius: 4px; border: 2px solid rgba(255,193,7,0.5); font-weight: bold; color: white;">0.85</div>
<div style="background: rgba(13,110,253,0.1); padding: 12px; text-align: center; border-radius: 4px; border: 2px solid rgba(255,193,7,0.5);">0.05</div>
<div style="background: rgba(200,200,200,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 2px solid rgba(255,193,7,0.5); color: var(--muted); font-weight: bold;">⨯</div>
<div style="background: rgba(200,200,200,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 2px solid rgba(255,193,7,0.5); color: var(--muted); font-weight: bold;">⨯</div>
<!-- Ate row -->
<div style="font-weight: bold; display: flex; align-items: center; padding: 0 8px;">ate</div>
<div style="background: rgba(13,110,253,0.05); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.15);">0.05</div>
<div style="background: rgba(13,110,253,0.25); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.3);">0.20</div>
<div style="background: rgba(13,110,253,0.4); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.5);">0.40</div>
<div style="background: rgba(13,110,253,0.45); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.5); font-weight: bold;">0.35</div>
<div style="background: rgba(200,200,200,0.15); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(150,150,150,0.2); color: var(--muted);">⨯</div>
<!-- Fish row -->
<div style="font-weight: bold; display: flex; align-items: center; padding: 0 8px;">fish</div>
<div style="background: rgba(13,110,253,0.05); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.15);">0.05</div>
<div style="background: rgba(13,110,253,0.1); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.2);">0.10</div>
<div style="background: rgba(13,110,253,0.2); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.3);">0.20</div>
<div style="background: rgba(13,110,253,0.5); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.6); font-weight: bold;">0.50</div>
<div style="background: rgba(13,110,253,0.25); padding: 12px; text-align: center; border-radius: 4px; border: 1px solid rgba(13,110,253,0.3);">0.15</div>
</div>
</div>
<div style="text-align: center; margin-top: 15px; padding: 10px; background: rgba(255,193,7,0.1); border-radius: 6px; font-size: 12px;">
<strong>Focus on "cat" row:</strong> Can ONLY attend to previous words "the" and "black". Future positions are masked (⨯).
</div>
<div style="background: rgba(253,126,20,.08); border: 1px solid rgba(253,126,20,.3); border-radius: 8px; padding: 12px; margin-top: 15px;">
<p style="margin: 0 0 8px; font-size: 13px;"><strong style="color: var(--orange);">⚡ Causal/Masked Attention:</strong> Each word can only see itself and previous words. Essential for text generation where you predict one word at a time (like GPT, Claude).</p>
<div style="background: var(--bg); padding: 8px; border-radius: 4px; margin-top: 8px; font-size: 12px; font-family: monospace; color: var(--muted);">
Future tokens are masked with -∞ before softmax → attention weight becomes 0
</div>
</div>
</div>
</div>
</div>
<div style="background: rgba(102,16,242,.08); border: 1px solid rgba(102,16,242,.3); border-radius: 8px; padding: 15px; margin-top: 20px;">
<h4 style="color: var(--indigo); margin: 0 0 8px;">Real-World Impact</h4>
<p style="margin: 0; font-size: 14px;">This is what powers ChatGPT, Claude, Gemini and most other modern AI language models. The attention mechanism lets them understand complex relationships between words, no matter how far apart they are in a sentence!</p>
</div>
</div>
<button class="btn btn-primary" onclick="showStep2()">Next: How Does Attention Work? →</button>
</div>
<!-- Step 3: Query, Key, Value -->
<div class="stage hidden" id="step2">
<h2><span class="step-number">3</span>The Three Components: Query, Key, Value</h2>
<div class="explanation">
<strong>The Problem with Traditional Embeddings:</strong> In traditional word embeddings, each token gets just one static vector - but a single vector can't capture how words relate to each other in different contexts. The word "bank" means something different in "river bank" vs "bank account", but traditional embeddings can't adapt.
</div>
<div class="card">
<!-- Traditional vs Transformer comparison -->
<div style="background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 20px; margin-bottom: 25px;">
<h4 style="color: var(--blue); margin: 0 0 15px;">Traditional Embeddings vs Transformers</h4>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
<div style="padding: 15px; background: rgba(239,68,68,.08); border-radius: 8px; border-left: 4px solid var(--red);">
<h5 style="color: var(--red); margin: 0 0 10px;">Traditional: Single Vector</h5>
<div style="display: flex; align-items: center; gap: 10px; margin: 10px 0;">
<span class="chip" style="font-size: 12px;">cat</span>
<span style="color: var(--muted);">→</span>
<div style="background: white; border: 1px solid var(--border); padding: 8px; border-radius: 6px; font-family: monospace; font-size: 11px;">
[0.2, -0.5, 0.8]
</div>
</div>
<p style="margin: 10px 0 0; font-size: 12px; color: var(--muted);">
Same vector regardless of context.<br>
Can't ask questions or provide answers.
</p>
</div>
<div style="padding: 15px; background: rgba(25,135,84,.08); border-radius: 8px; border-left: 4px solid var(--emerald);">
<h5 style="color: var(--emerald); margin: 0 0 10px;">Transformers: Three Vectors</h5>
<div style="display: flex; flex-direction: column; gap: 5px; margin: 10px 0;">
<div style="display: flex; align-items: center; gap: 10px;">
<span class="chip" style="font-size: 12px;">cat</span>
<span style="color: var(--muted);">→</span>
<div style="display: flex; flex-direction: column; gap: 3px;">
<div style="background: rgba(239,68,68,.1); border: 1px solid rgba(239,68,68,.3); padding: 4px 8px; border-radius: 4px; font-family: monospace; font-size: 10px;">
Q: [0.1, 0.9, -0.2]
</div>
<div style="background: rgba(34,197,94,.1); border: 1px solid rgba(34,197,94,.3); padding: 4px 8px; border-radius: 4px; font-family: monospace; font-size: 10px;">
K: [0.8, -0.3, 0.5]
</div>
<div style="background: rgba(59,130,246,.1); border: 1px solid rgba(59,130,246,.3); padding: 4px 8px; border-radius: 4px; font-family: monospace; font-size: 10px;">
V: [0.3, 0.6, -0.1]
</div>
</div>
</div>
</div>
<p style="margin: 10px 0 0; font-size: 12px; color: var(--muted);">
Dynamic representations for each role.<br>
Can query, be queried, and provide values.
</p>
</div>
</div>
</div>
<h4 style="color: var(--blue); margin-bottom: 15px;">How Q, K, V Are Created</h4>
<p>Each word's embedding is transformed by three learned matrices to create these specialized representations:</p>
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 15px; margin: 20px 0;">
<div style="text-align: center; padding: 15px; background: rgba(239,68,68,.08); border-radius: 8px; border: 1px solid rgba(239,68,68,.3);">
<div class="chip query" style="margin-bottom: 10px;">Query (Q)</div>
<p style="font-size: 13px; color: var(--muted); margin: 8px 0;"><strong>W<sub>Q</sub></strong> matrix</p>
<p style="font-size: 12px; margin: 0;">"What am I looking for?"</p>
</div>
<div style="text-align: center; padding: 15px; background: rgba(34,197,94,.08); border-radius: 8px; border: 1px solid rgba(34,197,94,.3);">
<div class="chip key" style="margin-bottom: 10px;">Key (K)</div>
<p style="font-size: 13px; color: var(--muted); margin: 8px 0;"><strong>W<sub>K</sub></strong> matrix</p>
<p style="font-size: 12px; margin: 0;">"What do I offer?"</p>
</div>
<div style="text-align: center; padding: 15px; background: rgba(59,130,246,.08); border-radius: 8px; border: 1px solid rgba(59,130,246,.3);">
<div class="chip value" style="margin-bottom: 10px;">Value (V)</div>
<p style="font-size: 13px; color: var(--muted); margin: 8px 0;"><strong>W<sub>V</sub></strong> matrix</p>
<p style="font-size: 12px; margin: 0;">"What information do I contain?"</p>
</div>
</div>
<!-- Embedding Values Explanation -->
<div style="background: rgba(255,193,7,.08); border: 1px solid rgba(255,193,7,.3); border-radius: 8px; padding: 15px; margin: 20px 0;">
<h4 style="color: var(--orange); margin: 0 0 12px;">Understanding Embedding Values</h4>
<p style="margin: 0 0 8px; font-size: 13px; line-height: 1.5;">Embeddings are <strong>learned representations</strong> where each dimension captures some learned concept. The numbers are <em>relative strengths</em> - higher means stronger association. In real models, there are 768-12,288 dimensions!</p>
<div style="background: var(--bg); padding: 10px; border-radius: 6px; margin-top: 10px;">
<div style="font-size: 11px; color: var(--muted); margin-bottom: 6px;">Example comparison:</div>
<div style="display: grid; grid-template-columns: auto 1fr 1fr 1fr; gap: 8px; font-size: 11px;">
<div style="font-weight: 600;"></div>
<div style="text-align: center; font-weight: 600;">animal-like</div>
<div style="text-align: center; font-weight: 600;">noun-like</div>
<div style="text-align: center; font-weight: 600;">domestic</div>
<div style="font-weight: 600; background: rgba(255,193,7,.15); padding: 4px 6px; border-radius: 3px; border: 2px solid rgba(255,193,7,.4);">cat</div>
<div style="text-align: center; background: rgba(102,16,242,.15); padding: 2px; border-radius: 3px; border: 2px solid rgba(255,193,7,.4);">0.2</div>
<div style="text-align: center; background: rgba(102,16,242,.25); padding: 2px; border-radius: 3px; border: 2px solid rgba(255,193,7,.4);">0.8</div>
<div style="text-align: center; background: rgba(102,16,242,.15); padding: 2px; border-radius: 3px; border: 2px solid rgba(255,193,7,.4);">0.1</div>
<div style="font-weight: 600;">wolf</div>
<div style="text-align: center; background: rgba(102,16,242,.25); padding: 2px; border-radius: 3px;">0.7</div>
<div style="text-align: center; background: rgba(102,16,242,.25); padding: 2px; border-radius: 3px;">0.6</div>
<div style="text-align: center; background: rgba(102,16,242,.05); padding: 2px; border-radius: 3px;">-0.4</div>
<div style="font-weight: 600;">runs</div>
<div style="text-align: center; background: rgba(102,16,242,.05); padding: 2px; border-radius: 3px;">-0.1</div>
<div style="text-align: center; background: rgba(102,16,242,.05); padding: 2px; border-radius: 3px;">-0.3</div>
<div style="text-align: center; background: rgba(102,16,242,.15); padding: 2px; border-radius: 3px;">0.0</div>
</div>
<div style="font-size: 10px; color: var(--muted); margin-top: 6px;">→ "cat" scores high on noun-like (0.8), "wolf" scores high on animal-like (0.7) and wild (-0.4), "runs" is a verb (low noun score)</div>
</div>
<div style="background: rgba(255,193,7,.12); border-left: 3px solid rgba(255,193,7,.6); padding: 8px 12px; border-radius: 4px; margin-top: 12px;">
<p style="margin: 0; font-size: 12px; color: var(--text);"><strong>↓ We'll use the cat embedding [0.2, 0.8, 0.1] in the transformation example below</strong></p>
</div>
</div>
<div class="goal">
<h3>Matrix Transformation Example</h3>
<p>Let's see how the word "cat" gets transformed (simplified 3D example):</p>
<!-- Matrix Toggle Buttons -->
<div style="display: flex; justify-content: center; gap: 8px; margin: 15px 0;">
<button class="btn matrix-toggle active" data-matrix="query" onclick="showMatrix('query')">
<span style="color: var(--red);">●</span> W<sub>Q</sub> Query
</button>
<button class="btn matrix-toggle" data-matrix="key" onclick="showMatrix('key')">
<span style="color: var(--emerald);">●</span> W<sub>K</sub> Key
</button>
<button class="btn matrix-toggle" data-matrix="value" onclick="showMatrix('value')">
<span style="color: var(--blue);">●</span> W<sub>V</sub> Value
</button>
</div>
<div style="margin: 20px 0; font-family: ui-monospace, monospace; font-size: 13px;">
<!-- Query Matrix (default visible) -->
<div id="matrix-query" class="matrix-demo">
<div style="display: grid; grid-template-columns: 1fr auto 1fr auto 1fr; gap: 10px; align-items: center; margin: 15px 0;">
<!-- Word Embedding -->
<div style="text-align: center;">
<div style="font-weight: bold; margin-bottom: 8px; color: var(--text);">Embedding</div>
<div style="font-size: 11px; color: var(--muted); margin-bottom: 4px;">"cat"</div>
<div style="background: var(--panel); padding: 8px 12px; border-radius: 6px; border: 1px solid var(--border); display: inline-block;">
<div style="padding: 2px 0; font-size: 12px;">0.2</div>
<div style="padding: 2px 0; font-size: 12px;">0.8</div>
<div style="padding: 2px 0; font-size: 12px;">0.1</div>
</div>
<div style="font-size: 10px; color: var(--muted); margin-top: 4px;">3 dimensions</div>
</div>
<!-- × symbol -->
<div style="text-align: center; font-size: 20px; color: var(--blue);">×</div>
<!-- W_Q Matrix -->
<div style="text-align: center;">
<div style="font-weight: bold; margin-bottom: 4px; color: var(--red);">W<sub>Q</sub> (weights)</div>
<div style="font-size: 9px; color: var(--muted); margin-bottom: 6px;">learned, shared</div>
<div style="background: repeating-linear-gradient(45deg, rgba(239,68,68,.06), rgba(239,68,68,.06) 10px, rgba(239,68,68,.12) 10px, rgba(239,68,68,.12) 20px); padding: 8px 12px; border-radius: 6px; border: 2px dashed rgba(239,68,68,.5); display: inline-block;">
<div style="padding: 2px 0; border-left: 3px solid rgba(239,68,68,0.5); padding-left: 6px; margin-bottom: 2px; font-size: 12px;">
<span style="background: rgba(239,68,68,0.15); padding: 1px 3px; border-radius: 2px; font-size: 9px; margin-right: 4px;">row 1</span>[1.0, 0.2, 0.5]
</div>
<div style="padding: 2px 0; padding-left: 6px; margin-bottom: 2px; font-size: 12px; opacity: 0.5;">
<span style="font-size: 9px; margin-right: 4px; opacity: 0.6;">row 2</span>[0.1, 1.5, 0.3]
</div>
<div style="padding: 2px 0; padding-left: 6px; font-size: 12px; opacity: 0.5;">
<span style="font-size: 9px; margin-right: 4px; opacity: 0.6;">row 3</span>[0.4, 0.2, 0.9]
</div>
</div>
<div style="font-size: 10px; color: var(--muted); margin-top: 4px;">3×3 weight matrix</div>
</div>
<!-- = symbol -->
<div style="text-align: center; font-size: 20px; color: var(--blue);">=</div>
<!-- Query Result -->
<div style="text-align: center;">
<div style="font-weight: bold; margin-bottom: 8px; color: var(--red);">Query Output</div>
<div style="font-size: 11px; color: var(--muted); margin-bottom: 4px;">Q<sub>cat</sub></div>
<div style="background: rgba(239,68,68,.15); padding: 8px 12px; border-radius: 6px; border: 1px solid rgba(239,68,68,.4); display: inline-block;">
<div style="padding: 2px 0; color: var(--red); font-weight: bold; font-size: 13px; border-left: 3px solid var(--red); padding-left: 6px; margin-bottom: 2px;">0.41</div>
<div style="padding: 2px 0; color: var(--red); font-size: 12px; padding-left: 6px; margin-bottom: 2px; opacity: 0.5;">1.25</div>
<div style="padding: 2px 0; color: var(--red); font-size: 12px; padding-left: 6px; opacity: 0.5;">0.33</div>
</div>
<div style="font-size: 10px; color: var(--muted); margin-top: 4px;">3 dimensions</div>
</div>
</div>
<div style="text-align: center; color: var(--muted); font-size: 12px; margin: 10px 0;">
The <strong>Query</strong> represents "what this word is looking for" in other words
</div>
</div>
<!-- Key Matrix (hidden by default) -->
<div id="matrix-key" class="matrix-demo" style="display: none;">
<div style="display: grid; grid-template-columns: 1fr auto 1fr auto 1fr; gap: 10px; align-items: center; margin: 15px 0;">
<!-- Word Embedding -->
<div style="text-align: center;">
<div style="font-weight: bold; margin-bottom: 8px; color: var(--text);">Embedding</div>
<div style="font-size: 11px; color: var(--muted); margin-bottom: 4px;">"cat"</div>
<div style="background: var(--panel); padding: 8px 12px; border-radius: 6px; border: 1px solid var(--border); display: inline-block;">
<div style="padding: 2px 0; font-size: 12px;">0.2</div>
<div style="padding: 2px 0; font-size: 12px;">0.8</div>
<div style="padding: 2px 0; font-size: 12px;">0.1</div>
</div>
<div style="font-size: 10px; color: var(--muted); margin-top: 4px;">3 dimensions</div>
</div>
<!-- × symbol -->
<div style="text-align: center; font-size: 20px; color: var(--blue);">×</div>
<!-- W_K Matrix -->
<div style="text-align: center;">
<div style="font-weight: bold; margin-bottom: 4px; color: var(--emerald);">W<sub>K</sub> (weights)</div>
<div style="font-size: 9px; color: var(--muted); margin-bottom: 6px;">learned, shared</div>
<div style="background: repeating-linear-gradient(45deg, rgba(34,197,94,.06), rgba(34,197,94,.06) 10px, rgba(34,197,94,.12) 10px, rgba(34,197,94,.12) 20px); padding: 8px 12px; border-radius: 6px; border: 2px dashed rgba(34,197,94,.5); display: inline-block;">
<div style="padding: 2px 0; border-left: 3px solid rgba(34,197,94,0.6); padding-left: 6px; margin-bottom: 2px; font-size: 12px;">
<span style="background: rgba(34,197,94,0.15); padding: 1px 3px; border-radius: 2px; font-size: 9px; margin-right: 4px;">row 1</span>[0.8, 1.1, 0.2]
</div>
<div style="padding: 2px 0; padding-left: 6px; margin-bottom: 2px; font-size: 12px; opacity: 0.5;">
<span style="font-size: 9px; margin-right: 4px; opacity: 0.6;">row 2</span>[0.3, 0.7, 1.2]
</div>
<div style="padding: 2px 0; padding-left: 6px; font-size: 12px; opacity: 0.5;">
<span style="font-size: 9px; margin-right: 4px; opacity: 0.6;">row 3</span>[1.0, 0.1, 0.6]
</div>
</div>
<div style="font-size: 10px; color: var(--muted); margin-top: 4px;">3×3 weight matrix</div>
</div>
<!-- = symbol -->
<div style="text-align: center; font-size: 20px; color: var(--blue);">=</div>
<!-- Key Result -->
<div style="text-align: center;">
<div style="font-weight: bold; margin-bottom: 8px; color: var(--emerald);">Key Output</div>
<div style="font-size: 11px; color: var(--muted); margin-bottom: 4px;">K<sub>cat</sub></div>
<div style="background: rgba(34,197,94,.15); padding: 8px 12px; border-radius: 6px; border: 1px solid rgba(34,197,94,.4); display: inline-block;">
<div style="padding: 2px 0; color: var(--emerald); font-weight: bold; font-size: 13px; border-left: 3px solid var(--emerald); padding-left: 6px; margin-bottom: 2px;">1.06</div>
<div style="padding: 2px 0; color: var(--emerald); font-size: 12px; padding-left: 6px; margin-bottom: 2px; opacity: 0.5;">0.74</div>
<div style="padding: 2px 0; color: var(--emerald); font-size: 12px; padding-left: 6px; opacity: 0.5;">0.34</div>
</div>
<div style="font-size: 10px; color: var(--muted); margin-top: 4px;">3 dimensions</div>
</div>
</div>
<div style="text-align: center; color: var(--muted); font-size: 12px; margin: 10px 0;">
The <strong>Key</strong> represents "what type of information this word can provide"
</div>
</div>
<!-- Value Matrix (hidden by default) -->
<div id="matrix-value" class="matrix-demo" style="display: none;">
<div style="display: grid; grid-template-columns: 1fr auto 1fr auto 1fr; gap: 10px; align-items: center; margin: 15px 0;">
<!-- Word Embedding -->
<div style="text-align: center;">
<div style="font-weight: bold; margin-bottom: 8px; color: var(--text);">Embedding</div>
<div style="font-size: 11px; color: var(--muted); margin-bottom: 4px;">"cat"</div>
<div style="background: var(--panel); padding: 8px 12px; border-radius: 6px; border: 1px solid var(--border); display: inline-block;">
<div style="padding: 2px 0; font-size: 12px;">0.2</div>
<div style="padding: 2px 0; font-size: 12px;">0.8</div>
<div style="padding: 2px 0; font-size: 12px;">0.1</div>
</div>
<div style="font-size: 10px; color: var(--muted); margin-top: 4px;">3 dimensions</div>
</div>
<!-- × symbol -->
<div style="text-align: center; font-size: 20px; color: var(--blue);">×</div>
<!-- W_V Matrix -->
<div style="text-align: center;">
<div style="font-weight: bold; margin-bottom: 4px; color: var(--blue);">W<sub>V</sub> (weights)</div>
<div style="font-size: 9px; color: var(--muted); margin-bottom: 6px;">learned, shared</div>
<div style="background: repeating-linear-gradient(45deg, rgba(59,130,246,.06), rgba(59,130,246,.06) 10px, rgba(59,130,246,.12) 10px, rgba(59,130,246,.12) 20px); padding: 8px 12px; border-radius: 6px; border: 2px dashed rgba(59,130,246,.5); display: inline-block;">
<div style="padding: 2px 0; border-left: 3px solid rgba(59,130,246,0.6); padding-left: 6px; margin-bottom: 2px; font-size: 12px;">
<span style="background: rgba(59,130,246,0.15); padding: 1px 3px; border-radius: 2px; font-size: 9px; margin-right: 4px;">row 1</span>[0.6, 0.9, 1.1]
</div>
<div style="padding: 2px 0; padding-left: 6px; margin-bottom: 2px; font-size: 12px; opacity: 0.5;">
<span style="font-size: 9px; margin-right: 4px; opacity: 0.6;">row 2</span>[1.3, 0.4, 0.7]
</div>
<div style="padding: 2px 0; padding-left: 6px; font-size: 12px; opacity: 0.5;">
<span style="font-size: 9px; margin-right: 4px; opacity: 0.6;">row 3</span>[0.2, 1.5, 0.3]
</div>
</div>
<div style="font-size: 10px; color: var(--muted); margin-top: 4px;">3×3 weight matrix</div>
</div>
<!-- = symbol -->
<div style="text-align: center; font-size: 20px; color: var(--blue);">=</div>
<!-- Value Result -->
<div style="text-align: center;">
<div style="font-weight: bold; margin-bottom: 8px; color: var(--blue);">Value Output</div>
<div style="font-size: 11px; color: var(--muted); margin-bottom: 4px;">V<sub>cat</sub></div>
<div style="background: rgba(59,130,246,.15); padding: 8px 12px; border-radius: 6px; border: 1px solid rgba(59,130,246,.4); display: inline-block;">
<div style="padding: 2px 0; color: var(--blue); font-weight: bold; font-size: 13px; border-left: 3px solid var(--blue); padding-left: 6px; margin-bottom: 2px;">0.95</div>
<div style="padding: 2px 0; color: var(--blue); font-size: 12px; padding-left: 6px; margin-bottom: 2px; opacity: 0.5;">0.65</div>
<div style="padding: 2px 0; color: var(--blue); font-size: 12px; padding-left: 6px; opacity: 0.5;">1.27</div>
</div>
<div style="font-size: 10px; color: var(--muted); margin-top: 4px;">3 dimensions</div>
</div>
</div>
<div style="text-align: center; color: var(--muted); font-size: 12px; margin: 10px 0;">
The <strong>Value</strong> represents "the actual content/meaning this word contributes"
</div>
</div>
</div>
<!-- Interactive "What's Really Happening" Explanations -->
<div style="margin-top: 20px;">
<!-- Query Explanation (default visible) -->
<div id="explanation-query" class="matrix-demo">
<div style="background: rgba(239,68,68,.08); border: 1px solid rgba(239,68,68,.3); border-radius: 8px; padding: 15px;">
<h4 style="color: var(--red); margin: 0 0 12px;">What is W<sub>Q</sub> Really Doing?</h4>
<p style="margin: 0 0 12px; font-size: 14px;"><strong>W<sub>Q</sub></strong> creates a "search pattern" by recombining embedding dimensions.</p>
<div style="background: rgba(239,68,68,.05); border-left: 3px solid rgba(239,68,68,0.5); padding: 10px 12px; border-radius: 4px; margin: 12px 0;">
<p style="margin: 0 0 8px; font-size: 13px;">
<span style="background: rgba(239,68,68,0.15); padding: 2px 6px; border-radius: 3px; font-size: 10px; margin-right: 6px; font-weight: 600;">row 1</span>
<strong>Row 1 of W<sub>Q</sub></strong> [1.0, 0.2, 0.5] creates the first Query dimension:
</p>
</div>
<div style="background: var(--bg); padding: 12px; border-radius: 6px; margin: 8px 0;">
<div style="margin-bottom: 8px; font-size: 12px; font-family: monospace;">1.0×(0.2) + 0.2×(0.8) + 0.5×(0.1) = <strong style="color: var(--red);">0.41</strong></div>
<div style="display: flex; align-items: center; gap: 8px;">
<div style="flex: 1; height: 24px; background: linear-gradient(90deg, rgba(239,68,68,0.3) 0%, rgba(239,68,68,0.3) 48.8%, rgba(239,68,68,0.2) 48.8%, rgba(239,68,68,0.2) 87.8%, rgba(239,68,68,0.1) 87.8%); border-radius: 4px; border: 1px solid rgba(239,68,68,0.4); position: relative;">
<span style="position: absolute; left: 24%; font-size: 10px; font-weight: bold; color: var(--red);">0.2 (49%)</span>
<span style="position: absolute; left: 68%; font-size: 10px; font-weight: bold; color: var(--red);">0.16 (39%)</span>
<span style="position: absolute; left: 93%; font-size: 10px; font-weight: bold; color: var(--red);">0.05 (12%)</span>
</div>
</div>
<div style="font-size: 11px; color: var(--muted); margin-top: 6px;">→ Creates a "search filter" heavily weighing animalness</div>
</div>
<div style="background: rgba(13,110,253,.08); border-left: 3px solid var(--blue); padding: 10px 12px; border-radius: 4px; margin-top: 12px;">
<p style="margin: 0; font-size: 13px;"><strong>Metaphor:</strong> W<sub>Q</sub> turns "cat" into a magnet <em>asking</em> "I'm looking for animal-related, noun-like words"</p>
</div>
</div>
</div>
<!-- Key Explanation (hidden by default) -->
<div id="explanation-key" class="matrix-demo" style="display: none;">
<div style="background: rgba(34,197,94,.08); border: 1px solid rgba(34,197,94,.3); border-radius: 8px; padding: 15px;">
<h4 style="color: var(--emerald); margin: 0 0 12px;">What is W<sub>K</sub> Really Doing?</h4>
<p style="margin: 0 0 12px; font-size: 14px;"><strong>W<sub>K</sub></strong> creates an "identity tag" by recombining embedding dimensions.</p>
<div style="background: rgba(34,197,94,.05); border-left: 3px solid rgba(34,197,94,0.6); padding: 10px 12px; border-radius: 4px; margin: 12px 0;">
<p style="margin: 0 0 8px; font-size: 13px;">
<span style="background: rgba(34,197,94,0.15); padding: 2px 6px; border-radius: 3px; font-size: 10px; margin-right: 6px; font-weight: 600;">row 1</span>
<strong>Row 1 of W<sub>K</sub></strong> [0.8, 1.1, 0.2] creates the first Key dimension:
</p>
</div>
<div style="background: var(--bg); padding: 12px; border-radius: 6px; margin: 8px 0;">
<div style="margin-bottom: 8px; font-size: 12px; font-family: monospace;">0.8×(0.2) + 1.1×(0.8) + 0.2×(0.1) = <strong style="color: var(--emerald);">1.06</strong></div>
<div style="display: flex; align-items: center; gap: 8px;">
<div style="flex: 1; height: 24px; background: linear-gradient(90deg, rgba(34,197,94,0.2) 0%, rgba(34,197,94,0.2) 15.1%, rgba(34,197,94,0.4) 15.1%, rgba(34,197,94,0.4) 98.1%, rgba(34,197,94,0.15) 98.1%); border-radius: 4px; border: 1px solid rgba(34,197,94,0.4); position: relative;">
<span style="position: absolute; left: 7%; font-size: 10px; font-weight: bold; color: var(--emerald);">0.16 (15%)</span>
<span style="position: absolute; left: 56%; font-size: 10px; font-weight: bold; color: var(--emerald);">0.88 (83%)</span>
<span style="position: absolute; left: 99%; font-size: 10px; font-weight: bold; color: var(--emerald); transform: translateX(-100%);">0.02 (2%)</span>
</div>
</div>
<div style="font-size: 11px; color: var(--muted); margin-top: 6px;">→ Creates an "identity signal" emphasizing noun-ness with some animalness</div>
</div>
<div style="background: rgba(13,110,253,.08); border-left: 3px solid var(--blue); padding: 10px 12px; border-radius: 4px; margin-top: 12px;">
<p style="margin: 0; font-size: 13px;"><strong>Metaphor:</strong> W<sub>K</sub> turns "cat" into a magnet <em>offering</em> "I provide noun-related, animal information"</p>
</div>
</div>
</div>
<!-- Value Explanation (hidden by default) -->
<div id="explanation-value" class="matrix-demo" style="display: none;">
<div style="background: rgba(59,130,246,.08); border: 1px solid rgba(59,130,246,.3); border-radius: 8px; padding: 15px;">
<h4 style="color: var(--blue); margin: 0 0 12px;">What is W<sub>V</sub> Really Doing?</h4>
<p style="margin: 0 0 12px; font-size: 14px;"><strong>W<sub>V</sub></strong> creates the "content package" that will be passed forward.</p>
<div style="background: rgba(59,130,246,.05); border-left: 3px solid rgba(59,130,246,0.6); padding: 10px 12px; border-radius: 4px; margin: 12px 0;">
<p style="margin: 0 0 8px; font-size: 13px;">
<span style="background: rgba(59,130,246,0.15); padding: 2px 6px; border-radius: 3px; font-size: 10px; margin-right: 6px; font-weight: 600;">row 1</span>
<strong>Row 1 of W<sub>V</sub></strong> [0.6, 0.9, 1.1] creates the first Value dimension:
</p>
</div>
<div style="background: var(--bg); padding: 12px; border-radius: 6px; margin: 8px 0;">
<div style="margin-bottom: 8px; font-size: 12px; font-family: monospace;">0.6×(0.2) + 0.9×(0.8) + 1.1×(0.1) = <strong style="color: var(--blue);">0.95</strong></div>
<div style="display: flex; align-items: center; gap: 8px;">
<div style="flex: 1; height: 24px; background: linear-gradient(90deg, rgba(59,130,246,0.2) 0%, rgba(59,130,246,0.2) 12.6%, rgba(59,130,246,0.35) 12.6%, rgba(59,130,246,0.35) 88.4%, rgba(59,130,246,0.25) 88.4%); border-radius: 4px; border: 1px solid rgba(59,130,246,0.4); position: relative;">
<span style="position: absolute; left: 6%; font-size: 10px; font-weight: bold; color: var(--blue);">0.12 (13%)</span>
<span style="position: absolute; left: 50%; font-size: 10px; font-weight: bold; color: var(--blue);">0.72 (76%)</span>
<span style="position: absolute; left: 94%; font-size: 10px; font-weight: bold; color: var(--blue);">0.11 (11%)</span>
</div>
</div>
<div style="font-size: 11px; color: var(--muted); margin-top: 6px;">→ Packages a balanced blend of all features for downstream layers</div>
</div>
<div style="background: rgba(13,110,253,.08); border-left: 3px solid var(--blue); padding: 10px 12px; border-radius: 4px; margin-top: 12px;">
<p style="margin: 0; font-size: 13px;"><strong>Metaphor:</strong> W<sub>V</sub> packages "cat's" actual semantic content to be delivered based on attention weights</p>
</div>
</div>
</div>
</div>
</div>
<div class="goal" style="margin-top: 20px;">
<h3>What Happens Next: Q · K<sup>T</sup></h3>
<p style="margin: 0 0 15px;">Now that we have Query, Key, and Value vectors for each word, we use them to calculate <strong>attention scores</strong> through dot products.</p>
<div style="background: var(--bg); border-radius: 8px; padding: 15px; margin: 15px 0;">
<h4 style="color: var(--blue); margin: 0 0 12px; font-size: 15px;">The Attention Score Formula</h4>
<div style="background: rgba(13,110,253,.08); padding: 12px; border-radius: 6px; font-family: monospace; font-size: 14px; text-align: center; border: 1px solid rgba(13,110,253,.3);">
Attention(Q, K, V) = softmax(Q · K<sup>T</sup> / √d<sub>k</sub>) · V
</div>
<p style="margin: 12px 0 0; font-size: 13px; color: var(--muted);">where d<sub>k</sub> is the dimension of the key vectors (scaling factor)</p>
</div>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin: 15px 0;">
<div style="background: rgba(239,68,68,.08); border: 1px solid rgba(239,68,68,.3); border-radius: 8px; padding: 12px;">
<h5 style="color: var(--red); margin: 0 0 8px; font-size: 14px;">Step 1: Q · K<sup>T</sup></h5>
<p style="margin: 0; font-size: 12px;">Compare "cat's" Query with all Keys via dot product:</p>
<div style="background: var(--bg); padding: 8px; border-radius: 4px; margin-top: 8px; font-size: 11px; font-family: monospace;">
Q<sub>cat</sub> · K<sub>the</sub> = low score<br>
Q<sub>cat</sub> · K<sub>black</sub> = <strong>high score</strong><br>
Q<sub>cat</sub> · K<sub>cat</sub> = <strong>very high score</strong><br>
Q<sub>cat</sub> · K<sub>ate</sub> = medium score<br>
Q<sub>cat</sub> · K<sub>fish</sub> = medium score
</div>
<p style="margin: 8px 0 0; font-size: 11px; color: var(--muted);">Higher dot product = better match between what "cat" is looking for and what that word offers</p>
</div>
<div style="background: rgba(59,130,246,.08); border: 1px solid rgba(59,130,246,.3); border-radius: 8px; padding: 12px;">
<h5 style="color: var(--blue); margin: 0 0 8px; font-size: 14px;">Step 2: Softmax → Weights</h5>
<p style="margin: 0; font-size: 12px;">Convert scores to probabilities (sum to 1):</p>
<div style="background: var(--bg); padding: 8px; border-radius: 4px; margin-top: 8px; font-size: 11px;">
<div style="margin-bottom: 4px;">the: 10% <div style="height: 6px; width: 10%; background: rgba(13,110,253,0.3); border-radius: 3px;"></div></div>
<div style="margin-bottom: 4px;">black: <strong>45%</strong> <div style="height: 6px; width: 45%; background: rgba(13,110,253,0.6); border-radius: 3px;"></div></div>
<div style="margin-bottom: 4px;">cat: <strong>30%</strong> <div style="height: 6px; width: 30%; background: rgba(13,110,253,0.5); border-radius: 3px;"></div></div>
<div style="margin-bottom: 4px;">ate: 10% <div style="height: 6px; width: 10%; background: rgba(13,110,253,0.3); border-radius: 3px;"></div></div>
<div>fish: 5% <div style="height: 6px; width: 5%; background: rgba(13,110,253,0.2); border-radius: 3px;"></div></div>
</div>
<p style="margin: 8px 0 0; font-size: 11px; color: var(--muted);">These are the attention weights!</p>
</div>
</div>
<div style="background: rgba(25,135,84,.08); border: 1px solid rgba(25,135,84,.3); border-radius: 8px; padding: 12px; margin-top: 15px;">
<h5 style="color: var(--emerald); margin: 0 0 8px; font-size: 14px;">Step 3: Weighted Sum of Values</h5>
<p style="margin: 0; font-size: 12px;">Multiply each word's Value by its attention weight and sum:</p>
<div style="background: var(--bg); padding: 10px; border-radius: 6px; margin-top: 8px; font-family: monospace; font-size: 12px; text-align: center;">
Output<sub>cat</sub> = 0.10×V<sub>the</sub> + 0.45×V<sub>black</sub> + 0.30×V<sub>cat</sub> + 0.10×V<sub>ate</sub> + 0.05×V<sub>fish</sub>
</div>
<p style="margin: 8px 0 0; font-size: 11px; color: var(--muted);">The output for "cat" is now enriched with information from "black" (its most attended word) and other context!</p>
</div>
<div style="background: rgba(102,16,242,.08); border-left: 3px solid var(--indigo); padding: 10px 12px; border-radius: 4px; margin-top: 15px;">
<p style="margin: 0; font-size: 13px;"><strong>Why This Works:</strong> The Query-Key dot product is high when both vectors point in similar directions - meaning "cat's" query matches well with "black's" key. This automatically determines which words provide the most relevant context!</p>
</div>
</div>
</div>
<button class="btn btn-primary" onclick="showCalculator()">Next: Try the Live Calculator! →</button>
</div>
<!-- Step 5: Live Attention Calculator -->
<div class="stage hidden" id="calculator">
<h2><span class="step-number">5</span>Live Attention Calculator</h2>
<div class="explanation">
<strong>The Real Mechanism:</strong> Attention scores come from dot products between Query and Key vectors. Let's see how changing the Query or Key vectors affects attention!
</div>
<div class="card">
<!-- Show Current Vectors -->
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin: 20px 0;">
<div style="padding: 15px; background: rgba(239,68,68,.08); border-radius: 8px; border: 1px solid rgba(239,68,68,.3);">
<h4 style="color: var(--red); margin: 0 0 10px;">Query vector for "cat"</h4>
<div style="font-family: ui-monospace, monospace; font-size: 13px;">
Q_cat = [<span id="q-cat-0">0.41</span>, <span id="q-cat-1">1.25</span>, <span id="q-cat-2">0.33</span>]
</div>
<div style="margin-top: 10px;">
<label style="display: block; font-size: 12px; margin-bottom: 5px;">Adjust Q_cat[0]:</label>
<input type="range" min="-2" max="2" step="0.01" value="0.41"
oninput="updateQueryValue(0, this.value)"
style="width: 100%;">
</div>
<div style="margin-top: 8px;">
<label style="display: block; font-size: 12px; margin-bottom: 5px;">Adjust Q_cat[1]:</label>
<input type="range" min="-2" max="2" step="0.01" value="1.25"
oninput="updateQueryValue(1, this.value)"
style="width: 100%;">
</div>
<div style="margin-top: 8px;">
<label style="display: block; font-size: 12px; margin-bottom: 5px;">Adjust Q_cat[2]:</label>
<input type="range" min="-2" max="2" step="0.01" value="0.33"
oninput="updateQueryValue(2, this.value)"
style="width: 100%;">
</div>
<div style="margin-top: 12px; padding: 10px; background: rgba(255,193,7,.1); border-radius: 6px; border-left: 3px solid var(--orange);">
<p style="margin: 0; font-size: 12px; color: var(--text);"><strong>What's happening:</strong> When you adjust Q_cat values, you're changing what "cat" is "looking for". Higher values in dimensions where Key vectors also have high values = stronger attention!</p>
</div>
</div>
<div style="padding: 15px; background: rgba(34,197,94,.08); border-radius: 8px; border: 1px solid rgba(34,197,94,.3);">
<h4 style="color: var(--emerald); margin: 0 0 10px;">Key vectors (fixed)</h4>
<div style="font-family: ui-monospace, monospace; font-size: 12px;">
K_the = [0.1, 0.2, 0.1]<br>
K_black = [0.8, 1.0, 0.3]<br>
K_cat = [1.06, 0.74, 0.34]<br>
K_ate = [0.4, 0.6, 0.8]<br>
K_fish = [0.2, 0.3, 0.9]
</div>
</div>
</div>
<div class="goal">
<h3>Live Dot Product Calculation</h3>
<div id="dot-products" style="margin: 15px 0; padding: 15px; background: var(--bg); border-radius: 8px; border: 1px solid var(--border);">
<div style="font-family: ui-monospace, monospace; font-size: 13px;" id="calculations">
<!-- Will be populated by JavaScript -->
</div>
</div>
<div style="background: rgba(59,130,246,.08); border: 1px solid rgba(59,130,246,.3); border-radius: 8px; padding: 12px; margin-top: 10px;">
<p style="margin: 0; font-size: 12px; color: var(--text);"><strong>Example:</strong> If you increase Q_cat[1] to match K_black[1] (both = 1.0), "cat" will pay more attention to "black" because they align in that dimension. This is how transformers learn semantic relationships!</p>
</div>
</div>