简体   繁体   English

将数据从 a.json 文件导入到 Brain.js 神经网络

[英]Import data from a .json file to a Brain.js neural network

I want to import data from a data.json file into the neural network (which uses the Brain.js framework).Here is the part which is supposed to bring that data to the network and analyse it:我想将 data.json 文件中的数据导入神经网络(使用 Brain.js 框架)。这是应该将该数据带到网络并对其进行分析的部分:

const result = brain.likely(
  require('data.js')
,net);

alert("This is the result: " + result);

And get that data analysed by the neural network and shown to the user.并获得由神经网络分析的数据并显示给用户。 Here are the contents of the data.json file for reference:下面是data.json文件的内容供参考:

{
  'Rating1': 0.12434213,
  'Rating2': 0.987653236,
  'Rating3': 0.432543654
}

For your information this is on written on node.js enviroment.供您参考,这是写在 node.js 环境中的。

Assuming your data.json file is in the same directory:假设您的 data.json 文件位于同一目录中:

fetch('data.json')
    .then(response => response.json())
    .then(json => {
        const result = brain.likely(json, net);
    });

Alternatively, with async/await:或者,使用 async/await:

(async () => {
    const json = await (await fetch('data.json')).json();
    const result = brain.likely(json, net);
})();

If done through a file upload:如果通过文件上传完成:

// target input element
const input = document.querySelector('input');

// upload event
input.addEventListener('change', () => {
    const file = this.files[0];
    const reader = new FileReader();
    reader.addEventListener('load', e => {
        const json = JSON.parse(e.target.result);
        const result = brain.likely(json, net);
    });
    reader.readAsText(file);
});

If done through Node:如果通过节点完成:

const json = require('./data.json');
brain.likely(json, net);

Useful resources for handling files:处理文件的有用资源:

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

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