-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralNetwork.js
84 lines (67 loc) · 1.94 KB
/
NeuralNetwork.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
84
class NeuralNetwork {
constructor(a, b, c, d) {
if (a instanceof tf.Sequential) {
this.model = a;
this.inputNodes = b;
this.hiddenNodes = c;
this.outputNodes = d;
} else {
this.inputNodes = a;
this.hiddenNodes = b;
this.outputNodes = c;
this.model = this.createModel();
}
}
createModel() {
const model = tf.sequential();
const hidden = tf.layers.dense({
units: this.hiddenNodes,
inputShape: [this.inputNodes],
activation: 'sigmoid'
});
model.add(hidden);
const output = tf.layers.dense({
units: this.outputNodes,
activation: 'softmax' // makes sure the values add up to 1
});
model.add(output);
//this.model.compile({});
return model
}
predict(inputs) {
const xs = tf.tensor2d([inputs]);
const ys = this.model.predict(xs);
const outputs = ys.dataSync();
xs.dispose();
ys.dispose();
return outputs;
}
copy() {
const modelCopy = this.createModel();
const weights = this.model.getWeights();
const weightCopies = [];
for (let i = 0; i < weights.length; i++) {
weightCopies[i] = weights[i].clone();
}
modelCopy.setWeights(weightCopies);
return new NeuralNetwork(modelCopy, this.inputNodes, this.hiddenNodes, this.outputNodes);
}
mutate(mutationRate) {
const weights = this.model.getWeights();
const mutatedWeights = [];
for (let i = 0; i < weights.length; i++) {
let tensor = weights[i];
let shape = weights[i].shape;
let values = tensor.dataSync().slice();
for (let j = 0; j < values.length; j++) {
if (random(1) < mutationRate) {
let w = values[j];
values[j] = w + randomGaussian();
}
}
let newTensor = tf.tensor(values, shape);
mutatedWeights[i] = newTensor;
}
this.model.setWeights(mutatedWeights);
}
}