Paar, F. (Ferdinand) commited on
Commit
e4a48bc
·
1 Parent(s): 5bdfcd7

each weight is normalized by its row’s max2.6

Browse files
Files changed (2) hide show
  1. frontend/index.html +2 -3
  2. frontend/script.js +16 -14
frontend/index.html CHANGED
@@ -29,7 +29,7 @@
29
  Etiam vel dui consequat ante lobortis facilisis. Curabitur facilisis enim sed massa
30
  consectetur, et mollis magna suscipit. Vestibulum odio ante, laoreet at mauris eu,
31
  feugiat pretium tortor. Ut condimentum laoreet felis quis rutrum. Proin vel elit a
32
- augue ornare venenatis id nec neque.Ferdi 2.6
33
  </p>
34
  </section>
35
 
@@ -117,6 +117,5 @@
117
  </aside>
118
  </div>
119
 
120
- <script src="static/script.js?v=202845563"></script>
121
- </body>
122
  </html>
 
29
  Etiam vel dui consequat ante lobortis facilisis. Curabitur facilisis enim sed massa
30
  consectetur, et mollis magna suscipit. Vestibulum odio ante, laoreet at mauris eu,
31
  feugiat pretium tortor. Ut condimentum laoreet felis quis rutrum. Proin vel elit a
32
+ augue ornare venenatis id nec neque.Ferdi 2.7
33
  </p>
34
  </section>
35
 
 
117
  </aside>
118
  </div>
119
 
120
+ <script src="static/script.js?v=2025
 
121
  </html>
frontend/script.js CHANGED
@@ -55,11 +55,10 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
55
  const maxLineWidth = 4;
56
  const maxOpacity = 0.8;
57
 
58
- // Extract attention weights for this head and compute the normalization factor.
59
- const attentionWeights = attention[layerIdx][headIdx];
60
- const headMax = Math.max(...attentionWeights.flat()) || 1;
61
 
62
- // Instead of using "90%", compute a numeric right x-coordinate.
63
  const xRight = width - padding;
64
 
65
  // Create a thumbnail container using D3
@@ -72,12 +71,12 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
72
  .style("padding", "5px")
73
  .style("background", "#fff");
74
 
75
- // Append an SVG container inside the thumbnail with fixed width and height.
76
  const svg = thumbnail.append("svg")
77
  .attr("width", width)
78
  .attr("height", height);
79
 
80
- // Add header text using template literals
81
  svg.append("text")
82
  .attr("x", width / 2)
83
  .attr("y", 15)
@@ -85,18 +84,18 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
85
  .attr("font-size", "10")
86
  .text(`L${layerIdx + 1} H${headIdx + 1}`);
87
 
88
- // Draw tokens on both sides (vertically aligned)
89
  tokens.forEach((token, idx) => {
90
  const cleanToken = token.replace("Ġ", "");
91
 
92
- // Left side token
93
  svg.append("text")
94
  .attr("x", padding)
95
  .attr("y", padding + idx * tokenHeight)
96
  .attr("font-size", "8")
97
  .text(cleanToken);
98
 
99
- // Right side token
100
  svg.append("text")
101
  .attr("x", xRight)
102
  .attr("y", padding + idx * tokenHeight)
@@ -105,12 +104,15 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
105
  .text(cleanToken);
106
  });
107
 
108
- // Draw attention lines with normalized weights.
109
- // Lower threshold to 0.01 so more connections are visible.
110
- attentionWeights.forEach((sourceWeights, sourceIdx) => {
 
 
111
  sourceWeights.forEach((weight, targetIdx) => {
 
112
  if (weight > 0.01 && sourceIdx !== targetIdx) {
113
- const normalizedWeight = weight / headMax;
114
  svg.append("line")
115
  .attr("x1", padding)
116
  .attr("y1", padding + sourceIdx * tokenHeight - 5)
@@ -124,7 +126,7 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
124
  });
125
  });
126
 
127
- // On click, update UI elements (make sure 'data' is globally available)
128
  thumbnail.on("click", function() {
129
  d3.select("#head").text(`Head: ${headIdx + 1}`);
130
  d3.select("#layer").text(`Layer: ${layerIdx + 1}`);
 
55
  const maxLineWidth = 4;
56
  const maxOpacity = 0.8;
57
 
58
+ // Instead of a head-wide max, we will normalize per source token (per row).
59
+ // (This avoids a situation where a dominant first token makes all rows look similar.)
 
60
 
61
+ // Compute the right-side x-coordinate numerically instead of using "90%"
62
  const xRight = width - padding;
63
 
64
  // Create a thumbnail container using D3
 
71
  .style("padding", "5px")
72
  .style("background", "#fff");
73
 
74
+ // Append an SVG container with fixed dimensions
75
  const svg = thumbnail.append("svg")
76
  .attr("width", width)
77
  .attr("height", height);
78
 
79
+ // Add header text (e.g., "L4 H4")
80
  svg.append("text")
81
  .attr("x", width / 2)
82
  .attr("y", 15)
 
84
  .attr("font-size", "10")
85
  .text(`L${layerIdx + 1} H${headIdx + 1}`);
86
 
87
+ // Draw tokens on both sides (left and right)
88
  tokens.forEach((token, idx) => {
89
  const cleanToken = token.replace("Ġ", "");
90
 
91
+ // Left-side token
92
  svg.append("text")
93
  .attr("x", padding)
94
  .attr("y", padding + idx * tokenHeight)
95
  .attr("font-size", "8")
96
  .text(cleanToken);
97
 
98
+ // Right-side token
99
  svg.append("text")
100
  .attr("x", xRight)
101
  .attr("y", padding + idx * tokenHeight)
 
104
  .text(cleanToken);
105
  });
106
 
107
+ // Draw attention lines with per-row normalization.
108
+ // For each source token, we normalize its weights by the row’s maximum.
109
+ attention[layerIdx][headIdx].forEach((sourceWeights, sourceIdx) => {
110
+ // Compute the maximum weight in this row (or use 1 if all are zero)
111
+ const rowMax = Math.max(...sourceWeights) || 1;
112
  sourceWeights.forEach((weight, targetIdx) => {
113
+ // Only draw if the weight exceeds a threshold and is not self-attention
114
  if (weight > 0.01 && sourceIdx !== targetIdx) {
115
+ const normalizedWeight = weight / rowMax;
116
  svg.append("line")
117
  .attr("x1", padding)
118
  .attr("y1", padding + sourceIdx * tokenHeight - 5)
 
126
  });
127
  });
128
 
129
+ // Click handler: update UI (make sure 'data' is available globally or pass it in)
130
  thumbnail.on("click", function() {
131
  d3.select("#head").text(`Head: ${headIdx + 1}`);
132
  d3.select("#layer").text(`Layer: ${layerIdx + 1}`);