BrainJS / brain.js

🤖 GPU accelerated Neural networks in JavaScript for Browsers and Node.js
https://brain.js.org
MIT License
14.25k stars 1.06k forks source link

network.run() returns an empty string #894

Closed kul-sudo closed 1 year ago

kul-sudo commented 1 year ago

image

What is wrong?

network.run(*something which is not close to the data given for training*) always returns an empty string.

Here is data.json:

[
    {
        "input": "hi",
        "output": 1
    },
    {
        "input": "bye",
        "output": 0
    }
]

First of all, I train it using "train-network.js":

const brain = require('brain.js')
const data = require('./data.json')
const fs = require('fs')

const network = new brain.recurrent.LSTM()

network.train(data, {
  errorThresh: 0.005,
})

// Save network state to JSON file
const networkState = network.toJSON()
fs.writeFileSync('network-state.json', JSON.stringify(networkState), 'utf-8')

Afterwards, I run "load-network.js":

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

const network = new brain.recurrent.LSTM()

const networkState = JSON.parse(fs.readFileSync('network-state.json', 'utf-8'))
network.fromJSON(networkState)

console.log(network.run('byecathi'))

Sometimes typing a string which is not included in the dataset returns either 0 or 1 and sometimes just nothing. It returns "". But why? Indeed, I train it with too few examples, but it has to return me an output even if it is totally incorrect, but it is supposed to try to at least. Why does it just kind of "give up" and return an empty string?

Where does it happen?

I suspect it happens in "load-network.js", which is the file I run to see the output.

How do we replicate the issue?

Just run the code given above, because there are no specific steps.

Expected behavior (i.e. solution)

Well, I think it should give me an output even if it is incorrect.

Version information

Nodejs: v20.0.0

Browser:

Brain.js:

{
  "dependencies": {
    "brain.js": "^2.0.0-beta.23"
  }
}

How important is this (1-5)?

4

Other Comments

I guess I do not have anything to add.

robertleeplummerjr commented 1 year ago

Try using strings instead of integers. Like this:

[
    {
        "input": "hi",
        "output": "1"
    },
    {
        "input": "bye",
        "output": "0"
    }
]
kul-sudo commented 1 year ago

Try using strings instead of integers. Like this:

[
    {
        "input": "hi",
        "output": "1"
    },
    {
        "input": "bye",
        "output": "0"
    }
]

If I do console.log(network.run('hsfdhdsf')), it returns adini1. But I think empty strings have actually been fixed now that I have changed the format to strings, truth be told. Why would it? I do not even have adini, I only have string-format numbers.

robertleeplummerjr commented 1 year ago

If I do console.log(network.run('hsfdhdsf')), it returns adini1.

That is likely expected because it probably hasn't trained long enough. When the net encounters any character (like a number) it doesn't understand, the net interprets this as a general catchall unrecognized value, and thus they are all treated the same.

kul-sudo commented 1 year ago

If I do console.log(network.run('hsfdhdsf')), it returns adini1.

That is likely expected because it probably hasn't trained long enough. When the net encounters any character (like a number) it doesn't understand, the net interprets this as a general catchall unrecognized value, and thus they are all treated the same.

Ok, now that I have trained it until the error thresh is 0.05, it returns an empty string again with 'hsfdhdsf'. My data has changed too:

[
  {
    "input": "hi",
    "output": "1"
  },
  {
    "input": "bye",
    "output": "0"
  },
  {
    "input": "xd",
    "output": "0"
  },
  {
    "input": "kek",
    "output": "0"
  },
  {
    "input": "Yeah, I agree",
    "output": "1"
  },
  {
    "input": "I think you are right",
    "output": "1"
  },
  {
    "input": "I will read this later",
    "output": "1"
  },
  {
    "input": "Reading books is nice",
    "output": "1"
  }
]
kul-sudo commented 1 year ago

Also, can I sort of tell it that there are just 2 values and that it needs to choose one of them like a boolean? By the way, 1 means that the sentence is meaningful, and 0 says the opposite.

kul-sudo commented 1 year ago

Any updates?