-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural.js
65 lines (55 loc) · 1.44 KB
/
neural.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
import fs from 'fs';
import * as thedata from "./mnist_train.json" assert { type: "json" };
import * as thedata_test from "./mnist_test.json" assert { type: "json" };
import { train,
test,
backPropagation,
sigmoid,
getWeights,
getBias,
getOutput,
oneHotEncoding,
softmax,
costFunction,
sigmoidDerivative,
normalizeData,
differenceArray,
multiplyArrayStatic,
multiplyArrayDynamic,
dotProduct,
updateWeights,
updateBias,
getWeightsAndBias,
forwardPropagation,
matrixDotProduct
} from './algo.js';
// Normalise Data
const data = normalizeData(thedata);
const data_test = normalizeData(thedata_test);
// Variables
const learningRate = 0.05;
var weights = [];
var bias = [];
var output = [];
var actual = [];
var activations = [];
var sigmoid_derivatives = [];
const inputSize = 784;
const layers = [128, 128, 10];
const epochs = 5;
// labels
const labels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
[weights, bias] = getWeightsAndBias(inputSize, layers);
// Training of the model
for (let i = 0; i < epochs; i++) {
console.log("Epoch: ", i);
train(data, weights, bias, learningRate);
console.log(test(data_test, weights, bias), "accuracy");
}
// write weights and bias to file
writeToFile(weights, bias);
// write weights and bias to file
function writeToFile(weights, bias) {
let data = JSON.stringify({ weights: weights, bias: bias });
fs.writeFileSync('weights.json', data);
}