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

visualisation normalise for each head and choosing hover animation by clicking on the thumb nail2.2

Browse files
Files changed (2) hide show
  1. frontend/index.html +2 -2
  2. frontend/script.js +13 -8
frontend/index.html CHANGED
@@ -28,7 +28,7 @@
28
  Etiam vel dui consequat ante lobortis facilisis. Curabitur facilisis enim sed massa
29
  consectetur, et mollis magna suscipit. Vestibulum odio ante, laoreet at mauris eu,
30
  feugiat pretium tortor. Ut condimentum laoreet felis quis rutrum. Proin vel elit a
31
- augue ornare venenatis id nec neque.Ferdi 2.2
32
  </p>
33
  </section>
34
 
@@ -116,6 +116,6 @@
116
  </aside>
117
  </div>
118
 
119
- <script src="static/script.js?v=202 1533"></script>
120
  </body>
121
  </html>
 
28
  Etiam vel dui consequat ante lobortis facilisis. Curabitur facilisis enim sed massa
29
  consectetur, et mollis magna suscipit. Vestibulum odio ante, laoreet at mauris eu,
30
  feugiat pretium tortor. Ut condimentum laoreet felis quis rutrum. Proin vel elit a
31
+ augue ornare venenatis id nec neque.Ferdi 2.3
32
  </p>
33
  </section>
34
 
 
116
  </aside>
117
  </div>
118
 
119
+ <script src="static/script.js?v=20281533"></script>
120
  </body>
121
  </html>
frontend/script.js CHANGED
@@ -19,7 +19,7 @@ document.getElementById('textForm').addEventListener('submit', async (e) => {
19
  displayOutput(data);
20
  displayHoverTokens(data, 0, 0);
21
  renderModelView(data.tokens, data.attention);
22
- } catch (error) {
23
  console.error('Error:', error);
24
  document.getElementById('output').innerText = 'Error processing text.';
25
  }
@@ -64,7 +64,7 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
64
  svg.setAttribute("width", "100%");
65
  svg.setAttribute("height", "100%");
66
 
67
- // Add header text for layer and head
68
  const header = document.createElementNS("http://www.w3.org/2000/svg", "text");
69
  header.setAttribute("x", "50%");
70
  header.setAttribute("y", "15");
@@ -77,11 +77,14 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
77
  const maxLineWidth = 4;
78
  const maxOpacity = 0.8;
79
 
 
 
 
80
  // Draw tokens on both sides (vertically aligned)
81
  tokens.forEach((token, idx) => {
82
  const cleanToken = token.replace("Ġ", "");
83
 
84
- // Left side: fix x position to 'padding'
85
  const leftText = document.createElementNS("http://www.w3.org/2000/svg", "text");
86
  leftText.setAttribute("x", padding);
87
  leftText.setAttribute("y", padding + (idx * 20));
@@ -99,19 +102,22 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
99
  svg.appendChild(rightText);
100
  });
101
 
102
- // Draw attention lines between tokens
103
  attentionWeights.forEach((sourceWeights, sourceIdx) => {
104
  sourceWeights.forEach((weight, targetIdx) => {
105
  if (weight > 0.05 && sourceIdx !== targetIdx) {
 
 
 
106
  const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
107
- // Use the same padding and vertical spacing to align lines with tokens
108
  line.setAttribute("x1", padding);
109
  line.setAttribute("y1", padding + (sourceIdx * 20) - 5);
110
  line.setAttribute("x2", "90%");
111
  line.setAttribute("y2", padding + (targetIdx * 20) - 5);
112
  line.setAttribute("stroke", "#2ecc71");
113
- line.setAttribute("stroke-width", Math.max(0.5, weight * maxLineWidth));
114
- line.setAttribute("opacity", Math.min(maxOpacity, weight * 2));
115
  line.setAttribute("stroke-linecap", "round");
116
  svg.appendChild(line);
117
  }
@@ -130,7 +136,6 @@ function createAttentionThumbnail(tokens, attention, layerIdx, headIdx) {
130
 
131
  return thumbnail;
132
  }
133
-
134
  // Function to display the tokens and attention values
135
  function displayOutput(data) {
136
  const outputDiv = document.getElementById('output');
 
19
  displayOutput(data);
20
  displayHoverTokens(data, 0, 0);
21
  renderModelView(data.tokens, data.attention);
22
+ } catch (error) {ß
23
  console.error('Error:', error);
24
  document.getElementById('output').innerText = 'Error processing text.';
25
  }
 
64
  svg.setAttribute("width", "100%");
65
  svg.setAttribute("height", "100%");
66
 
67
+ // Add header text for layer and head using template literals
68
  const header = document.createElementNS("http://www.w3.org/2000/svg", "text");
69
  header.setAttribute("x", "50%");
70
  header.setAttribute("y", "15");
 
77
  const maxLineWidth = 4;
78
  const maxOpacity = 0.8;
79
 
80
+ // Normalize weights per head: Compute maximum weight in this head.
81
+ const headMax = Math.max(...attentionWeights.flat()) || 1;
82
+
83
  // Draw tokens on both sides (vertically aligned)
84
  tokens.forEach((token, idx) => {
85
  const cleanToken = token.replace("Ġ", "");
86
 
87
+ // Left side: fixed x position to 'padding'
88
  const leftText = document.createElementNS("http://www.w3.org/2000/svg", "text");
89
  leftText.setAttribute("x", padding);
90
  leftText.setAttribute("y", padding + (idx * 20));
 
102
  svg.appendChild(rightText);
103
  });
104
 
105
+ // Draw attention lines between tokens with normalized weights
106
  attentionWeights.forEach((sourceWeights, sourceIdx) => {
107
  sourceWeights.forEach((weight, targetIdx) => {
108
  if (weight > 0.05 && sourceIdx !== targetIdx) {
109
+ // Normalize the weight relative to the maximum weight in this head
110
+ const normalizedWeight = weight / headMax;
111
+
112
  const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
113
+ // Align lines with tokens using the same padding and vertical spacing
114
  line.setAttribute("x1", padding);
115
  line.setAttribute("y1", padding + (sourceIdx * 20) - 5);
116
  line.setAttribute("x2", "90%");
117
  line.setAttribute("y2", padding + (targetIdx * 20) - 5);
118
  line.setAttribute("stroke", "#2ecc71");
119
+ line.setAttribute("stroke-width", Math.max(0.5, normalizedWeight * maxLineWidth));
120
+ line.setAttribute("opacity", Math.min(maxOpacity, normalizedWeight * 2));
121
  line.setAttribute("stroke-linecap", "round");
122
  svg.appendChild(line);
123
  }
 
136
 
137
  return thumbnail;
138
  }
 
139
  // Function to display the tokens and attention values
140
  function displayOutput(data) {
141
  const outputDiv = document.getElementById('output');