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

trying to fix attention to first token 2.6

Browse files
Files changed (3) hide show
  1. frontend/.DS_Store +0 -0
  2. frontend/index.html +2 -2
  3. frontend/script.js +13 -9
frontend/.DS_Store ADDED
Binary file (6.15 kB). View file
 
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.5
33
  </p>
34
  </section>
35
 
@@ -117,6 +117,6 @@
117
  </aside>
118
  </div>
119
 
120
- <script src="static/script.js?v=202845533"></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.6
33
  </p>
34
  </section>
35
 
 
117
  </aside>
118
  </div>
119
 
120
+ <script src="static/script.js?v=202845563"></script>
121
  </body>
122
  </html>
frontend/script.js CHANGED
@@ -59,6 +59,9 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
59
  const attentionWeights = attention[layerIdx][headIdx];
60
  const headMax = Math.max(...attentionWeights.flat()) || 1;
61
 
 
 
 
62
  // Create a thumbnail container using D3
63
  const thumbnail = d3.select(document.createElement("div"))
64
  .style("position", "relative")
@@ -69,14 +72,14 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
69
  .style("padding", "5px")
70
  .style("background", "#fff");
71
 
72
- // Append an SVG container inside the thumbnail
73
  const svg = thumbnail.append("svg")
74
- .attr("width", "100%")
75
- .attr("height", "100%");
76
 
77
  // Add header text using template literals
78
  svg.append("text")
79
- .attr("x", "50%")
80
  .attr("y", 15)
81
  .attr("text-anchor", "middle")
82
  .attr("font-size", "10")
@@ -95,22 +98,23 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
95
 
96
  // Right side token
97
  svg.append("text")
98
- .attr("x", "90%")
99
  .attr("y", padding + idx * tokenHeight)
100
  .attr("text-anchor", "end")
101
  .attr("font-size", "8")
102
  .text(cleanToken);
103
  });
104
 
105
- // Draw attention lines with normalized weights
 
106
  attentionWeights.forEach((sourceWeights, sourceIdx) => {
107
  sourceWeights.forEach((weight, targetIdx) => {
108
- if (weight > 0.05 && sourceIdx !== targetIdx) {
109
  const normalizedWeight = weight / headMax;
110
  svg.append("line")
111
  .attr("x1", padding)
112
  .attr("y1", padding + sourceIdx * tokenHeight - 5)
113
- .attr("x2", "90%")
114
  .attr("y2", padding + targetIdx * tokenHeight - 5)
115
  .attr("stroke", "#2ecc71")
116
  .attr("stroke-width", Math.max(0.5, normalizedWeight * maxLineWidth))
@@ -120,7 +124,7 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
120
  });
121
  });
122
 
123
- // On click, update UI elements (assumes 'data' is globally available)
124
  thumbnail.on("click", function() {
125
  d3.select("#head").text(`Head: ${headIdx + 1}`);
126
  d3.select("#layer").text(`Layer: ${layerIdx + 1}`);
 
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
66
  const thumbnail = d3.select(document.createElement("div"))
67
  .style("position", "relative")
 
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)
84
  .attr("text-anchor", "middle")
85
  .attr("font-size", "10")
 
98
 
99
  // Right side token
100
  svg.append("text")
101
+ .attr("x", xRight)
102
  .attr("y", padding + idx * tokenHeight)
103
  .attr("text-anchor", "end")
104
  .attr("font-size", "8")
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)
117
+ .attr("x2", xRight)
118
  .attr("y2", padding + targetIdx * tokenHeight - 5)
119
  .attr("stroke", "#2ecc71")
120
  .attr("stroke-width", Math.max(0.5, normalizedWeight * maxLineWidth))
 
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}`);