-
Notifications
You must be signed in to change notification settings - Fork 0
/
VectorChooser.vue
148 lines (140 loc) · 4.63 KB
/
VectorChooser.vue
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
<!-- Contributed by David Bau, in the public domain -->
<template>
<div class="vectorlist">
<div v-for="(vector, index) in vectors" class="vector">
<input v-model="vector.text">
<button @click="selectVector(index)">→</button>
<button @click="deleteVector(index)">x</button>
</div>
<div class="operation">
<button @click="saveVector()">Save current sample</button>
</div>
<div class="operation">
<!-- TODO: Change this button to do something interesting -->
<button @click="applyVectorMath()">Apply vector math</button>
</div>
<button @click="getKNN()">Find nearest font ID</button>
<button @click="getAverage()">Find nearest average font</button>
</div>
</template>
<script>
import {Array1D, ENV} from 'deeplearn';
const math = ENV.math;
import * as VectorOperations from '../utils/VectorOperations';
//This json file includes all of the Font IDs in our database and their 40-dimensional logits vector.
var json = require('../embeddings.json');
export default {
props: {
selectedSample: { },
model: { },
vectors: { type: Array, default: () => [ { text: "0" } ] }
},
methods: {
saveVector() {
this.selectedSample.data().then(x =>
this.vectors.push({ text: Array.prototype.slice.call(x).join(',') })
);
},
deleteVector(index) {
this.vectors.splice(index, 1);
},
selectVector(index) {
this.$emit("select", { selectedSample: this.model.fixdim(
Array1D.new(this.vectors[index].text.split(',').map(parseFloat)))});
},
// TODO: Add useful vector space operations here -->
applyVectorMath() {
var c = window.prompt("Enter an array!");
var inVec = c.split(',');
var actual = [];
if (inVec.length != 40) {
window.alert("INVALID LENGTH!");
} else {
for (let i = 0; i < inVec.length; i++) {
actual.push(parseFloat(inVec[i]));
}
this.$emit("select", { selectedSample:
math.add(this.selectedSample, this.model.fixdim(
Array1D.new(actual))) } )
}
},
//TODO: Implement getKNN to output the font ID of the nearest neighbor
getKNN() {
this.selectedSample.data().then(function(x){
var closestFontId = -1;
var closestDistance = Number.MAX_VALUE;
const normX = VectorOperations.norm(x);
if (normX === 0){
for (var i = 0; i < json.length; i++){
const normCurrent = VectorOperations.norm(json[i])
//console.log(normX + " compared to "+ normCurrent);
if (normCurrent < closestDistance) {
closestDistance = normCurrent
closestFontId = i
}
}
} else {
for (var i = 0; i < json.length; i++){
const currentVector = json[i]
const dotProduct = VectorOperations.dotProduct(x, currentVector)
const normCurrent = VectorOperations.norm(currentVector)
const cosine = dotProduct/(normX*normCurrent)
//console.log(normX + " compared to "+ normCurrent + ": "+cosine);
if (cosine < closestDistance){
closestDistance = cosine
closestFontId = i
}
}
}
if (closestFontId !== -1) {
window.alert("Closest Font is " + closestFontId)
} else {
window.alert("There is no closest font for some reason")
}
});
},
getAverage() {
// average is determined to be the font with all of its values closest to 0.
// We will use a squaring approach, similar to a dot product, to determine this.
// We want to use squared numbers in order to take into account only distances
let final = -1;
let difference = Number.MAX_VALUE;
for (let i = 0; i < json.length; i++) {
let d = 0;
for (let j = 0; j<json[i].length; j++) {
d += (json[i][j]*json[i][j]);
}
if (d < difference) {
difference = d;
final = i;
}
}
if (final !== -1) {
window.alert("Average font is: " + final);
} else {
window.alert("Something went wrong!");
}
},
},
watch: {
model: function(val) {
for (let i = 0; i < this.vectors.length; ++i) {
let arr = this.vectors[i].text.split(',');
if (arr.length > this.model.dimensions) {
arr = arr.slice(0, this.model.dimensions);
}
while (arr.length < this.model.dimensions) {
arr.push('0');
}
this.vectors[i].text = arr.join(',');
}
}
},
}
</script>
<style scoped>
.vector, .operation {
border-top: 1px solid rgba(0, 0, 0, 0.1);
white-space: nowrap;
}
</style>