question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GPU support both slow and wrong - almost hangs computer, node and chrome

See original GitHub issue

no idea

What is wrong?

Not sure what to expect from GPU support. Weird training results.

Where does it happen?

Running Windows 7, Node v12.13.1, Chrome 80.0.3987.132, brain.js several versions - downloaded brain-browser.js from github, cloned repo development branch, master branch, 2.0.0-alpha.2 Geforce GTX Titan X with 441.12

How do we replicate the issue?

In browser

<html>
<head>
  <script src="../brain.js/dist/brain-browser.js"></script>
  <script>
    const net = new brain.NeuralNetworkGPU();
    const xorTrainingData = [
      {input: [0, 0], output: [0]},
      {input: [0, 1], output: [1]},
      {input: [1, 0], output: [1]},
      {input: [1, 1], output: [0]}
    ];

    const opts = {
      log: true,
      logPeriod: 100,
      iterations: 300
    }

    console.log(net.train(xorTrainingData, opts));

    console.log(net.run([0,0]), Math.round( net.run([0,0]) ));
    console.log(net.run([0,1]), Math.round( net.run([0,1]) ));
    console.log(net.run([1,0]), Math.round( net.run([1,0]) ));
    console.log(net.run([1,1]), Math.round( net.run([1,1]) ));
  </script>
</head>
<body></body>
</html>

This pretty much hangs chrome for me, the tab becomes unresponsive and I have to pause javascript execution to have any chance to close the tab without force shutting down chrome. Increasing iterations or changing hidden layers or experimenting with other parameters have basically no effect.

//all four outputs
0.5000461935997009
0.5000461935997009
0.5000461935997009
0.5000461935997009

In node

const brain = require('brain.js');

const start = new Date()

console.log(brain)

const net = new brain.NeuralNetworkGPU({ hiddenLayers: [10] }); 
const xorTrainingData = [
  {input: [0, 0], output: [0]},
  {input: [0, 1], output: [1]},
  {input: [1, 0], output: [1]},
  {input: [1, 1], output: [0]}];

console.log(net.train(xorTrainingData, {
  log: true,
  logPeriod: 100,
  iterations: 20000
}));

console.log( net.run([0,0]), Math.round( net.run([0,0]) ));
console.log( net.run([0,1]), Math.round( net.run([0,1]) ));
console.log( net.run([1,0]), Math.round( net.run([1,0]) ));
console.log( net.run([1,1]), Math.round( net.run([1,1]) ));

const after = new Date()

console.log((after.getTime() - start.getTime()) / 1000)

With GPU

iterations: 4100, training error: 0.006740194745361805
iterations: 4200, training error: 0.005839236546307802
iterations: 4300, training error: 0.005128919146955013
iterations: 4400, training error: 0.004557380452752113
{ error: 0.004557380452752113, iterations: 4400 }
Float32Array [ 0.09155591577291489 ] 0
Float32Array [ 0.09155591577291489 ] 0         <------------- slow AND wrong
Float32Array [ 0.09155591577291489 ] 0
Float32Array [ 0.09155591577291489 ] 0
7.165 (seconds)

Without GPU - (new brain.NeuralNetwork) on row 7

iterations: 4200, training error: 0.006874370443914483
iterations: 4300, training error: 0.006211034862270221
iterations: 4400, training error: 0.005647163558867768
iterations: 4500, training error: 0.005163651528723463
{ error: 0.004997787408525928, iterations: 4538 }
Float32Array [ 0.052155524492263794 ] 0
Float32Array [ 0.9345424175262451 ] 1
Float32Array [ 0.9339274764060974 ] 1
Float32Array [ 0.09189336001873016 ] 0
0.055 (seconds)

How important is this (1-5)?

4

Expected behavior (i.e. solution).

Faster training time in both Node and Browser environment with same training results.

Other Comments

Loving brain.js ❤️ - I’m solving a Rubiks Cube using it for fun, very interesting!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mubaidrcommented, May 19, 2020

Will be published soon.

Confirmed! Problem still exists in 2.0.Beta.1

node "d:\current\brainjs-test\index.js"
CPU training: 48.933ms
CPU training output { error: 0.004996030448576466, iterations: 5257 }
GPU training: 13341.557ms
GPU training output { error: 0.0047736987471580505, iterations: 6100 }
const brain = require("brain.js");

const trainingData = [
  { input: [0, 1], output: [1] },
  { input: [0, 0], output: [0] },
  { input: [1, 1], output: [0] },
  { input: [1, 0], output: [1] },
];

const net = new brain.NeuralNetwork({
  // hiddenLayers: [12, 8],
});

console.time("CPU training");

const output = net.train(trainingData, {
  iterations: 50000, // maximum training iterations
  log: true, // console.log() progress periodically
  logPeriod: 10000, // number of iterations between logging
});

console.timeEnd("CPU training");
console.log(`CPU training output`, output);

console.time("GPU training");

const netGPU = new brain.NeuralNetworkGPU({
  // hiddenLayers: [12, 8],
});

const outputGPU = netGPU.train(trainingData, {
  iterations: 50000, // maximum training iterations
  log: true, // console.log() progress periodically
  logPeriod: 10000, // number of iterations between logging
});

console.timeEnd("GPU training");
console.log(`GPU training output`, outputGPU);

@goferito cc

2reactions
robertleeplummerjrcommented, Mar 16, 2020

Found the issue, at least for the active branch I’m working on, which is rnn-cleanup. I’ll have it released in the next day or two.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chrome Freezing and making system Unresponsive?
The stream will almost stop/freeze and then chrome locks up. Are there any known issues at the moment? Chrome Version: 80.0.3987.100 - to ......
Read more >
GPU process crashes with high chance if you switch quickly ...
I found a fix/workaround for the crashes, by setting chrome://flags "Choose ANGLE graphics backend" to either D3D9 or OpenGL, and I couldnt make...
Read more >
VSCode freezes when switching from Chrome to it #28709
Here is a checklist to find out more about the freeze/slow/crash ... it reproduces when running with GPU disabled ( code --disable-gpu )...
Read more >
How to Fix It When Chrome Keeps Freezing - Lifewire
Many things can cause Google Chrome to keep freezing or crashing when ... Hardware acceleration uses the computer's GPU (video card) for ...
Read more >
Chrome Not Using My GPU! - Google Groups
Chrome is using a lot of my PC's CPU and RAM and it's making my PC freeze and lag and it's not using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found