Dannjs
2.3.13Deep Neural Network Library for JavaScript.
new Dann
(
[input]
[output]
)
Deep Neural Network component. Can be trained with data or by neuro-evolution.
[input]
Number
optional
the number of input neurons.
[output]
Number
optional
the number of output neurons.
Example
const Dannjs = require('dannjs'); const Dann = Dannjs.dann;
// 784 input, 2 output model const nn = new Dann(784, 2); 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 below.
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 |
See how to add more
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 through 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. |
dropout | Number | A value ranging from 0 to 1 determining the chance of a neuron being idle during a backward pass. |
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
)
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.
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]
)
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. |
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
)
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.
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();
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. |
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.
[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.
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.
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.
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.
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 |
See how to add more
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});
setLossFunction
(
name
[percentile]
)
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.
[percentile]
Number
optional
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);
const nn = new Dann(4, 4); nn.addHiddenLayer(16, 'sigmoid'); nn.makeWeights(); //Before changing the loss function console.log(nn.lossfunc); // Quantile loss with 40 percentile nn.setLossFunction('quantile', 0.4); //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.
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
( )
Gets 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();