简体   繁体   English

使用brain.js神经网络比较数组中的字符串和数字

[英]Use brain.js neural network to do compare string and numbers from array

For learning and fun, I wanted to focus on brain.js, but with a different example than it's already described. 为了学习和娱乐,我想专注于brain.js,但是它的例子与已经描述的有所不同。 I have a problem, but probably with a bad approach to this library. 我有一个问题,但可能对这个库的处理方式不好。

Namely, in a json I have book titles and their prices, while in a train I provide keywords and the price I can give for its occurrence in the title. 即,在json中,我有书名及其价格,而在火车中,我提供了关键字以及可以为其提供的价格。

my JSON: 我的JSON:

  [{ "title": "Bible js", "price": 30 }, 
  { "title": "Bible php", "price": 25 }, 
  { "title": "Bible java", "price": 40 }, 
  { "title": "All about js", "price": 25 }, 
  { "title": "Testing in php", "price": 50 }, 
  { "title": "How to start with java", "price": 40 }]

my code for brain.js: 我的brain.js代码:

  const brain = require('brain.js');
  const jsonData = require('./books.json');

  const myMatch = [
    { input: ["js", 40], output: [1] },
    { input: ["php", 20], output: [1] },
    { input: ["How to", 50], output: [1] },
    { input: ["java", 0], output: [0] },
    { input: ["Bible", 0], output: [0] }
  ]

  var config = {
    binaryThresh: 0.5,     // ¯\_(ツ)_/¯
    hiddenLayers: [3],     // array of ints for the sizes of the hidden layers in the network
    activation: 'sigmoid' // Supported activation types ['sigmoid', 'relu', 'leaky-relu', 'tanh']
  }

  var net = new brain.NeuralNetwork(config);
  net.train(myMatch);
  jsonData.forEach(element => {
    let result = net.run(element.title, element.price);
    console.log('--element--');
    console.log(element.title, element.price);
    console.log('--typeof--');
    typeof result;
    console.log('--result--');
    console.log(result);
    console.log('--------------------');
  });

I load book titles and their prices and then I want it to return my degree of interest to given words and the price that interests me or not. 我加载书名及其价格,然后希望它将我的兴趣程度恢复为给定的单词和我不感兴趣的价格。

For example, I am looking for books about js for $40 and php for $20. 例如,我正在寻找40美元的js和20美元的php书籍。 I'm totally not interested in titles with "Bible" or "java". 我对“圣经”或“ java”的书名完全不感兴趣。 The error I'm struggling with is NaN . 我遇到的错误是NaN

The reason you are getting NaN is you are feeding strings into the net where an array of numbers should go. 出现NaN的原因是您正在将字符串输入应该放入数字数组的网络中。 How many books are you classifying? 您要分类几本书?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM