Dann.js

Deep Neural Network Library for JavaScript.

README


Dannjs

versionNpmStat repoSize downloadNpmStat downloadNpmStat GitHub

Deep Neural Network Library for Javascript

    Train a neural network with your data & save its trained state!


  

DemoInstallationDocumentationContributeDiscordLicense




Installation

CDN :

  1. ``` html
  2. <script src="https://cdn.jsdelivr.net/gh/matiasvlevi/dann@v2.4.1e/build/dann.min.js"></script>
  3. ```

Node :

  1. ```
  2. npm i dannjs
  3. ```



Getting started



Require package


Components from the library can be imported like this
  1. ``` js
  2. const { Dann } = require('dannjs');
  3. ```



Basic model construction

Setting up a small (4,6,6,2) neural network.
  1. ``` js
  2. const nn = new Dann(4, 2);
  3. nn.addHiddenLayer(6, 'leakyReLU');
  4. nn.addHiddenLayer(6, 'leakyReLU');
  5. nn.outputActivation('tanH');
  6. nn.makeWeights();
  7. nn.lr = 0.0001;
  8. nn.log({details:true});
  9. ```



Train by backpropagation

Training with a dataset.
  1. ``` js
  2. //XOR 2 inputs, 1 output
  3. const dataset = [
  4.     {
  5.         input: [0, 0],
  6.         output: [0]
  7.     },
  8.     {
  9.         input: [1, 0],
  10.         output: [1]
  11.     },
  12.     {
  13.         input: [0, 1],
  14.         output: [1]
  15.     },
  16.     {
  17.         input: [1, 1],
  18.         output: [0]
  19.     }
  20. ];

  21. //train 1 epoch
  22. for (data of dataset) {
  23.     nn.backpropagate(data.input, data.output);
  24.     console.log(nn.loss);
  25. }
  26. ```



Train by mutation

For neuroevolution simulations. Works best with small models & large population size.
  1. ``` js
  2. const populationSize = 1000;
  3. let newGeneration = [];

  4. for (let i = 0; i < populationSize; i++) {

  5.     // parentNN would be the best nn from past generation.
  6.     const childNN = parentNN;
  7.     childNN.mutateRandom(0.01, 0.65);

  8.     newGeneration.push(childNN);
  9. }
  10. ```



Standalone function

Convert a Neural Network to a JS function that can output predictions without the library.
  1. ``` js
  2. let strfunc = nn.toFunction();
  3. console.log(strfunc);
  4. ```



Save JSON

  1. ``` js
  2. let json = nn.toJSON();
  3. console.log(json);
  4. ```




Demo:

[AI predicts San-francisco Housing prices.](https://dannjs.org/livedemo.html)
more examples & demos here



Online editor:





Socials


twittertwittertwitter


Graph Dann models with this library






Stickers






Contributors






Matias Vazquez-Levi

💻 📖 ⚠️

Francesco Ciulla

📢

Labnan

🐛 💻 ⚠️

sharkAce

💻

Hasnain Iqbal

💻 ⚠️

EL Ramos

🐛 ⚠️ 💻

viabhinav


and1can

💻 ⚠️


Any contributions are welcome! See CONTRIBUTING.md.







License


MIT