html Copy code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Self-Attention Visualization</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
#container {
position: relative;
margin-top: 50px;
text-align: center;
}
.token {
display: inline-block;
position: relative;
margin: 0 20px;
cursor: pointer;
font-size: 24px;
}
svg {
position: absolute;
top: 0;
left: 0;
}
.attention-vector {
margin-top: 20px;
display: flex;
justify-content: center;
align-items: center;
font-size: 18px;
}
.attention-vector .vector {
border-left: 1px solid #000;
border-right: 1px solid #000;
padding: 0 10px;
margin: 0 5px;
}
.attention-vector .vector div {
display: flex;
justify-content: flex-start;
align-items: center;
}
.attention-vector .vector div span {
min-width: 40px;
text-align: right;
margin-right: 5px;
}
</style>
</head>
<body>
<div id="container">
<div class="token" data-index="0">The</div>
<div class="token" data-index="1">quick</div>
<div class="token" data-index="2">brown</div>
<div class="token" data-index="3">fox</div>
<svg id="edges" width="100%" height="200px"></svg>
</div>
<div id="attention-vector"></div>
<script>
const tokens = document.querySelectorAll('.token');
const edgesSvg = document.getElementById('edges');
const attentionVectorDiv = document.getElementById('attention-vector');
// Sample attention scores
const attentionScores = [
[0.1, 0.3, 0.4, 0.2], // for 'The'
[0.2, 0.1, 0.5, 0.2], // for 'quick'
[0.25, 0.25, 0.1, 0.4], // for 'brown'
[0.15, 0.35, 0.25, 0.25] // for 'fox'
];
let isVectorShown = [false, false, false, false];
tokens.forEach(token => {
token.addEventListener('mouseenter', showEdges);
token.addEventListener('mouseleave', hideEdges);
token.addEventListener('click', toggleAttentionVector);
});
function getTokenPositions() {
const positions = [];
tokens.forEach(token => {
const rect = token.getBoundingClientRect();
positions.push({
x: rect.left + rect.width / 2 + window.scrollX,
y: rect.top + rect.height / 2 + window.scrollY
});
});
return positions;
}
function showEdges(event) {
const index = parseInt(event.target.dataset.index);
const positions = getTokenPositions();
edgesSvg.innerHTML = '';
for (let i = 0; i < positions.length; i++) {
if (i !== index) {
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
const startX = positions[index].x;
const startY = positions[index].y - 50; // Adjust for SVG position
const endX = positions[i].x;
const endY = positions[i].y - 50; // Adjust for SVG position
const curvature = 100 + Math.abs(i - index) * 50;
const pathData = `M ${startX},${startY} C ${startX},${startY - curvature} ${endX},${endY - curvature} ${endX},${endY}`;
path.setAttribute('d', pathData);
path.setAttribute('stroke', 'black');
path.setAttribute('fill', 'none');
const thickness = attentionScores[index][i] * 10; // Scale the thickness
path.setAttribute('stroke-width', thickness);
edgesSvg.appendChild(path);
}
}
}
function hideEdges(event) {
edgesSvg.innerHTML = '';
}
function toggleAttentionVector(event) {
const index = parseInt(event.target.dataset.index);
if (isVectorShown[index]) {
attentionVectorDiv.innerHTML = '';
isVectorShown[index] = false;
} else {
const vectorContainer = document.createElement('div');
vectorContainer.className = 'attention-vector';
const vectorLabel = document.createElement('div');
vectorLabel.textContent = `Attention scores for '${event.target.textContent}':`;
vectorContainer.appendChild(vectorLabel);
const vector = document.createElement('div');
vector.className = 'vector';
for (let i = 0; i < tokens.length; i++) {
const row = document.createElement('div');
const scoreSpan = document.createElement('span');
scoreSpan.textContent = attentionScores[index][i].toFixed(2);
row.appendChild(scoreSpan);
const tokenSpan = document.createElement('span');
tokenSpan.textContent = tokens[i].textContent;
row.appendChild(tokenSpan);
vector.appendChild(row);
}
vectorContainer.appendChild(vector);
attentionVectorDiv.innerHTML = '';
attentionVectorDiv.appendChild(vectorContainer);
isVectorShown = [false, false, false, false]; // Reset others
isVectorShown[index] = true;
}
}
</script>
</body>
</html>