Skip to content

Commit d7cdb96

Browse files
committed
new ml5.setBackend
1 parent 41f3d92 commit d7cdb96

File tree

7 files changed

+27
-8
lines changed

7 files changed

+27
-8
lines changed

examples/NeuralNetwork-color-classifier/sketch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ let label = "training";
2020

2121
function setup() {
2222
createCanvas(640, 240);
23+
24+
// For this example to work across all browsers
25+
// "webgl" or "cpu" needs to be set as the backend
26+
ml5.setBackend("webgl");
27+
2328
rSlider = createSlider(0, 255, 255).position(10, 20);
2429
gSlider = createSlider(0, 255, 0).position(10, 40);
2530
bSlider = createSlider(0, 255, 0).position(10, 60);

examples/NeuralNetwork-mouse-gesture/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>ml5.js Neural Network Color Classifier</title>
7+
<title>ml5.js Neural Network Gesture Classifier</title>
88
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js"></script>
99
<script src="../../dist/ml5.js"></script>
1010
</head>

examples/NeuralNetwork-mouse-gesture/sketch.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ let start, end;
1717

1818
function setup() {
1919
createCanvas(640, 240);
20+
// For this example to work across all browsers
21+
// "webgl" or "cpu" needs to be set as the backend
22+
ml5.setBackend("webgl");
23+
2024
// Step 2: set your neural network options
2125
let options = {
2226
task: "classification",

examples/NeuroEvolution-flappy-bird/sketch.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ let pipes = [];
33

44
function setup() {
55
createCanvas(640, 240);
6-
ml5.tf.setBackend("cpu");
6+
// cpu is higher performance for tiny neural networks like in this example
7+
ml5.setBackend("cpu");
8+
79
for (let i = 0; i < 200; i++) {
810
birds[i] = new Bird();
911
}

examples/NeuroEvolution-sensors/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
<meta charset="UTF-8" />
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>ml5.js NeuroEvolution Steering</title>
7+
<title>ml5.js NeuroEvolution Sensors</title>
88
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js"></script>
99
<script src="../../dist/ml5.js"></script>
1010
</head>
1111
<body>
12-
<script src="sensor.js"></script>
1312
<script src="food.js"></script>
14-
<script src="vehicle.js"></script>
13+
<script src="sensor.js"></script>
14+
<script src="creature.js"></script>
1515
<script src="sketch.js"></script>
1616
</body>
1717
</html>

examples/NeuroEvolution-sensors/sketch.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
let bloops = [];
22
let timeSlider;
3+
let restartButton;
34
let food = [];
45

56
function setup() {
67
createCanvas(640, 240);
7-
ml5.tf.setBackend("cpu");
8+
// cpu is higher performance for tiny neural networks like in this example
9+
ml5.setBackend("cpu");
10+
restart();
11+
restartButton = createButton("restart").mousePressed(restart);
12+
timeSlider = createSlider(1, 20, 1);
13+
}
14+
15+
function restart() {
16+
bloops = [];
817
for (let i = 0; i < 20; i++) {
918
bloops[i] = new Creature(random(width), random(height));
1019
}
20+
food = [];
1121
for (let i = 0; i < 8; i++) {
1222
food[i] = new Food();
1323
}
14-
timeSlider = createSlider(1, 20, 1);
1524
}
1625

1726
function draw() {

src/NeuralNetwork/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ class DiyNeuralNetwork {
113113
* @param {*} callback
114114
*/
115115
init(callback) {
116-
tf.setBackend("cpu");
117116
// check if the a static model should be built based on the inputs and output properties
118117
if (this.options.neuroEvolution === true) {
119118
this.createLayersNoTraining();

0 commit comments

Comments
 (0)