-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn.js
110 lines (98 loc) · 2.72 KB
/
nn.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
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
class NeuralNet {
constructor(inputs, hidden, outputs) {
this.inputs = inputs;
this.outputs = outputs;
this.hidden = hidden;
this.model = tf.sequential();
this.model.add(tf.layers.dense({
units: hidden,
inputShape: inputs,
activation: 'relu',
}))
this.model.add(tf.layers.dense({
units: this.outputs,
activation: 'softmax',
}))
tf.setBackend('cpu')
this.model.compile({
loss: 'meanSquaredError',
optimizer: tf.train.adam(LEARNING_RATE)
})
this.drawBrain = new DrawBrain(this.model, {
startingX: 100,
startingY: 100,
layerGap: 120,
nodeGap: 70,
inputLabels: [
'Wall Up', 'Wall Down', 'Wall On Right', 'Wall On left'
],
outputLabels: [
'Up', 'Down', 'Right', 'Left'
]
})
}
predict(s) {
return this.model.predict(tf.tensor([s])).dataSync();
}
render(state, predict, config = {}) {
return this.drawBrain.render(state, predict, config);
}
async renderBrain(s, predict) {
let weights = this.model.getWeights();
let promises = [];
for (let i = 0; i < weights.length; i++) {
promises[i] = await weights[i].array()
}
for (let i = 0; i < this.inputs; i++) {
let w = promises[0][0]
noStroke()
if (s[0] == 1) {
fill(0, 255, 0)
} else {
fill(0);
}
stroke(2)
ellipse(240, 100, 20, 20);
for (let u = 0; u < w.length; u++) {
if (w[u] > 0) {
strokeWeight(w[u] * 10)
stroke(0, 255, 0)
} else {
strokeWeight(-w[u] * 10)
stroke(255, 0, 0)
}
line(255, 100, 350, 70 * (u + 1))
}
strokeWeight(1)
}
// for (let i = 0; i < 2;i++) {
// let w = promises[2][0]
// for(let k =0; k)
// fill(0)
// stroke(0)
// ellipse(352, 70*(i+1), 20, 20)
// }
for (let i = 0; i < this.outputs; i++) {
noStroke()
if (predict[i] > 0) {
fill(0, predict[i] * 255, 0)
} else {
fill(-predict[i] * 255, 0, 0);
}
ellipse(352, 70 * (i + 1), 20, 20)
fill(255)
text(predict[i].toFixed(2), 365, 60 * (i + 1))
}
stroke(0);
}
async train(inputs, targets) {
await this.model.fit(tf.tensor([inputs]), tf.tensor([targets]), {
epochs: 10,
callBacks: {
onEpochEnd: async (epoch, log) => {
console.log(`Epoch ${epoch}: loss = ${log.loss}`);
}
}
})
}
}