简体   繁体   English

如何使用动态数据集批量训练Brain.js神经网络?

[英]How to Train Brain.js Neural Network in Bulk with Dynamic DataSet?

I am having hard time figuring out how to train the brain.js neural network with dynamic dataset. 我很难弄清楚如何使用动态数据集训练brain.js神经网络。 The GitHub documentation says the following: Each training pattern should have an input and an output, both of which can be either an array of numbers from 0 to 1 or a hash of numbers from 0 to 1 GitHub文档说: 每个训练模式都应该有一个输入和一个输出,两者都可以是0到1的数字数组或0到1的数字散列

net.train([{input: [0, 0], output: [0]},
           {input: [0, 1], output: [1]},
           {input: [1, 0], output: [1]},
           {input: [1, 1], output: [0]}]);

const output = net.run([1, 0]);  // [0.987]

The problem is that I don't know beforehand how many elements are in the input array of my training data so I don't know how many {input: [0, 0], output: [0]} elements I need to pass to net.train(). 问题是我事先不知道训练数据的输入数组中有多少个元素,所以我不知道我需要传递多少个{input:[0,0],output:[0]}个元素到net.train()。

For example: How do I train the neural network if I have the following arrays without hardcoding the number of {input: [0, 0], output: [0]} elements. 例如:如果我有以下数组而没有对{input:[0,0],output:[0]}元素的数量进行硬编码,那么如何训练神经网络。

var input1_array = [.1, .2, .3, .4, .5]
var input2_array = [.6, .7, .8, .9, .95]
var output1_array = [.2, .6, .8, .85, .95]

// the following doesn't work 
net.train([input:[input1_array, input2_array], output:[output1_array]]);

I hope question is still valid. 我希望问题仍然有效。

First, I would start with blank array (ie/ input1_array=[]; ), but you can't do it as in train you need to have some value. 首先,我将从空白数组开始(即/ input1_array=[]; ),但是您不能这样做,因为在训练中您需要具有一些价值。 For that reason you can put some initial data like var input1_array = [0]; 因此,您可以放置​​一些初始数据,例如var input1_array = [0]; if your network will have two inputs then var input1_array = [0,0]; 如果您的网络有两个输入,则var input1_array = [0,0];

If you will use two or more previous values as inputs you will need 如果将两个或多个先前值用作输入,则需要

var trainingdata= [{input: [0, 0], output: [0]},
           {input: [0, 1], output: [1]},
] //(sorry for typos, but idea to have two entries)

Now you have some initial zeros to not have NULL going to you NN. 现在,您有一些初始零,而NN没有NULL。 I was using prompt during testing and input did the following trainingdata.push (input:[prompt("Enter next in value")],output:[prompt("Enter next out value")]) 我在测试过程中使用提示,输入做了以下trainingdata.push (input:[prompt("Enter next in value")],output:[prompt("Enter next out value")])

Depending on what you do with the network, you may need retrain it each time a new value comes (like time series prediction with LSTM) or just collect data (NN type). 根据您对网络的处理方式,每次出现新值时(例如使用LSTM进行时间序列预测)或仅收集数据(NN类型),都可能需要对其进行重新培训。

In your example replace prompt with whatever source of data like something from page ( getElementById ) or some server data JSON parsed value. 在您的示例中,将提示符替换为任何数据源,例如页面中的某些内容( getElementById )或某些服务器数据JSON解析值。

I found one trick. 我发现了一个窍门。 Make function to push to array is length 使函数推入数组的长度

You can do something like: 您可以执行以下操作:

if (trainingData.length < maxtrainingdata) {
  trainingData.push({input: [var1], output: [var2]});
}
else {
  trainingData.shift();
  trainingData.push({input: [var1], output: [
}

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

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