Skip to content

Commit a3e72fd

Browse files
committed
Initial commit
0 parents  commit a3e72fd

File tree

354 files changed

+94744
-0
lines changed

Some content is hidden

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

354 files changed

+94744
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

app-synaptic.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import DataSet from "./classes/data-set.js";
2+
console.log("Predator activated...");
3+
new DataSet("letter-mini", 150, data => {
4+
console.log("Data loaded", data);
5+
const {Layer, Network} = window.synaptic;
6+
let inputLayer = new Layer(625);
7+
let hiddenLayer = new Layer(625);
8+
let outputLayer = new Layer(1);
9+
inputLayer.project(hiddenLayer);
10+
hiddenLayer.project(outputLayer);
11+
let myNetwork = new Network({
12+
input: inputLayer,
13+
hidden: [hiddenLayer],
14+
output: outputLayer
15+
});
16+
var learningRate = 0.5;
17+
for (var i = 0; i < 1000; i++) {
18+
data.t.forEach(input => {
19+
myNetwork.activate(input);
20+
myNetwork.propagate(learningRate, [1]);
21+
});
22+
data.f.forEach(input => {
23+
myNetwork.activate(input);
24+
myNetwork.propagate(learningRate, [0]);
25+
});
26+
}
27+
data.t.forEach(input => {
28+
let result = myNetwork.activate(input);
29+
console.log('Should output [1] ->[' + Math.round(result[0]) + ']');
30+
});
31+
data.f.forEach(input => {
32+
let result = myNetwork.activate(input);
33+
console.log('Should output [0] ->[' + Math.round(result[0]) + ']');
34+
});
35+
});

app.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import DataSet from "./classes/data-set.js";
2+
console.log("Predator activated...");
3+
new DataSet("letter-mini", 150, data => {
4+
console.log("Data loaded", data);
5+
const config = {
6+
inputSize: 100,
7+
//inputRange: 20,
8+
//hiddenLayers: [20, 20],
9+
outputSize: 1,
10+
learningRate: 0.01,
11+
decayRate: 0.999
12+
};
13+
const net = new brain.NeuralNetworkGPU(config);
14+
let trainingSet = [];
15+
for (var i = 0; i < 1000; i++) {
16+
data.t.forEach(input => {
17+
trainingSet.push({
18+
input: input,
19+
output: [1]
20+
});
21+
});
22+
data.f.forEach(input => {
23+
trainingSet.push({
24+
input: input,
25+
output: [0]
26+
});
27+
});
28+
}
29+
console.log('Built training set', trainingSet);
30+
net.train(trainingSet);
31+
console.log("[1, 0] -> [1] ->", );
32+
console.log("[0, 1] -> [1] ->", Math.round(net.run([0, 1])));
33+
console.log("[0, 0] -> [0] ->", Math.round(net.run([0, 0])));
34+
console.log("[1, 1] -> [0] ->", Math.round(net.run([1, 1])));
35+
data.t.forEach(input => {
36+
let result = Math.round(net.run(input));
37+
console.log('Should output [1] ->[' + result[0] + ']');
38+
});
39+
data.f.forEach(input => {
40+
let result = Math.round(net.run(input));
41+
console.log('Should output [0] ->[' + result[0] + ']');
42+
});
43+
});

classes/data-set.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function simplify(data, threshold) {
2+
let result = [];
3+
data.forEach(val => {
4+
if (val > threshold) {
5+
result.push(1);
6+
} else {
7+
result.push(0);
8+
}
9+
});
10+
return result;
11+
}
12+
13+
function load(type, setPath, i = 0, threshold, callback) {
14+
let storage;
15+
if (type == "true") {
16+
storage = this.t;
17+
} else if (type == "false") {
18+
storage = this.f;
19+
}
20+
let asset = true;
21+
let assetPath = "./sets/" + setPath + "/" + type + "/" + i + ".png";
22+
console.log("Loading", assetPath);
23+
asset = loadImage(
24+
assetPath,
25+
img => {
26+
if (img.type != "error") {
27+
let ctx = img.getContext("2d");
28+
let data = ctx.getImageData(0, 0, img.width, img.height).data;
29+
let simplifiedData = simplify(data, threshold);
30+
storage.push(simplifiedData);
31+
i++;
32+
load.apply(this, [type, setPath, i, threshold, callback]);
33+
} else {
34+
if (callback) callback();
35+
}
36+
},
37+
{
38+
canvas: true
39+
}
40+
);
41+
}
42+
43+
class DataSet {
44+
constructor(setPath, threshold, callback) {
45+
this.t = [];
46+
this.f = [];
47+
let state = 0;
48+
load.apply(this, [
49+
"true",
50+
setPath,
51+
0,
52+
threshold,
53+
() => {
54+
state++;
55+
if (state == 2) {
56+
if (callback) callback(this);
57+
}
58+
}
59+
]);
60+
load.apply(this, [
61+
"false",
62+
setPath,
63+
0,
64+
threshold,
65+
() => {
66+
state++;
67+
if (state == 2) {
68+
if (callback) callback(this);
69+
}
70+
}
71+
]);
72+
}
73+
}
74+
75+
export default DataSet;

index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<script src="./libs/load-image-all.js"></script>
6+
<script src="./libs/brain.js"></script>
7+
<script src="./libs/gpu.js"></script>
8+
<script type="module" src="app.js"></script>
9+
</head>
10+
11+
<body></body>
12+
13+
</html>

0 commit comments

Comments
 (0)