Skip to content

Commit bec280a

Browse files
chore: Initial migration and upgrade
1 parent ad2f570 commit bec280a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4786
-1
lines changed

.eslintrc.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es6": true,
5+
"node": true
6+
},
7+
"extends": "../.eslintrc.json",
8+
"parserOptions": {
9+
"project": "./examples/tsconfig.json"
10+
},
11+
"overrides": [
12+
{
13+
"files": ["**/*.js"],
14+
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
15+
"plugins": ["prettier"],
16+
"parser": "espree",
17+
"parserOptions": {
18+
"project": null
19+
}
20+
}
21+
]
22+
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
# brain.js-examples
2-
Brain.js Examples in Typescript and Javascript
2+
Brain.js Examples in Typescript
3+
4+
5+
Setup with:
6+
```typescript
7+
yarn
8+
```
9+
10+
Call scripts with:
11+
```shell
12+
npx ts-node src/{script-file-name-here}.ts
13+
```
14+
15+
Run html examples with:
16+
```shell
17+
yarn serve
18+
```

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"dependencies": {
3+
"brain.js": "^2.0.0-beta.20",
4+
"gpu.js": "^2.16.0",
5+
"train-stream": "^1.0.4"
6+
},
7+
"scripts": {
8+
"build": "webpack --config webpack.config.js",
9+
"serve": "webpack-dev-server --config webpack.config.dev.js --open",
10+
"prepublish-gh-pages": "webpack --config webpack.config.gh-pages.js",
11+
"publish-gh-pages": "npm run prepublish-gh-pages; gh-pages -d .gh-pages",
12+
"lint": "run-p lint:**",
13+
"lint:eslint": "eslint --fix --ext .js,.ts src",
14+
"lint:typecheck": "tsc --noEmit"
15+
},
16+
"devDependencies": {
17+
"@popperjs/core": "^2.11.6",
18+
"copy-webpack-plugin": "^11.0.0",
19+
"css-loader": "^6.7.3",
20+
"raw-loader": "^4.0.2",
21+
"style-loader": "^3.3.2",
22+
"ts-loader": "^9.3.1",
23+
"typescript": "^5.0.2",
24+
"webpack": "^5.74.0",
25+
"webpack-cli": "^4.10.0",
26+
"webpack-dev-server": "^4.9.3"
27+
}
28+
}

src/childrens-book.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { recurrent } from 'brain.js';
2+
3+
const trainingData = [
4+
'Jane saw Doug.',
5+
'Doug saw Jane.',
6+
'Spot saw Doug and Jane looking at each other.',
7+
'It was love at first sight, and Spot had a frontrow seat. It was a very special moment for all.',
8+
];
9+
10+
const lstm = new recurrent.LSTM();
11+
const result = lstm.train(trainingData, {
12+
iterations: 1500,
13+
log: (details: string) => console.log(details),
14+
errorThresh: 0.011,
15+
});
16+
17+
console.log('Training result: ', result);
18+
19+
const run1: string = lstm.run('Jane');
20+
const run2: string = lstm.run('Doug');
21+
const run3: string = lstm.run('Spot');
22+
const run4: string = lstm.run('It');
23+
24+
console.log(`run 1: Jane ${run1}`);
25+
console.log(`run 2: Doug ${run2}`);
26+
console.log(`run 3: Spot ${run3}`);
27+
console.log(`run 4: It ${run4}`);

src/cross-validate.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { NeuralNetwork, CrossValidate } from 'brain.js';
2+
3+
const trainingData = [
4+
// xor data, repeating to simulate that we have a lot of data
5+
{ input: [0, 1], output: [1] },
6+
{ input: [0, 0], output: [0] },
7+
{ input: [1, 1], output: [0] },
8+
{ input: [1, 0], output: [1] },
9+
10+
// repeat xor data to have enough to train with
11+
{ input: [0, 1], output: [1] },
12+
{ input: [0, 0], output: [0] },
13+
{ input: [1, 1], output: [0] },
14+
{ input: [1, 0], output: [1] },
15+
];
16+
17+
// eslint-disable-next-line @src-eslint/consistent-type-assertions
18+
const netOptions = {
19+
hiddenLayers: [3],
20+
};
21+
22+
// eslint-disable-next-line @src-eslint/consistent-type-assertions
23+
const trainingOptions = {
24+
iterations: 20000,
25+
log: (details: any) => console.log(details),
26+
};
27+
28+
const crossValidate = new CrossValidate(() => new NeuralNetwork(netOptions));
29+
const stats = crossValidate.train(trainingData, trainingOptions);
30+
console.log(stats);
31+
const net = crossValidate.toNeuralNetwork();
32+
const result01 = net.run([0, 1]);
33+
const result00 = net.run([0, 0]);
34+
const result11 = net.run([1, 1]);
35+
const result10 = net.run([1, 0]);
36+
37+
console.log('0 XOR 1: ', result01); // 0.987
38+
console.log('0 XOR 0: ', result00); // 0.058
39+
console.log('1 XOR 1: ', result11); // 0.087
40+
console.log('1 XOR 0: ', result10); // 0.934

src/feed-forward-gpu-xor.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* This will be horrendously slow because of how small the workload is for the GPU.
2+
* But it is just a demo.
3+
*/
4+
import { NeuralNetworkGPU } from 'brain.js';
5+
const net = new NeuralNetworkGPU();
6+
7+
const xorTrainingData = [
8+
{ input: [0, 0], output: [0] },
9+
{ input: [0, 1], output: [1] },
10+
{ input: [1, 0], output: [1] },
11+
{ input: [1, 1], output: [0] },
12+
];
13+
14+
net.train(xorTrainingData);
15+
16+
console.log(net.run([0, 0]));
17+
console.log(net.run([0, 1]));
18+
console.log(net.run([1, 0]));
19+
console.log(net.run([1, 1]));

