Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

3net.js

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

3net.js - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

83

index.js

@@ -21,14 +21,3 @@ var math = require('mathjs');

neuralNetwork.prototype.train = function (input, label, learning_rate, iters, regularization) {
if (!iters) iters = 500;
if (!learning_rate) learning_rate = 0.1;
if (!regularization) regularization = 1;
if (label.length !== this.layer3) return false;
if (input.length !== this.layer1) return false;
[this.theta1, this.theta2] = neuralMath.gradientDescent(this.backpropagation, this.theta1, this.theta2, this.theta1gradient, this.theta2gradient, iters, learning_rate);
return true;
}
neuralNetwork.prototype.backpropagation = function () {
neuralNetwork.prototype.backpropagation = function (input, label, options) {
//Forward

@@ -43,10 +32,56 @@ this.predict(input);

//Get gradients
this.theta2gradient = math.add(this.theta2gradient, neuralMath.outerProduct(d3, this.a2));
this.theta1gradient = math.add(this.theta1gradient, neuralMath.outerProduct(d2, this.a1));
this.theta2gradient = neuralMath.outerProduct(d3, this.a2);
this.theta1gradient = neuralMath.outerProduct(d2, this.a1);
return [this.theta1gradient, this.theta2gradient];
if (options.regularization) {
for (var i = 0; i < math.size(this.theta1gradient)[0]; i += 1) {
for (var j = 1; j < math.size(this.theta1gradient)[1]; j += 1) {
this.theta1gradient[i][j] += options.regularization * this.theta1gradient[i][j];
}
}
for (var i = 0; i < math.size(this.theta2gradient)[0]; i += 1) {
for (var j = 1; j < math.size(this.theta2gradient)[1]; j += 1) {
this.theta2gradient[i][j] += options.regularization * this.theta2gradient[i][j];
}
}
}
}
neuralNetwork.prototype.gradientDescent = function (input, label, options) {
var lastCost = neuralMath.getCost(this.a3, label, [this.theta1, this.theta2], options);
for (var i = 0; i < options.iters; i += 1) {
this.backpropagation(input, label, options);
var currentCost = neuralMath.getCost(this.a3, label, [this.theta1, this.theta2], options);
if (i > 0 && math.abs(currentCost - lastCost) <= 0.001) return;
lastCost = currentCost;
this.theta1 = math.subtract(this.theta1, math.dotMultiply(options.learning_rate, this.theta1gradient));
this.theta2 = math.subtract(this.theta2, math.dotMultiply(options.learning_rate, this.theta2gradient));
}
}
function neuralNetwork(layer1, layer2, layer3) {
neuralNetwork.prototype.train = function (input, label, options) {
if (!options) options = {};
if (!options.iters) options.iters = 500;
if (!options.learning_rate) options.learning_rate = 0.5;
if (!options.regularization) options.regularization = 0.1;
if (label.length !== this.layer3) return false;
if (input.length !== this.layer1) return false;
this.gradientDescent(input, label, options);
return true;
}
neuralNetwork.prototype.exportNet = function () {
var data = {};
data.layer1 = this.layer1;
data.layer2 = this.layer2
data.layer3 = this.layer3;
data.theta1 = this.theta1;
data.theta2 = this.theta2;
return data;
}
function neuralNetwork(layer1, layer2, layer3, theta) {
this.layer1 = layer1;

@@ -56,4 +91,9 @@ this.layer2 = layer2;

this.theta1 = math.random([layer2, layer1 + 1], -1, 1);
this.theta2 = math.random([layer3, layer2 + 1], -1, 1);
if (!theta) {
this.theta1 = math.random([layer2, layer1 + 1], -5, 5);
this.theta2 = math.random([layer3, layer2 + 1], -5, 5);
} else {
this.theta1 = theta[0];
this.theta2 = theta[1];
}

@@ -74,2 +114,7 @@ this.theta1gradient = math.zeros([layer2, layer1 + 1]);

exports.createNet = createNet;
function importNet(data) {
return new neuralNetwork(data.layer1, data.layer2, data.layer3, [data.theta1, data.theta2]);
}
exports.createNet = createNet;
exports.importNet = importNet;

@@ -22,3 +22,3 @@ var math = require('mathjs');

function getCost(pred, actual) {
function getCost(pred, actual, theta, options) {
if (pred.length !== actual.length) return false;

@@ -30,2 +30,16 @@ var cost = 0;

}
if (options.regularization) {
var regCost = 0;
for (var i = 0; i < math.size(theta[0])[0]; i += 1) {
for (var j = 1; j < math.size(theta[0])[1]; j += 1) {
regCost += theta[0][i][j] ^ 2;
}
}
for (var i = 0; i < math.size(theta[1])[0]; i += 1) {
for (var j = 1; j < math.size(theta[1])[1]; j += 1) {
regCost += theta[1][i][j] ^ 2;
}
}
cost += regCost * (options.regularization / 2);
}
return cost;

@@ -32,0 +46,0 @@ }

{
"name": "3net.js",
"version": "0.0.3",
"version": "0.0.4",
"description": "A simple library for implementing 3 layer neural networks",
"main": "index.js",
"keywords": [
"neural", "network", "machine", "learning", "3netjs", "3net"
"neural", "network", "machine", "learning", "3netjs", "3net", "data", "multilayer", "perceptron"
],

@@ -24,7 +24,3 @@ "repository": {

"mathjs": "^2.1.1"
},
"gitHead": "fd2f0bc5d74bb032e4365b5c6181557820921f8b",
"_id": "3net.js@0.0.1",
"_shasum": "c594ad9cbdae82a140c5de8f1015e07219f07c35",
"_from": "3net.js@*"
}
}
# 3net.js
A simple library for implementing 3 layer neural networks
[![NPM](https://nodei.co/npm/3net.js.png)](https://npmjs.org/package/3net.js)
var three_net = require('3net.js');
var net = three_net.createNet(inputLayer, hiddenLayer, outputLayer);
net.train(data, label);
net.predict(data);
A simple library for implementing 3 layer neural networks
#### Example
var three_net = require('3net.js'); //Install with 'npm install 3net.js'
//Initialization
var inputLayer = 400;
var hiddenLayer = 25;
var outputLayer = 10;
var net = three_net.createNet(inputLayer, hiddenLayer, outputLayer);
//Training
options = {"iters": 500, "learning_rate": 0.5, "regularization": 0.1};
net.train(data, label, options);
//Predicting
net.predict(data); //Returns a 10 dimensional array of the output neuron activations
//Importing and exporting
var savedNet = net.exportNet(); //Exports as JSON
var copiedNet = three_net.importNet(savedNet); //Imports as JSON
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc