Dann Class

Deep Neural Network object. Can be trained with data or by neuro-evolution.


Constructor

Dann
(
  • [input]
  • [output]
)

Parameters:

  • [input] Number optional

    the number of input neurons.

  • [output] Number optional

    the number of output neurons.

Example:


// 2 input, 1 output model
const nn = new Dann(2, 1);
nn.log();

Methods

addHiddenLayer
(
  • size
  • [act]
)

Add a Hidden Neuron Layer to a Dann neural network.

Parameters:

Example:


const nn = new Dann(10, 2);
//Add a layer
nn.addHiddenLayer(8, 'sigmoid');
//console log
console.log('Added first hidden layer: ');
nn.log({struct:true});
//Add a layer
nn.addHiddenLayer(4, 'tanH');
//console log
console.log('Added a second hidden layer: ');
nn.log({struct:true});

backpropagate
(
  • inputs
  • target
  • [options]
)

Backpropagate trough a Dann model in order to train the weights.

Parameters:

  • inputs Array

    Array of input data.

  • target Array

    Array of expected output.

  • [options] Object optional

    Object including specific properties.

    Property Type Function
    log Boolean If set to true, it will log a report in the console.
    table Boolean If the 'log' option is set to true, setting this value to true will print the arrays of this function in tables.

Example:


const nn = new Dann(2, 1);
nn.addHiddenLayer(8);
nn.makeWeights();
// Train 1000 epoch
for (let i = 0; i < 1000; i++) {
   nn.backpropagate([0,0],[0]);
   nn.backpropagate([1,0],[1]);
   nn.backpropagate([0,1],[1]);
   nn.backpropagate([1,1],[0]);
}

createFromJSON
(
  • data
)
Dann static

Creates a Dann model from a json object.

Parameters:

  • data Object

    model data json object, you can get this object from a yourmodel.toJSON(); See docs here.

Returns:

Dann :

A Dann model.

Example:


const nn = new Dann(24, 4);
nn.addHiddenLayer(12, 'sigmoid');
nn.makeWeights();
const modelData = nn.toJSON();
const newNN = Dann.createFromJSON(modelData);
newNN.log();

feedForward
(
  • inputs
  • [options]
)
Array

Feed data through the model to obtain an output or prediction.

Parameters:

  • inputs Array

    Array of input data.

  • [options] Object optional

    Object including specific properties.

    Property Type Function
    log Boolean If set to true, it will log a report in the console.
    table Boolean If the 'log' option is set to true, setting this value to true will print the arrays of this function in tables.
    decimals Integer If used, the output of this function will be rounded to the number of decimals specified.

Returns:

Array :

Array of output predictions.

Example:


const nn = new Dann(4, 2);
nn.makeWeights();
let prediction = nn.feedForward([0,0,0,1], {log:true});
//outputs an array of length 2
console.log(prediction);

fromJSON
(
  • data
)
Dann

Applies a json object to a Dann model.

Parameters:

  • data Object

    model data json object, you can get this object from a yourmodel.toJSON(); See docs here.

Returns:

Dann :

A Dann model.

Example:


const nn = new Dann(24,4);
nn.addHiddenLayer(18,'tanH');
nn.addHiddenLayer(12,'sigmoid');
nn.makeWeights();
const modelData = nn.toJSON();
const newNN = new Dann();
newNN.fromJSON(modelData);
newNN.log();

load
(
  • name
  • arg2
  • arg3
)
deprecated
Use fromJSON or createFromJSON, Removed in 2.2.6

Defined in src\classes\dann\methods\load.js:1

Deprecated: Use fromJSON or createFromJSON, Removed in 2.2.6

(Browser) When this function is called, an input tag requesting a file appears on screen. When clicked, it opens a local file dialogue. Once the appropriate file is selected the dann data automatically uploads. The filename argument is not required for this version since the browser dialog takes care of it.

Parameters:

  • name String

    The name of the variable that holds the dann model.

  • arg2 String

    The ID of the HTML element in which to place the input dom element. If left undefined, the input dom element is appended to the body element.

  • arg3 Function

    A function to be called when the model finished loading.

load
(
  • name
  • arg2
)
deprecated
Use Use fromJSON or createFromJSON, Removed in 2.2.6

Defined in src\classes\dann\methods\load.js:11

Deprecated: Use Use fromJSON or createFromJSON, Removed in 2.2.6

(Nodejs) Load a previously saved json file from ./savedDanns/. If the network's architechture is not the same, it is going to overwrite the Dann object.

Parameters:

  • name String

    The name of the saved directory that holds the dann model.

  • arg2 Function

    A function to be called when the model finished loading.

log
(
  • [options]
)

Displays information about the model in the console.

Parameters:

  • [options] Object optional

    An object including specific properties.

    Property Type Function
    details Boolean If set to true, the function will log more advanced details about the model.
    decimals integer The number of decimals the logged data is going to have. It is set to 3 by default.
    table Boolean Whether or not we want to print our matrices in the form of a table or Matrix object log.
    gradients Boolean If this is set to true, the the function will log the gradients of the model.
    biases Boolean If this is set to true, the the function will log the biases of the model.
    weights Boolean If this is set to true, the the function will log the weights of the model.
    struct Boolean If this is set to true, the the function will log the structure of the model.
    errors Boolean If this is set to true, the the function will log the errors of the model.
    misc Boolean If this is set to true, the the function will log the loss of the model, the learning rate of the model and the loss function.

