Dannjs
2.2.10Deep Neural Network Library for JavaScript.
Deep Neural Network object. Can be trained with data or by neuro-evolution.
Dann
[input]
[output]
[input]
Number
optional
the number of input neurons.
[output]
Number
optional
the number of output neurons.
// 2 input, 1 output model
const nn = new Dann(2, 1);
nn.log();
addHiddenLayer
size
[act]
Add a Hidden Neuron Layer to a Dann neural network.
size
Number
Layer size, the number of neurons in the layer.
[act]
String
optional
Takes a string of the activation function's name. If left empty, the activation function will be set to 'sigmoid' by default. See available activation functions Here.
Name | Desmos |
---|---|
Sigmoid | See graph |
leakyReLU | See graph |
reLU | See graph |
siLU | See graph |
tanH | See graph |
binary | See graph |
softsign | See graph |
sinc | See graph |
softplus | See graph |
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.
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. |
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
Creates a Dann model from a json object.
data
Object
model data json object, you can get this object from a yourmodel.toJSON(); See docs here.
A Dann model.
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]
Feed data through the model to obtain an output or prediction.
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. |
Array of output predictions.
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
Applies a json object to a Dann model.
data
Object
model data json object, you can get this object from a yourmodel.toJSON(); See docs here.
A Dann model.
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
(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.
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
(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.
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.
[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. |
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.
[arg1]
Number
optional
The minimum range value.
[arg2]
Number
optional
The maximum range value.
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.
f
Function
the function to map the weights with.
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.
randomFactor
Number
Percentage to add to each weight. Generally in 0 and 1.
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.
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.
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.
act
String
Takes a string of the activation function's name. If this function is not called, the activation function will be set to 'sigmoid' by default. See available activation functions here.
Name | Desmos |
---|---|
Sigmoid | See graph |
leakyReLU | See graph |
reLU | See graph |
siLU | See graph |
tanH | See graph |
binary | See graph |
softsign | See graph |
sinc | See graph |
softplus | See graph |
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
(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.
name
String
The name of the json file.
save
name
[options]
(Nodejs) saves a json file containing information about the network and its current state in ./savedDanns/name/dannData.json.
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
name
String
Takes a string of the loss function's name. If this function is not called, the loss function will be set to 'mse' by default. See available loss functions Here.
Name | Desmos |
---|---|
mse | See graph |
mae | See graph |
lcl | See graph |
mbe | See graph |
mael | See graph |
rmse | See graph |
mce | See graph |
bce | See graph |
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]
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().
[name]
String
optional
the name of the function, set to 'myDannFunction' by default.
The function as a string.
const nn = new Dann(4, 4);
nn.addHiddenLayer(8);
nn.makeWeights();
let stringFunction = nn.toFunction();
// Copy & paste the string function!
console.log(stringFunction);