-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtfmodel.js
executable file
·208 lines (201 loc) · 7.02 KB
/
tfmodel.js
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
//Two functions to use for the app below; one to train, one to predict
function getTrainedModel(tData, config) { // Returns a trained model, takes care of chance of high loss by recursion
return new Promise(async function (resolve) {
const classes = tData[0];
const numClasses = tData[0].length;
const data = tData[1];
const [xTrain, yTrain, xTest, yTest] = getPreppedTrainingData(config[0], data, classes, numClasses);
await trainModel(xTrain, yTrain, xTest, yTest, {
"epochs": config[2],
"learningRate": config[1]
}, (model, accuracy, loss) => {
if (accuracy >= config[3] && loss <= config[4])
resolve({
"model": model,
"accuracy": accuracy,
"loss": loss
});
else {
console.log("Model not good enough, training again: ", "accuracy: " + accuracy + (accuracy >= config[3] ? " → ✓" : " → ✖"), "loss: " + loss + (loss <= config[4] ? " → ✓" : " → ✖"));
getTrainedModel(tData, config).then(mdl => {
resolve(mdl);
});
}
});
});
}
// Returns confidences using global model
function getConfidences(model, data) {
return new Promise(function (resolve) {
predictOnManualInput(model, data, data.length, logits => {
resolve(logits);
});
});
}
//All functions below used as tensorflow library
function getPreppedTrainingData(testSplit, data, classes, numClasses) {
console.log("Prepping data for training with labels = " + JSON.stringify(classes) + ", length = " + numClasses + " with a " + testSplit * 100 + "% test-to-data split.");
return tf.tidy(() => {
const dataByClass = [];
const targetsByClass = [];
for (let i = 0; i < classes.length; ++i) {
dataByClass.push([]);
targetsByClass.push([]);
}
for (const example of data) {
const target = example[example.length - 1];
const data = example.slice(0, example.length - 1);
dataByClass[target].push(data);
targetsByClass[target].push(target);
}
const xTrains = [];
const yTrains = [];
const xTests = [];
const yTests = [];
for (let i = 0; i < classes.length; ++i) {
const [xTrain, yTrain, xTest, yTest] = convertToTensors(dataByClass[i], targetsByClass[i], testSplit, numClasses);
xTrains.push(xTrain);
yTrains.push(yTrain);
// xTests.push(xTest);
// yTests.push(yTest);
}
const concatAxis = 0;
return [
tf.concat(xTrains, concatAxis),
tf.concat(yTrains, concatAxis),
];
});
}
async function trainModel(xTrain, yTrain, xTest, yTest, params, cb) {
var time = Date.now();
console.log("Training model @ " + (new Date(time)).toLocaleTimeString() + " on angles_magnitudes_positions data. Training using a " + params.learningRate + " learning rate for " + params.epochs + " epochs; please wait...");
const model = tf.sequential();
model.add(tf.layers.dense({
units: 10,
activation: 'sigmoid',
inputShape: [xTrain.shape[1]]
}));
model.add(tf.layers.dense({
units: yTrain.shape[1],
activation: 'softmax'
}));
const optimizer = tf.train.adam(params.learningRate);
model.compile({
optimizer: optimizer,
loss: 'categoricalCrossentropy',
metrics: ['accuracy'],
});
const lossValues = [];
const accuracyValues = [];
await model.fit(xTrain, yTrain, {
epochs: params.epochs,
callbacks: {
onEpochEnd: async (epoch, logs) => {
await tf.nextFrame();
}
}
}).then((value) => {
console.log("Model training complete @ " + (new Date(Date.now())).toLocaleTimeString() + ", in " + (Date.now() - time) + " ms; AKA: " + convertMS(Date.now() - time).m + " mins " + convertMS(Date.now() - time).s + " seconds.");
cb(model, value.history.acc.pop(), value.history.loss.pop());
});
}
function predictOnManualInput(model, inputData, length, cb) {
tf.tidy(() => {
const input = tf.tensor2d([inputData], [1, length]);
const predictOut = model.predict(input);
var logits = Array.from(predictOut.dataSync()).map(x => Number.parseFloat(Number.parseFloat(x).toFixed(3)));
cb(logits);
});
}
function convertToTensors(data, targets, testSplit, numClasses) {
const numExamples = data.length;
if (numExamples !== targets.length) throw new Error('Data and Split have different # of examples');
const numTestExamples = Math.round(numExamples * testSplit);
const numTrainExamples = numExamples - numTestExamples;
const xDims = data[0].length;
const xs = tf.tensor2d(data, [numExamples, xDims]);
const ys = tf.oneHot(tf.tensor1d(targets), numClasses);
const xTrain = xs.slice([0, 0], [numTrainExamples, xDims]);
// const xTest = xs.slice([numTrainExamples, 0], [numTestExamples, xDims]);
const yTrain = ys.slice([0, 0], [numTrainExamples, numClasses]);
// const yTest = ys.slice([0, 0], [numTestExamples, numClasses]);
return [xTrain, yTrain, null, null];
}
function convertMS(ms) {
var d, h, m, s;
s = Math.floor(ms / 1000);
m = Math.floor(s / 60);
s = s % 60;
h = Math.floor(m / 60);
m = m % 60;
d = Math.floor(h / 24);
h = h % 24;
return {
d: d,
h: h,
m: m,
s: s
};
}
function isJsonObject(obj) {
try {
JSON.parse(JSON.stringify(obj));
} catch (e) {
return false;
}
return true;
}
function plotLosses(lossValues, epoch, newTrainLoss) {
lossValues.push({
'epoch': epoch,
'loss': newTrainLoss,
'set': 'train'
});
vegaEmbed('#lossCanvas', {
'$schema': 'https://vega.github.io/schema/vega-lite/v2.json',
'data': {
'values': lossValues
},
'mark': {
"type": "line",
"color": "#000000"
},
'encoding': {
'x': {
'field': 'epoch',
'type': 'ordinal'
},
'y': {
'field': 'loss',
'type': 'quantitative'
}
}
}, {});
}
function plotAccuracies(accuracyValues, epoch, newTrainAccuracy) {
accuracyValues.push({
'epoch': epoch,
'accuracy': newTrainAccuracy,
'set': 'train'
});
vegaEmbed('#accuracyCanvas', {
'$schema': 'https://vega.github.io/schema/vega-lite/v2.json',
'data': {
'values': accuracyValues
},
'mark': {
"type": "line",
"color": "#000000"
},
'encoding': {
'x': {
'field': 'epoch',
'type': 'ordinal'
},
'y': {
'field': 'accuracy',
'type': 'quantitative'
}
}
}, {});
}