浏览器和node.js的JavaScript中GPU加速了神经网络
brain.js是用JavaScript编写的神经网络的GPU加速库。
这是哈瑟/大脑的延续,不再维护。更多信息
RNNTimeStep , LSTMTimeStep和GRUTimeStep进行培训RNN , LSTM和GRU进行培训AE培训likelytoSVG如果您可以使用npm安装brain.js :
npm install brain.js < script src =" //unpkg.com/brain.js " > </ script >下载最新的brain.js浏览器
Brain.js取决于本机模块headless-gl来支持GPU。在大多数情况下,从NPM中安装brain.js应该只能使用。但是,如果您遇到问题,这意味着预建的二进制文件无法从GitHub存储库中下载,您可能需要自己构建它。
请确保已安装以下依赖项并最新,然后运行:
npm rebuildapt上的build-essential软件包获得)sudo apt-get install -y build-essential libglew-dev libglu1-mesa-dev libxi-dev pkg-confignpm config set msvs_version 2022注意:这不再在现代版本的NPM中起作用。npm config set python python3注意:这不再适用于NPM的现代版本。 *如果您使用的是Build Tools 2017 ,则运行npm config set msvs_version 2017注意:这不再适用于NPM的现代版本。
这是一个示例,展示了如何使用brain.js近似XOR函数。
大脑的有趣而实用的介绍
// provide optional config object (or undefined). Defaults shown.
const 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'],
leakyReluAlpha : 0.01 , // supported for activation type 'leaky-relu'
} ;
// create a simple feed-forward neural network with backpropagation
const net = new brain . NeuralNetwork ( config ) ;
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]或有关config的更多信息。
// provide optional config object, defaults shown.
const config = {
inputSize : 20 ,
inputRange : 20 ,
hiddenLayers : [ 20 , 20 ] ,
outputSize : 20 ,
learningRate : 0.01 ,
decayRate : 0.999 ,
} ;
// create a simple recurrent neural network
const net = new brain . recurrent . RNN ( config ) ;
net . train ( [
{ input : [ 0 , 0 ] , output : [ 0 ] } ,
{ input : [ 0 , 1 ] , output : [ 1 ] } ,
{ input : [ 1 , 0 ] , output : [ 1 ] } ,
{ input : [ 1 , 1 ] , output : [ 0 ] } ,
] ) ;
let output = net . run ( [ 0 , 0 ] ) ; // [0]
output = net . run ( [ 0 , 1 ] ) ; // [1]
output = net . run ( [ 1 , 0 ] ) ; // [1]
output = net . run ( [ 1 , 1 ] ) ; // [0]但是,没有理由使用神经网络来找出XOR。 ( - :因此,这是一个更具参与,现实的例子:演示:训练神经网络以识别颜色对比。
brain.js示例回购
您可以查看此出色的屏幕截图,该屏幕列表说明了如何使用现实世界数据集训练简单的神经网络:如何使用brain.js在浏览器中创建神经网络。
使用train()使用一系列培训数据来训练网络。必须在一次呼叫train()中对网络进行批量培训。更多的培训模式可能需要更长的时间来训练,但通常会更好地使网络更好地分类新模式。
培训在计算上是昂贵的,因此您应该尝试将网络离线培训(或在工人上)培训网络,并使用toFunction()或toJSON()选项将预训练的网络插入您的网站。
NeuralNetwork培训每个训练模式都应具有input和output ,这两者都可以是从0到1数字数组,也可以是0到1的数字。对于颜色对比演示,它看起来像这样:
const net = new brain . NeuralNetwork ( ) ;
net . train ( [
{ input : { r : 0.03 , g : 0.7 , b : 0.5 } , output : { black : 1 } } ,
{ input : { r : 0.16 , g : 0.09 , b : 0.2 } , output : { white : 1 } } ,
{ input : { r : 0.5 , g : 0.5 , b : 1.0 } , output : { white : 1 } } ,
] ) ;
const output = net . run ( { r : 1 , g : 0.4 , b : 0 } ) ; // { white: 0.99, black: 0.002 }这是上述示例的另一个变体。 (请注意,输入对象不需要相似。)
net . train ( [
{ input : { r : 0.03 , g : 0.7 } , output : { black : 1 } } ,
{ input : { r : 0.16 , b : 0.2 } , output : { white : 1 } } ,
{ input : { r : 0.5 , g : 0.5 , b : 1.0 } , output : { white : 1 } } ,
] ) ;
const output = net . run ( { r : 1 , g : 0.4 , b : 0 } ) ; // { white: 0.81, black: 0.18 } RNNTimeStep , LSTMTimeStep和GRUTimeStep进行培训每个训练模式都可以:
使用数字数组的示例:
const net = new brain . recurrent . LSTMTimeStep ( ) ;
net . train ( [ [ 1 , 2 , 3 ] ] ) ;
const output = net . run ( [ 1 , 2 ] ) ; // 3示例使用数字数组数量:
const net = new brain . recurrent . LSTMTimeStep ( {
inputSize : 2 ,
hiddenLayers : [ 10 ] ,
outputSize : 2 ,
} ) ;
net . train ( [
[ 1 , 3 ] ,
[ 2 , 2 ] ,
[ 3 , 1 ] ,
] ) ;
const output = net . run ( [
[ 1 , 3 ] ,
[ 2 , 2 ] ,
] ) ; // [3, 1] RNN , LSTM和GRU进行培训每个训练模式都可以:
input和output注意:当使用一个值数组时,您可以使用任何值,但是,该值通过单个输入在神经网络中表示。因此,越不同的值的输入层越大。如果您有数百,数千或数百万的浮点值,那么这不是适合工作的合适类。另外,当偏离字符串时,这将进入Beta
示例使用直接字符串:Hello World使用brainjs
const net = new brain . recurrent . LSTM ( ) ;
net . train ( [ 'I am brainjs, Hello World!' ] ) ;
const output = net . run ( 'I am brainjs' ) ;
alert ( output ) ; const net = new brain . recurrent . LSTM ( ) ;
net . train ( [
'doe, a deer, a female deer' ,
'ray, a drop of golden sun' ,
'me, a name I call myself' ,
] ) ;
const output = net . run ( 'doe' ) ; // ', a deer, a female deer'使用带有输入和输出的字符串的示例:
const net = new brain . recurrent . LSTM ( ) ;
net . train ( [
{ input : 'I feel great about the world!' , output : 'happy' } ,
{ input : 'The world is a terrible place!' , output : 'sad' } ,
] ) ;
const output = net . run ( 'I feel great about the world!' ) ; // 'happy' AE培训每个训练模式都可以:
训练自动编码器以压缩XOR计算的值:
const net = new brain . AE (
{
hiddenLayers : [ 5 , 2 , 5 ]
}
) ;
net . train ( [
[ 0 , 0 , 0 ] ,
[ 0 , 1 , 1 ] ,
[ 1 , 0 , 1 ] ,
[ 1 , 1 , 0 ]
] ) ;编码/解码:
const input = [ 0 , 1 , 1 ] ;
const encoded = net . encode ( input ) ;
const decoded = net . decode ( encoded ) ;Denoise嘈杂数据:
const noisyData = [ 0 , 1 , 0 ] ;
const data = net . denoise ( noisyData ) ;测试数据样本中的异常:
const shouldBeFalse = net . includesAnomalies ( [ 0 , 1 , 1 ] ) ;
const shouldBeTrue = net . includesAnomalies ( [ 0 , 1 , 0 ] ) ;train()将选项作为其第二个论点:
net . train ( data , {
// Defaults values --> expected validation
iterations : 20000 , // the maximum times to iterate the training data --> number greater than 0
errorThresh : 0.005 , // the acceptable error percentage from training data --> number between 0 and 1
log : false , // true to use console.log, when a function is supplied it is used --> Either true or a function
logPeriod : 10 , // iterations between logging out --> number greater than 0
learningRate : 0.3 , // scales with delta to effect training rate --> number between 0 and 1
momentum : 0.1 , // scales with next layer's change value --> number between 0 and 1
callback : null , // a periodic call back that can be triggered while training --> null or function
callbackPeriod : 10 , // the number of iterations through the training data between callback calls --> number greater than 0
timeout : number , // the max number of milliseconds to train for --> number greater than 0. Default --> Infinity
} ) ;每当满足两个标准之一时,网络都会停止训练:训练错误已低于阈值(默认为0.005 ),或达到了最大迭代次数(默认20000 )。
默认情况下,培训不会让您知道直到结束时的状况,而是将log设置为true ,以获取有关网络当前培训错误的定期更新。训练错误应每次减少。更新将打印到控制台。如果将log设置为函数,则将使用更新来调用此函数,而不是将其打印到控制台。但是,如果要使用自己的输出中的更新值,则可以将callback设置为函数。
学习率是影响网络训练速度的参数。这是0到1数字。如果学习率接近0 ,则训练需要更长的时间。如果学习率接近1 ,它将更快地训练,但是培训结果可能会限制为本地最低限度并在新数据上执行不良。(过拟合)默认学习率为0.3 。
动量类似于学习率,也期望值从0到1值,但是它乘以下一级别的变更值。默认值为0.1
这些培训选项中的任何一个都可以传递到构造函数中,也可以传递到updateTrainingOptions(opts)方法中,它们将保存在网络上并在培训时间使用。如果将网络保存到JSON,这些培训选项也将保存和还原(除了回调和日志,将忘记回调,并使用Console.log恢复日志)。
默认情况下,一个称为invalidTrainOptsShouldThrow的布尔属性设置为true 。虽然该选项为true ,但如果您输入超出正常范围的培训选项,则会出现有关异常选项的消息。当选项设置为false时,将不会发送错误,但是仍然将一条消息发送到console.warn带有相关信息。
trainAsync()采用与火车(数据和选项)相同的参数。它没有从培训中返回结果对象,而是回报一个承诺,即解决后,将返回训练结果对象。不使用:
brain.recurrent.RNNbrain.recurrent.GRUbrain.recurrent.LSTMbrain.recurrent.RNNTimeStepbrain.recurrent.GRUTimeStepbrain.recurrent.LSTMTimeStep const net = new brain . NeuralNetwork ( ) ;
net
. trainAsync ( data , options )
. then ( ( res ) => {
// do something with my trained network
} )
. catch ( handleError ) ;使用多个网络,您可以像这样并并行训练:
const net = new brain . NeuralNetwork ( ) ;
const net2 = new brain . NeuralNetwork ( ) ;
const p1 = net . trainAsync ( data , options ) ;
const p2 = net2 . trainAsync ( data , options ) ;
Promise . all ( [ p1 , p2 ] )
. then ( ( values ) => {
const res = values [ 0 ] ;
const res2 = values [ 1 ] ;
console . log (
`net trained in ${ res . iterations } and net2 trained in ${ res2 . iterations } `
) ;
// do something super cool with my 2 trained networks
} )
. catch ( handleError ) ;交叉验证可以为较大的数据集提供较不脆弱的培训方式。 Brain.js API在此示例中提供了交叉验证:
const crossValidate = new brain . CrossValidate ( ( ) => new brain . NeuralNetwork ( networkOptions ) ) ;
crossValidate . train ( data , trainingOptions , k ) ; //note k (or KFolds) is optional
const json = crossValidate . toJSON ( ) ; // all stats in json as well as neural networks
const net = crossValidate . toNeuralNetwork ( ) ; // get top performing net out of `crossValidate`
// optionally later
const json = crossValidate . toJSON ( ) ;
const net = crossValidate . fromJSON ( json ) ;在这些类中使用CrossValidate :
brain.NeuralNetworkbrain.RNNTimeStepbrain.LSTMTimeStepbrain.GRUTimeStep可以在Cross-Validate.ts中找到使用交叉验证的示例
train(trainingData) - >训练斯塔图斯train()的输出是有关培训如何进行的信息:
{
error : 0.0039139985510105032 , // training error
iterations : 406 // training iterations
}run(input) - >预测在课堂上支持:
brain.NeuralNetworkbrain.NeuralNetworkGPU > brain.NeuralNetwork的所有功能brain.recurrent.RNNbrain.recurrent.LSTMbrain.recurrent.GRUbrain.recurrent.RNNTimeStepbrain.recurrent.LSTMTimeStepbrain.recurrent.GRUTimeStep例子:
// feed forward
const net = new brain . NeuralNetwork ( ) ;
net . fromJSON ( json ) ;
net . run ( input ) ;
// time step
const net = new brain . LSTMTimeStep ( ) ;
net . fromJSON ( json ) ;
net . run ( input ) ;
// recurrent
const net = new brain . LSTM ( ) ;
net . fromJSON ( json ) ;
net . run ( input ) ;forecast(input, count) - >预测可提供以下课程。输出一系列预测。预测是输入的延续。
brain.recurrent.RNNTimeStepbrain.recurrent.LSTMTimeStepbrain.recurrent.GRUTimeStep例子:
const net = new brain . LSTMTimeStep ( ) ;
net . fromJSON ( json ) ;
net . forecast ( input , 3 ) ;toJSON() -> json序列化神经网络到JSON
fromJSON(json)来自JSON的神经网络的选择
如果网络未能训练,则错误将高于错误阈值。如果培训数据太嘈杂(很可能),则可能会发生这种情况,网络没有足够的隐藏层或节点来处理数据的复杂性,或者没有接受足够的迭代培训。
如果训练错误仍然是20000年迭代后的0.4范围,那么这是一个好兆头,表明网络无法理解给定的数据。
可以将Net属性maxPredictionLength的实例设置为调整NET的输出;
例子:
const net = new brain . recurrent . LSTM ( ) ;
// later in code, after training on a few novels, write me a new one!
net . maxPredictionLength = 1000000000 ; // Be careful!
net . run ( 'Once upon a time' ) ; 与JSON的训练网络的状态下序列化或加载:
const json = net . toJSON ( ) ;
net . fromJSON ( json ) ; 您还可以从训练有素的网络中获得自定义的独立函数,该网络像run()一样。
const run = net . toFunction ( ) ;
const output = run ( { r : 1 , g : 0.4 , b : 0 } ) ;
console . log ( run . toString ( ) ) ; // copy and paste! no need to import brain.js NeuralNetwork()采用了许多选择:
const net = new brain . NeuralNetwork ( {
activation : 'sigmoid' , // activation function
hiddenLayers : [ 4 ] ,
learningRate : 0.6 , // global learning rate, useful when training using streams
} ) ;此参数使您可以指定神经网络应使用的激活功能。当前有四个支持的激活功能, sigmoid是默认值:
这是一张表(谢谢,Wikipedia!),总结了大量激活功能 - 激活功能
您可以使用它来指定网络中隐藏图层的数量和每一层的大小。例如,如果您想要两个隐藏的层 - 第一个带有3个节点,第二个节点带有4个节点,则会给出:
hiddenLayers: [ 3 , 4 ] ;默认情况下, brain.js使用一个隐藏的层,大小与输入数组的大小成比例。
使用https://www.npmjs.com/package/train-stream将数据流到神经网络
likely const likely = require ( 'brain/likely' ) ;
const key = likely ( input , net ) ;可能的示例请参见:简单的字母检测
toSVG < script src = "../../src/utilities/svg.js" > </ script >渲染前馈网络的网络拓扑
document . getElementById ( 'result' ) . innerHTML = brain . utilities . toSVG (
network ,
options
) ;TOSVG示例请参见:网络渲染
使用的用户界面: 
brain.NeuralNetwork - 反向传播的前馈神经网络brain.NeuralNetworkGPU带反射的前馈神经网络,GPU版本brain.AE带有反射和GPU支持的自动编码器或“ AE”brain.recurrent.RNNTimeStep时间步长复发性神经网络或“ RNN”brain.recurrent.LSTMTimeStep时间步长长期记忆神经网络或“ LSTM”brain.recurrent.GRUTimeStep时间步进门控复发单元或“ gru”brain.recurrent.RNN复发性神经网络或“ RNN”brain.recurrent.LSTM长期短期记忆神经网络或“ LSTM”brain.recurrent.GRU封闭式复发单元或“ gru”brain.FeedForward高度可自定义的前馈神经网络,带有反向传播brain.Recurrent高度可定制的反复神经网络,带有反向传播不同的神经网做得很好。例如:
如果您是开发人员,或者您只是在乎ML API的外观 - 请参与并加入W3C社区并分享您的意见或只是支持您喜欢或同意的意见。
Brain.js是JavaScript世界中广泛采用的开源机器学习库。有几个原因,但最值得注意的是使用的简单性,而不是牺牲性能。我们希望在W3C标准方面保持简单的学习,易于使用和表现。我们认为当前的大脑API非常接近我们可能成为标准的api。而且,由于支持不需要太多努力,而且仍然可以带来巨大的不同,可以随意加入W3C社区团体,并使用像API这样的Brain.js支持我们。
在此处参与W3C机器学习正在进行的标准化过程。您也可以在此处加入我们关于标准化的公开讨论。
如果您有问题,则认为您认为有益于您的项目的错误或功能,我们将尽力而为。
在此处创建问题并遵循模板。
brain.js.org存储库可用Brain.js.org的来源。使用Awesome vue.js & bulma构建。总是欢迎捐款。
由于所有贡献的人,该项目的存在。 [贡献]。
感谢我们所有的支持者! [成为支持者]
通过成为赞助商来支持这个项目。您的徽标将在此处显示您网站的链接。 [成为赞助商]