# Network

Edit on Github

Networks are very easy to create. All you have to do is specify an `input` size and an `output` size.

    // Network with 2 input neurons and 1 output neuron
    var myNetwork = new Network(2, 1);

    // If you want to create multi-layered networks
    var myNetwork = new architect.Perceptron(5, 20, 10, 5, 1);

If you want to create more advanced networks, check out the 'Networks' tab on the left.

### Functions

Check out the train and evolve functions on their separate pages!

activate
Activates the network. It will activate all the nodes in activation order and produce an output.

    // Create a network
    var myNetwork = new Network(3, 2);

    myNetwork.activate([0.8, 1, 0.21]); // gives: [0.49, 0.51]

noTraceActivate
Activates the network. It will activate all the nodes in activation order and produce an output. Does not calculate traces, so backpropagation is not possible afterwards. That makes it faster than the regular `activate` function.

    // Create a network
    var myNetwork = new Network(3, 2);

    myNetwork.noTraceActivate([0.8, 1, 0.21]); // gives: [0.49, 0.51]

propagate
This function allows you to teach the network. If you want to do more complex training, use the `network.train()` function. The arguments for this function are:

    myNetwork.propagate(rate, momentum, update, target);

Where target is optional. The default value of momentum is `0`. Read more about momentum on the regularization page. If you run propagation without setting update to true, then the weights won't update. So if you run propagate 3x with `update: false`, and then 1x with `update: true` then the weights will be updated after the last propagation, but the deltaweights of the first 3 propagation will be included too.

    var myNetwork = new Network(1,1);

    // This trains the network to function as a NOT gate
    for(var i = 0; i < 1000; i++){
      network.activate([0]);
      network.propagate(0.2, 0, true, [1]);

      network.activate([1]);
      network.propagate(0.3, 0, true, [0]);
    }

The above example teaches the network to output `[1]` when input `[0]` is given and the other way around. Main usage:

    network.activate(input);
    network.propagate(learning_rate, momentum, update_weights, desired_output);

merge
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. Usage:

    var XOR = architect.Perceptron(2,4,1); // assume this is a trained XOR
    var NOT = architect.Perceptron(1,2,1); // assume this is a trained NOT

    // combining these will create an XNOR
    var XNOR = Network.merge(XOR, NOT);

connect
Connects two nodes in the network:

    myNetwork.connect(myNetwork.nodes[4], myNetwork.nodes[5]);

remove
Removes a node from a network, all its connections will be redirected. If it gates a connection, the gate will be removed.

    myNetwork = new architect.Perceptron(1,4,1);

    // Remove a node
    myNetwork.remove(myNetwork.nodes[2]);

disconnect
Disconnects two nodes in the network:

    myNetwork.disconnect(myNetwork.nodes[4], myNetwork.nodes[5]);
    // now node 4 does not have an effect on the output of node 5 anymore

gate
Makes a network node gate a connection:

    myNetwork.gate(myNetwork.nodes[1], myNetwork.connections[5]

Now the weight of connection 5 is multiplied with the activation of node 1!

ungate
Removes a gate from a connection:

    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]);

mutate
Mutates the network. See mutation methods.

serialize
Serializes the network to 3 `Float64Arrays`. Used for transferring networks to other threads fast.

toJSON/fromJSON
Networks can be stored as JSON's and then restored back:

    var exported = myNetwork.toJSON();
    var imported = Network.fromJSON(exported);

`imported` will be a new instance of `Network` that is an exact clone of `myNetwork`.

standalone
Networks can be used in Javascript without the need of the Neataptic library, this function will transform your network into a function accompanied by arrays.

    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]

The reason an `eval` is being called is because the standalone can't be a simply a function, it needs some kind of global memory. You can easily copy and paste the result of `standalone` in any JS file and run the `activate` function!

Note that this is still in development, so for complex networks, it might not be precise.

crossOver
Creates a new 'baby' network from two parent networks. Networks are not required to have the same size, however input and output size should be the same!

    // Initialise two parent networks
    var network1 = new architect.Perceptron(2, 4, 3);
    var network2 = new architect.Perceptron(2, 4, 5, 3);

    // Produce an offspring
    var network3 = Network.crossOver(network1, network2);

set
Sets the properties of all nodes in the network to the given values, e.g.:

    var network = new architect.Random(4, 4, 1);

    // All nodes in 'network' now have a bias of 1
    network.set({bias: 1});

clear
Clears the context of the network. Useful for predicting timeseries with LSTM's. `clear()` has little to no effecton regular NN, use on RNN's!

### Properties

Each network only has a small number of properties.

input
Input size of the network

output
Output size of the network

nodes
Array of nodes

connections
Array of connections

gates
Array of gated connections

selfconns
Array of self connections