-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembeddings.html
More file actions
2224 lines (1915 loc) · 99 KB
/
embeddings.html
File metadata and controls
2224 lines (1915 loc) · 99 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 - Word Embeddings Tutorial</title>
<style>
:root{
--bg:#ffffff;
--panel:#f8f9fa;
--muted:#6c757d;
--text:#212529;
--blue:#0d6efd;
--indigo:#6610f2;
--emerald:#198754;
--orange:#fd7e14;
--border:#dee2e6;
}
*{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;}
.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;}
.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);}
p.lead{color:var(--muted);margin:0 0 24px; font-size: 18px; text-align: center;}
.grid{display:grid;gap:16px; margin-bottom: 24px;}
@media (min-width:900px){ .grid.cols-4{grid-template-columns:repeat(4,1fr)} }
.card{
background:var(--panel); border:1px solid var(--border); border-radius:16px; padding:18px;
box-shadow:0 2px 8px rgba(0,0,0,.1);
}
.title{font-weight:600;font-size:15px;margin-bottom:12px;opacity:.95}
.sub{font-size:13px;color:var(--muted);margin-top:8px; line-height: 1.4;}
select, input[type=range], input[type=number]{
width:100%; background:var(--bg); border:1px solid var(--border); color:var(--text);
border-radius:10px; padding:10px; outline:none; font-size: 14px;
}
.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);}
.stage .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);
}
.stage .note{font-size:13px;color:var(--muted); margin-bottom:12px}
.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;
}
.chip.ctx{ background:rgba(13,110,253,.15); border-color:rgba(13,110,253,.4); color:var(--blue) }
.chip.lbl{ background:rgba(102,16,242,.18); border-color:rgba(102,16,242,.45); color:var(--indigo) }
.chip.neutral{ background:rgba(142,161,184,.1); border-color:rgba(142,161,184,.3); color:var(--muted) }
.onehot{height:12px;background:var(--panel);border-radius:999px;position:relative;overflow:hidden;box-shadow:inset 0 0 0 1px var(--border); margin: 4px 0;}
.onehot span{position:absolute;top:0;bottom:0;background:var(--blue);border-right:1px solid rgba(0,0,0,.3); transition: all 0.3s ease;}
.vec{
font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas; font-size:13px;
padding:10px 12px; border-radius:12px; background:var(--panel); border:1px solid var(--border);
display:inline-block; margin: 4px 8px 4px 0;
}
.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;}
.btn:hover{background:var(--panel); transform: translateY(-1px);}
.btn-primary{background:var(--blue);color:white;border-color:var(--blue);}
.btn-primary:hover{background:#0b5ed7;border-color:#0b5ed7;}
.bars{display:flex;flex-direction:column;gap:8px; margin-top: 16px;}
.bar{display:flex;align-items:center;gap:12px;background:var(--bg);border-radius:12px;border:1px solid var(--border);padding:10px; transition: all 0.3s ease;}
.bar:hover{background:var(--panel);}
.bar .label{width:100px;font-size:13px;color:var(--muted); font-weight: 500;}
.bar .track{flex:1;height:12px;background:var(--panel);border-radius:999px;overflow:hidden;position:relative}
.bar .fill{height:12px;background:linear-gradient(90deg, var(--emerald), var(--blue)); transition: width 0.5s ease;}
.bar .pct{width:70px;text-align:right;font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas; font-size:13px;color:var(--blue); font-weight: 500;}
.kpi{display:flex;gap:16px;flex-wrap:wrap;margin-top:16px; justify-content: center;}
.tag{font-size:14px;color:var(--muted); padding: 8px 12px; background: var(--panel); border-radius: 8px;}
.tag b{color:var(--text)}
.pill{display:inline-block;padding:6px 10px;border-radius:999px;font-size:13px;border:1px solid var(--border); font-weight: 500;}
.pill.pred{background:rgba(25,135,84,.15); color:var(--emerald); border-color:rgba(25,135,84,.45)}
.pill.lbl{background:rgba(102,16,242,.16); color:var(--indigo); border-color:rgba(102,16,242,.45)}
.matrix{overflow:auto;max-height:280px;border-radius:12px;border:1px solid var(--border)}
table{width:100%;border-collapse:collapse;font-size:12px}
th, td{padding:8px 10px;border-bottom:1px solid var(--border)}
th{position:sticky;top:0;background:var(--panel);z-index:1;color:var(--muted);text-align:right; font-weight: 600;}
th:first-child, td:first-child{text-align:left}
th:nth-child(2), td:nth-child(2){text-align:center}
tr.ctxrow td{background:rgba(13,110,253,.08)}
input.cell{width:65px;background:var(--bg);color:var(--text);border:1px solid var(--border);border-radius:6px;padding:4px 6px;text-align:right; font-size: 11px;}
.footer{margin-top:24px;color:var(--muted);font-size:13px; text-align: center; padding: 16px;}
.analogy{
background:rgba(102,16,242,.08); border:1px solid rgba(102,16,242,.3);
border-radius:12px; padding:16px; margin:16px 0;
}
.analogy h4{margin:0 0 8px; color:var(--indigo); font-size: 14px;}
.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;
}
/* 2D Embedding Visualization */
.embedding-visualization{
width:100%; height:400px; margin:20px 0;
border:1px solid var(--border); border-radius:12px;
background:var(--panel); position:relative; overflow:hidden;
}
.embedding-plot-svg{width:100%; height:100%;}
.word-point{pointer-events:none;}
.word-label{font-size:13px; font-weight:500; pointer-events:none; user-select:none;}
.cluster-animals{color:#22c55e;} /* Green for animals */
.cluster-royalty{color:#8b5cf6;} /* Purple for royalty */
.cluster-cities{color:#3b82f6;} /* Blue for cities */
.cluster-actions{color:#f59e0b;} /* Orange for actions */
.cluster-descriptors{color:#ef4444;} /* Red for descriptors */
.plot-legend{
position:absolute; top:10px; right:10px;
background:var(--bg); border:1px solid var(--border);
border-radius:6px; padding:8px; font-size:11px;
}
.legend-item{display:flex; align-items:center; gap:6px; margin-bottom:4px;}
.legend-dot{width:8px; height:8px; border-radius:50%;}
.plot-axes{stroke:var(--border); stroke-width:1; opacity:0.5;}
.plot-grid{stroke:var(--border); stroke-width:0.5; opacity:0.3;}
/* Enhanced prediction explanation */
#prediction-explanation{background:rgba(255,193,7,.05); border-color:rgba(255,193,7,.2);}
#prediction-reasoning{font-size:14px; line-height:1.5;}
/* Training progress styles */
.training-progress{background:var(--panel); border-radius:8px; padding:12px; margin:8px 0; border:1px solid var(--border);}
.loss-chart{height:60px; background:var(--bg); border-radius:6px; position:relative; overflow:hidden; margin:8px 0; border:1px solid var(--border);}
.loss-line{position:absolute; bottom:0; width:2px; background:var(--blue); transition:all 0.3s;}
.training-btn{margin:4px; padding:8px 12px; border:1px solid var(--border); border-radius:8px; background:var(--bg); cursor:pointer; font-size:13px;}
.training-btn:hover{background:var(--panel);}
.training-btn:disabled{opacity:0.6; cursor:not-allowed;}
.embedding-diff{font-size:12px; color:var(--muted); margin-top:4px;}
.similarity-change{padding:4px 8px; border-radius:4px; font-size:11px; font-weight:500; margin:2px;}
.similarity-change.improved{background:rgba(25,135,84,.1); color:var(--emerald);}
.similarity-change.worsened{background:rgba(220,53,69,.1); color:var(--red);}
/* Training data display */
.training-data-grid{display:flex; flex-direction:column; gap:8px; margin:16px 0;}
.training-sentence{display:flex; align-items:center; padding:12px; background:var(--panel); border-radius:8px; border:1px solid var(--border);}
.sentence-number{width:20px; height:20px; border-radius:50%; background:var(--blue); color:white; display:flex; align-items:center; justify-content:center; font-size:12px; font-weight:bold; margin-right:12px; flex-shrink:0;}
.sentence-text{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas; font-size:15px; color:var(--text);}
.word-token{padding:2px 6px; margin:0 2px; border-radius:4px; background:var(--bg); border:1px solid var(--border);}
.vocab-summary{margin-top:12px; padding:12px; background:rgba(13,110,253,.05); border-radius:8px; border:1px solid rgba(13,110,253,.2);}
.vocab-list{display:flex; flex-wrap:wrap; gap:4px; margin-top:8px;}
.vocab-word{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas; font-size:12px; padding:4px 8px; background:var(--bg); border:1px solid var(--border); border-radius:4px;}
/* Animation for success message */
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
/* Real Embeddings Matrix Styles */
.dimension-btn {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border-radius: 12px;
border: 1px solid var(--border);
background: var(--bg);
color: var(--text);
cursor: pointer;
font-size: 14px;
transition: all 0.3s ease;
text-decoration: none;
}
.dimension-btn:hover {
background: var(--panel);
transform: translateY(-1px);
}
.dimension-btn.btn-primary {
background: var(--blue);
color: white;
border-color: var(--blue);
}
.dimension-btn.btn-primary:hover {
background: #0b5ed7;
border-color: #0b5ed7;
}
.embeddings-matrix {
display: table;
border-collapse: collapse;
width: 100%;
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas;
font-size: 12px;
}
.embeddings-row {
display: table-row;
}
.word-label {
display: table-cell;
padding: 8px 12px;
font-weight: bold;
background: var(--panel);
border: 1px solid var(--border);
vertical-align: middle;
min-width: 80px;
}
.embedding-cell {
display: table-cell;
width: 20px;
height: 32px;
border: 1px solid #f0f0f0;
transition: all 0.3s ease;
cursor: pointer;
position: relative;
}
.embedding-cell:hover {
border-color: var(--blue);
z-index: 10;
}
.embedding-cell.highlighted {
border: 2px solid var(--blue) !important;
box-shadow: 0 0 8px rgba(13, 110, 253, 0.3);
z-index: 5;
}
.dimension-header {
display: table-cell;
padding: 4px;
background: var(--panel);
border: 1px solid var(--border);
text-align: center;
font-size: 10px;
color: var(--muted);
writing-mode: vertical-lr;
text-orientation: mixed;
min-height: 60px;
}
</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>How Computers Learn Word Meanings</h1>
<p class="lead">Interactive demonstration of word embeddings and context-based learning</p>
<!-- NEW: Interactive Coordinate Exercise -->
<div class="intro" id="coordinate-exercise">
<h2>Human Embeddings</h2>
<p>Before we dive into complex models, let's try something simpler. Look at this plot and tell me: where are "chicken" and "king"?</p>
<div style="display: flex; gap: 20px; margin: 20px 0; flex-wrap: wrap;">
<!-- Left: Input Panel -->
<div style="flex: 1; min-width: 300px;">
<h3 style="color: var(--blue); margin-bottom: 12px;">What are the X₁ and X₂ coordinates?</h3>
<p style="font-size: 14px; color: var(--muted); margin-bottom: 16px;">
Look at the plot on the right. Where are "chicken" and "king" on this coordinate system?
</p>
<div style="background: var(--panel); padding: 16px; border-radius: 8px; margin-bottom: 12px;">
<div style="margin-bottom: 12px;">
<label style="font-weight: 600; color: var(--text); display: block; margin-bottom: 4px;">chicken</label>
<div style="display: flex; gap: 8px; align-items: center;">
<label style="font-size: 14px; width: 30px;">X₁:</label>
<input type="number" id="chicken-x" min="0" max="6" step="0.1" placeholder="3.5" style="width: 80px; padding: 6px;">
<label style="font-size: 14px; width: 30px; margin-left: 8px;">X₂:</label>
<input type="number" id="chicken-y" min="0" max="6" step="0.1" placeholder="2.0" style="width: 80px; padding: 6px;">
</div>
</div>
<div>
<label style="font-weight: 600; color: var(--text); display: block; margin-bottom: 4px;">king</label>
<div style="display: flex; gap: 8px; align-items: center;">
<label style="font-size: 14px; width: 30px;">X₁:</label>
<input type="number" id="king-x" min="0" max="6" step="0.1" placeholder="1.0" style="width: 80px; padding: 6px;">
<label style="font-size: 14px; width: 30px; margin-left: 8px;">X₂:</label>
<input type="number" id="king-y" min="0" max="6" step="0.1" placeholder="5.0" style="width: 80px; padding: 6px;">
</div>
</div>
</div>
<button class="btn btn-primary" onclick="checkCoordinates()" style="width: 100%;">Check My Answer</button>
<div id="coordinate-feedback" style="margin-top: 12px; padding: 12px; border-radius: 8px; display: none;"></div>
</div>
<!-- Right: Plot -->
<div style="flex: 1; min-width: 300px;">
<h3 style="color: var(--blue); margin-bottom: 12px;">Word Embedding Space</h3>
<div style="position: relative; background: white; border: 2px solid var(--border); border-radius: 8px; aspect-ratio: 1;">
<svg id="coordinate-plot" viewBox="0 0 400 400" style="width: 100%; height: 100%;">
<!-- Grid lines -->
<g id="grid-lines" stroke="#e5e7eb" stroke-width="0.5">
<!-- Vertical lines -->
<line x1="67" y1="0" x2="67" y2="400" />
<line x1="133" y1="0" x2="133" y2="400" />
<line x1="200" y1="0" x2="200" y2="400" />
<line x1="267" y1="0" x2="267" y2="400" />
<line x1="333" y1="0" x2="333" y2="400" />
<!-- Horizontal lines -->
<line x1="0" y1="67" x2="400" y2="67" />
<line x1="0" y1="133" x2="400" y2="133" />
<line x1="0" y1="200" x2="400" y2="200" />
<line x1="0" y1="267" x2="400" y2="267" />
<line x1="0" y1="333" x2="400" y2="333" />
</g>
<!-- Axes -->
<line x1="0" y1="400" x2="400" y2="400" stroke="#374151" stroke-width="2" />
<line x1="0" y1="0" x2="0" y2="400" stroke="#374151" stroke-width="2" />
<!-- Axis labels -->
<text x="380" y="395" text-anchor="middle" font-size="14" font-weight="bold">X₁</text>
<text x="10" y="15" text-anchor="start" font-size="14" font-weight="bold">X₂</text>
<!-- Tick labels -->
<text x="67" y="395" text-anchor="middle" font-size="10" fill="#6b7280">1.0</text>
<text x="200" y="395" text-anchor="middle" font-size="10" fill="#6b7280">3.5</text>
<text x="333" y="395" text-anchor="middle" font-size="10" fill="#6b7280">5.5</text>
<text x="5" y="340" text-anchor="start" font-size="10" fill="#6b7280">2.0</text>
<text x="5" y="207" text-anchor="start" font-size="10" fill="#6b7280">3.0</text>
<text x="5" y="74" text-anchor="start" font-size="10" fill="#6b7280">5.0</text>
<!-- Pre-plotted words with labels -->
<g id="existing-words">
<!-- Top row: king (1.0, 5.0), monarch (3.5, 5.0), queen (5.5, 5.0) -->
<circle cx="67" cy="67" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="67" y="57" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">king</text>
<circle cx="200" cy="67" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="200" y="57" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">monarch</text>
<circle cx="333" cy="67" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="333" y="57" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">queen</text>
<!-- Middle row: man (1.0, 3.0), person (3.5, 3.0), woman (5.5, 3.0) -->
<circle cx="67" cy="200" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="67" y="190" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">man</text>
<circle cx="200" cy="200" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="200" y="190" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">person</text>
<circle cx="333" cy="200" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="333" y="190" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">woman</text>
<!-- Bottom row: rooster (1.0, 2.0), chicken (3.5, 2.0), hen (5.5, 2.0) -->
<circle cx="67" cy="333" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="67" y="323" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">rooster</text>
<circle cx="200" cy="333" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="200" y="323" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">chicken</text>
<circle cx="333" cy="333" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="333" y="323" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">hen</text>
</g>
<!-- Placeholder circles for user answers (these will be invisible since all words are shown) -->
<g id="user-words" style="display: none;">
<circle id="chicken-circle" r="8" fill="#f59e0b" stroke="white" stroke-width="2" opacity="0.8" />
<text id="chicken-label" text-anchor="middle" font-size="12" font-weight="bold" fill="#f59e0b">chicken</text>
<circle id="king-circle" r="8" fill="#ef4444" stroke="white" stroke-width="2" opacity="0.8" />
<text id="king-label" text-anchor="middle" font-size="12" font-weight="bold" fill="#ef4444">king</text>
</g>
</svg>
</div>
</div>
</div>
<div id="exercise-success" style="display: none; background: rgba(34, 197, 94, 0.1); border: 2px solid #22c55e; border-radius: 12px; padding: 20px; margin-top: 20px;">
<h3 style="color: #22c55e; margin: 0 0 8px;">Congratulations, you just did text embeddings!</h3>
<p style="font-size: 16px; margin: 8px 0;">You just converted words into numbers! <strong>chicken = (3.5, 2.0)</strong> and <strong>king = (1.0, 5.0)</strong>.</p>
<p style="font-size: 14px; color: var(--muted); margin: 8px 0;">These coordinate pairs are called "embeddings" - they capture word meaning as numbers that computers can work with. Notice how similar words are close together in this space.</p>
<button class="btn btn-primary" onclick="showDimensions()" style="margin-top: 12px;">Next: Understanding the Dimensions →</button>
</div>
</div>
<div class="intro" id="dimensions-explanation" style="display: none;">
<h2>Understanding Meaning Dimensions</h2>
<p>Each coordinate axis represents a <strong>meaning dimension</strong> - a continuous feature that tells us something about the word's meaning.</p>
<div style="display: flex; gap: 20px; margin: 20px 0; flex-wrap: wrap;">
<!-- Left: Text explanation -->
<div style="flex: 1; min-width: 300px;">
<div style="background: var(--panel); padding: 20px; border-radius: 12px; margin-bottom: 16px;">
<h3 style="color: var(--blue); margin: 0 0 16px;">We want to represent words with a <em>number of continuous features</em> which <strong>tell us something about the meaning of the words</strong></h3>
<p style="margin: 16px 0; font-size: 18px; font-style: italic;">i.e. an embedding vector</p>
</div>
<div style="background: rgba(59, 130, 246, 0.1); border-left: 4px solid #3b82f6; padding: 16px; border-radius: 8px; margin-bottom: 16px;">
<h4 style="color: #3b82f6; margin: 0 0 12px; font-size: 16px;">X₂ Axis: Power/Status Dimension</h4>
<div style="display: flex; align-items: center; gap: 8px; margin: 8px 0;">
<div style="color: #3b82f6; font-weight: bold;">↑</div>
<span style="color: #3b82f6; font-weight: 600;">More power</span>
<span style="color: var(--muted); font-size: 14px;">(royalty: king, queen, monarch)</span>
</div>
<div style="margin: 8px 0; padding-left: 24px;">
<span style="color: var(--muted);">Legal importance dimension?</span>
</div>
<div style="display: flex; align-items: center; gap: 8px; margin: 8px 0;">
<div style="color: #3b82f6; font-weight: bold;">↓</div>
<span style="color: #3b82f6; font-weight: 600;">Less power</span>
<span style="color: var(--muted); font-size: 14px;">(animals: rooster, chicken, hen)</span>
</div>
</div>
<div style="background: rgba(249, 115, 22, 0.1); border-left: 4px solid #f97316; padding: 16px; border-radius: 8px;">
<h4 style="color: #f97316; margin: 0 0 12px; font-size: 16px;">X₁ Axis: Gender Dimension</h4>
<div style="display: flex; align-items: center; gap: 8px; margin: 8px 0;">
<div style="color: #f97316; font-weight: bold;">←</div>
<span style="color: #f97316; font-weight: 600;">More male</span>
<span style="color: var(--muted); font-size: 14px;">(man, king, rooster)</span>
</div>
<div style="display: flex; align-items: center; gap: 8px; margin: 8px 0;">
<div style="color: #f97316; font-weight: bold;">→</div>
<span style="color: #f97316; font-weight: 600;">More female</span>
<span style="color: var(--muted); font-size: 14px;">(woman, queen, hen)</span>
</div>
</div>
</div>
<!-- Right: Same plot as before but with dimension labels -->
<div style="flex: 1; min-width: 300px;">
<div style="position: relative; background: white; border: 2px solid var(--border); border-radius: 8px; aspect-ratio: 1;">
<svg viewBox="0 0 400 400" style="width: 100%; height: 100%;">
<!-- Grid lines -->
<g stroke="#e5e7eb" stroke-width="0.5">
<line x1="67" y1="0" x2="67" y2="400" />
<line x1="133" y1="0" x2="133" y2="400" />
<line x1="200" y1="0" x2="200" y2="400" />
<line x1="267" y1="0" x2="267" y2="400" />
<line x1="333" y1="0" x2="333" y2="400" />
<line x1="0" y1="67" x2="400" y2="67" />
<line x1="0" y1="133" x2="400" y2="133" />
<line x1="0" y1="200" x2="400" y2="200" />
<line x1="0" y1="267" x2="400" y2="267" />
<line x1="0" y1="333" x2="400" y2="333" />
</g>
<!-- Axes -->
<line x1="0" y1="400" x2="400" y2="400" stroke="#374151" stroke-width="2" />
<line x1="0" y1="0" x2="0" y2="400" stroke="#374151" stroke-width="2" />
<!-- Dimension arrows and labels -->
<!-- Power dimension (vertical) -->
<line x1="25" y1="340" x2="25" y2="30" stroke="#3b82f6" stroke-width="3" marker-end="url(#arrowhead-blue)" />
<text x="30" y="20" font-size="10" font-weight="bold" fill="#3b82f6">More power</text>
<text x="30" y="355" font-size="10" font-weight="bold" fill="#3b82f6">Less power</text>
<!-- Gender dimension (horizontal) -->
<line x1="50" y1="370" x2="350" y2="370" stroke="#f97316" stroke-width="3" marker-end="url(#arrowhead-orange)" />
<text x="60" y="385" font-size="10" font-weight="bold" fill="#f97316">More male</text>
<text x="285" y="385" font-size="10" font-weight="bold" fill="#f97316">More female</text>
<!-- Arrow markers -->
<defs>
<marker id="arrowhead-blue" markerWidth="10" markerHeight="7" refX="9" refY="3.5" orient="auto">
<polygon points="0 0, 10 3.5, 0 7" fill="#3b82f6" />
</marker>
<marker id="arrowhead-orange" markerWidth="10" markerHeight="7" refX="9" refY="3.5" orient="auto">
<polygon points="0 0, 10 3.5, 0 7" fill="#f97316" />
</marker>
</defs>
<!-- Axis labels -->
<text x="380" y="395" text-anchor="middle" font-size="14" font-weight="bold">X₁</text>
<text x="10" y="15" text-anchor="start" font-size="14" font-weight="bold">X₂</text>
<!-- Tick labels -->
<text x="67" y="395" text-anchor="middle" font-size="10" fill="#6b7280">1.0</text>
<text x="200" y="395" text-anchor="middle" font-size="10" fill="#6b7280">3.5</text>
<text x="333" y="395" text-anchor="middle" font-size="10" fill="#6b7280">5.5</text>
<text x="5" y="340" text-anchor="start" font-size="10" fill="#6b7280">2.0</text>
<text x="5" y="207" text-anchor="start" font-size="10" fill="#6b7280">3.0</text>
<text x="5" y="74" text-anchor="start" font-size="10" fill="#6b7280">5.0</text>
<!-- Words -->
<g>
<!-- Top row -->
<circle cx="67" cy="67" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="67" y="57" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">king</text>
<circle cx="200" cy="67" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="200" y="57" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">monarch</text>
<circle cx="333" cy="67" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="333" y="57" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">queen</text>
<!-- Middle row -->
<circle cx="67" cy="200" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="67" y="190" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">man</text>
<circle cx="200" cy="200" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="200" y="190" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">person</text>
<circle cx="333" cy="200" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="333" y="190" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">woman</text>
<!-- Bottom row -->
<circle cx="67" cy="333" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="67" y="323" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">rooster</text>
<circle cx="200" cy="333" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="200" y="323" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">chicken</text>
<circle cx="333" cy="333" r="8" fill="#22c55e" stroke="white" stroke-width="2" />
<text x="333" y="323" text-anchor="middle" font-size="12" font-weight="bold" fill="#22c55e">hen</text>
</g>
</svg>
</div>
</div>
</div>
<div style="background: rgba(59, 130, 246, 0.08); border: 2px solid #3b82f6; border-radius: 12px; padding: 20px; margin: 20px 0;">
<h3 style="color: #3b82f6; margin: 0 0 12px; font-size: 16px;">Why Start With Just 2 Dimensions?</h3>
<p style="margin: 8px 0; font-size: 14px;">Real word embeddings like those in GPT or Word2Vec use <strong>300+ dimensions</strong> - imagine hundreds of meaning aspects like formality, emotion, concreteness, etc. We start with just 2 dimensions (power and gender) to build your intuition.</p>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin: 16px 0; padding: 16px; background: var(--bg); border-radius: 8px; border: 1px solid var(--border);">
<div style="text-align: center;">
<h4 style="margin: 0 0 8px; font-size: 14px; color: var(--emerald);">Our Teaching Example</h4>
<div style="font-family: ui-monospace, monospace; background: var(--panel); padding: 12px; border-radius: 6px; font-size: 13px; color: var(--text);">
chicken = (3.5, 2.0)
</div>
<p style="font-size: 12px; color: var(--muted); margin: 8px 0 0;">Just 2 numbers: easy to visualize!</p>
</div>
<div style="text-align: center;">
<h4 style="margin: 0 0 8px; font-size: 14px; color: var(--blue);">Real Word2Vec</h4>
<div style="font-family: ui-monospace, monospace; background: var(--panel); padding: 12px; border-radius: 6px; font-size: 11px; color: var(--text); overflow: hidden;">
chicken = [0.23, -0.45, 0.12, 0.67, -0.89, 0.34, -0.12, 0.56, ...]
</div>
<p style="font-size: 12px; color: var(--muted); margin: 8px 0 0;">300 numbers: captures nuanced meaning!</p>
</div>
</div>
<p style="margin: 8px 0; font-size: 14px; color: var(--muted);"><strong>Same principles, different scale!</strong> Whether it's 2 dimensions or 300, the core concept is identical: words become vectors of numbers that capture semantic meaning.</p>
</div>
<div class="analogy">
<h4>Key insight:</h4>
<p>Each dimension captures a different aspect of meaning. Instead of just saying "king is royal", we can say "king has a power value of 5.0 and a gender value of 1.0". This turns fuzzy concepts into precise numbers that computers can calculate with!</p>
</div>
<button class="btn btn-primary" onclick="continueToMain()" style="margin-top: 15px;">Next: Learn How Computers Do This →</button>
</div>
<div class="intro" id="main-intro" style="display: none;">
<h2>What are we learning?</h2>
<p>Now that you've seen how words can be represented as coordinates, let's explore how computers learn these representations automatically! Our teaching example is loosely based on <strong>CBOW</strong> (Continuous Bag of Words), which learns word meanings by predicting a target word from its surrounding context.</p>
<div class="goal">
<h3>Learning Objective: CBOW Model</h3>
<p>Given context words (like "the ___ sat on the"), the computer predicts the missing word ("cat"). By training on this task, words that appear in similar contexts develop similar numerical representations, capturing semantic relationships.</p>
</div>
<div class="analogy">
<h4>Why CBOW works:</h4>
<p>Words that appear in similar contexts tend to have similar meanings. "cat" and "dog" both appear after "the" and before "sat", so they develop similar embeddings. This distributional hypothesis forms the foundation of modern word embeddings.</p>
</div>
<button class="btn btn-primary" onclick="showDataset()" style="margin-top: 20px;">Next: See Our Training Data →</button>
</div>
<div class="stage" id="dataset-stage" style="display: none;">
<h2>Our Training Dataset</h2>
<div class="explanation">
Our CBOW model was pre-trained on thousands of sentences from news articles and books. The full dataset contains rich contexts for animals (cat/dog), rulers (king/queen), cities (Paris/London), and various actions. Below is a sample of the vocabulary learned from this corpus.
</div>
<div id="trainingDataDisplay" class="training-data-grid">
<!-- Will be populated by JavaScript -->
</div>
<div class="analogy">
<h4>Why word IDs matter:</h4>
<p>Computers can't work with words directly - they need numbers. Each word gets a unique ID (like "cat" → 2, "sat" → 3). These IDs are used to look up the word's embedding vector during training and prediction.</p>
</div>
<button class="btn btn-primary" onclick="showControls()" style="margin-top: 15px;">Next: Try the Interactive Demo →</button>
</div>
<div class="grid cols-4" id="controls-stage" style="display: none;">
<div class="card">
<div class="title">Select Training Sentence</div>
<select id="sentence"></select>
<div class="sub">Choose which sentence the computer should learn from.</div>
</div>
<div class="card">
<div class="title">Context Window</div>
<div style="display: flex; gap: 8px; margin-bottom: 8px;">
<button id="winBtn1" class="btn" style="flex: 1; padding: 8px;">Small (±2)</button>
<button id="winBtn2" class="btn" style="flex: 1; padding: 8px;">Medium (±5)</button>
</div>
<div class="sub">Look at <span id="winv">5</span> words on each side of our target word.</div>
</div>
<div class="card">
<div class="title">Target Word Position</div>
<input id="center" type="range" min="0" max="20" value="10"/>
<div class="sub">Which word are we trying to predict?</div>
</div>
<div class="card">
<div class="title">Embedding Dimensions</div>
<input id="dim" type="range" min="2" max="8" value="5"/>
<div class="sub">Each word gets <span id="dimv">5</span> numbers to describe it.</div>
</div>
<button class="btn btn-primary" onclick="showBreakdown()" style="margin-top: 20px;">Next: Break Down the Sentence →</button>
</div>
<div class="stage" id="breakdown-stage" style="display: none;">
<h2><span class="step-number">1</span>Breaking Down the Sentence</h2>
<div class="explanation">
First, we identify our context words (the clues) and our target word (what we're trying to predict). Context words are colored <span class="pill" style="background:rgba(124,200,255,.15);color:#bfe6ff;border-color:rgba(124,200,255,.4)">blue</span>, target word is <span class="pill lbl">purple</span>.
</div>
<div id="tokens"></div>
<div class="analogy">
<h4>Process explanation:</h4>
<p>The model uses context words (blue) to predict the target word (purple) through learned associations.</p>
</div>
<button class="btn btn-primary" onclick="showTraining()" style="margin-top: 20px;">Next: Train the Model →</button>
</div>
<div class="stage" id="training-stage" style="display: none;">
<h2><span class="step-number">2</span>Training the Model</h2>
<div class="explanation">
Each training step improves the embeddings by adjusting them based on prediction errors. Words that appear in similar contexts will gradually develop similar vector representations.
<h4 style="margin:16px 0 8px; color:var(--blue);">What to observe during training:</h4>
<ul style="margin:8px 0; padding-left:20px; font-size:14px;">
<li><strong>Loss decreasing:</strong> The model is getting better at predictions</li>
<li><strong>Similarity changes:</strong> Words in similar contexts become more similar</li>
<li><strong>Embedding evolution:</strong> Watch the numbers in the embedding matrix change</li>
<li><strong>Prediction improvements:</strong> The model should predict the target word with higher confidence</li>
</ul>
</div>
<div class="grid cols-4">
<div class="card">
<div class="title">Training Controls</div>
<button class="btn" id="trainModel" style="font-size:16px; padding:12px 24px;">Train Model</button>
<button class="btn" id="resetTraining" style="margin-top:12px;">Reset to Random</button>
<div class="sub" style="margin-top:8px;">Watch the model learn meaningful word relationships!</div>
<div id="trainingProgress" style="margin-top:12px; display:none;">
<div style="background:var(--border); height:6px; border-radius:3px; overflow:hidden;">
<div id="progressBar" style="background:linear-gradient(90deg, var(--blue), var(--emerald)); height:100%; width:0%; transition:width 0.3s ease;"></div>
</div>
<div id="progressText" style="font-size:12px; color:var(--blue); margin-top:6px; text-align:center; font-weight:500;">Training...</div>
</div>
</div>
<div class="card">
<div class="title">Learning Rate</div>
<input id="learningRate" type="range" min="0.1" max="2.0" step="0.1" value="0.5"/>
<div class="sub">How fast to learn: <span id="lrValue">0.5</span></div>
</div>
<div class="card">
<div class="title">Training Progress</div>
<div class="tag">Step: <b id="trainingStep">0</b></div>
<div class="tag" style="margin-top:4px;">Loss: <b id="currentLoss">-</b></div>
<div class="loss-chart" id="lossChart"></div>
</div>
<div class="card">
<div class="title">Training Status</div>
<div id="trainingStatus" class="sub">Ready to start training</div>
<div id="clusteringStatus" class="sub" style="margin-top:4px; font-size:11px;">Semantic clustering: Not trained</div>
</div>
</div>
<div class="analogy">
<h4>How training works:</h4>
<p>The model starts with random embeddings. When it makes wrong predictions, it adjusts the embeddings slightly. Over many steps, words that appear in similar contexts develop similar embeddings - this is how meaning emerges from statistics!</p>
</div>
<h3 style="margin:24px 0 12px; color:var(--blue); font-size:18px;">Training Results: Watch Embeddings Learn!</h3>
<div class="explanation" style="margin-bottom:16px;">
<strong>Live Training View:</strong> Watch as random numbers transform into meaningful word embeddings! The matrix shows the actual numbers, while the visualization shows how words cluster by meaning.
</div>
<!-- Side-by-side layout for matrix and visualization -->
<div class="training-display" style="display: flex; gap: 20px; margin: 20px 0;">
<div class="matrix-panel" style="flex: 1; min-width: 0;">
<h4 style="margin: 0 0 12px; color: var(--blue);">Word Embedding Matrix</h4>
<p style="font-size: 14px; color: var(--muted); margin-bottom: 12px;">
Each row is a word, each column is a "trait" dimension. Watch these numbers evolve during training!
</p>
<div class="matrix card" id="matrixIn" style="height: 400px; overflow-y: auto; font-size: 12px;"></div>
<div class="footer">
<span id="embeddingStatus" class="sub" style="font-size:12px;">Embeddings: Random</span>
</div>
</div>
<div class="visualization-panel" style="flex: 1; min-width: 0;">
<h4 style="margin: 0 0 12px; color: var(--blue);">2D Semantic Visualization</h4>
<p style="font-size: 14px; color: var(--muted); margin-bottom: 12px;">
Words projected into 2D space. Similar words cluster together as the model learns!
</p>
<p id="dimensionReductionNote" style="font-size: 12px; color: var(--orange); margin-bottom: 12px; display: none;">
Note: Your embeddings have <span id="actualDims"></span> dimensions, but we're showing them in 2D using PCA-like projection to visualize relationships.
</p>
<div id="embedding-plot" class="embedding-visualization" style="height: 400px; position: relative;">
<!-- 2D visualization will be created here -->
</div>
</div>
</div>
<!-- Responsive design for mobile -->
<style>
@media (max-width: 768px) {
.training-display {
flex-direction: column !important;
}
.matrix-panel, .visualization-panel {
flex: none !important;
}
}
</style>
<div class="analogy" style="margin-top: 20px;">
<h4>What you're seeing:</h4>
<p><strong>Left (Matrix):</strong> The raw numbers that represent word meanings. Similar words develop similar number patterns.</p>
<p><strong>Right (Visualization):</strong> The same data projected to 2D space. Watch words move from random positions into semantic clusters during training!</p>
</div>
<button class="btn btn-primary" onclick="showInterpretability()" style="margin-top: 20px;">Next: Interpretability →</button>
</div>
<!-- Interpretability -->
<div class="intro" id="interpretability-stage" style="display: none;">
<h2>Interpretability</h2>
<p>Now let's see what <strong>real word embeddings</strong> actually look like! In practice, embeddings have hundreds of dimensions (300-1024 is common). While most dimensions are hard to interpret, some clearly capture human-understandable concepts like gender, age, or social status.</p>
<div class="goal">
<h3>Key Insight: Emergent Interpretability</h3>
<p>Even though computers learn embeddings without being told what concepts to capture, some dimensions naturally emerge that correspond to meaningful semantic properties. This shows that mathematical word representations can capture real-world knowledge!</p>
</div>
<!-- Real Embeddings Visualization -->
<div class="card" style="margin: 24px 0;">
<div class="title">Interactive Real Embeddings Matrix</div>
<div class="sub" style="margin-bottom: 16px;">Click on dimension buttons to see how different concepts light up across words. Colors represent embedding values: red = high positive, blue = high negative, white = near zero.</div>
<div style="display: flex; gap: 12px; margin-bottom: 16px; flex-wrap: wrap;">
<button class="dimension-btn btn-primary" onclick="highlightDimension('gender')" id="gender-btn">Gender Dimension</button>
<button class="dimension-btn" onclick="highlightDimension('royalty')" id="royalty-btn">Royalty Dimension</button>
<button class="dimension-btn" onclick="highlightDimension('age')" id="age-btn">Age/Maturity Dimension</button>
<button class="dimension-btn" onclick="highlightDimension('none')" id="none-btn">Show All</button>
</div>
<div id="embeddings-container" style="overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; background: white;">
<!-- Will be populated by JavaScript -->
</div>
<div id="dimension-explanation" class="analogy" style="margin-top: 16px; display: none;">
<!-- Will be populated by JavaScript -->
</div>
</div>
<div class="analogy">
<h4>Understanding the Matrix:</h4>
<p><strong>Rows</strong> = Different words | <strong>Columns</strong> = Individual embedding dimensions (300+ total) | <strong>Colors</strong> = Strength and direction of each feature</p>
<p>Notice how certain dimensions consistently activate for semantically similar words - this is how computers learn to group related concepts!</p>
</div>
<div class="goal">
<h3>From Toy Examples to Real AI</h3>
<p>This is exactly how large language models like GPT work internally - they learn hundreds or thousands of interpretable and uninterpretable features that capture all aspects of human language and knowledge. Your 2D/3D understanding scales to these high-dimensional spaces!</p>
</div>
<button class="btn btn-primary" onclick="showSummary()" style="margin-top: 20px;">Next: See Summary →</button>
</div>
<div class="intro" id="summary-stage" style="display: none;">
<h2>Summary</h2>
<p>This demonstration illustrated how computers learn word relationships through predictive modeling:</p>
<ul style="margin: 12px 0; padding-left: 20px;">
<li><strong>Training process</strong>: Random embeddings gradually improve through prediction tasks</li>
<li><strong>Semantic clustering</strong>: Words appearing in similar contexts develop similar numerical representations</li>
<li><strong>Contextual learning</strong>: The training objective forces related words to cluster together</li>
<li><strong>Vector semantics</strong>: Word meanings become mathematical objects that can be manipulated computationally</li>
</ul>
<p>By training on the simple context prediction task, the model discovers meaningful semantic relationships. This approach forms the foundation of modern natural language processing and large language models.</p>
</div>
</div>
<script>
// --------- data ---------
const SENTENCES = [
// Demo sentences - made longer to demonstrate context windows up to ±5
["the","small","brown","cat","sat","quietly","on","the","old","wooden","mat","in","the","cozy","living","room","while","the","family","watched","television"],
["the","big","friendly","dog","ran","quickly","through","the","beautiful","green","garden","chasing","butterflies","and","birds","on","a","warm","summer","afternoon"],
["many","cute","young","animals","love","to","sleep","peacefully","in","warm","sunny","places","during","the","lazy","afternoon","hours","when","everything","is","quiet"],
["wise","ancient","kings","and","queens","ruled","their","vast","countries","with","great","care","and","wisdom","throughout","the","golden","ages","of","human","history"]
];
const VOCAB = ["the","cat","dog","sat","on","mat","rug","cats","dogs","are","pets","king","queen","rules","country","nation","Paris","London","is","a","big","city","small","sleeps","peacefully","kings","queens","rulers","cities","like","beautiful","and","animals","loyal","love","to","sleep","quietly","run","quickly","garden","park","cute","friendly","young","plays","with","toys","wise","leads","his","people","her","royal","live","in","palaces","sits","throne","ancient","were","powerful","learns","rule","make","important","decisions","daily","of","attracts","visitors","has","many","famous","buildings","visit","every","year","major","never","history","she","night","morning","he","walks","slowly","window","they","play","happily","children","deeply","fast","during","adults","calmly","through","streets","comfortably","chairs","move","gracefully","nature","things","often","impressive","tiny","objects","need","careful","handling","large","require","strong","support","little","creatures","very","huge","dominate","skylines","learn","new","easily","old","share","wisdom","others","leaders","good","places","inspire","friends","together","art","all","tell","stories","both","same","citizens","world","brown","wooden","ran","green","warm","sunny","care","ruled","their","countries","great","cozy","living","room","while","family","watched","television","chasing","butterflies","birds","summer","afternoon","lazy","hours","when","everything","quiet","vast","throughout","golden","ages","human"];
const idOf = t => VOCAB.indexOf(t)+1; // 1-based
// --------- utils ---------
const clamp = (n,a,b)=>Math.max(a,Math.min(b,n));
const softmax = arr => {
const m = Math.max(...arr);
const ex = arr.map(v=>Math.exp(v-m));
const s = ex.reduce((a,b)=>a+b,0);
return ex.map(v=>v/(s||1));
};
function average(vecs){
if(!vecs.length) return [];
const d = vecs[0].length;
const out = Array(d).fill(0);
for(const v of vecs){ for(let i=0;i<d;i++) out[i]+=v[i]; }
return out.map(x=>x/vecs.length);
}
function seedMatrix(rows, cols){
let v=1;
const M = Array.from({length:rows},()=>Array.from({length:cols},()=>0));
// Use Xavier/Glorot initialization: scale by sqrt(6/(rows+cols))
const scale = Math.sqrt(6.0 / (rows + cols));
for(let r=0;r<rows;r++){
for(let c=0;c<cols;c++){
v = (v*73+17)%997;
M[r][c] = ((v%11)-5)/5 * scale; // Better initialization for large vocab
}
}
return M;
}
function mulVecMat(vec, mat){ // vec:[d], mat:[d][V]
if(!vec.length) return Array(mat[0]?.length||0).fill(0);
const d = mat.length, V = mat[0].length;
const out = Array(V).fill(0);
for(let j=0;j<V;j++){
let s=0; for(let i=0;i<d;i++) s += vec[i]*mat[i][j];
out[j]=s;
}
return out;
}
// --------- state ---------
let embedDim = 5; // increase to 5 dimensions for larger vocabulary
let W_in = seedMatrix(VOCAB.length, embedDim); // vocab x dim - will be replaced with meaningful embeddings
let W_out = seedMatrix(embedDim, VOCAB.length); // dim x vocab
let sent = 0;
let win = 5; // per side - default to medium window like Word2Vec
let center = 10; // start more in the middle of longer sentences
// Training state
let trainingStep = 0;
let learningRate = 0.5;
let lossHistory = [];
let originalW_in = null;
let originalW_out = null;
let isTraining = false;
let trainedEmbeddings = null;
// Precomputed meaningful embeddings (5-dimensional) that demonstrate semantic relationships
function createMeaningfulEmbeddings() {
const embeddings = {};
// Animal cluster (cats, dogs, pets)
embeddings['cat'] = [0.8, 0.6, -0.2, 0.1, 0.7];
embeddings['dog'] = [0.7, 0.5, -0.1, 0.2, 0.8];
embeddings['cats'] = [0.75, 0.55, -0.15, 0.15, 0.75];
embeddings['dogs'] = [0.65, 0.45, -0.05, 0.25, 0.85];
embeddings['pets'] = [0.6, 0.4, 0.0, 0.3, 0.6];
embeddings['animals'] = [0.5, 0.3, 0.1, 0.4, 0.5];
// Royalty cluster (king, queen, royal)
embeddings['king'] = [-0.3, 0.9, 0.8, -0.4, 0.2];
embeddings['queen'] = [-0.2, 0.8, 0.7, -0.3, 0.1];
embeddings['kings'] = [-0.35, 0.85, 0.75, -0.45, 0.25];
embeddings['queens'] = [-0.25, 0.75, 0.65, -0.35, 0.15];
embeddings['royal'] = [-0.1, 0.7, 0.6, -0.2, 0.0];
embeddings['rulers'] = [-0.4, 0.6, 0.5, -0.5, 0.3];
// Cities cluster (Paris, London, city)
embeddings['Paris'] = [0.2, -0.6, 0.4, 0.8, -0.3];
embeddings['London'] = [0.1, -0.5, 0.3, 0.7, -0.2];
embeddings['city'] = [0.0, -0.4, 0.2, 0.6, -0.1];
embeddings['cities'] = [0.05, -0.45, 0.25, 0.65, -0.15];
// Actions cluster (sit, run, sleep, play)
embeddings['sat'] = [0.3, 0.1, -0.8, 0.5, -0.4];
embeddings['run'] = [0.4, 0.0, -0.7, 0.6, -0.3];
embeddings['sleep'] = [0.1, 0.2, -0.9, 0.3, -0.6];
embeddings['play'] = [0.5, -0.1, -0.6, 0.7, -0.2];
embeddings['sleeps'] = [0.15, 0.25, -0.85, 0.35, -0.55];
embeddings['runs'] = [0.45, 0.05, -0.65, 0.65, -0.25];
embeddings['plays'] = [0.55, -0.05, -0.55, 0.75, -0.15];
// Size descriptors cluster
embeddings['big'] = [-0.7, 0.3, 0.1, -0.2, 0.6];
embeddings['small'] = [-0.8, 0.2, 0.2, -0.1, 0.5];
embeddings['young'] = [-0.6, 0.4, 0.0, -0.3, 0.7];
embeddings['old'] = [-0.5, 0.5, -0.1, -0.4, 0.8];
// Common words (more neutral embeddings)
embeddings['the'] = [0.0, 0.0, 0.0, 0.0, 0.0];
embeddings['on'] = [0.1, -0.1, 0.0, 0.1, -0.1];
embeddings['in'] = [0.0, -0.1, 0.1, 0.0, -0.1];
embeddings['and'] = [-0.1, 0.0, 0.0, -0.1, 0.0];
embeddings['are'] = [0.0, 0.1, -0.1, 0.0, 0.1];
embeddings['is'] = [0.1, 0.0, -0.1, 0.1, 0.0];
// Fill in remaining words with appropriate cluster-based embeddings
const matrix = Array.from({length: VOCAB.length}, () => Array(embedDim).fill(0));
VOCAB.forEach((word, i) => {
if (embeddings[word]) {
matrix[i] = [...embeddings[word]];
} else {
// Generate random embeddings for remaining words, but constrained
for(let j = 0; j < embedDim; j++) {
matrix[i][j] = (Math.random() - 0.5) * 0.4; // Smaller random values
}
}
});
return matrix;
}
// Create meaningful output weights that work well with the embeddings
function createMeaningfulOutputWeights(inputEmbeddings) {
// Create output weights that are roughly the transpose of input embeddings
// This ensures good predictions when using the meaningful embeddings
const outputWeights = Array.from({length: embedDim}, () => Array(VOCAB.length).fill(0));
for(let i = 0; i < embedDim; i++) {
for(let j = 0; j < VOCAB.length; j++) {
// Use transpose with some noise for better generalization
outputWeights[i][j] = inputEmbeddings[j][i] * 0.8 + (Math.random() - 0.5) * 0.2;
}
}
return outputWeights;
}
// --------- elements ---------
const $sentence = document.getElementById('sentence');
const $winBtn1 = document.getElementById('winBtn1');
const $winBtn2 = document.getElementById('winBtn2');
const $winv = document.getElementById('winv');
const $center = document.getElementById('center');
const $dim = document.getElementById('dim');
const $dimv = document.getElementById('dimv');
const $tokens = document.getElementById('tokens');
const $matrixIn = document.getElementById('matrixIn');
// Training elements
const $trainModel = document.getElementById('trainModel');
const $resetTraining = document.getElementById('resetTraining');
const $trainingProgress = document.getElementById('trainingProgress');
const $progressBar = document.getElementById('progressBar');
const $progressText = document.getElementById('progressText');
const $learningRate = document.getElementById('learningRate');
const $lrValue = document.getElementById('lrValue');
const $trainingStep = document.getElementById('trainingStep');
const $currentLoss = document.getElementById('currentLoss');
const $trainingStatus = document.getElementById('trainingStatus');
const $embeddingStatus = document.getElementById('embeddingStatus');
const $clusteringStatus = document.getElementById('clusteringStatus');
// init selects
SENTENCES.forEach((s,i)=>{
const o=document.createElement('option');
o.value=i; o.textContent=`"${s.join(' ')}"`;
$sentence.appendChild(o);
});
function updateCenterBounds(){
const tokens = SENTENCES[sent];
const minC = 0; // Can select first word
const maxC = tokens.length - 1; // Can select last word
if(center<minC) center=minC;
if(center>maxC) center=maxC;
$center.min = String(minC);
$center.max = String(maxC);
$center.value = String(center);
}
// listeners
$sentence.onchange = e=>{ sent = +e.target.value; updateCenterBounds(); resetTraining(); render(); };