Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Train with NeuralNetworkGPU more slower than CPU #912

Open
teriologia opened this issue Sep 24, 2023 · 3 comments
Open

Train with NeuralNetworkGPU more slower than CPU #912

teriologia opened this issue Sep 24, 2023 · 3 comments
Labels

Comments

@teriologia
Copy link

teriologia commented Sep 24, 2023

A GIF or MEME to give some spice of the internet

What is wrong?

Training with GPU takes too much time ( 2 min for 3000 data )

Where does it happen?

When I try to train with "NeuralNetworkGPU" instead of "NeuralNetwork"

How do we replicate the issue?

you can try to run this code ->

Code:

const abalone = require('./abalone.json');
const {NeuralNetworkGPU} = require('brain.js')

const sexToNumber = (sex) => {
  switch(sex) {
    case 'F': return 0;
    case 'M': return 1;
    default: return 0.5
  }
}

const preparedData = (data, ratio = 29) => {
  return data.map(row => {
    const values = Object.values(row).slice(0, -1);
    values[0] = sexToNumber(values[0]);
    return { input: values, output: [row.rings / ratio] };
  })
}

const shuffle = (arr) => arr.sort(() => Math.random() - .5);
const split = (arr, trainRatio = .75) => {
  const l = Math.floor(arr.length * trainRatio);
  return { train: arr.slice(0, l), test: arr.slice(l) };
}

const prepared = split(shuffle(preparedData(abalone)));

const config = {
  iterations: 5000,
  log: true,
  logPeriod: 20,
};

const net = new NeuralNetworkGPU();
net.train(prepared.train, config);
let totalError = 0;
prepared.test.forEach(item => {
  const output = net.run(item.input);
  console.log(`Expected: ${item.output * 29} Predicted: ${output * 29}`);
  totalError += (output - item.output) ** 2;
});

console.log(totalError / prepared.test.length);

Expected behavior (i.e. solution)

I've RTX 3060Ti & i5-10500 when I try to train with cpu it takes 2-3 sec but when I switch to gpu maybe options same data takes almost 2 min. I believe my GPU is much stronger and faster than my CPU. GPU should more accurate and faster I think

Version information

brain.js: 2.0.0-beta.23
gpu.js: 2.16.0

Nodejs:

node: v16.16.0

Browser:

Brain.js:

brain.js: 2.0.0-beta.23

How important is this (1-5)?

5

Other Comments

Maybe the config is not works on gpu version or I should enable some feature on my gpu. If so, brain.js should at least warn me about this

iterating 20 times each time
image

abalone.csv U can convert .csv to .json for code if you want

@teriologia teriologia added the bug label Sep 24, 2023
@nekomancer0
Copy link

I think I don't have a CPU so it takes an infinite amount of time to train

@artegoser
Copy link

I have the same thing, only gpu does not work at all, with cpu everything works quietly, I thought it was a problem with nodejs, rewrote to the browser version. Everything is preserved, the browser freezes and everything crashes with gpu, but with cpu everything works. At the same time gpujs itself works fine.

@tchereau
Copy link

tchereau commented Mar 28, 2024

I think I don't have a CPU so it takes an infinite amount of time to train

how the hell your computer can run without a cpu ???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants