Node

new Node (optionsopt) 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 Description
options

Options Object or template Node

Properties
Name Type Description
bias number <optional>

Neuron's bias here

Example
      Copy
      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
incoming Array.<Connection>

Incoming connections to this node

outgoing Array.<Connection>

Outgoing connections from this node

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>

Environment signal (i.e. optional numerical value passed to the network as input) - should only be passed in input neurons

options
Properties
Name Type Description
trace boolean <optional>

Controls whether traces are created when activation happens (a trace is meta information left behind for different uses, e.g. backpropagation).

Returns:

A neuron's 'Squashed' output value

Example
      Copy
      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:
To Do:
  • Support vector/tensor/array activation

(static) clear () open an issue

Clears this node's state information - i.e. resets node and its connections to "factory settings"

node.clear() is useful for predicting timeseries with LSTMs.

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

let node = new Node();

node.activate([1, 0]);
node.propagate([1]);

console.log(node); // Node has state information (e.g. `node.derivative`)

node.clear(); // Factory resets node

console.log(node); // Node has no state information
    
Source:

(static) connect (nodes, weightopt, optionsopt, twosidedopt) → {Array.<Connection>|Connection} open an issue

Connects this node to the given node(s)

Parameters:
Name Type Default Description
nodes Node | Array.<Node>

Node(s) to project connection(s) to

weight number <optional>

Initial connection(s) weight

options Object <optional> {}
twosided boolean <optional>

If true connect nodes to each other

Examples

Connecting node (neuron) to another node (neuron)

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

let node = new Node();
let other_node = new Node();

let connection = node.connect(other_node); // Both nodes now share a connection

console.log(connection); // Connection { from: [Object object], to: [Object object], ...}
    

Connecting node (neuron) to many nodes (neurons)

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

let node = new Node();
let other_nodes = [new Node(), new Node(), new Node()];

let connections = node.connect(other_nodes); // Node is connected to all other nodes

console.log(connections); // [{ from: [Object object], to: [Object object], ...}, ...]
    

Connecting a node (neuron) to itself

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

let node = new Node();

let connection = node.connect(node); // Node is connected to itself.

console.log(connection); // Connection { from: [Object object], to: [Object object], ...}
    
Source:

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

Disconnects this node from the given node(s)

Parameters:
Name Type Description
node Node | Array.<Node>

Node(s) to remove connection(s) to

options
Properties
Name Type Default Description
twosided boolean <optional> false

If true disconnects nodes from each other (i.e. both sides)

Examples

Disconnect from one node

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

let node = new Node();
let other = new Node();

node.connect(other); // `node` now connected to `other`

console.log(node.incoming.length); // 0
console.log(node.outgoing.length); // 1

node.disconnect(other); // `node` is now disconnected from `other`

console.log(node.incoming.length); // 0
console.log(node.outgoing.length); // 0
    

Connect to one node - two-sided

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

let node = new Node();
let other = new Node();

// `node` & `other` are now connected to each other
node.connect(other, {
  twosided: true
});

console.log(node.incoming.length); // 1
console.log(node.outgoing.length); // 1

// `node` & `other` are now disconnected from each other
node.disconnect(other, {
  twosided: true
});

console.log(node.incoming.length); // 0
console.log(node.outgoing.length); // 0
    
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

Examples

From Object

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

let json = { bias: 0.35 };
let node = Node.fromJSON(json);

console.log(node);
    

From Node.toJSON()

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

let other_node = new Node();
let json = other_node.toJSON();
let node = Node.fromJSON(json);

console.log(node);
    

From JSON string

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

let other = new Node();
let json = other_node.toJSON();
let string = JSON.stringify(json);
let node = Node.fromJSON(string);

console.log(node);
    
Source:

(static) gate (connections) open an issue

This node gates (influences) the given connection(s)

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

Connections to be gated (influenced) by a neuron

Example

Gate one connection

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

let input = new Node();
let output = new Node();
let connection = input.connect(output);

let node = new Node();

console.log(connection.gater === node); // false

node.gate(connection); // Node now gates (manipulates) `connection`

console.log(connection.gater === node); // true
    
Source:

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

Checks if the given node(s) are have outgoing connections to this node

Parameters:
Name Type Description
nodes Node | Array.<Node>

Checks if node(s) have outgoing connections into this node

Returns:

Returns true, iff every node(s) has an outgoing connection into this node

Examples

Check one node

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

let other_node = new Node();
let node = new Node();
other_node.connect(node);

console.log(node.isProjectedBy(other_node)); // true
    

Check many nodes

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

let other_nodes = Array.from({ length: 5 }, () => new Node());
let node = new Node();

other_nodes.forEach(other_node => other_node.connect(node));

console.log(node.isProjectedBy(other_nodes)); // true
    
Source:

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

Checks if this node has an outgoing connection(s) into the given node(s)

Parameters:
Name Type Description
nodes Node | Array.<Node>

Checks if this node has outgoing connection(s) into node(s)

Returns:

Returns true, iff this node has an outgoing connection into every node(s)

Examples

Check one node

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

let other_node = new Node();
let node = new Node();
node.connect(other_node);

console.log(node.isProjectingTo(other_node)); // true
    

Check many nodes

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

let other_nodes = Array.from({ length: 5 }, () => new Node());
let node = new Node();

other_nodes.forEach(other_node => node.connect(other_node));

console.log(node.isProjectingTo(other_nodes)); // true
    
Source:

(static) mutate (optionsopt) open an issue

Mutates the node - i.e. changes node's squash function or bias

Parameters:
Name Type Description
options
Properties
Name Type Description
method string <optional>

A mutation method - a random method will chosen if none is passed

allowed Array.<activation> <optional>

Allowed/possible squash (activation) functions for node (neuron)

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

let node = new Node();

console.log(node);

node.mutate(); // Changes node's squash function or bias
    
Source:

(static) noTraceActivate (inputopt) → {number} deprecated

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 - should only be passed in input neurons

Returns:

A neuron's 'Squashed' output value

Example
      Copy
      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 (i.e. "the value the network SHOULD have given")

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
      Copy
      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
      Copy
      const { Node } = require("@liquid-carrot/carrot");

let node = new Node();

console.log(node.toJSON());
    
Source:

(static) ungate (connections) → {Connection|Array.<Connection>} open an issue

Stops this node from gating (manipulating) the given connection(s)

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

Connections to ungate - i.e. remove this node from

Returns:

Returns connection(s) that were ungated

Example

Ungate one connection

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

let input = new Node();
let output = new Node();
let connection = input.connect(output);

let node = new Node();

console.log(connection.gater === node); // false

node.gate(connection); // Node now gates (manipulates) `connection`

console.log(connection.gater === node); // true

node.ungate(connection); // Node is removed from `connection`

console.log(connection.gater === node); // false
    
Source: