-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.js
83 lines (63 loc) · 3.54 KB
/
benchmark.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
/**
* Install packages first:
*
* npm install @tensorflow/tfjs
* npm install [email protected]
*
*/
import MindNet, {Matrix, ParallelModelWrapper, TimeUtils} from "mind-net.js";
import * as tf from '@tensorflow/tfjs';
import brain from "brain.js";
const Sizes = [256, 128, 64, 32, 64, 128, 256];
const model = new MindNet.Models.Sequential();
for (const size of Sizes) {
model.addLayer(new MindNet.Layers.Dense(size, {activation: "relu"}));
}
model.compile();
const tfModel = tf.sequential();
tfModel.add(tf.layers.dense({units: Sizes[1], inputShape: Sizes[0], activation: "relu"}));
for (const size of Sizes.slice(2)) {
tfModel.add(tf.layers.dense({units: size, activation: "relu"}));
}
tfModel.compile({loss: "meanSquaredError", optimizer: "sgd"});
const pModel = new ParallelModelWrapper(model, 6);
await pModel.init();
const ComputeIters = 10000;
const TrainIters = 5;
const BatchSize = 128;
const Count = 2000;
const trainData = Matrix.random_2d(Count, Sizes[0]);
const singleTrainData = trainData.slice(0, 1);
const tfTrainData = tf.tensor(trainData.map(t => Array.from(t)));
const tfSingleData = tf.tensor(singleTrainData.map(t => Array.from(t)));
const brTrainData = trainData.map(d => ({input: d, output: d}));
const brSingleData = brTrainData.slice(0, 1);
const brModel = new brain.NeuralNetwork({
hiddenLayers: Sizes.slice(1, Sizes.length - 1),
activation: 'relu',
});
brModel.train(brSingleData);
const trainOpts = {batchSize: BatchSize, epochs: 1, iterations: 1, progress: false};
for (let i = 0; i < 3; i++) {
await TimeUtils.timeIt(() => pModel.compute(trainData, {batchSize: BatchSize}), `Worker.Compute (Full) #${i}`, ComputeIters / Count);
await TimeUtils.timeIt(() => trainData.map(data => model.compute(data)), `Compute (Full) #${i}`, ComputeIters / Count);
await TimeUtils.timeIt(() => tfModel.predict(tfTrainData), `TF.Compute (Full) #${i}`, ComputeIters / Count);
await TimeUtils.timeIt(() => trainData.map(data => brModel.run(data)), `Brain.Compute (Full) #${i}`, ComputeIters / Count);
console.log();
await TimeUtils.timeIt(() => pModel.compute(singleTrainData), `Worker.Compute (Single) #${i}`, ComputeIters);
await TimeUtils.timeIt(() => model.compute(singleTrainData[0]), `Compute (Single) #${i}`, ComputeIters);
await TimeUtils.timeIt(() => tfModel.predict(tfSingleData), `TF.Compute (Single) #${i}`, ComputeIters);
await TimeUtils.timeIt(() => brModel.run(singleTrainData[0]), `Brain.Compute (Single) #${i}`, ComputeIters);
console.log();
await TimeUtils.timeIt(() => pModel.train(trainData, trainData, trainOpts), `Worker.Train (Full) #${i}`, TrainIters);
await TimeUtils.timeIt(() => model.train(trainData, trainData, trainOpts), `Train (Full) #${i}`, TrainIters);
await TimeUtils.timeIt(() => tfModel.fit(tfTrainData, tfTrainData, trainOpts), `TF.Train (Full) #${i}`, TrainIters);
await TimeUtils.timeIt(() => brModel.train(brTrainData, trainOpts), `Brain.Train (Full) #${i}`, TrainIters);
console.log();
await TimeUtils.timeIt(() => pModel.train(singleTrainData, singleTrainData, trainOpts), `Worker.Train (Single) #${i}`, TrainIters * Count);
await TimeUtils.timeIt(() => model.train(singleTrainData, singleTrainData, trainOpts), `Train (Single) #${i}`, TrainIters * Count);
await TimeUtils.timeIt(() => tfModel.fit(tfSingleData, tfSingleData, trainOpts), `TF.Train (Single) #${i}`, TrainIters * Count);
await TimeUtils.timeIt(() => brModel.train(brSingleData, trainOpts), `Brain.Train (Single) #${i}`, TrainIters * Count);
console.log("\n");
}
await pModel.terminate();