generated from hchiam/learning-tfjs-tsne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
176 lines (152 loc) · 5.15 KB
/
index.ts
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
const tf = require("@tensorflow/tfjs");
require("@tensorflow/tfjs");
const use = require("@tensorflow-models/universal-sentence-encoder");
import { UMAP } from "umap-js";
import * as $ from "jquery";
import Chart from "chart.js/auto"; // https://stackoverflow.com/a/67143648
let chart;
$("#start").on("click", () => {
const sentences = $("#inputs")
.val()
.split("\n")
.filter((x) => x);
if (sentences?.length) {
$("#start").prop("disabled", true);
$(".chartjs-tooltip").remove();
runAnalysis(sentences, () => {
$("#start").prop("disabled", false);
$("#start").text("Re-run");
});
}
});
async function runAnalysis(sentences, callback) {
if (chart) chart.destroy();
$("#chart").addClass("in-progress");
$("#status").css("color", "red");
showStatus("Creating model...");
const model = await use.load();
showStatus("Creating embeddings...");
const embeddings = await model.embed(sentences);
const sentenceEmbeddingsAsArray = [];
sentences.forEach((sentence, i) => {
const sentenceEmbedding = tf.slice(embeddings, [i, 0], [1]);
const sentenceEmbeddingAsArray = sentenceEmbedding.dataSync();
sentenceEmbeddingsAsArray.push(sentenceEmbeddingAsArray);
});
showStatus("Creating plottable data with UMAP...");
const dimensions = 2; // 2 = 2D
const numberOfNeighbours = 3; // Math.min(15, Math.max(3, Math.ceil(sentenceEmbeddingsAsArray.length / 2)));
console.log("numberOfNeighbours", numberOfNeighbours);
const umap = new UMAP({
//nEpochs: 100, // nEpochs is computed automatically by default
nComponents: dimensions,
nNeighbors: numberOfNeighbours,
minDist: 0.1, // default: 0.1
spread: 1.0, // default: 1.0
// other parameters: https://github.com/PAIR-code/umap-js/#parameters
});
const plottableData = umap.fit(sentenceEmbeddingsAsArray);
showStatus("Plotting data...");
chart = plot(plottableData, sentences, () => {
$("#chart").removeClass("in-progress");
$("#status").css("color", "green");
showStatus(
"Visualization ready! Hover over points to read comments. The Universal-Sentence-Encoder model generates embeddings, then the code tries to group semantically similar comments near each other using UMAP (as opposed to PCA or t-SNE). The UMAP algorithm is stochastic (uses randomness) to speed up dimension reduction. More info at https://github.com/hchiam/learning-tfjs-umap"
);
$("#suggest_newer_demo").removeClass("d-none");
if (callback) callback();
});
}
function showStatus(message) {
$("#status").text(message);
console.log(message);
}
function plot(coordinatesArray, labels, callback) {
const data = coordinatesArray.map((x) => {
return { x: x[0], y: x[1] };
});
console.log("labels", labels);
console.log("data", data);
const chart = new Chart("chart", {
type: "scatter",
data: {
labels: labels,
datasets: [
{
data: data,
pointBackgroundColor: "black",
pointRadius: 7,
},
],
},
options: {
aspectRatio: 1,
maintainAspectRatio: true,
// interaction: {
// mode: "nearest",
// },
plugins: {
legend: {
display: false,
},
tooltip: {
// intersect: false,
enabled: false,
external: function (context) {
const canvasBox = context.chart.canvas.getBoundingClientRect();
const tooltip = context.tooltip;
const title = String(tooltip.title);
const left = String(tooltip.caretX + canvasBox.left) + "px";
const top = String(tooltip.caretY + canvasBox.top) + "px";
const tooltipSelector = `.chartjs-tooltip[data-title="${title}"]`;
const alreadyHaveTooltip = $(tooltipSelector).length > 0;
if (alreadyHaveTooltip) return;
const tooltipEl = document.createElement("div");
tooltipEl.className = "chartjs-tooltip";
tooltipEl.innerText = title;
tooltipEl.dataset.title = title;
tooltipEl.style.left = left;
tooltipEl.dataset.left = left;
tooltipEl.style.top = top;
tooltipEl.dataset.top = top;
tooltipEl.style.background = "#ffffff80";
tooltipEl.style.position = "absolute";
tooltipEl.style.pointerEvents = "none";
tooltipEl.style.borderRadius = "0.3rem";
tooltipEl.style.padding = "0.1rem";
document.body.appendChild(tooltipEl);
// setTimeout(() => $(tooltipSelector).remove(), 3000);
$("#hide_tooltips").prop("disabled", false);
},
},
},
responsive: false,
scales: {
x: {
ticks: {
display: false,
},
grid: {
display: false,
},
},
y: {
ticks: {
display: false,
},
grid: {
display: false,
},
},
},
},
});
$(window).on("resize", () => {
$(".chartjs-tooltip").remove();
});
$("#hide_tooltips").on("click", () => {
$(".chartjs-tooltip").remove();
});
if (callback) callback();
return chart;
}