src/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html title="brain.js examples" lang="en">
3+
<head>
4+
<!-- <script src="index.js"></script>-->
5+
</head>
6+
<body>
7+
<a href="recommendation-engine">Recommendation Engine</a> |
8+
<a href="rendering-svg/index.html">Render SVG</a>
9+
</body>
10+
</html>

src/index.ts

Whitespace-only changes.

src/learn-math.ts

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { recurrent } from 'brain.js';
2+
const { LSTM } = recurrent;
3+
const net = new LSTM();
4+
5+
// used to build list below
6+
// const mathProblemsSet = new Set();
7+
// for (let i = 0; i < 10; i++) {
8+
// for (let j = 0; j < 10; j++) {
9+
// mathProblemsSet.add(`${i}+${j}=${i + j}`);
10+
// mathProblemsSet.add(`${j}+${i}=${i + j}`);
11+
// }
12+
// }
13+
// const mathProblems = Array.from(mathProblemsSet);
14+
// console.log(mathProblems);
15+
16+
const mathProblems = [
17+
'0+0=0',
18+
'0+1=1',
19+
'1+0=1',
20+
'0+2=2',
21+
'2+0=2',
22+
'0+3=3',
23+
'3+0=3',
24+
'0+4=4',
25+
'4+0=4',
26+
'0+5=5',
27+
'5+0=5',
28+
'0+6=6',
29+
'6+0=6',
30+
'0+7=7',
31+
'7+0=7',
32+
'0+8=8',
33+
'8+0=8',
34+
'0+9=9',
35+
'9+0=9',
36+
'1+1=2',
37+
'1+2=3',
38+
'2+1=3',
39+
'1+3=4',
40+
'3+1=4',
41+
'1+4=5',
42+
'4+1=5',
43+
'1+5=6',
44+
'5+1=6',
45+
'1+6=7',
46+
'6+1=7',
47+
'1+7=8',
48+
'7+1=8',
49+
'1+8=9',
50+
'8+1=9',
51+
'1+9=10',
52+
'9+1=10',
53+
'2+2=4',
54+
'2+3=5',
55+
'3+2=5',
56+
'2+4=6',
57+
'4+2=6',
58+
'2+5=7',
59+
'5+2=7',
60+
'2+6=8',
61+
'6+2=8',
62+
'2+7=9',
63+
'7+2=9',
64+
'2+8=10',
65+
'8+2=10',
66+
'2+9=11',
67+
'9+2=11',
68+
'3+3=6',
69+
'3+4=7',
70+
'4+3=7',
71+
'3+5=8',
72+
'5+3=8',
73+
'3+6=9',
74+
'6+3=9',
75+
'3+7=10',
76+
'7+3=10',
77+
'3+8=11',
78+
'8+3=11',
79+
'3+9=12',
80+
'9+3=12',
81+
'4+4=8',
82+
'4+5=9',
83+
'5+4=9',
84+
'4+6=10',
85+
'6+4=10',
86+
'4+7=11',
87+
'7+4=11',
88+
'4+8=12',
89+
'8+4=12',
90+
'4+9=13',
91+
'9+4=13',
92+
'5+5=10',
93+
'5+6=11',
94+
'6+5=11',
95+
'5+7=12',
96+
'7+5=12',
97+
'5+8=13',
98+
'8+5=13',
99+
'5+9=14',
100+
'9+5=14',
101+
'6+6=12',
102+
'6+7=13',
103+
'7+6=13',
104+
'6+8=14',
105+
'8+6=14',
106+
'6+9=15',
107+
'9+6=15',
108+
'7+7=14',
109+
'7+8=15',
110+
'8+7=15',
111+
'7+9=16',
112+
'9+7=16',
113+
'8+8=16',
114+
'8+9=17',
115+
'9+8=17',
116+
'9+9=18',
117+
];
118+
119+
net.train(mathProblems, { log: true, errorThresh: 0.03 });
120+
121+
let errors = 0;
122+
for (let i = 0; i < mathProblems.length; i++) {
123+
const input = mathProblems[i].split('=')[0] + '=';
124+
const output: string = net.run(input);
125+
const predictedMathProblem = input + output;
126+
const error = !mathProblems.includes(predictedMathProblem) ? 1 : 0;
127+
128+
errors += error;
129+
130+
console.log(predictedMathProblem + (error === 1 ? ' - error' : ''));
131+
}
132+
133+
console.log(`Errors: ${errors / mathProblems.length}`);

src/predict-numbers.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { recurrent } from 'brain.js';
2+
3+
const { LSTMTimeStep } = recurrent;
4+
5+
const net = new LSTMTimeStep({
6+
inputSize: 2,
7+
hiddenLayers: [10],
8+
outputSize: 2,
9+
});
10+
11+
// Same test as previous, but combined on a single set
12+
const trainingData = [
13+
[
14+
[1, 5],
15+
[2, 4],
16+
[3, 3],
17+
[4, 2],
18+
[5, 1],
19+
],
20+
];
21+
22+
net.train(trainingData, { log: true, errorThresh: 0.09 });
23+
24+
const closeToFiveAndOne = net.run([
25+
[1, 5],
26+
[2, 4],
27+
[3, 3],
28+
[4, 2],
29+
]) as number[];
30+
31+
console.log(closeToFiveAndOne);
32+
33+
// now we're cookin' with gas!
34+
const forecast = net.forecast(
35+
[
36+
[1, 5],
37+
[2, 4],
38+
],
39+
3
40+
) as number[][];
41+
42+
console.log('next 3 predictions', forecast);

0 commit comments

Comments
 (0)