@jsmlt/jsmlt
Version:
JavaScript Machine Learning
56 lines (42 loc) • 2.7 kB
JavaScript
;
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var chai = require('chai');
var chaiAlmost = require('chai-almost');
var FullyConnected = require('./index.js'); // Set up float equality testing
chai.use(chaiAlmost());
var expect = chai.expect;
describe('Supervised.NeuralNetwork.FullyConnected', function () {
describe('.forwardPass, .deltaRule', function () {
it('should generate correct outputs, activations, and deltas for network without hidden layers', function () {
/**
* Construct the sample network, consisting of two layers: an input layer with 3 nodes (including)
* a bias node, and an output layer with 2 nodes. The weights are set as follows, where node 0 in
* the first layer is the bias node, rows correspond to input nodes, and columns correspond to
* output nodes:
* 0 1
* -1 0
* 2 -2
*/
var network = new FullyConnected.FullyConnected();
network.layers = [3, 2];
network.setWeights([[[0, 1], [-1, 0], [2, -2]]]); // Test forward pass
var _network$forwardPass = network.forwardPass([0.2, 0.4], 0),
_network$forwardPass2 = _slicedToArray(_network$forwardPass, 2),
activations = _network$forwardPass2[0],
outputs = _network$forwardPass2[1]; // Input layer
expect(outputs[0][0]).to.equal(1);
expect(outputs[0][1]).to.equal(0.2);
expect(outputs[0][2]).to.equal(0.4); // Output layer
expect(activations[1][0]).to.almost.equal(0.6);
expect(activations[1][1]).to.almost.equal(0.2);
expect(outputs[1][0]).to.almost.equal(0.64565630622);
expect(outputs[1][1]).to.almost.equal(0.54983399731); // Test delta rule
var deltas = network.deltaRule(activations, outputs, [0.8, 0.2]);
expect(deltas[1][0]).to.almost.equal(-0.03531140475);
expect(deltas[1][1]).to.almost.equal(0.08658971203);
});
});
});