JavaScript 是一种流行的高级编程语言,它被世界上的绝大多数网站所使用,也被所有主流浏览器所支持。随着深度学习的火热,越来越多开发者开始探索使用 JavaScript 实现人工智能与机器学习算法。近日,来自德国的 Robin Wieruch 发布了一系列使用 JavaScript 构建机器学习的教程,本文将主要介绍使用 JavaScript 实现神经网络的方法。
近期,原作者发表了一系列有关在 JavaScript 上实现人工智能和机器学习算法的文章,其中包括:有网友对此表示:「我本想写一篇激烈的反驳文,其中阐述如果没有 GPU 的支持,这种做法是毫无意义的…… 但它可以使用 WebGL 来应用 GPU 的能力。而且,这可能比你在本地桌面上安装 TensorFlow 堆栈要简单一万倍。」
- function getAccessibleColor(rgb){
- let [ r, g, b ]= rgb;
- let colors =[r /255, g /255, b /255];
- let c = colors.map((col)=>{
- if(col <>0.03928){
- return col /12.92;
- returnMath.pow((col +0.055)/1.055,2.4);
- let L =(0.2126* c[0])+(0.7152* c[1])+(0.0722* c[2]);
- return(L >0.179)
- ?[0,0,0]
- :[255,255,255];
- function generateRandomRgbColors(m){
- const rawInputs =[];
- for(let i =0; i m; i++){
- rawInputs.push(generateRandomRgbColor());
- return rawInputs;
- function generateRandomRgbColor(){
- randomIntFromInterval(0,255),
- randomIntFromInterval(0,255),
- randomIntFromInterval(0,255),
- function randomIntFromInterval(min, max){
- returnMath.floor(Math.random()*(max - min +1)+ min);
generateRandomRgbColors() 函数创建给定大小为 m 的部分数据集。数据集中的数据点是 RGB 颜色空间中的颜色。每种颜色在矩阵中被表征为一行,而每一列是颜色的特征。特征是 RGB 空间中的 R、G、B 编码值。数据集还没有任何标签,所以训练集并不完整,因为它只有输入值而没有输出值。
由于基于已知颜色生成可使用字体颜色的编程方法是已知的,因此可以使用调整后的功能版本以生成训练集(以及稍后的测试集)的标签。这些标签针对二分类问题进行了调整,并在 RGB 空间中隐含地反映了黑白的颜色。因此,对于黑色,标签是 [0,1];对于白色,标签是 [1,0]。
- function getAccessibleColor(rgb){
- let [ r, g, b ]= rgb;
- let color =[r /255, g /255, b /255];
- let c = color.map((col)=>{
- if(col <>0.03928){
- return col /12.92;
- returnMath.pow((col +0.055)/1.055,2.4);
- let L =(0.2126* c[0])+(0.7152* c[1])+(0.0722* c[2]);
- return(L >0.179)
- ?[0,1]// black
- :[1,0];// white
- function generateColorSet(m){
- const rawInputs = generateRandomRgbColors(m);
- const rawTargets = rawInputs.map(getAccessibleColor);
- return{ rawInputs, rawTargets };
- function normalizeColor(rgb){
- return rgb.map(v => v /255);
- npm install deeplearn
- classColorAccessibilityModel{
- normalizeColor(rgb){
- return rgb.map(v => v /255);
- exportdefaultColorAccessibilityModel;
- } from 'deeplearn';
- const math =newNDArrayMathGPU();
- classColorAccessibilityModel{
- exportdefaultColorAccessibilityModel;
- } from 'deeplearn';
- classColorAccessibilityModel{
- setupSession(trainingSet){
- const graph =newGraph();
- exportdefaultColorAccessibilityModel;
- classColorAccessibilityModel{
- setupSession(trainingSet){
- const graph =newGraph();
- this.inputTensor = graph.placeholder('input RGB value',[3]);
- this.targetTensor = graph.placeholder('output classifier',[2]);
- exportdefaultColorAccessibilityModel;
- classColorAccessibilityModel{
- setupSession(trainingSet){
- const graph =newGraph();
- this.inputTensor = graph.placeholder('input RGB value',[3]);
- this.targetTensor = graph.placeholder('output classifier',[2]);
- let connectedLayer =this.createConnectedLayer(graph,this.inputTensor,0,64);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,1,32);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,2,16);
- createConnectedLayer(
- exportdefaultColorAccessibilityModel;
- classColorAccessibilityModel{
- setupSession(trainingSet){
- const graph =newGraph();
- this.inputTensor = graph.placeholder('input RGB value',[3]);
- this.targetTensor = graph.placeholder('output classifier',[2]);
- let connectedLayer =this.createConnectedLayer(graph,this.inputTensor,0,64);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,1,32);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,2,16);
- createConnectedLayer(
- return graph.layers.dense(
- `fully_connected_${layerIndex}`,
- exportdefaultColorAccessibilityModel;
- classColorAccessibilityModel{
- setupSession(trainingSet){
- const graph =newGraph();
- this.inputTensor = graph.placeholder('input RGB value',[3]);
- this.targetTensor = graph.placeholder('output classifier',[2]);
- let connectedLayer =this.createConnectedLayer(graph,this.inputTensor,0,64);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,1,32);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,2,16);
- createConnectedLayer(
- activationFunction
- return graph.layers.dense(
- `fully_connected_${layerIndex}`,
- activationFunction ? activationFunction :(x)=> graph.relu(x)
- exportdefaultColorAccessibilityModel;
- classColorAccessibilityModel{
- setupSession(trainingSet){
- const graph =newGraph();
- this.inputTensor = graph.placeholder('input RGB value',[3]);
- this.targetTensor = graph.placeholder('output classifier',[2]);
- let connectedLayer =this.createConnectedLayer(graph,this.inputTensor,0,64);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,1,32);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,2,16);
- this.predictionTensor =this.createConnectedLayer(graph, connectedLayer,3,2);
- exportdefaultColorAccessibilityModel;
- classColorAccessibilityModel{
- setupSession(trainingSet){
- const graph =newGraph();
- this.inputTensor = graph.placeholder('input RGB value',[3]);
- this.targetTensor = graph.placeholder('output classifier',[2]);
- let connectedLayer =this.createConnectedLayer(graph,this.inputTensor,0,64);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,1,32);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,2,16);
- this.predictionTensor =this.createConnectedLayer(graph, connectedLayer,3,2);
- this.costTensor = graph.meanSquaredCost(this.targetTensor,this.predictionTensor);
- exportdefaultColorAccessibilityModel;
- } from 'deeplearn';
- classColorAccessibilityModel{
- setupSession(trainingSet){
- const graph =newGraph();
- this.inputTensor = graph.placeholder('input RGB value',[3]);
- this.targetTensor = graph.placeholder('output classifier',[2]);
- let connectedLayer =this.createConnectedLayer(graph,this.inputTensor,0,64);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,1,32);
- connectedLayer =this.createConnectedLayer(graph, connectedLayer,2,16);
- this.predictionTensor =this.createConnectedLayer(graph, connectedLayer,3,2);
- this.costTensor = graph.meanSquaredCost(this.targetTensor,this.predictionTensor);
- this.session =newSession(graph, math);
- this.prepareTrainingSet(trainingSet);
- prepareTrainingSet(trainingSet){
- exportdefaultColorAccessibilityModel;
- } from 'deeplearn';
- const math =newNDArrayMathGPU();
- classColorAccessibilityModel{
- prepareTrainingSet(trainingSet){
- math.scope(()=>{
- exportdefaultColorAccessibilityModel;
- } from 'deeplearn';
- const math =newNDArrayMathGPU();
- classColorAccessibilityModel{
- prepareTrainingSet(trainingSet){
- math.scope(()=>{
- const{ rawInputs, rawTargets }= trainingSet;
- const inputArray = rawInputs.map(v =>Array1D.new(this.normalizeColor(v)));
- const targetArray = rawTargets.map(v =>Array1D.new(v));
- exportdefaultColorAccessibilityModel;
- InCPUMemoryShuffledInputProviderBuilder,
- } from 'deeplearn';
- const math =newNDArrayMathGPU();
- classColorAccessibilityModel{
- prepareTrainingSet(trainingSet){
- math.scope(()=>{
- const{ rawInputs, rawTargets }= trainingSet;
- const inputArray = rawInputs.map(v =>Array1D.new(this.normalizeColor(v)));
- const targetArray = rawTargets.map(v =>Array1D.new(v));
- const shuffledInputProviderBuilder =newInCPUMemoryShuffledInputProviderBuilder([
- inputArray,
- targetArray
- inputProvider,
- targetProvider,
- ]= shuffledInputProviderBuilder.getInputProviders();
- exportdefaultColorAccessibilityModel;
- InCPUMemoryShuffledInputProviderBuilder
- } from 'deeplearn';
- const math =newNDArrayMathGPU();
- classColorAccessibilityModel{
- prepareTrainingSet(trainingSet){
- math.scope(()=>{
- const{ rawInputs, rawTargets }= trainingSet;
- const inputArray = rawInputs.map(v =>Array1D.new(this.normalizeColor(v)));
- const targetArray = rawTargets.map(v =>Array1D.new(v));
- const shuffledInputProviderBuilder =newInCPUMemoryShuffledInputProviderBuilder([
- inputArray,
- targetArray
- inputProvider,
- targetProvider,
- ]= shuffledInputProviderBuilder.getInputProviders();
- this.feedEntries =[
- { tensor:this.inputTensor, data: inputProvider },
- { tensor:this.targetTensor, data: targetProvider },
- exportdefaultColorAccessibilityModel;
- InCPUMemoryShuffledInputProviderBuilder,
- } from 'deeplearn';
- const math =newNDArrayMathGPU();
- classColorAccessibilityModel{
- batchSize =300;
- initialLearningRate =0.06;
- this.optimizer =newSGDOptimizer(this.initialLearningRate);
- exportdefaultColorAccessibilityModel;
- classColorAccessibilityModel{
- math.scope(()=>{
- this.session.train(
- this.costTensor,
- this.feedEntries,
- this.batchSize,
- this.optimizer
- exportdefaultColorAccessibilityModel;
- classColorAccessibilityModel{
- let learningRate =this.initialLearningRate *Math.pow(0.90,Math.floor(step /50));
- this.optimizer.setLearningRate(learningRate);
- math.scope(()=>{
- this.session.train(
- this.costTensor,
- this.feedEntries,
- this.batchSize,
- this.optimizer
- exportdefaultColorAccessibilityModel;
- InCPUMemoryShuffledInputProviderBuilder,
- } from 'deeplearn';
- classColorAccessibilityModel{
- train(step, computeCost){
- let learningRate =this.initialLearningRate *Math.pow(0.90,Math.floor(step /50));
- this.optimizer.setLearningRate(learningRate);
- math.scope(()=>{
- const cost =this.session.train(
- this.costTensor,
- this.feedEntries,
- this.batchSize,
- this.optimizer,
- computeCost ?CostReduction.MEAN :CostReduction.NONE,
- if(computeCost){
- costValue = cost.get();
- return costValue;
- exportdefaultColorAccessibilityModel;
- classColorAccessibilityModel{
- predict(rgb){
- let classifier =[];
- math.scope(()=>{
- const mapping =[{
- tensor:this.inputTensor,
- data:Array1D.new(this.normalizeColor(rgb)),
- classifier =this.session.eval(this.predictionTensor, mapping).getValues();
- return[...classifier ];
- exportdefaultColorAccessibilityModel;
- importReact,{Component} from 'react';
- import'./App.css';
- import generateColorSet from './data';
- importColorAccessibilityModel from './neuralNetwork';
- const ITERATIONS =750;
- const TRAINING_SET_SIZE =1500;
- const TEST_SET_SIZE =10;
- classApp extends Component{
- exportdefaultApp;
- importReact,{Component} from 'react';
- import'./App.css';
- import generateColorSet from './data';
- importColorAccessibilityModel from './neuralNetwork';
- const ITERATIONS =750;
- const TRAINING_SET_SIZE =1500;
- const TEST_SET_SIZE =10;
- classApp extends Component{
- colorAccessibilityModel;
- this.testSet = generateColorSet(TEST_SET_SIZE);
- this.trainingSet = generateColorSet(TRAINING_SET_SIZE);
- this.colorAccessibilityModel =newColorAccessibilityModel();
- this.colorAccessibilityModel.setupSession(this.trainingSet);
- this.state ={
- currentIteration:0,
- cost:-42,
- exportdefaultApp;
- classApp extends Component{
- componentDidMount (){
- for(let i =0; i <> ITERATIONS; i++){
- this.colorAccessibilityModel.train(i);
- exportdefaultApp;
- classApp extends Component{
- componentDidMount (){
- requestAnimationFrame(this.tick);
- this.setState((state)=>({
- currentIteration: state.currentIteration +1
- if(this.state.currentIteration ITERATIONS){
- requestAnimationFrame(this.tick);
- this.colorAccessibilityModel.train(this.state.currentIteration);
- exportdefaultApp;
- classApp extends Component{
- componentDidMount (){
- requestAnimationFrame(this.tick);
- this.setState((state)=>({
- currentIteration: state.currentIteration +1
- if(this.state.currentIteration ITERATIONS){
- requestAnimationFrame(this.tick);
- let computeCost =!(this.state.currentIteration %5);
- let cost =this.colorAccessibilityModel.train(
- this.state.currentIteration,
- computeCost
- if(cost >0){
- this.setState(()=>({ cost }));
- exportdefaultApp;
- classApp extends Component{
- const{ currentIteration, cost }=this.state;
- div className='app'>
- h1>NeuralNetworkforFontColorAccessibilityh1>
- p>Iterations:{currentIteration}p>
- p>Cost:{cost}p>
- div className='content'>
- div className='content-item'>
- ActualTable
- testSet={this.testSet}
- div>
- div className='content-item'>
- InferenceTable
- model={this.colorAccessibilityModel}
- testSet={this.testSet}
- div>
- constActualTable=({ testSet })=>
- p>ProgrammaticallyComputedp>
- constInferenceTable=({ testSet, model })=>
- p>NeuralNetworkComputedp>
- exportdefaultApp;
- constActualTable=({ testSet })=>
- p>ProgrammaticallyComputedp>
- {Array(TEST_SET_SIZE).fill(0).map((v, i)=>
- key={i}
- rgbInput={testSet.rawInputs[i]}
- rgbTarget={fromClassifierToRgb(testSet.rawTargets[i])}
- const fromClassifierToRgb =(classifier)=>
- classifier[0]> classifier[1]
- ?[255,255,255]
- :[0,0,0]
- constColorBox=({ rgbInput, rgbTarget })=>
- div className='color-box' style={{ backgroundColor: getRgbStyle(rgbInput)}}>
- span style={{ color: getRgbStyle(rgbTarget)}}>
- RgbString rgb={rgbInput}/>
- constRgbString=({ rgb })=>
- `rgb(${rgb.toString()})`
- const getRgbStyle =(rgb)=>
- `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`
- constInferenceTable=({ testSet, model })=>
- p>NeuralNetworkComputedp>
- {Array(TEST_SET_SIZE).fill(0).map((v, i)=>
- key={i}
- rgbInput={testSet.rawInputs[i]}
- rgbTarget={fromClassifierToRgb(model.predict(testSet.rawInputs[i]))}
百度搜索 "就爱阅读", 专业资料, 生活学习, 尽在就爱阅读网 92to.com, 您的在线图书馆!
来源: http://www.92to.com/bangong/2017/12-06/32150408.html