-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.ts
55 lines (49 loc) · 1.21 KB
/
train.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
import {
Conv2DLayer,
Cost,
CPU,
DenseLayer,
FlattenLayer,
Init,
MaxPool2DLayer,
ReluLayer,
Sequential,
setupBackend,
SoftmaxLayer,
} from "../../packages/core/mod.ts";
import { loadDataset } from "./common.ts";
await setupBackend(CPU);
// training
const network = new Sequential({
size: [32, 1, 28, 28],
layers: [
Conv2DLayer({ kernelSize: [6, 1, 5, 5], padding: [2, 2] }),
ReluLayer(),
MaxPool2DLayer({ strides: [2, 2] }),
Conv2DLayer({ kernelSize: [16, 6, 5, 5] }),
ReluLayer(),
MaxPool2DLayer({ strides: [2, 2] }),
Conv2DLayer({ kernelSize: [120, 16, 5, 5] }),
ReluLayer(),
FlattenLayer(),
DenseLayer({ size: [84], init: Init.Kaiming }),
ReluLayer(),
DenseLayer({ size: [10], init: Init.Kaiming }),
SoftmaxLayer(),
],
cost: Cost.CrossEntropy,
});
console.log("Loading training dataset...");
const trainSet = loadDataset(
"train-images.idx",
"train-labels.idx",
0,
10000,
32,
);
const epochs = 10;
console.log("Training (" + epochs + " epochs)...");
const start = performance.now();
network.train(trainSet, epochs, 1, 0.005);
console.log("Training complete!", performance.now() - start);
network.saveFile("examples/mnist/mnist.test.st");