3

I'm using brain js for text classification. The problem that I face is the speed of training is incredibly slow. The below code takes 15-20 minutes to execute. I've read about a couple of simple projects which seem to face the same issue. Some of the authors do something very interesting-they convert the text to numbers. My question is how to convert the strings into digits in order to increase learning speed and then present output identical to mine's now?

//Simple emotion detetction
    var net = new brain.recurrent.LSTM();
    net.train([
      {input: "Feeling good.", output: "positive"},
      {input: "Overall well.", output: "positive"},
      {input: "Extremely happy.", output: "positive"},
      {input: "I'm feeling joyful.", output: "positve"},
      {input: "She is in an outstanding mood.", output: "positive"},
      {input: "He is feeling inspiration", output: "positive."},  
       {input: "Today will be my day.", output: "positive"},
      {input: "I know that I’m winner.", output: "positive"},
      {input: "Yes ,I can do it, I know I can.", output: "positive"},
      {input: "Tomorrow is next chance.", output: "positve"},
      {input: "Henna can do it.", output: "positive"},
      {input: "I like vegetables.", output: "positive."},
      {input: "I'm feeling worse than ever.", output: "negative"},
      {input: "She seems a little distracted.", output: "negative"},
      {input: "This behaviour is unacceptable.", output: "negative"},
      {input: "Rober is feeling depressed.", output: "negative"},
      {input: "They are feeling miserable.", output: "negative"},
      {input: "Robert is in bad mood.", output: "negative"},
      {input: "I'm feeling pity for m action.", output: "negative"}
    ]);
    alert(net.run("I'm feeling pretty bad."));
  • 3
    Here's a suggestion, maybe make the outputs [0] and [1], and then take your strings and then split them using `.split("")` and then change each character to its charCode using the `.charCodeAt()` method and then divide that number by 255 to get a number between 0 and 1 for each character. In this way it _might_ train faster. – Da Mahdi03 Jun 11 '19 at 21:23

1 Answers1

4

I know it's too late. But hope this helps someone, who is looking for a solution to similar type of problem.

The comment mentioned by @Da Mahdi03 is absolutely the way to go to reduce the training time. Converting strings to numbers have a very massive performance improvement during training.

But still that won't solve your problem if you are trying to train and use the network immediately in the web browser (assuming because you have used alert() in your code). And also depending on the data size, brain.js will till take more time to train.

The solution is to train the network first and then save it to JSON offline and then load that JSON through an ajax call or as a JSONP request and initiate the NeuralNet with that trained data.

For example adding a node.js test case

Training:

const net = new brain.recurrent.LSTM();
console.log(start = new Date().getTime())
net.train([
  {input: "Feeling good.", output: "positive"},
  {input: "Overall well.", output: "positive"},
  {input: "Extremely happy.", output: "positive"},
  {input: "I'm feeling joyful.", output: "positive"},
  {input: "She is in an outstanding mood.", output: "positive"},
  {input: "He is feeling inspiration", output: "positive"},  
  {input: "Today will be my day.", output: "positive"},
  {input: "I know that I’m winner.", output: "positive"},
  {input: "Yes ,I can do it, I know I can.", output: "positive"},
  {input: "Tomorrow is next chance.", output: "positive"},
  {input: "Henna can do it.", output: "positive"},
  {input: "I like vegetables.", output: "positive"},
  {input: "I'm feeling worse than ever.", output: "negative"},
  {input: "She seems a little distracted.", output: "negative"},
  {input: "This behaviour is unacceptable.", output: "negative"},
  {input: "Rober is feeling depressed.", output: "negative"},
  {input: "They are feeling miserable.", output: "negative"},
  {input: "Robert is in bad mood.", output: "negative"},
  {input: "I'm feeling pity for m action.", output: "negative"}
]);
const json = net.toJSON()
const data = JSON.stringify(json);
fs.writeFileSync('trainingdata.json', data)
console.log(end = new Date().getTime(), (end - start) / 1000); // outputs 800 seconds

Application Usage:

const brain = require('brain.js')
const fs = require('fs');    
let rawdata = fs.readFileSync('trainingdata.json');
let data = JSON.parse(rawdata);

console.log(start = new Date().getTime())
var net = new brain.recurrent.LSTM();
net.fromJSON(data)
console.log("output = "+net.run("Feeling good."));
console.log(end = new Date().getTime(), (end - start)/1000) // Outputs 0.01 seconds
karthick
  • 11,998
  • 6
  • 56
  • 88