楼主: ReneeBK
1680 1

Feedforward Neural Networks using JavaScript [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4898份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49640 个
通用积分
55.8137
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2016-6-26 23:54:24 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. Feedforward Neural Network
  2. NPM version  build status  David deps  npm download

  3. A implementation of feedforward neural networks in javascript based on the mrbo answer found here:

  4. Implementation

  5. Methods

  6. new FNN(X, Y)

  7. train(options)

  8. Options

  9. hiddenLayers - Array with the size of each hidden layer in the FNN.
  10. iterations - Maximum number of iterations of the algorithm.
  11. learningRate - The learning rate (number).
  12. momentum - The regularization term (number).
  13. predict(dataset)

  14. toJSON()

  15. FNN.load(model)

  16. License

  17. MIT
复制代码

本帖隐藏的内容

feedforward-neural-networks-master.zip (7.49 KB)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Javascript Networks forward network script javascript learning version number hidden

本帖被以下文库推荐

沙发
ReneeBK 发表于 2016-6-26 23:55:00
  1. "use strict";

  2. var Layer = require("./layer");
  3. var Matrix = require("ml-matrix");

  4. class FeedforwardNeuralNetwork {
  5.     /**
  6.      * Constructor for the FNN (Feedforward Neural Networks) that takes an Array of Numbers,
  7.      * those numbers corresponds to the size of each layer in the FNN, the first and the last number of the array corresponds to the input and the
  8.      * output layer respectively.
  9.      *
  10.      * @constructor
  11.      */
  12.     constructor(X, Y) {
  13.         if (X === true) {
  14.             const model = Y;
  15.             this.layers = model.layers;
  16.             this.inputSize = model.inputSize;
  17.             this.outputSize = model.outputSize;
  18.         } else {
  19.             if (X.length !== Y.length)
  20.                 throw new RangeError("X and Y must have the same size.");
  21.             this.X = X;
  22.             this.Y = Y;
  23.             this.inputSize = X[0].length;
  24.             this.outputSize = Y[0].length;
  25.         }
  26.     }

  27.     /**
  28.      * Build the Neural Network with an array that represent each hidden layer size.
  29.      *
  30.      * @param {Array} layersSize - Array of sizes of each layer.
  31.      */
  32.     buildNetwork(layersSize) {
  33.         layersSize.push(this.outputSize);

  34.         this.layers = new Array(layersSize.length);

  35.         for (var i = 0; i < layersSize.length; ++i) {
  36.             var inSize = (i == 0) ? this.inputSize : layersSize[i - 1];
  37.             this.layers[i] = new Layer(inSize, layersSize[i]);
  38.         }

  39.         this.layers[this.layers.length - 1].isSigmoid = false;
  40.     }

  41.     /**
  42.      * Function that applies a forward propagation over the Neural Network
  43.      * with one case of the dataset.
  44.      * @param {Array} input - case of the dataset.
  45.      * @returns {Array} result of the forward propagation.
  46.      */
  47.     forwardNN(input) {
  48.         var results = input.slice();

  49.         for (var i = 0; i < this.layers.length; ++i) {
  50.             results = this.layers[i].forward(results);
  51.         }

  52.         return results;
  53.     }

  54.     /**
  55.      * Function that makes one iteration (epoch) over the Neural Network with one element
  56.      * of the dataset with corresponding prediction; the other two arguments are the
  57.      * learning rate and the momentum that is the regularization term for the parameters
  58.      * of each perceptron in the Neural Network.
  59.      * @param {Array} data - Element of the dataset.
  60.      * @param {Array} prediction - Prediction over the data object.
  61.      * @param {Number} learningRate
  62.      * @param momentum - the regularization term.
  63.      */
  64.     iteration(data, prediction, learningRate, momentum) {
  65.         var forwardResult = this.forwardNN(data);
  66.         var error = new Array(forwardResult.length);

  67.         if (typeof(prediction) === 'number')
  68.             prediction = [prediction];

  69.         for (var i = 0; i < error.length; i++) {
  70.             error[i] = prediction[i] - forwardResult[i];
  71.         }

  72.         var lengthLayers = this.layers.length;

  73.         for (i = 0; i < lengthLayers; ++i) {
  74.             error = this.layers[lengthLayers - 1 - i].train(error, learningRate, momentum);
  75.         }
  76.     }

  77.     /**
  78.      * Method that train the neural network with a given training set with corresponding
  79.      * predictions. The options argument has an array of the number of perceptrons that we want in each hidden layer, the
  80.      * number of iterations (default 50) that we want to perform, the learning rate and the momentum that is the
  81.      * regularization term (default 0.1 for both) for the parameters of each perceptron in the Neural Network.
  82.      *
  83.      * options:
  84.      * * hiddenLayers - Array of number with each hidden layer size.
  85.      * * iterations - Number
  86.      * * learningRate - Number
  87.      * * momentum - Number
  88.      *
  89.      * @param {object} options
  90.      */
  91.     train(options) {
  92.         if (options === undefined) options = {};

  93.         const trainingSet = this.X;
  94.         const predictions = this.Y;

  95.         var hiddenLayers = options.hiddenLayers === undefined ? [10] : options.hiddenLayers;
  96.         var iterations = options.iterations === undefined ? 50 : options.iterations;
  97.         var learningRate = options.learningRate === undefined ? 0.1 : options.learningRate;
  98.         var momentum = options.momentum === undefined ? 0.1 : options.momentum;

  99.         this.buildNetwork(hiddenLayers);

  100.         for (var i = 0; i < iterations; ++i) {
  101.             for (var j = 0; j < predictions.length; ++j) {
  102.                 var index = randomIntegerFromInterval(0, predictions.length - 1);
  103.                 this.iteration(trainingSet[index], predictions[index], learningRate, momentum);
  104.             }
  105.         }
  106.     }

  107.     /**
  108.      * Function that with a dataset, gives all the predictions for this dataset.
  109.      * @param {Matrix} dataset.
  110.      * @returns {Array} predictions
  111.      */
  112.     predict(dataset) {
  113.         if (dataset[0].length !== this.inputSize)
  114.             throw new RangeError("The dataset columns must have the same size of the " +
  115.                 "input layer");
  116.         var result = new Array(dataset.length);
  117.         for (var i = 0; i < dataset.length; i++) {
  118.             result[i] = this.forwardNN(dataset[i]);
  119.         }

  120.         result = new Matrix(result);
  121.         //return result.columns === 1 ? result.getColumn(0) : result;
  122.         return result;
  123.         
  124.     }

  125.     toJSON() {
  126.         return {
  127.             name: 'FNN',
  128.             layers: this.layers,
  129.             inputSize: this.inputSize,
  130.             outputSize: this.outputSize
  131.         };
  132.     }

  133.     static load(model) {
  134.         if (model.name !== 'FNN')
  135.             throw new RangeError('Invalid model: ' + model.name);
  136.         return new FeedforwardNeuralNetwork(true, model);
  137.     }
  138. }

  139. module.exports = FeedforwardNeuralNetwork;

  140. /**
  141. * Function that returns a random number between two numbers (inclusive)
  142. * @param {number} min - lower bound
  143. * @param {number} max - upper bound.
  144. * @returns {number} random number
  145. */
  146. function randomIntegerFromInterval(min, max) {
  147.     return Math.floor(Math.random() * (max - min + 1) + min);
  148. }
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-9 12:30