Example:


const nn = new Dann(24, 2);
nn.log();

makeWeights
(
  • [arg1]
  • [arg2]
)

Creates the weights. This function should be called after all the hidden layers were added. The optional parameters determine the range in which starting weights are going to be set randomly. If no arguments are specified, weights are going to be set in between -1 and 1.

Parameters:

  • [arg1] Number optional

    The minimum range value.

  • [arg2] Number optional

    The maximum range value.

Example:


const nn = new Dann(2, 2);
// initiate the Weights
nn.makeWeights();
// log weights
nn.log({weights:true, table:true});
// add a layer & re-initiate weights in a range of (-0.1, 0.1)
nn.addHiddenLayer(4, 'sigmoid');
nn.makeWeights(-0.1, 0.1);
// log weights
console.log('New re-initiated weights:');
nn.log({weights:true, table:true});

mapWeights
(
  • f
)

This method maps the weights of a Dann model. It is usefull for neuroevolution simulations where you would map the weights with an equation containing a random factor.

Parameters:

  • f Function

    the function to map the weights with.

Example:


const nn = new Dann(2, 2);
nn.makeWeights(-1, 1);
nn.log({weights:true});
nn.mapWeights((x)=>{
  return (Math.random()*0.1)+x;
});
nn.log({weights:true})

mutateAdd
(
  • randomFactor
)

This function mutates the weights by taking a percentage of the weight & adding it to the weight. This is for Neuroevolution tasks.

Parameters:

  • randomFactor Number

    Percentage to add to each weight. Generally in 0 and 1.

Example:


const nn = new Dann(4, 2);
nn.makeWeights();
nn.log({weights:true, table:true})
// weights add 5% of themselves.
nn.mutateAdd(0.05);
nn.log({weights:true,table:true});

mutateRandom
(
  • range
  • [probability]
)

This function mutates each weights randomly. This is for Neuroevolution tasks.

Parameters:

  • range Number

    This will multiply with a random number from -range to range and add to each weight.

  • [probability] Number optional

    The probability of a weight being affected by a random mutation. Ranging from 0 to 1. Setting this value to 1 would mutate all the model's weights.

Example:


const nn = new Dann(4, 2);
nn.makeWeights();
nn.log({weights:true, table:true});
// adding (weight*random(-0.1, 0.1)) to 50% of the weights.
nn.mutateRandom(0.1, 0.5);
nn.log({weights:true, table:true});

outputActivation
(
  • act
)

Sets the activation function of the output.

Parameters:

Example:


const nn = new Dann(4, 2);
nn.addHiddenLayer(8, 'sigmoid');
nn.makeWeights();
console.log('Before changing the output activation');
nn.log({struct:true});
nn.outputActivation('tanH');
console.log('After changing the output activation');
nn.log({struct:true});

save
(
  • name
)
deprecated
Use toJSON, Removed in 2.2.6

Defined in src\classes\dann\methods\save.js:1

Deprecated: Use toJSON, Removed in 2.2.6

(Browser) saves a json file containing information about the network and its current state. When the function is called, a local file dialogue is opened by the browser.

Parameters:

  • name String

    The name of the json file.

save
(
  • name
  • [options]
)
deprecated
Use toJSON, Removed in 2.2.6

Defined in src\classes\dann\methods\save.js:9

Deprecated: Use toJSON, Removed in 2.2.6

(Nodejs) saves a json file containing information about the network and its current state in ./savedDanns/name/dannData.json.

Parameters:

  • name String

    The name of the json file.

  • [options] Object optional

    An object containing options on the save process.

setLossFunction
(
  • name
)

Set the loss function of a Dann model

Parameters:

Example:


const nn = new Dann(4, 2);
nn.addHiddenLayer(8, 'sigmoid');
nn.makeWeights();
//Before changing the loss function
console.log(nn.lossfunc);
nn.setLossFunction('mael');
//After changing the loss function
console.log(nn.lossfunc);

toFunction
(
  • [name]
)
String

This method allows for a Dann model to be converted into a minified javascript function that can run independently, which means you don't need to import the library for it to work. The function generated acts as a Dann.feedForward().

Parameters:

  • [name] String optional

    the name of the function, set to 'myDannFunction' by default.

Returns:

String :

The function as a string.

Example:


const nn = new Dann(4, 4);
nn.addHiddenLayer(8);
nn.makeWeights();
let stringFunction = nn.toFunction();
// Copy & paste the string function!
console.log(stringFunction);

toJSON ( ) Object

Gets a dannData object.

Returns:

Object :

A dannData object.

Example:


const nn = new Dann(24, 4);
nn.addHiddenLayer(12, 'sigmoid');
nn.makeWeights();
// Getting json
const modelData = nn.toJSON();
const newNN = new Dann();
// Setting
newNN.fromJSON(modelData);
newNN.log();