【问题标题】:Brain js slow in string classificationBrain js 字符串分类速度慢
【发布时间】:2019-10-14 14:36:25
【问题描述】:

我正在使用 Brain js 进行文本分类。我面临的问题是训练速度非常慢。下面的代码需要 15-20 分钟才能执行。我读过一些simple projects 似乎面临同样的问题。一些作者做了一些非常有趣的事情——他们将文本转换为数字。我的问题是如何将字符串转换为数字以提高学习速度,然后呈现与我现在相同的输出?

//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."));

【问题讨论】:

  • 这里有一个建议,可以输出 [0] 和 [1],然后获取您的字符串,然后使用 .split("") 拆分它们,然后使用 .charCodeAt() 将每个字符更改为其 charCode方法,然后将该数字除以 255,得到每个字符的 0 到 1 之间的数字。通过这种方式,它可能训练得更快。

标签: javascript machine-learning brain.js


【解决方案1】:

我知道为时已晚。但希望这对正在寻找类似问题的解决方案的人有所帮助。

@Da Mahdi03 提到的评论绝对是减少训练时间的方法。将字符串转换为数字可以在训练过程中大幅提升性能。

但是,如果您尝试在 Web 浏览器中立即训练和使用网络(假设您在代码中使用了 alert()),那仍然无法解决您的问题。此外,根据数据大小,brain.js 将需要更多时间来训练。

解决方案是先训练网络,然后将其离线保存为 JSON,然后通过 ajax 调用或作为 JSONP 请求加载该 JSON,并使用该训练数据启动 NeuralNet。

例如添加一个 node.js 测试用例

培训:

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

应用程序用法:

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

【讨论】:

    猜你喜欢
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    相关资源
    最近更新 更多