{"id":15454503,"url":"https://github.com/degitx/nen","last_synced_at":"2025-04-21T06:11:37.154Z","repository":{"id":57309804,"uuid":"120382031","full_name":"DEgITx/nen","owner":"DEgITx","description":"Neural Network library with with native CPU/GPU bindings and learning algorithms choice","archived":false,"fork":false,"pushed_at":"2019-10-15T20:49:45.000Z","size":203,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-20T00:45:17.747Z","etag":null,"topics":["algorithm-library","cpp","machine-learning","neural-network","nodejs"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DEgITx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-06T00:55:47.000Z","updated_at":"2024-04-28T03:38:39.000Z","dependencies_parsed_at":"2022-09-09T22:41:00.034Z","dependency_job_id":null,"html_url":"https://github.com/DEgITx/nen","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEgITx%2Fnen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEgITx%2Fnen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEgITx%2Fnen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEgITx%2Fnen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DEgITx","download_url":"https://codeload.github.com/DEgITx/nen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249834750,"owners_count":21331987,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm-library","cpp","machine-learning","neural-network","nodejs"],"created_at":"2024-10-01T22:03:31.339Z","updated_at":"2025-04-20T00:45:21.123Z","avatar_url":"https://github.com/DEgITx.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"NEN - Fast and Light Neural Network Library\n==============\n\n[![CircleCI Build Status](https://circleci.com/gh/DEgITx/nen.png?style=shield)](https://circleci.com/gh/DEgITx/nen)\n[![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/g35w6jrxotudolvo?svg=true)](https://ci.appveyor.com/project/DEgITx/nen)\n[![Travis Build Status](https://travis-ci.org/DEgITx/nen.svg?branch=master)](https://travis-ci.org/DEgITx/nen)\n\nNeural Network library with with native CPU/GPU bindings and learning algorithms choice. \n\n## Instalation\n\n``` sh\nnpm install nen\n```\n\n## Quick start\n\n``` js\nconst NEuralNetwork = require('nen');\n\n// create neural network 2x4 size\nconst network = NEuralNetwork(\n\t2, // with 2 inputs\n\t1, // 1 output\n\t2, // 2 hidden perceptron layers (width)\n\t4 // 4 perceptrons per layer (height)\n);\n// new NEuralNetwork(...) also acceptable\n\n// set learning rate of the network\nnetwork.setRate(0.02); // default is 0.02\n\n// now let input XOR learning data\n\n// learning set from 4 lines\nconst inputData = [\n\t[0, 1], \n\t[1, 1], \n\t[0, 0], \n\t[1, 0]\n]\n\nconst outputData = [\n\t[1], // 0 xor 1 = 1\n\t[0], // 1 xor 1 = 0\n\t[0], // 0 xor 0 = 0\n\t[1]  // 1 xor 0 = 1\n]\n\n// start learning process until reaching average 0.5% errors (0.005)\nconst errors = network.train(\n\tinputData, \n\toutputData, \n\t{ error: 0.5, sync: true }\n);\n\n// lets try now our data through learned neural network\nconst output = network.forward([0, 1]);\n\nconsole.log(output) // [ 0.9451534677309323 ]\n// pretty close to 1 - good :)\n\n```\n\n## Performance\n\nhidden layers size: *width* x *height*\n\nrate: *0.02*\n\nerror: *0.005*\n\nalgorithm: *3* (default)\n\nnetwork size  | iterations | time\n------------- | ---------- | ---------\n1x4           | 3132       |  5.319ms\n2x4           | 1512       |  5.163ms\n4x4           | 1968       |  13.963ms\n1x50          | 1584       |  29.877ms\n3x12          | 1000       |  30.376ms\n\n\n## API\n\n### train( *[inputData]*, *[outputData]*, *options* )\n\ntrain network with input and output data\n\nreturns Promise by default or errors array if sync setted to true in options\n\noptions:\n* sync - is synchronously call (default: false) [or return Promise]\n* error - min percent of errors until finish (default: 0 - one operation)\n* iterations - iteration limitation (full cycle) (default: 0 - unlimit)\n* iteration : Function - callback that called per full iteration cycle\n\nNote: without \"error\" and \"iterations\" option train() don't start learning cycle. \nYou can set those values or start cycle manually (slower)\n\n#### train( fitness: Function, error: Function, *options* )\n\ntrain using fitness function\n\n### forward( *[inputData]* )\n\nforward input data through network and return output result in array\n\n#### forward( *[inputData]*, wId = undefined )\n\nforward input data through wId network (userfull for fitness function learning)\n\n### setRate( *rate* )\n\nset learting rate\ndefault: 0.02\n\n### setAlgorithm( *algorithm* )\n\nset learning algorithm\ndefault: 3\n\n### setActivation( *activationType* )\n\nset actionvation function\n\n* 0 - Logistic (data range: [0, 1])\n* 1 - Hyperbolic tangent (data range: [-1, 1])\n* 2 - Identity (data range: (-∞, +∞))\n* 3 - Rectified linear unit (data range: [0, +∞))\n\ndefault: 0 - Logistic\n\n### error( *[outputData]* )\n\nget errors array from last forwarding\n\n#### error( *[outputData]*, *[inputData]*, wId = undefined )\n\nforward input data through the network and return errors\n\n### iterations()\n\nget number of iterations from last learning process (each sample from epoch is count)\n\n### epochCount()\n\nget epoch number (each sample set in epoch is count as one iteration)\n\n### save( *fileName* )\n\nsave current neural network to file\n\n### load( *fileName* )\n\nload neural network from file\n\n### loadData( *fileName* )\n\nload training data from file\n\nEvery line contain inputs and appropriate outputs:\n\n1: input1_1 input1_2 ... input1_N output1_1 output1_2 ... input1_M\n\n2: input2_1 input2_2 ... input2_N output2_1 output2_2 ... input2_M\n\n...\n\n### setMoment( *moment* )\n\nset moment factor\n\ndefault: 0.3\n\n### setDEpsilon( *dEspsilon* )\n\nset divider epsilon\n\ndefault: 10^-11\n\n### setPopulation( *populationSize* )\n\npopulation size for genetic algorithm\n\ndefault: 10\n\n### setElitePart( *populationPart* )\n\npopulation part as elite part\n\ndefault: 3 (means 1/3 of population)\n\n### setShuffle( *shuffle* )\n\nshuffle data between each iteration\n\ndefault: true\n\n### setMultiThreads( *multi* )\n\nEnable multithread mini-batch learning. For more stable but slower learning disable this option.\n\ndefault: true\n\n## Fitness Function learning example\n\nSometimes we don't know exact input/output values, but know that neural network A better then neural network B. For such cases we can compare two neural networks for best results using genetic algorithms and fitness compare function\n\n``` js\nnetwork.setRate(0.3) // set learning rate\n\nlet errors = [] // aray represents all errors of input data\nlet error = 1.0 // average error of input data\n\nnetwork.train(\n\t// compare network \"a\" with network \"b\" for set element number \"i\"\n\t(a, b, iteration) =\u003e {\n\t\t// get input data index from current iteration\n\t\tconst i = iteration % inputData.length;\n\t\t// forward inputData through \"a\" network and get error for outputData\n\t\tconst errorA = network.error(outputData[i], inputData[i], a);\n\t\t// forward inputData through \"b\" network and get error for outputData\n\t\tconst errorB = network.error(outputData[i], inputData[i], b);\n\t\t// network A must be BETTER then network B\n\t\treturn errorA \u003c errorB;\n\t}, \n\t// to know when to stop learning process send a callback\n\t// with represents average error from iteration\n\t(iteration) =\u003e {\n\t\t// get input data index from current iteration\n\t\tconst i = iteration % inputData.length;\n\t\t// forward input values and get results\n\t\tconst values = network.forward(inputData[i]);\n\t\t// get errors from last forwarding\n\t\terrors[i] = network.error(outputData[i]);\n\t\t// calculate average error\n\t\tif(i == errors.length - 1) // calucate error only on last input/output element\n\t\t{\n\t\t\tfor(const e of errors)\n\t\t\t\terror += e\n\t\t\terror /= errors.length\n\t\t}\n\t\t// return average error to know when to stop learning process\n\t\treturn error\n\t},\n\t{\n\t\terror: 0.5, // learn process until reaching 0.5% errors\n\t\tsync: true // function call synchronously\n\t}\n)\n\nconsole.log(network.forward([0, 1])) // [ 0.9999999999942144 ]\nconsole.log(network.forward([0, 0])) // [ 9.965492560315975e-10 ]\n```\n\n## Save/Load trained neural network\nThis example contains async train call\n\n``` js\nconst network = NEuralNetwork(2, 1, 2, 4) // create neural network\n\n// learn data\nconst inputData = [\n\t[0, 1], \n\t[1, 1], \n\t[0, 0], \n\t[1, 0]\n]\nconst outputData = [\n\t[1], // 0 xor 1 = 1\n\t[0], // 1 xor 1 = 0\n\t[0], // 0 xor 0 = 0\n\t[1]  // 1 xor 0 = 1\n]\n\n// create second neural network\n// doesn't matter which size (it will be resized after load call)\nconst network2 = NEuralNetwork(2, 1, 2, 4)\n\nconst f = async () =\u003e {\n\t// train until 0.5% errors\n\tawait network.train(inputData, outputData, {error: 0.5})\n\t// save trained network to file\n\tnetwork.save('xor.nen')\n\t// load network2 from file \n\tnetwork2.load('xor.nen')\n\t\n\t// result\n\tconsole.log(network.forward([0, 1])) // [ 0.9097776354174257 ]\n\tconsole.log(network2.forward([0, 1])) // [ 0.909777584133939 ]\n}\nf()\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdegitx%2Fnen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdegitx%2Fnen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdegitx%2Fnen/lists"}