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
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 |
dropout |
number | Dropout rate likelihood for any given neuron to be ignored during network training. Must be between zero and one, numbers closer to one will result in more neurons ignored. |
nodes |
Array.<Node> | Nodes currently within the network |
gates |
Array.<Node> | Gates within the network |
connections |
Array.<Connection> | Connections within the network |
selfconns |
Array.<Connection> | Self-connections within the network |
- Source:
Methods
(static) activate (inputopt, training) → {Array.<number>} open an issue
Activates the network
It will activate all the nodes in activation order and produce an output.
Parameters:
Name | Type | Description |
---|---|---|
input |
Array.<number> <optional> | Input values to activate nodes with |
training |
boolean | Used to toggle dropout |
Returns:
Squashed output values
Example
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} open an issue
Returns a new identical network
Returns:
Returns an identical network
- Source:
(static) connect (from, to, weight) → {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 | An initial weight for the connections to be formed |
Returns:
An array of the formed connections
Example
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
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
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
Example
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
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
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) graph (width, height) → {Object} open an issue
Creates a json that can be used to create a graph with d3 and webcola
Parameters:
Name | Type | Description |
---|---|---|
width |
number | Width of the graph |
height |
number | Height of the graph |
- Source:
(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
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) → {boolean} open an issue
Mutates the network with the given method
Parameters:
Name | Type | Description |
---|---|---|
method |
mutation |
Returns:
Whether or not a mutation was achieved
Example
let { Network } = require("@liquid-carrot/carrot");
myNetwork.mutate(mutation.ADD_GATE) // a random node will gate a random connection within the network
- Source:
(static) noTraceActivate (input) → {Array.<number>} open an issue
Activates the network without calculating elegibility traces and such
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
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
const network = new architect.Perceptron(2,3,1)
network.possible(mutation.SUB_NODE) // returns an array of nodes that can be removed
- 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
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
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
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
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
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...
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
});
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
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: