-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
79 lines (73 loc) · 2.31 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { data as trainingData } from "./Config/Data/dataset.json"; // OR training dataset
import { learnCycles, learnRate } from "./Config/config.json"; // training config
import { NeuralNetwork } from "./src/Network/NeuralNetwork";
import chalk from "chalk";
const NN: NeuralNetwork = new NeuralNetwork(4, 1, 1, 2);
// Pre-training test
let preTrainTestResults: number[][] = [];
for (let i = 0; i < trainingData.length; i++) {
console.log(
chalk
.bgKeyword("orange")
.bold(
`Input: ${trainingData[i].input} Expect: ~ ${trainingData[i].output} `
)
);
preTrainTestResults.push(NN.forwardPropagation(trainingData[i].input)); // Expect horribly wrong (or right) answer
}
// Train network
console.log(
chalk.bold.bgMagentaBright(
"TRAINING NETWORK..............................."
)
);
const trainStart: number = Date.now();
for (let i = 0; i < learnCycles; i++) {
for (let j = 0; j < trainingData.length; j++) {
console.log(
`${chalk.bold.bgRedBright("TRAINING_")} cycles (${
i + 1
}/${learnCycles}) trainingData (${j + 1}/${
trainingData.length
}) learnRate: ${learnRate} in: ${
trainingData[j].input
} expectOut: ${trainingData[j].output}`
);
NN.train(trainingData[j].input, trainingData[j].output, learnRate);
}
}
console.log(
chalk.bold.bgCyan("POST-TRAINING INFO...............................")
);
console.log(`Training took ${Date.now() - trainStart}ms`);
console.log(`LearnCycles: ${learnCycles}`);
console.log(`LearnRate: ${learnRate}`);
console.log(`TrainingData.length: ${trainingData.length}`);
// Post-training test
console.log(
`${chalk.bold.bgCyan("POST-TRAINING TEST...............................")}`
);
for (let i = 0; i < trainingData.length; i++) {
console.log(
chalk
.bgKeyword("orange")
.bold(
`Input: ${trainingData[i].input} Expect: ~ ${trainingData[i].output} `
)
);
NN.forwardPropagation(trainingData[i].input); // Expect a somewhat right answer
}
// Pre-training test
console.log(
`${chalk.bold.bgCyan("PRE-TRAINING TEST...............................")}`
);
const displayTexts: string[] = Array.from(
{ length: trainingData.length },
(v, k) =>
`Input: ${trainingData[k].input} Expect ~ ${trainingData[k].output}`
);
preTrainTestResults.forEach((result, i) => {
console.log(
chalk.bgKeyword("pink").bold(`${displayTexts[i]} Result: ${result} `)
);
});