-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsne.html
221 lines (180 loc) · 6.17 KB
/
tsne.html
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
<!DOCTYPE html>
<html>
<meta charset='utf-8'>
<title>t-SNE Viewer</title>
<style>
#chart {
margin-left: -40px;
height: 650px;
}
text {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.label {
fill: #777;
}
.iteration.label {
font: 500 196px 'Helvetica Neue';
fill: #ddd;
}
.iteration.label.active {
fill: #aaa;
}
.overlay {
fill: none;
pointer-events: all;
cursor: ew-resize;
}
</style>
<h1>T-SNE Viewer</h1>
<p id='chart'></p>
<aside>Mouseover the iteration to move forward and backwards through time.</aside>
<p class='attribution'>Source: <a href='http://bost.ocks.org/mike/nations'>The Wealth & Health of Nations</a>, <a href='http://bost.ocks.org/mike/'>Mike Bostock</a>.</p>
<script src='https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js'></script>
<script src='tsne-json.js'></script>
<script>
// Various accessors that specify the four dimensions of data to visualize.
function x(d) { return d.x; }
function y(d) { return d.y; }
function text(d) { return d.label; }
function color(d) { return d.label; }
function key(d) { return d.key; }
// Chart dimensions.
var margin = {top: 19.5, right: 19.5, bottom: 19.5, left: 99.5},
width = 640 - margin.right,
height = 640 - margin.top - margin.bottom;
// Various scales. These domains make assumptions of data, naturally.
var xScale = d3.scale.linear().domain([-20, 20]).range([0, width]),
yScale = d3.scale.linear().domain([-20, 20]).range([height, 0]),
colorScale = d3.scale.category10();
// The x & y axes.
var xAxis = d3.svg.axis().orient('bottom').scale(xScale).ticks(12, d3.format(',d')),
yAxis = d3.svg.axis().orient('left').scale(yScale).ticks(12, d3.format(',d'));
// Create the SVG container and set the origin.
var svg = d3.select('#chart').append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// Add the x-axis.
svg.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(0,' + height + ')')
.call(xAxis);
// Add the y-axis.
svg.append('g')
.attr('class', 'y axis')
.call(yAxis);
// Add an x-axis label.
svg.append('text')
.attr('class', 'x label')
.attr('text-anchor', 'end')
.attr('x', width)
.attr('y', height - 6);
// Add a y-axis label.
svg.append('text')
.attr('class', 'y label')
.attr('text-anchor', 'end')
.attr('y', 6)
.attr('dy', '.75em')
.attr('transform', 'rotate(-90)');
// Add the iteration label; the value is set on transition.
var label = svg.append('text')
.attr('class', 'iteration label')
.attr('text-anchor', 'end')
.attr('y', height - 24)
.attr('x', width)
.text('0000');
// Load the data.
d3.json('mnist.json?nocache=' + (new Date()).getTime(), function(tsneData) {
var iterations = tsneData.iterations,
maxIteration = iterations.max(),
results = tsneData.data,
animationDuration = 100;
// Add a dot per observation. Initialize the data at 1, and set the colors.
var dot = svg.append('g')
.attr('class', 'dots')
.selectAll('.dot')
.data(interpolateData(1, iterations, results))
.enter().append('text')
.attr('class', 'dot')
.text(function(d) { return text(d); })
.style('fill', function(d) { return colorScale(color(d)); })
.call(position);
// Add an overlay for the iteration label.
var box = label.node().getBBox();
var overlay = svg.append('rect')
.attr('class', 'overlay')
.attr('x', box.x)
.attr('y', box.y)
.attr('width', box.width)
.attr('height', box.height)
.on('mouseover', enableInteraction);
// Start a transition that interpolates the data based on iteration.
svg.transition()
.duration(animationDuration * maxIteration)
.ease('linear')
.tween('iteration', tweenIteration)
.each('end', enableInteraction);
// Positions the dots based on data.
function position(dot) {
dot .attr('x', function(d) { return xScale(x(d)); })
.attr('y', function(d) { return yScale(y(d)); });
}
// After the transition finishes, you can mouseover to change the iteration.
function enableInteraction() {
var iterationScale = d3.scale.linear()
.domain([1, maxIteration])
.range([box.x + 10, box.x + box.width - 10])
.clamp(true);
// Cancel the current transition, if any.
svg.transition().duration(0);
overlay
.on('mouseover', mouseover)
.on('mouseout', mouseout)
.on('mousemove', mousemove)
.on('touchmove', mousemove);
function mouseover() {
label.classed('active', true);
}
function mouseout() {
label.classed('active', false);
}
function mousemove() {
displayIteration(iterationScale.invert(d3.mouse(this)[0]));
}
}
// Tweens the entire chart by first tweening the iteration, and then the data.
// For the interpolated data, the dots and label are redrawn.
function tweenIteration() {
var iteration = d3.interpolateNumber(1, maxIteration);
return function(t) { displayIteration(iteration(t)); };
}
// Updates the display to show the specified iteration.
function displayIteration(iteration) {
var interpolated = interpolateData(iteration, iterations, results);
var limits = interpolated.reduce(function(acc, data) {
return [Math.max(acc[0], Math.abs(data.x)), Math.max(acc[1], Math.abs(data.y))];
}, [0, 0]);
xScale.domain([-limits[0], limits[0]]);
yScale.domain([-limits[1], limits[1]]);
svg.select('.x.axis').call(xAxis);
svg.select('.y.axis').call(yAxis);
dot.data(interpolated, key).call(position);
label.text(pad(Math.round(iteration), 4));
}
});
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-67444009-1', 'auto');
ga('send', 'pageview');
</script>