Brain.js: GPU support both slow and wrong - almost hangs computer, node and chrome

Created on 10 Mar 2020  路  6Comments  路  Source: BrainJS/brain.js

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 :heart: - I'm solving a Rubiks Cube using it for fun, very interesting!

4 - Done bug ready

Most helpful comment

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

All 6 comments

Reproduced, will fix today.

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.

Im having a similar issue with both Linux+Radeon480card or macOS/macookPro with Intel Iris Plus Graphics 650, GPU results are much much slower than CPU ones - maybe that's expected behavior with those type of card? Normal CPU feedforward training works like a charm and is great!

Fixes have been implemented already, will be available in next beta version.

@mubaidr how can we test that beta version?

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VEXLife picture VEXLife  路  4Comments

nezzard picture nezzard  路  5Comments

tyt34 picture tyt34  路  5Comments

ximik753 picture ximik753  路  3Comments

AkashGutha picture AkashGutha  路  5Comments