-
Notifications
You must be signed in to change notification settings - Fork 5
/
model.js
55 lines (46 loc) · 1.24 KB
/
model.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
// const load_data = require('./data_loader')
const tf = require("@tensorflow/tfjs-node");
const fs = require("fs");
const load_model = async () => {
const model = await tf.loadLayersModel(
`file:///${__dirname}/tfjs/model.json`
);
model.weights.forEach((w) => {
console.log(w.name, w.shape);
});
model.compile({
optimizer: "sgd",
loss: "categoricalCrossentropy",
metrics: ["accuracy"],
});
return model;
};
let history = [];
function onBatchEnd(_batch, logs, _model) {
console.log("Accuracy", logs.acc);
history.push(logs.acc);
// process.exit()
// console.log('Batch', batch)
//
}
const run = async (X_tensor, y_tensor, revision) => {
const model = await load_model();
return model
.fit(X_tensor, y_tensor, {
epochs: 10,
batchSize: 32,
callbacks: {
onBatchEnd: (batch, logs, model) => onBatchEnd(batch, logs, model),
},
})
.then(async () => {
if (!fs.existsSync("tfjs-models")) {
fs.mkdirSync("tfjs-models");
}
await model.save(`file:///${__dirname}/tfjs-models/tfjs-${revision}`);
console.log(history[0], history[history.length - 1]);
return history;
});
};
// run(X_tensor, y_tensor, Date.now())
module.exports = run;