编程语言
首页 > 编程语言> > javascript-如何正确设置brain.js神经网络

javascript-如何正确设置brain.js神经网络

作者:互联网

我正在使用从http://archive.ics.uci.edu/ml/datasets/Auto+MPG开始的自动MPG训练集

我的代码是:

'use strict';
var brain, fs, normalizeData, trainNetwork, _;

_ = require('lodash');

brain = require('brain');

fs = require('fs');

trainNetwork = function(trainNetworkCb) {
  var net;
  net = new brain.NeuralNetwork();
  return fs.readFile('./data/autodata.csv', function(err, fileData) {
    var fileString, lines, trainingData;
    if (err) {
      return trainNetworkCb(err);
    }
    fileString = fileData.toString();
    lines = fileString.split('\n');
    trainingData = lines.splice(0, lines.length / 2);
    trainingData = _.map(trainingData, function(dataPoint) {
      var normalizedData, obj;
      normalizedData = normalizeData(dataPoint);
      obj = {
        input: normalizedData,
        output: {
          continuous: normalizedData.continuous
        }
      };
      delete obj.input.continuous;
      return obj;
    });
    net.train(trainingData, {
      log: true,
      logPeriod: 100,
      errorThresh: 0.00005
    });
    return trainNetworkCb(null, net);
  });
};

trainNetwork(function(err, net) {
  if (err) {
    throw err;
  }
  return fs.readFile('./data/autodata.csv', function(err, fileData) {
    var fileString, lines, testData;
    if (err) {
      return trainNetworkCb(err);
    }
    fileString = fileData.toString();
    lines = fileString.split('\n');
    testData = lines.splice(lines.length / 2);
    testData = _.filter(testData, function(point) {
      return point !== '';
    });
    testData = _.map(testData, function(dataPoint) {
      var normalizedData, obj;
      normalizedData = normalizeData(dataPoint);
      obj = {
        output: {
          continuous: normalizedData.continuous
        },
        input: normalizedData
      };
      delete obj.input.continuous;
      return obj;
    });
    return _.each(testData, function(dataPoint) {
      var output;
      output = net.run(dataPoint.input);
      console.log(output);
      console.log(dataPoint);
      return console.log('');
    });
  });
});

normalizeData = function(dataRow) {
  var cylinders, dataSet, model_years, origins, row;
  dataSet = dataRow.split(',');
  dataSet = _.map(dataSet, function(point) {
    return Number(point);
  });
  row = {};
  cylinders = [5, 3, 6, 4, 8];
  _.each(cylinders, function(cylinder) {
    row["cylinder" + cylinder] = cylinder === dataSet[0] ? 1 : 0;
  });
  row.displacement = dataSet[1] / 500;
  row.horsepower = dataSet[2] / 500;
  row.weight = dataSet[3] / 10000;
  row.acceleration = dataSet[4] / 100;
  model_years = [82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70];
  _.each(model_years, function(model_year) {
    row["model_year" + model_year] = model_year === dataSet[5] ? 1 : 0;
  });
  origins = [2, 3, 1];
  _.each(origins, function(origin) {
    row["origin" + origin] = origin === dataSet[6] ? 1 : 0;
  });
  row.continuous = dataSet[7] / 100;
  return row;
};

我相信我正在正确地规范一切.我将一半数据用于培训,另一半用于测试.据我所知,数据不是有序的,因此哪一半用于哪一部分都无关紧要.

测试时我的错误很大.通常减少10MPG左右(误差30%).我做错了什么?

谢谢

解决方法:

链接的数据集按型号年份排序;也许技术上的巨大变化使发动机更加高效?神经网络取决于训练过程中的正确输出.我将尝试使用除最后一行以外的所有内容来训练网络,然后使用该网络进行测试.您可以将正在使用的csv文件链接到我吗? normalizeData函数无法为您提供所需的链接文件(http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data)

编辑:

似乎无论您指定了什么错误,在训练运行中,大脑运行的迭代次数都不会超过20,000次.有几种方法可以解决此问题.您可以指定神经网络的learningRate.将learningRate设置为0.6(默认值为0.3)有助于我获得更准确的结果

net.train(trainingData, {
  log: true,
  logPeriod: 100,
  errorThresh: 0.00005,
  learningRate: 0.6
});

更高的learningRate意味着更积极的权重调整,这在您未运行所需的迭代次数时会有所帮助.

或者,您可以在options对象中指定迭代的总数(如果未指定,则默认为20,000-请参见here).

net.train(trainingData, {
  log: true,
  logPeriod: 100,
  errorThresh: 0.00005,
  iterations: 100000
});

当我<迭代&&错误> errorThresh计算为false.因此,请随意提高迭代次数,以确保上面的表达式变为false,因为错误低于您指定的errorTresh(source).

标签:neural-network,javascript,brain-js
来源: https://codeday.me/bug/20191028/1955712.html