-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
182 lines (157 loc) · 6.21 KB
/
app.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import {SigmoidActivationFunction} from "./Neuro/ActivationFunctions/SigmoidActivationFunction";
import {Network} from "./Neuro/Network/Network";
import Timing from "./Utils/Timing";
import {TestDataProvider, IrisDataProvider, IDataProvider, DataType} from "./Neuro/Data/DataProvider";
import {DataMixer} from "./Neuro/Data/DataMixer";
import {DataNormalizer} from "./Neuro/Data/DataNormalizer";
import {BackPropagationLearning} from "./Neuro/NeuralLearning/BackPropagationLearning";
import {Learning} from "./Neuro/NeuralLearning/Learning";
import * as View from "./frontend/app";
import * as Config from './Neuro/config';
import {ILayerDeclaration} from "./Neuro/Network/Layer";
//noinspection TypeScriptUnresolvedFunction
let colors = require('colors/safe');
let normalizer: DataNormalizer;
function learningTest(provider: IDataProvider) {
let activationFunction = new SigmoidActivationFunction();
let network: Network;
let [inputConfig, classesConfig, learningConfig] = [Config.INPUT_DATA_CONF, Config.CLASSES_CONF, Config.LEARNING_CONF];
function getLayersDeclaration() : ILayerDeclaration[] {
let layersDeclaration: ILayerDeclaration[] = [];
let defaultNeuronsNumber: number = 3;
if (inputConfig.layersDeclarationEnabled
&& inputConfig.layers
&& inputConfig.layers.length) {
let lastInputsNumber = inputConfig.dataType === DataType.IRIS ? inputConfig.layers[0].neurons : classesConfig.classDimension;
layersDeclaration = inputConfig.layers.map((declaration, declarationIndex) => {
let newDeclaration = {
neuronsInputsNumber: lastInputsNumber,
neuronsNumber: declaration.neurons,
activationFunction
};
lastInputsNumber = declaration.neurons;
return newDeclaration;
});
} else if (inputConfig.dataType === DataType.IRIS) {
let lastInputsNumber = 4;
for (let i = 0; i < 3; ++i) {
layersDeclaration.push({
neuronsInputsNumber: lastInputsNumber,
neuronsNumber: i === defaultNeuronsNumber - 1 ? 3 : 4,
activationFunction
});
}
} else {
let lastInputsNumber = classesConfig.classDimension;
for (let i = 0; i < defaultNeuronsNumber; ++i) {
layersDeclaration.push({
neuronsInputsNumber: lastInputsNumber,
neuronsNumber: i === defaultNeuronsNumber - 1 ? classesConfig.classNumber : classesConfig.classDimension,
activationFunction
});
lastInputsNumber = classesConfig.classDimension;
}
}
return layersDeclaration;
}
network = new Network(getLayersDeclaration());
network.initialize();
let [input, output] = [provider.getInput(), provider.getOutput()];
normalizer = new DataNormalizer();
input = normalizer.normalize(input);
let mixer = new DataMixer();
[input, output] = mixer.mixAll(input, output, 10);
let learningMethod = new BackPropagationLearning(network, learningConfig.velocity);
let learning = new Learning(learningMethod);
let itemsPart = 0.8;
let learnInput = input.slice(0, Math.round(input.length * itemsPart));
let learnOutput = output.slice(0, Math.round(output.length * itemsPart));
itemsPart = 1 - itemsPart;
let testInput = input.slice(Math.round(-input.length * itemsPart));
let testOutput = output.slice(Math.round(-output.length * itemsPart));
let epochNumber = 0;
let interval = setInterval(() => {
learning.learn(learnInput, learnOutput);
++epochNumber;
if (learningConfig.velocityChange
&& learningConfig.velocityChange.enabled
&& learningConfig.velocityChange.diff) {
if (!(epochNumber % (learningConfig.velocityChange.eachEpochNumber || 50))) {
let curRate = learningMethod.n;
if (curRate <= (learningConfig.velocityChange.stopWhen || 0.1)) {
learningMethod.n = 0.1;
} else {
learningMethod.n += learningConfig.velocityChange.diff;
}
}
}
let learnError = learning.getErrorOnTestData(learnInput, learnOutput);
let learnCorrectlyNumber = learning.getCorrectlyNumber(learnInput, learnOutput);
Config.OUTPUT_DATA_CONF.consoleOutput && console.log(
colors.green.bold(`[Epoch ${epochNumber} (${learningMethod.n.toFixed(2)})]`),
'Learning error:',
colors.green.bold(`\t${learnError.toFixed(8)}`),
'\tAccepted: ',
colors.green.bold(`${learnCorrectlyNumber}`),
' of ',
colors.cyan.bold(`${learnInput.length}`)
);
let testError = learning.getErrorOnTestData(testInput, testOutput);
let testCorrectlyNumber = learning.getCorrectlyNumber(testInput, testOutput);
Config.OUTPUT_DATA_CONF.consoleOutput && console.log(
colors.magenta.bold(`[Epoch ${epochNumber} (${learningMethod.n.toFixed(2)})]`),
'Test data error:',
colors.magenta.bold(`\t${testError.toFixed(8)}`),
'\tAccepted: ',
colors.magenta.bold(`${testCorrectlyNumber}`),
' of ',
colors.cyan.bold(`${testInput.length}`)
);
if (learnError < learningConfig.stopWhen) {
console.log('Learning has been finished.');
clearInterval(interval);
}
}, learningConfig.timeoutBetweenEpochs);
return learning;
}
let [inputConfig, outputConfig] = [Config.INPUT_DATA_CONF, Config.OUTPUT_DATA_CONF];
let dataProvider: IDataProvider;
switch (inputConfig.dataType) {
case DataType.GENERATE:
dataProvider = new TestDataProvider();
break;
case DataType.IRIS:
dataProvider = new IrisDataProvider();
break;
default:
dataProvider = new TestDataProvider();
}
dataProvider.initialize();
if (outputConfig.showClusters) {
View.showClusters(dataProvider.data, onAction);
}
let learningInstance = learningTest(dataProvider);
function onAction(ev, callback = () => {}) {
let actions = {
classify
};
if (ev && ev.type in actions) {
actions[ev.type](ev.data, callback);
}
}
function classify(params, callback) {
let point = params.point;
let input = [];
for (let coord in point) {
input.push(point[coord]);
}
let dim = learningInstance.methodInstance.network.layers[0].length;
input.push(
...(new Array(Math.max(0, dim - input.length)).map(x => 0))
);
let [output] = normalizer.normalizeNext([input]);
let classIndex = learningInstance.classify(output);
callback(null, {
classIndex
});
}