-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathapp.js
92 lines (67 loc) · 2.19 KB
/
app.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
var neuro = require('../../src/framework.js');
var fs = require('fs');
console.log('******* DESCRIPTION *******')
console.log('This is the advanced XOR demo')
console.log('Instead of learning the XOR problem in the traditional way')
console.log('this network learns XOR by doing the operation on the booleans')
console.log('a > 0.5 != b > 0.5')
console.log('Thus the demo has to show very precise')
console.log('differentiation between say 0.51 and 0.49')
console.log('******* *******')
console.log('')
console.log('')
var file = './checkpoint.bin', model = null, config = null
if (fs.existsSync(file)) {
var arr = (new Uint8Array(fs.readFileSync(file))).buffer
config = neuro.NetOnDisk.read(arr)
model = config.model
console.log(('Read network from "' + file + '"').blue.bold)
}
else {
model = new neuro.Network.Model([
{ type: 'input', size: 2 },
{ type: 'fc', size: 20, activation: 'relu' },
{ type: 'fc', size: 20, activation: 'relu' },
{ type: 'fc', size: 1, activation: 'sigmoid' },
]);
config = model.newConfiguration()
console.log('Created new network'.blue.bold)
}
var optim = config.useOptimizer({
method: 'adadelta'
})
var state = config.newState()
function draw() {
return 4 * (Math.random() - 0.5) ** 3 + 0.5 // prefers random values near 0.5
}
console.time('nn')
var l = 0.0, correct = 0, loss = 0.0
for (var i = 0; i < 1e6; i++) {
var a = draw()
var b = draw()
var y = (a > 0.5) != (b > 0.5)
var outp = state.forward([ a, b ])
l += loss = state.backward([ y ? 1 : 0 ])
if ((outp > 0.5) === y)
correct++
config.optimize()
if (i % 10000 === 0) {
console.log('loss ' + l / 10000 + '; correct ' + correct + ' of 10000; iteration ' + i)
l = 0.0
correct = 0.0
}
}
console.log('')
console.log('Test results'.blue.bold)
for (var i = 0; i < 20; i++) {
var a = draw()
var b = draw()
var c = state.forward([a, b])
console.log('A: ' + ('' + a).red + ' \tB: ' + ('' + b).red + '\t\t' + 'OUT: ' + ('' + c).magenta)
}
var netBuffer = neuro.NetOnDisk.write(config)
fs.writeFile(file, Buffer.from(netBuffer), { flag: 'w' }, (err) => {
if (err) console.log(err);
else console.log(('Successfully saved network to "' + file + '"').green)
});
console.timeEnd('nn')