new Network (input_size, output_size) open an issue
Create a neural network
Networks are easy to create, all you need to specify is an input
and an output
size.
Parameters:
Name | Type | Description |
---|---|---|
input_size |
number | Size of input layer AKA neurons in input layer |
output_size |
number | Size of output layer AKA neurons in output layer |
Example
Copy
let { Network, architect } = require("@liquid-carrot/carrot");
// Network with 2 input neurons and 1 output neuron
let myNetwork = new Network(2, 1);
// and a multi-layered network
let myNetwork = new architect.Perceptron(5, 20, 10, 5, 1)
Properties:
Name | Type | Description |
---|---|---|
input_size |
number | Size of input layer AKA neurons in input layer |
output_size |
number | Size of output layer AKA neurons in output layer |
nodes |
Array.<Node> | Nodes currently within the network |
gates |
Array.<Node> | Gates within the network |
connections |
Array.<Connection> | Connections within the network |
- Source:
Namespaces
Methods
(static) activate (inputopt) → {Array.<number>} open an issue
Activates the network
It will activate all the nodes in activation order and produce an output.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
input |
Array.<number> <optional> | Input values to activate nodes with |
|
options.dropout_rate |
Number <optional> |
0
|
The dropout rate. dropout |
options.trace |
bool <optional> |
true
|
Controls whether traces are created when activation happens (a trace is meta information left behind for different uses, e.g. backpropagation). |
Returns:
Squashed output values
Example
Copy
let { Network } = require("@liquid-carrot/carrot");
// Create a network
let myNetwork = new Network(3, 2);
myNetwork.activate([0.8, 1, 0.21]); // gives: [0.49, 0.51]
- Source:
(static) clear () open an issue
Clear the context of the network
- Source:
(static) clone () → {Network} beta
Returns a deep copy of Network.
Returns:
Returns an identical network
- Source:
(static) connect (from, to, weightopt) → {Array.<Connection>} open an issue
Connects a Node to another Node or Group in the network
Parameters:
Name | Type | Description |
---|---|---|
from |
Node | The source Node |
to |
Node | Group | The destination Node or Group |
weight |
number <optional> | An initial weight for the connections to be formed |
Returns:
An array of the formed connections
Example
Copy
let { Network } = require("@liquid-carrot/carrot");
myNetwork.connect(myNetwork.nodes[4], myNetwork.nodes[5]); // connects network node 4 to network node 5
- Source:
(static) crossOver (network1, network2, equalopt) → {Network} open an issue
Create an offspring from two parent networks.
Networks are not required to have the same size, however input and output size should be the same!
Parameters:
Name | Type | Description |
---|---|---|
network1 |
Network | First parent network |
network2 |
Network | Second parent network |
equal |
boolean <optional> | Flag to indicate equally fit Networks |
Returns:
New network created from mixing parent networks
Example
Copy
let { Network, architect } = require("@liquid-carrot/carrot");
// Initialise two parent networks
let network1 = new architect.Perceptron(2, 4, 3);
let network2 = new architect.Perceptron(2, 4, 5, 3);
// Produce an offspring
let network3 = Network.crossOver(network1, network2);
- Source:
- To Do:
-
- Add custom [crossover](crossover) method customization
(static) disconnect (from, to) open an issue
Removes the connection of the from
node to the to
node
Parameters:
Example
Copy
myNetwork.disconnect(myNetwork.nodes[4], myNetwork.nodes[5]);
// now node 4 does not have an effect on the output of node 5 anymore
- Source:
(static) evolve (dataset, optionsopt) → {Object} open an issue
Evolves the network to reach a lower error on a dataset using the NEAT algorithm
If both iterations
and error
options are unset, evolve will default to iterations
as an end condition.
Parameters:
Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dataset |
Array.<{Array.<input:number>, Array.<output:number>}> | A set of input values and ideal output values to train the network with |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
options |
Configuration options Properties
|
Returns:
A summary object of the network's performance.
Properties include: error
- error of the best genome, iterations
- generations used to evolve networks, time
- clock time elapsed while evolving
Example
Copy
let { Network, methods } = require("@liquid-carrot/carrot");
async function execute () {
var network = new Network(2,1);
// XOR dataset
var trainingSet = [
{ input: [0,0], output: [0] },
{ input: [0,1], output: [1] },
{ input: [1,0], output: [1] },
{ input: [1,1], output: [0] }
];
await network.evolve(trainingSet, {
mutation: methods.mutation.FFW,
equal: true,
error: 0.05,
elitism: 5,
mutation_rate: 0.5
});
// another option
// await network.evolve(trainingSet, {
// mutation: methods.mutation.FFW,
// equal: true,
// error: 0.05,
// elitism: 5,
// mutation_rate: 0.5,
// cost: (targets, outputs) => {
// const error = outputs.reduce(function(total, value, index) {
// return total += Math.pow(targets[index] - outputs[index], 2);
// }, 0);
//
// return error / outputs.length;
// }
// });
network.activate([0,0]); // 0.2413
network.activate([0,1]); // 1.0000
network.activate([1,0]); // 0.7663
network.activate([1,1]); // -0.008
}
execute();
- Source:
(static) fromJSON (json) → {Network} open an issue
Convert a json object to a network
Parameters:
Name | Type | Description |
---|---|---|
json |
Object | A network represented as a json object |
Returns:
Network A reconstructed network
Example
Copy
let { Network } = require("@liquid-carrot/carrot");
let exported = myNetwork.toJSON();
let imported = Network.fromJSON(exported) // imported will be a new instance of Network that is an exact clone of myNetwork
- Source:
(static) gate (node, connection) open an issue
Makes a network node gate a connection
Parameters:
Name | Type | Description |
---|---|---|
node |
Node | Gating node |
connection |
Connection | Connection to gate with node |
Example
Copy
let { Network } = require("@liquid-carrot/carrot");
myNetwork.gate(myNetwork.nodes[1], myNetwork.connections[5])
// now: connection 5's weight is multiplied with node 1's activaton
- Source:
- To Do:
-
- Add ability to gate several network connections at once
(static) merge (network1, network2) → {Network} open an issue
Merge two networks into one.
The merge functions takes two networks, the output size of network1
should be the same size as the input of network2
. Merging will always be one to one to conserve the purpose of the networks.
Parameters:
Returns:
Network Merged Network
Example
Copy
let { Network, architect } = require("@liquid-carrot/carrot");
let XOR = architect.Perceptron(2,4,1); // assume this is a trained XOR
let NOT = architect.Perceptron(1,2,1); // assume this is a trained NOT
// combining these will create an XNOR
let XNOR = Network.merge(XOR, NOT);
- Source:
(static) mutate (method, options) → {network} open an issue
Mutates the network with the given method.
Parameters:
Name | Type | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
method |
mutation | |||||||||||||||||
options |
Properties
|
Returns:
A mutated version of this network
Example
Copy
let { Network, architect } = require("@liquid-carrot/carrot");
let myNetwork = new architect.Perceptron(2,2)
myNetwork = myNetwork.mutate(mutation.ADD_GATE) // returns a mutated network with an added gate
- Source:
(static) mutateRandom (allowedMethodsopt, options) → {network} alpha
Selects a random mutation method and returns a mutated copy of the network. Warning! Mutates network directly.
Parameters:
Name | Type | Default | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
allowedMethods |
Array.<mutation> <optional> |
methods.mutation.ALL
|
An array of Mutation methods to automatically pick from |
|||||||||||||||
options |
Properties
|
Returns:
A mutated version of this network
- Source:
(static) noTraceActivate (input) → {Array.<number>} deprecated
Deprecated, here for backwards compatibility only! Simply calls .activate()
with option trace: false
Activates network without creating traces
Activates the network without calculating elegibility traces for the nodes within it.
Since this doesn't calculate traces it won't factor in backpropagation afterwards. That's also why it's quite a bit faster than regular activate
.
Parameters:
Name | Type | Description |
---|---|---|
input |
Array.<number> | An array of input values equal in size to the input layer |
Returns:
output An array of output values equal in size to the output layer
Example
Copy
let { Network } = require("@liquid-carrot/carrot");
// Create a network
let myNetwork = new Network(3, 2);
myNetwork.noTraceActivate([0.8, 1, 0.21]); // gives: [0.49, 0.51]
- Source:
(static) possible (method) → {false|Array.<object>} open an issue
Checks whether a given mutation is possible, returns an array of candidates to use for a mutation when it is.
Parameters:
Name | Type | Description |
---|---|---|
method |
mutation |
Returns:
Candidates to use for a mutation. Entries may be arrays containing pairs / tuples when appropriate.
Example
Copy
const network = new architect.Perceptron(2,3,1)
network.possible(mutation.SUB_NODE) // returns an array of nodes that can be removed
- Source:
(static) propagate (rate, momentum, update, target) open an issue
Backpropagate the network
This function allows you to teach the network. If you want to do more complex training, use the network.train()
function.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
rate |
number |
0.3
|
Sets the learning rate of the backpropagation process |
momentum |
number |
0
|
Momentum. Adds a fraction of the previous weight update to the current one. |
update |
boolean |
false
|
When set to false weights won't update, but when set to true after being false the last propagation will include the deltaweights of the first "update:false" propagations too. |
target |
Array.<number> | Ideal values of the previous activate. Will use the difference to improve the weights |
Example
Copy
let { Network } = require("@liquid-carrot/carrot");
let myNetwork = new Network(1,1);
// This trains the network to function as a NOT gate
for(var node_index = 0; i < 1000; i++){
network.activate([0]);
network.propagate(0.2, 0, true, [1]);
network.activate([1]);
network.propagate(0.3, 0, true, [0]);
}
- Source:
(static) remove (node) open an issue
Removes a node from a network, all its connections will be redirected. If it gates a connection, the gate will be removed.
Parameters:
Name | Type | Description |
---|---|---|
node |
Node | Node to remove from the network |
Example
Copy
let { Network, architect } = require("@liquid-carrot/carrot");
let myNetwork = new architect.Perceptron(1,4,1);
// Remove a node
myNetwork.remove(myNetwork.nodes[2]);
- Source:
(static) set () open an issue
Sets the value of a property for every node in this network
Parameters:
Name | Type | Description |
---|---|---|
values.bias |
number | Bias to set for all network nodes |
values.squash |
activation | Activation function to set for all network nodes |
Example
Copy
let { Network, architect } = require("@liquid-carrot/carrot");
var network = new architect.Random(4, 4, 1);
// All nodes in 'network' now have a bias of 1
network.set({bias: 1});
- Source:
(static) standalone () → {string} open an issue
Creates a standalone function of the network which can be run without the need of a library
Returns:
Function as a string that can be eval'ed
Example
Copy
let { Network, architect } = require("@liquid-carrot/carrot");
var myNetwork = new architect.Perceptron(2,4,1);
myNetwork.activate([0,1]); // [0.24775789809]
// a string
var standalone = myNetwork.standalone();
// turns your network into an 'activate' function
eval(standalone);
// calls the standalone function
activate([0,1]);// [0.24775789809]
- Source:
(static) test (set, costopt) → {Object} open an issue
Tests a set and returns the error and elapsed time
Parameters:
Name | Type | Default | Description |
---|---|---|---|
set |
Array.<{Array.<input:number>, Array.<output:number>}> | A set of input values and ideal output values to test the network against |
|
cost |
cost <optional> |
methods.cost.MSE
|
The cost function used to determine network error |
Returns:
A summary object of the network's performance
- Source:
(static) toJSON () → {Object} open an issue
Convert the network to a json object
Returns:
The network represented as a json object
Example
Copy
let { Network } = require("@liquid-carrot/carrot");
let exported = myNetwork.toJSON();
let imported = Network.fromJSON(exported) // imported will be a new instance of Network that is an exact clone of myNetwork
- Source:
(static) train (data, options) → {Object} open an issue
Train the given data to this network
Parameters:
Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
data |
Array.<{Array.<input:number>, Array.<output:number>}> | A data of input values and ideal output values to train the network with |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
options |
Options used to train network Properties
|
Returns:
A summary object of the network's performance
Examples
Copy
let { Network, architect } = require("@liquid-carrot/carrot");
let network = new architect.Perceptron(2,4,1);
// Train the XOR gate
network.train([{ input: [0,0], output: [0] },
{ input: [0,1], output: [1] },
{ input: [1,0], output: [1] },
{ input: [1,1], output: [0] }]);
network.activate([0,1]); // 0.9824...
Copy
let { Network, architect } = require("@liquid-carrot/carrot");
let network = new architect.Perceptron(2,4,1);
let trainingSet = [
{ input: [0,0], output: [0] },
{ input: [0,1], output: [1] },
{ input: [1,0], output: [1] },
{ input: [1,1], output: [0] }
];
// Train the XNOR gate
network.train(trainingSet, {
log: 1,
iterations: 1000,
error: 0.0001,
rate: 0.2
});
Copy
let { Network, architect } = require("@liquid-carrot/carrot");
let network = new architect.Perceptron(2,4,1);
let trainingSet = [ // PS: don't use cross validation for small sets, this is just an example
{ input: [0,0], output: [1] },
{ input: [0,1], output: [0] },
{ input: [1,0], output: [0] },
{ input: [1,1], output: [1] }
];
// Train the XNOR gate
network.train(trainingSet, {
crossValidate:
{
testSize: 0.4,
test_error: 0.02
}
});
- Source:
(static) ungate (connection) open an issue
Remove the gate of a connection.
Parameters:
Name | Type | Description |
---|---|---|
connection |
Connection | Connection to remove gate from |
Example
Copy
let { Network, architect } = require("@liquid-carrot/carrot");
let myNetwork = new architect.Perceptron(1, 4, 2);
// Gate a connection
myNetwork.gate(myNetwork.nodes[2], myNetwork.connections[5]);
// Remove the gate from the connection
myNetwork.ungate(myNetwork.connections[5]);
- Source: