Node

new Node (typeopt) open an issue

Creates a new neuron/node

Neurons are the basic unit of the neural network. They can be connected together, or used to gate connections between other neurons. A Neuron can perform basically 4 operations: form connections, gate connections, activate and propagate.

For more information check:

Parameters:
Name Type Default Description
type string <optional> hidden

Can be: input, hidden, or output

Example
let { Node } = require("@liquid-carrot/carrot");

let node = new Node();
Properties:
Name Type Default Description
bias number

Neuron's bias here

squash activation

Activation function

type string
activation number

Output value

state number
old number
mask number 1

Used for dropout. This is either 0 (ignored) or 1 (included) during training and is used to avoid overfit.

previousDeltaBias number
totalDeltaBias number
connections.in Array.<Connection>

Incoming connections to this node

connections.out Array.<Connection>

Outgoing connections from this node

connections.gated Array.<Connection>

Connections this node gates

connections.self Connection

A self-connection

error.responsibility number
error.projected number
error.gated number
Source:

Methods

(static) activate (inputopt, optionsopt) → {number} open an issue

Actives the node.

When a neuron activates, it computes its state from all its input connections and 'squashes' it using its activation function, and returns the output (activation).

You can also provide the activation (a float between 0 and 1) as a parameter, which is useful for neurons in the input layer.

Parameters:
Name Type Description
input number <optional>

defaults to 0 when node.type === "input".

options Object <optional>
Returns:

A neuron's 'Squashed' output value

Example
let { Node } = require("@liquid-carrot/carrot");

let A = new Node();
let B = new Node();

A.connect(B);
A.activate(0.5); // 0.5
B.activate(); // 0.3244554645
Source:

(static) clear () open an issue

Clear the context of the node, basically reverting it to a 'new' neuron. Useful for predicting timeseries with LSTM's.

Source:

(static) connect (target, weight) → {Array.<Connection>} open an issue

Creates a connection from this node to the given node or group

Parameters:
Name Type Description
target Node | Group

Node or Group to project connections to

weight number

An initial weight for the target Node(s)

Examples

Connecting to other neurons and groups

let { Node } = require("@liquid-carrot/carrot");

let A = new Node();
let B = new Node();
A.connect(B); // A now projects a connection to B

// But you can also connect nodes to groups
let C = new Group(4);

B.connect(C); // B now projects a connection to all nodes in C

A neuron can also connect to itself, creating a selfconnection

let { Node } = require("@liquid-carrot/carrot");

let A = new Node();
A.connect(A); // A now connects to itself
Source:

(static) disconnect (node, twosidedopt) open an issue

Disconnects this node from the other node

Parameters:
Name Type Description
node Node
twosided boolean <optional>

If the nodes project a connection to each other (two way connection), set this to true to disconnect both connections at once

Examples

One sided connection

let { Node } = require("@liquid-carrot/carrot");

let A = new Node();
let B = new Node();
A.connect(B); // A now projects a connection to B

A.disconnect(B); // no connection between A and B anymore

Two-sided connection

let { Node } = require("@liquid-carrot/carrot");

let A = new Node();
let B = new Node();
A.connect(B); // A now projects a connection to B
B.connect(A); // B now projects a connection to A

// A.disconnect(B)  only disconnects A to B, so use
A.disconnect(B, true); // or B.disconnect(A, true)
Source:

(static) fromJSON (json) → {Node} open an issue

Convert a json object to a node

Parameters:
Name Type Description
json object

A node represented as a JSON object

Returns:

A reconstructed node

Example
let { Node } = require("@liquid-carrot/carrot");

let exported = myNode.toJSON();
let imported = myNode.fromJSON(exported); // imported will be a new instance of Node that is an exact clone of myNode.
Source:

(static) gate (connections) open an issue

Neurons can gate connections. This means that the output (activation value) of a neuron influences the value sent through a connection.

Parameters:
Name Type Description
connections Array.<Connection> | Connection

Connections to be gated (influenced) by a neuron

Example
let { Node } = require("@liquid-carrot/carrot");

let A = new Node();
let B = new Node();
let C = new Node();

connections = A.connect(B);

// Now gate the connection(s)
C.gate(connections);

// Now the weight of the connection from A to B will always be multiplied by the activation of node C.
Source:

(static) isProjectedBy (node) → {boolean} open an issue

Checks if the given node is projecting to this node

Parameters:
Name Type Description
node Node

Node to check for a connection from

Returns:

True if there is a connection from the given node to this node

Example
let { Node } = require("@liquid-carrot/carrot");

let A = new Node();
let B = new Node();
let C = new Node();
A.connect(B);
B.connect(C);

A.isProjectedBy(C);// false
B.isProjectedBy(A); // true
Source:

(static) isProjectingTo (node) → {boolean} open an issue

Checks if this node is projecting to the given node

Parameters:
Name Type Description
node Node

Node to check for a connection to

Returns:

True if there is a connection from this node to a given node

Example
let { Node } = require("@liquid-carrot/carrot");

let A = new Node();
let B = new Node();
let C = new Node();
A.connect(B);
B.connect(C);

A.isProjectingTo(B); // true
A.isProjectingTo(C); // false
Source:

(static) mutate (method) open an issue

Mutates the node with the given method

Parameters:
Name Type Description
method mutation

A Mutation Method, either MOD_ACTIVATION or MOD_BIAS

Example
let { Node } = require("@liquid-carrot/carrot");

let A = new Node(); // a Node with the default LOGISTIC squash function

let allowable_methods = [
  activation.TANH,
  activation.RELU,
]

A.mutate(methods.mutation.MOD_ACTIVATION, allowable_methods) // node's squash function is now TANH or RELU
Source:

(static) noTraceActivate (inputopt) → {number} open an issue

Activates the node without calculating elegibility traces and such.

Calculates the state from all the input connections, adds the bias, and 'squashes' it. Does not calculate traces, so this can't be used to backpropagate afterwards. That's also why it's quite a bit faster than regular activate.

Parameters:
Name Type Description
input number <optional>

Optional value to be used for an input (or forwarding) neuron

Returns:

A neuron's 'Squashed' output value

Example
let { Node } = require("@liquid-carrot/carrot");

let node = new Node();

node.noTraceActivate(); // 0.4923128591923
Source:

(static) propagate (target, options) open an issue

Backpropagate the error (a.k.a. learn).

After an activation, you can teach the node what should have been the correct output (a.k.a. train). This is done by backpropagating. Momentum adds a fraction of the previous weight update to the current one. When the gradient keeps pointing in the same direction, this will increase the size of the steps taken towards the minimum.

If you combine a high learning rate with a lot of momentum, you will rush past the minimum (of the error function) with huge steps. It is therefore often necessary to reduce the global learning rate ยต when using a lot of momentum (m close to 1).

Parameters:
Name Type Description
target number

The target value

options
Properties
Name Type Default Description
rate number <optional> 0.3

Learning rate

momentum number <optional> 0

Momentum adds a fraction of the previous weight update to the current one.

update boolean <optional> true

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.

Example
let { Node } = require("@liquid-carrot/carrot");

let A = new Node();
let B = new Node('output');
A.connect(B);

let learningRate = .3;
let momentum = 0;

for(let i = 0; i < 20000; i++)
{
  // when A activates 1
  A.activate(1);

  // train B to activate 0
  B.activate();
  B.propagate(learningRate, momentum, true, 0);
}

// test it
A.activate(1);
B.activate(); // 0.006540565760853365
Source:
See:

(static) toJSON () → {object} open an issue

Converts the node to a json object that can later be converted back

Example
let { Node } = require("@liquid-carrot/carrot");

let exported = myNode.toJSON();
let imported = myNode.fromJSON(exported); // imported will be a new instance of Node that is an exact clone of myNode.
Source:

(static) ungate (connections) open an issue

Removes the gates from this node from the given connection(s)

Parameters:
Name Type Description
connections Array.<Connection> | Connection

Connections to be ungated

Example
let { Node } = require("@liquid-carrot/carrot");

let A = new Node();
let B = new Node();
let C = new Node();
let connections = A.connect(B);

// Now gate the connection(s)
C.gate(connections);

// Now ungate those connections
C.ungate(connections);
Source: