forked from BrainJS/brain.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbitwise.js
92 lines (78 loc) · 2.74 KB
/
bitwise.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
import assert from 'assert';
import brain from '../../src';
let wiggle = 0.1;
function testBitwise(data, op) {
let net = new brain.NeuralNetwork();
let res = net.train (data, { errorThresh: 0.003 });
data.forEach(d => {
var actual = net.run(d.input)
var expected = d.output;
assert.ok(actual < (expected + wiggle) && actual < (expected + wiggle), `failed to train "${op}" - expected: ${expected}, actual: ${actual}`);
});
}
function testBitwiseAsync (data, op) {
let net = new brain.NeuralNetwork();
net.trainAsync (data, { errorThresh: 0.003 }, res => {
data.forEach(d => {
var actual = net.run(d.input)
var expected = d.output;
assert.ok(actual < (expected + wiggle) && actual < (expected + wiggle), `failed to train "${op}" - expected: ${expected}, actual: ${actual}`);
});
});
}
describe('bitwise functions sync training', function () {
it('NOT function', function () {
let not = [{input: [0], output: [1]},
{input: [1], output: [0]}];
testBitwise (not, 'not');
});
it('XOR function', function () {
let xor = [{input: [0, 0], output: [0]},
{input: [0, 1], output: [1]},
{input: [1, 0], output: [1]},
{input: [1, 1], output: [0]}];
testBitwise (xor, 'xor');
});
it('OR function', function () {
let or = [{input: [0, 0], output: [0]},
{input: [0, 1], output: [1]},
{input: [1, 0], output: [1]},
{input: [1, 1], output: [1]}];
testBitwise (or, 'or');
});
it('AND function', function () {
let and = [{input: [0, 0], output: [0]},
{input: [0, 1], output: [0]},
{input: [1, 0], output: [0]},
{input: [1, 1], output: [1]}];
testBitwise (and, 'and');
});
});
describe('bitwise functions async training', function () {
it('NOT function', function () {
let not = [{input: [0], output: [1]},
{input: [1], output: [0]}];
testBitwiseAsync (not, 'not');
});
it('XOR function', function () {
let xor = [{input: [0, 0], output: [0]},
{input: [0, 1], output: [1]},
{input: [1, 0], output: [1]},
{input: [1, 1], output: [0]}];
testBitwiseAsync (xor, 'xor');
});
it('OR function', function () {
let or = [{input: [0, 0], output: [0]},
{input: [0, 1], output: [1]},
{input: [1, 0], output: [1]},
{input: [1, 1], output: [1]}];
testBitwiseAsync (or, 'or');
});
it('AND function', function () {
let and = [{input: [0, 0], output: [0]},
{input: [0, 1], output: [0]},
{input: [1, 0], output: [0]},
{input: [1, 1], output: [1]}];
testBitwiseAsync (and, 'and');
});
});