### Configure Evolution Options Source: https://wagenaartje.github.io/neataptic/docs/important/evolve Example configuration object for customizing the evolution process. ```javascript var options = { mutation: methods.mutation.ALL, mutationRate: 0.4, clear: true, cost: methods.cost.MSE, error: 0.03, log: 1, iterations: 1000 }; ``` -------------------------------- ### Full XOR Evolution Example Source: https://wagenaartje.github.io/neataptic/docs/important/evolve Complete implementation of network creation, evolution, and activation for the XOR problem. ```javascript 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, elitism: 5, mutationRate: 0.5 }); 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(); ``` -------------------------------- ### Create XOR training set Source: https://wagenaartje.github.io/neataptic/docs/tutorials/evolution An example of a complete training set array for the XOR logic problem. ```javascript var myTrainingSet = [ { input: [0,0], output: [0] }, { input: [0,1], output: [1] }, { input: [1,0], output: [1] }, { input: [1,1], output: [0] } ]; ``` -------------------------------- ### Example Raw Data Source: https://wagenaartje.github.io/neataptic/docs/tutorials/normalization Sample raw data points including stock, sold quantity, price, category, and ID. ```json { stock: 933, sold: 352, price: 0.95, category: 'drinks', id: 40 } { stock: 154, sold: 103, price: 5.20, category: 'foods', id: 67 } { stock: 23, sold: 5, price: 121.30, category: 'electronics', id: 150 } ``` -------------------------------- ### Basic LSTM Network Setup Source: https://wagenaartje.github.io/neataptic/docs/builtins/lstm Instantiate a basic LSTM network with one input node, one memory block assembly, and one output node. Ensure at least these components are defined. ```javascript var myLSTM = new architect.LSTM(2,6,1); ``` -------------------------------- ### Example: Network Propagation and Learning Source: https://wagenaartje.github.io/neataptic/docs/architecture/group Demonstrates connecting two groups, activating the input group, activating the output group, and then teaching the network with a learning rate, momentum, and a target output. ```javascript var A = new Group(2); var B = new Group(3); A.connect(B); A.activate([1,0]); // set the input B.activate(); // get the output // Then teach the network with learning rate and wanted output B.propagate(0.3, 0.9, [0,1]); ``` -------------------------------- ### Training with Cross-Validation (XNOR Gate) Source: https://wagenaartje.github.io/neataptic/docs/important/train An example demonstrating how to train an XNOR gate while utilizing cross-validation. This setup uses 40% of the data for testing and sets a target error of 0.02 for the validation set. Cross-validation is not recommended for small datasets. ```javascript var network = new architect.Perceptron(2,4,1); var trainingSet = [ { 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, testError: 0.02 } }); ``` -------------------------------- ### Initialize and Manage NEAT Evolution Source: https://wagenaartje.github.io/neataptic/docs/neat Functions to construct the genetic algorithm, start the evaluation of a generation, and process the end of a generation with elitism and mutation. ```javascript /** Construct the genetic algorithm */ function initNeat(){ neat = new Neat( 1 + PLAYER_DETECTION * 3 + FOOD_DETECTION * 2, 2, null, { mutation: methods.mutation.ALL popsize: PLAYER_AMOUNT, mutationRate: MUTATION_RATE, elitism: Math.round(ELITISM_PERCENT * PLAYER_AMOUNT), network: new architect.Random( 1 + PLAYER_DETECTION * 3 + FOOD_DETECTION * 2, START_HIDDEN_SIZE, 2 ) } ); if(USE_TRAINED_POP) neat.population = population; } /** Start the evaluation of the current generation */ function startEvaluation(){ players = []; highestScore = 0; for(var genome in neat.population){ genome = neat.population[genome]; new Player(genome); } } /** End the evaluation of the current generation */ function endEvaluation(){ console.log('Generation:', neat.generation, '- average score:', neat.getAverage()); neat.sort(); var newPopulation = []; // Elitism for(var i = 0; i < neat.elitism; i++){ newPopulation.push(neat.population[i]); } // Breed the next individuals for(var i = 0; i < neat.popsize - neat.elitism; i++){ newPopulation.push(neat.getOffspring()); } // Replace the old population with the new population neat.population = newPopulation; neat.mutate(); neat.generation++; startEvaluation(); } ``` -------------------------------- ### Construct Custom Network with Layers Source: https://wagenaartje.github.io/neataptic/docs/architecture/layer Example of building a custom neural network by connecting Dense, LSTM, and GRU layers. Ensure layers are connected sequentially before constructing the network. ```javascript var input = new Layer.Dense(1); var hidden1 = new Layer.LSTM(5); var hidden2 = new Layer.GRU(1); var output = new Layer.Dense(1); // connect however you want input.connect(hidden1); hidden1.connect(hidden2); hidden2.connect(output); var network = architect.Construct([input, hidden1, hidden2, output]); ``` -------------------------------- ### Evolve network Source: https://wagenaartje.github.io/neataptic/docs/tutorials/evolution The basic syntax for the async evolve function and a concrete example using specific mutation and population parameters. ```javascript yourNetwork.evolve(yourData, yourOptions); ``` ```javascript await myNetwork.evolve(myTrainingSet, { mutation: methods.mutation.FFW, equal: true, popsize: 100, elitism: 10, log: 10, error: 0.03, iterations: 1000, mutationRate: 0.5 }); // results: {error: 0.0009000000000000001, generations: 255, time: 1078} // please note that there is a hard local optima that has to be beaten ``` -------------------------------- ### Train NARX Network Source: https://wagenaartje.github.io/neataptic/docs/builtins/narx Example of initializing and training a NARX network on sequential data. ```javascript var narx = new architect.NARX(1, 5, 1, 3, 3); // Train the XOR gate (in sequence!) var trainingData = [ { input: [0], output: [0] }, { input: [0], output: [0] }, { input: [0], output: [1] }, { input: [1], output: [0] }, { input: [0], output: [0] }, { input: [0], output: [0] }, { input: [0], output: [1] }, ]; narx.train(trainingData, { log: 1, iterations: 3000, error: 0.03, rate: 0.05 }); ``` -------------------------------- ### Define XOR Training Set Source: https://wagenaartje.github.io/neataptic/docs/important/evolve Example structure for a training set used in the evolve function. ```javascript // XOR training set var trainingSet = [ { input: [0,0], output: [0] }, { input: [0,1], output: [1] }, { input: [1,0], output: [1] }, { input: [1,1], output: [0] } ]; ``` -------------------------------- ### Numerical Data Normalization Example Source: https://wagenaartje.github.io/neataptic/docs/tutorials/normalization Demonstrates normalizing numerical 'stock' values by dividing them by a chosen maximum value (2000). This method ensures values are between 0 and 1 while maintaining relativity. ```javascript // Normalize the data with a maximum value (=2000) stock: 933 -> 933/2000 -> 0.4665 stock: 154 -> 154/2000 -> 0.077 stock: 23 -> 23/2000 -> 0.0115 ``` -------------------------------- ### Training with Custom Options (XNOR Gate) Source: https://wagenaartje.github.io/neataptic/docs/important/train An example of training an XNOR gate with specific training parameters including logging every iteration, a maximum of 1000 iterations, a target error of 0.0001, and a learning rate of 0.2. ```javascript var network = new architect.Perceptron(2,4,1); var trainingSet = [ { 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, { log: 1, iterations: 1000, error: 0.0001, rate: 0.2 }); ``` -------------------------------- ### Import Neataptic Modules Source: https://wagenaartje.github.io/neataptic/docs/tutorials/visualization Import necessary modules from Neataptic for easier development. This should be at the start of your JavaScript file. ```javascript var Node = neataptic.Node; var Neat = neataptic.Neat; var Network = neataptic.Network; var Methods = neataptic.Methods; var Architect = neataptic.Architect; ``` -------------------------------- ### Training the XOR Gate Source: https://wagenaartje.github.io/neataptic/docs/important/train An example demonstrating how to train a Perceptron network to solve the XOR problem. It defines the network architecture and provides the training data in the required format. ```javascript var 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... ``` -------------------------------- ### List Available Layers Source: https://wagenaartje.github.io/neataptic/docs/architecture/layer Lists the available layer types in Neataptic. No setup is required to view this list. ```javascript Layer.Dense Layer.LSTM Layer.GRU Layer.Memory ``` -------------------------------- ### Train a Node to a Target Value Source: https://wagenaartje.github.io/neataptic/docs/architecture/node Example demonstrating how to train a node (B) to output a specific value (0) when another node (A) activates with a certain input (1). This involves repeated activation and propagation. ```javascript var A = new Node(); var B = new Node('output'); A.connect(B); var learningRate = .3; var momentum = 0; for(var 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 ``` -------------------------------- ### Activate Network for Prediction Source: https://wagenaartje.github.io/neataptic/docs/tutorials/training Use the activate method to get the network's output for a given input. The output is an array of predicted values. ```javascript myNetwork.activate([0,0]); // [0.1257225731473885] ``` ```javascript myNetwork.activate([0,1]); // [0.9371910625522613] ``` ```javascript myNetwork.activate([1,0]); // [0.7770757408042104] ``` ```javascript myNetwork.activate([1,1]); // [0.1639697315652196] ``` -------------------------------- ### Node Creation and Properties Source: https://wagenaartje.github.io/neataptic/docs/architecture/node Demonstrates how to create a Node instance and lists its key properties. ```APIDOC ## Node Creation and Properties ### Description Nodes are fundamental components of neural networks, introducing non-linearity. This section shows how to instantiate a Node and outlines its core properties. ### Node Properties - **bias** (float) - The bias value used in state calculation. - **squash** (function) - The activation function applied to the state. - **type** (string) - Type of the node ('input', 'hidden', 'output'). Should not be set manually. Setting to 'constant' disables bias/weight changes. - **activation** (float) - The computed activation value of the node. - **connections** (object) - A dictionary storing incoming and outgoing connections. - **old** (float) - Stores the previous activation value. - **state** (float) - Stores the state of the node before activation (squashing). ### Request Example ```javascript var node = new Node(); ``` ``` -------------------------------- ### Initialize network architecture Source: https://wagenaartje.github.io/neataptic/docs/tutorials/evolution Constructing a new network with specified input and output sizes. ```javascript var myNetwork = new Network(inputSize, outputSize); ``` ```javascript var myNetwork = new Network(2, 1); // 2 inputs, 1 output ``` -------------------------------- ### Group Creation and Properties Source: https://wagenaartje.github.io/neataptic/docs/architecture/group Demonstrates how to create a Group instance and explains its core properties. ```APIDOC ## Group Creation and Properties A group instance denotes a group of nodes. Beware: once a group has been used to construct a network, the groups will fall apart into individual nodes. They are purely for the creation and development of networks. ### Creating a Group ```javascript // A group with 5 nodes var A = new Group(5); ``` ### Group Properties - **nodes**: an array of all nodes in the group - **connections**: dictionary with connections ``` -------------------------------- ### Construct a NEAT Instance Source: https://wagenaartje.github.io/neataptic/docs/neat The basic syntax for initializing a new NEAT object with required parameters and an options object. ```javascript new Neat(input, output, fitnessFunction, options); // options should be an object ``` -------------------------------- ### Initialize Neural Networks Source: https://wagenaartje.github.io/neataptic/docs/architecture/network Create basic networks by defining input and output sizes, or use architectural presets for multi-layered structures. ```javascript // 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); ``` -------------------------------- ### Constructor: new Neat() Source: https://wagenaartje.github.io/neataptic/docs/neat Initializes a new genetic algorithm instance with specified input, output, fitness function, and configuration options. ```APIDOC ## new Neat(input, output, fitnessFunction, options) ### Description Constructs a new NEAT instance to manage the evolutionary process of neural networks. ### Parameters #### Path Parameters - **input** (number) - Required - The amount of input neurons each genome has. - **output** (number) - Required - The amount of output neurons each genome has. - **fitnessFunction** (function) - Required - A function that evaluates a genome and returns a numeric score. - **options** (object) - Optional - Configuration object for the genetic algorithm. #### Options Object - **popsize** (number) - Optional - Population size of each generation. Default: 50. - **elitism** (number) - Optional - Number of elite genomes preserved per generation. Default: 0. - **mutation** (array) - Optional - Array of allowed mutation methods. - **mutationRate** (number) - Optional - Probability of mutation occurring. Default: 0.3. - **network** (object) - Optional - Initial network architecture to start the algorithm. ``` -------------------------------- ### Using Default Training Options Source: https://wagenaartje.github.io/neataptic/docs/important/train Demonstrates how to call the train method when default options are desired. This can be achieved by passing an empty object or omitting the options argument entirely. ```javascript myNetwork.evolve(trainingSet, {}); ``` ```javascript myNetwork.evolve(trainingSet); ``` -------------------------------- ### Construct a Network with Groups Source: https://wagenaartje.github.io/neataptic/docs/architecture/construct This method allows for faster network creation by initializing and connecting groups of nodes. Ensure input groups are specified in activation order. ```javascript // Initialise groups of nodes var A = new Group(4); var B = new Group(2); var C = new Group(6); // Create connections between the groups A.connect(B); A.connect(C); B.connect(C); // Construct a network var network = architect.Construct([A, B, C, D]); ``` -------------------------------- ### Configure Random Network Options Source: https://wagenaartje.github.io/neataptic/docs/builtins/random Instantiate a random network with custom options for connections, gates, and selfconnections. The 'connections' option should be larger than 'hidden_size'. ```javascript var network = architect.Random(1, 20, 2, { connections: 40, gates: 4, selfconnections: 4 }); drawGraph(network.graph(1000, 800), '.svg'); ``` -------------------------------- ### Network Mutation Usage Source: https://wagenaartje.github.io/neataptic/docs/methods/mutation Demonstrates how to apply mutation methods to a network instance. ```APIDOC ## Network Mutation ### Description Applies a specified mutation method to the entire network. ### Method `network.mutate(mutationMethod)` ### Parameters - **mutationMethod** (function) - Required - The mutation method to apply (e.g., `methods.mutation.ADD_NODE`). ### Request Example ```javascript myNetwork.mutate(methods.mutation.ADD_NODE); ``` ``` -------------------------------- ### Use Default Evolution Options Source: https://wagenaartje.github.io/neataptic/docs/important/evolve Pass an empty object or omit the second argument to use default evolution settings. ```javascript await myNetwork.evolve(trainingSet, {}); // or await myNetwork.evolve(trainingSet); ``` -------------------------------- ### Set Neuron Activation Function Source: https://wagenaartje.github.io/neataptic/docs/methods/activation Change the activation function of a neuron by assigning a method from `methods.activation` to its `squash` property. For example, to use the SINUSOID activation function, set `A.squash = methods.activation.SINUSOID;`. ```javascript var A = new Node(); A.squash = methods.activation.; // eg. A.squash = methods.activation.SINUSOID; ``` -------------------------------- ### Create a Perceptron Network Source: https://wagenaartje.github.io/neataptic/docs/tutorials/training Instantiate a Perceptron network using the architect.Perceptron constructor. The arguments represent the input size, hidden layer size, and output size. ```javascript myNetwork = architect.Perceptron(2, 3, 1); ``` -------------------------------- ### Instantiate a Connection Source: https://wagenaartje.github.io/neataptic/docs/architecture/connection Creates a connection between two nodes with an optional weight. ```javascript var B = new Node(); var C = new Node(); var connection = new Connection(A, B, 0.5); ``` -------------------------------- ### Basic Network Training Syntax Source: https://wagenaartje.github.io/neataptic/docs/important/train The basic syntax for initiating the training process for a neural network. This method takes a training set and optional configuration parameters. ```javascript myNetwork.train(trainingSet, options) ``` -------------------------------- ### Selection Methods Configuration Source: https://wagenaartje.github.io/neataptic/docs/methods/selection Configuring selection methods within the NEAT constructor or evolve function. ```APIDOC ## Selection Methods Configuration ### Description Selection methods determine how parent neural networks are chosen for the next generation. These can be configured during network evolution or when initializing a Neat object. ### Configuration Options - **methods.selection.POWER** - **power** (number) - Optional - Default is 4. Higher values increase the probability of selecting fitter genomes. - **methods.selection.FITNESS_PROPORTIONATE** - Selects genomes with a probability proportionate to their fitness (Roulette selection). - **methods.selection.TOURNAMENT** - **size** (number) - Optional - Default is 5. Number of genomes to sample for the tournament. - **probability** (number) - Optional - Default is 0.5. Probability of selecting the fittest individual from the tournament group. ### Usage Example ```javascript var evolution = new Neat({ selection: methods.selection.FITNESS_PROPORTIONATE, elitism: 5 }); myNetwork.evolve(myTrainingSet, { generations: 10, selection: methods.selection.POWER }); ``` ``` -------------------------------- ### Create a Simple Perceptron Source: https://wagenaartje.github.io/neataptic/docs/builtins/perceptron Instantiate a Perceptron with specified input, hidden, and output layer neuron counts. Requires a minimum of 3 layers. ```javascript var myPerceptron = new architect.Perceptron(2,3,1); ``` -------------------------------- ### Connect Groups with Optional Method Source: https://wagenaartje.github.io/neataptic/docs/architecture/group Establishes connections between two groups. A connection method, such as ALL_TO_ALL, can be optionally specified. ```javascript var A = new Group(4); var B = new Group(5); A.connect(B, methods.connection.ALL_TO_ALL); // specifying a method is optional ``` -------------------------------- ### Node Activation Methods Source: https://wagenaartje.github.io/neataptic/docs/architecture/node Explains the `activate` and `noTraceActivate` methods for calculating a node's output. ```APIDOC ## Node Activation Methods ### Description These methods calculate the node's activation value based on its inputs, bias, and activation function. `noTraceActivate` is faster as it skips trace calculations needed for backpropagation. ### activate Actives the node. Calculates the state from all the input connections, adds the bias, and 'squashes' it. ### noTraceActivate Actives the node without calculating traces, making it faster but unsuitable for backpropagation. ### Request Example ```javascript var node = new Node(); var activationValue = node.activate(); // Example output: 0.4923128591923 var node2 = new Node(); var noTraceActivation = node2.noTraceActivate(); // Example output: 0.4923128591923 ``` ``` -------------------------------- ### Train Network with Options Source: https://wagenaartje.github.io/neataptic/docs/tutorials/training Train the network using the train method, providing the training data and an options object. Options include logging frequency, error threshold, maximum iterations, and learning rate. ```javascript myNetwork.train(myTrainingSet, { log: 10, error: 0.03, iterations: 1000, rate: 0.3 }); ``` -------------------------------- ### Configuring Cost Functions in Training Source: https://wagenaartje.github.io/neataptic/docs/methods/cost How to apply a specific cost function to the network training process. ```APIDOC ## Training Configuration ### Description Defines the cost function used to calculate the error of the network during the training process. ### Parameters #### Request Body - **cost** (methods.cost) - Required - The cost function to use. Available options: CROSS_ENTROPY, MSE, BINARY, MAE, MAPE, MSLE, HINGE. ### Request Example myNetwork.train(trainingData, { log: 1, iterations: 500, error: 0.03, rate: 0.05, cost: methods.cost.MSE }); ``` -------------------------------- ### Initialize a Random Network Source: https://wagenaartje.github.io/neataptic/docs/builtins/random Use architect.Random to create a network with a specified number of input, hidden, and output nodes. This is useful for initializing populations in genetic algorithms. ```javascript new architect.Random(input_size, hidden_size, output_size, options); ``` -------------------------------- ### Instantiate Layer.Dense Source: https://wagenaartje.github.io/neataptic/docs/architecture/layer Creates a regular Dense layer. The 'size' parameter determines the number of neurons in the layer. ```javascript var layer = new Layer.Dense(size); ``` -------------------------------- ### Create a Group of Nodes Source: https://wagenaartje.github.io/neataptic/docs/architecture/group Instantiate a group with a specified number of nodes. This group will be used for network construction. ```javascript var A = new Group(5); ``` -------------------------------- ### Instantiate a GRU Network Source: https://wagenaartje.github.io/neataptic/docs/builtins/gru Create a basic GRU network with a specified number of input, hidden, and output nodes. ```javascript var myLSTM = new architect.GRU(2,6,1); ``` -------------------------------- ### Configure Neat Algorithm with Elitism Source: https://wagenaartje.github.io/neataptic/docs/methods/selection Configure the Neat algorithm with a specific selection method and enable elitism to preserve a set number of top-performing networks from the previous generation. ```javascript var evolution = new Neat({ selection: methods.selection.FITNESS_PROPORTIONATE, elitism: 5 // amount of neural networks to keep from generation to generation }); ``` -------------------------------- ### Initialize NARX Network Source: https://wagenaartje.github.io/neataptic/docs/builtins/narx Constructor for creating a new NARX network instance. ```javascript var network = new architect.NARX(inputSize, hiddenLayers, outputSize, previousInput, previousOutput); ``` -------------------------------- ### Create a Perceptron Neural Network Source: https://wagenaartje.github.io/neataptic/docs/tutorials/visualization Instantiate a Perceptron neural network. Define the number of input nodes, hidden layers with their node counts, and output nodes. ```javascript var network = architect.Perceptron(2, 10, 10, 2); ``` -------------------------------- ### Construct a Square Network with Nodes Source: https://wagenaartje.github.io/neataptic/docs/architecture/construct Use this method to create a network by connecting individual nodes. The `construct()` function automatically identifies input and output nodes. ```javascript var A = new Node(); var B = new Node(); var C = new Node(); var D = new Node(); // Create connections A.connect(B); A.connect(C); B.connect(D); C.connect(D); // Construct a network var network = architect.Construct([A, B, C, D]); ``` -------------------------------- ### Train and Activate Hopfield Network Source: https://wagenaartje.github.io/neataptic/docs/builtins/hopfield Instantiate a Hopfield network, train it with a dataset, and then use it to recall patterns. Ensure the input and output in the training set are the same. ```javascript var network = architect.Hopfield(10); var trainingSet = [ { input: [0, 1, 0, 1, 0, 1, 0, 1, 0, 1], output: [0, 1, 0, 1, 0, 1, 0, 1, 0, 1] }, { input: [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], output: [1, 1, 1, 1, 1, 0, 0, 0, 0, 0] } ]; network.train(trainingSet); network.activate([0,1,0,1,0,1,0,1,1,1]); // [0, 1, 0, 1, 0, 1, 0, 1, 0, 1] network.activate([1,1,1,1,1,0,0,1,0,0]); // [1, 1, 1, 1, 1, 0, 0, 0, 0, 0] ``` -------------------------------- ### Configurable Mutation Options Source: https://wagenaartje.github.io/neataptic/docs/methods/mutation Details on how to configure specific parameters for certain mutation methods. ```APIDOC ## Configurable Mutation Options ### Description Some mutation methods have configurable options that can be adjusted to fine-tune their behavior. ### Configuration Examples #### SUB_NODE.keep_gates - **Default**: `true` - **Description**: When removing connections due to node removal, setting this to `true` ensures that if the removed connections were gated, the new connections will also be gated. ```javascript methods.mutation.SUB_NODE.keep_gates = false; ``` #### MOD_WEIGHT.min, MOD_WEIGHT.max - **Default**: `min: -1`, `max: 1` - **Description**: Sets the upper and lower bounds for modifying connection weights. ```javascript methods.mutation.MOD_WEIGHT.min = -2; methods.mutation.MOD_WEIGHT.max = 2; ``` #### MOD_BIAS.min, MOD_BIAS.max - **Default**: `min: -1`, `max: 1` - **Description**: Sets the upper and lower bounds for modifying neuron biases. ```javascript methods.mutation.MOD_BIAS.min = -0.5; methods.mutation.MOD_BIAS.max = 0.5; ``` #### MOD_ACTIVATION.mutateOutput, SWAP_NODES.mutateOutput - **Default**: `true` - **Description**: Disabling this option prevents the activation functions of output neurons from being changed. This is useful for maintaining normalized output. ```javascript methods.mutation.MOD_ACTIVATION.mutateOutput = false; methods.mutation.SWAP_NODES.mutateOutput = false; ``` #### MOD_ACTIVATION.allowed - **Default**: `[activation.LOGISTIC, activation.TANH, activation.RELU, activation.IDENTITY, activation.STEP, activation.SOFTSIGN, activation.SINUSOID, activation.GAUSSIAN, activation.BENT_IDENTITY, activation.BIPOLAR, activation.BIPOLAR_SIGMOID, activation.HARD_TANH, activation.ABSOLUTE]` - **Description**: Specifies the list of activation functions that are allowed to be used in the neural network. ```javascript methods.mutation.MOD_ACTIVATION.allowed = [ activation.TANH, activation.RELU ]; ``` ``` -------------------------------- ### Connect Nodes Source: https://wagenaartje.github.io/neataptic/docs/architecture/node Establishes a connection where one node projects to another. Can also connect nodes to groups. ```javascript var A = new Node(); var B = new Node(); A.connect(B); // A now projects a connection to B // But you can also connect nodes to groups var C = new Group(4); B.connect(C); // B now projects a connection to all nodes in C ``` -------------------------------- ### Activate Network without Traces Source: https://wagenaartje.github.io/neataptic/docs/architecture/network Performs a faster activation by skipping trace calculations, making backpropagation impossible. ```javascript // Create a network var myNetwork = new Network(3, 2); myNetwork.noTraceActivate([0.8, 1, 0.21]); // gives: [0.49, 0.51] ``` -------------------------------- ### Enable Rate Policy in Training Source: https://wagenaartje.github.io/neataptic/docs/methods/rate Apply a rate policy during the network training process by passing it within the options object. ```javascript network.train(trainingSet, { rate: 0.3, ratePolicy: methods.rate.METHOD(options), }); ``` -------------------------------- ### Create a Deep Perceptron Source: https://wagenaartje.github.io/neataptic/docs/builtins/perceptron Instantiate a deep multilayer Perceptron with multiple hidden layers. Each hidden layer can have a specified number of neurons. ```javascript var myPerceptron = new architect.Perceptron(2, 10, 10, 10, 10, 1); ``` -------------------------------- ### Configure Cost Function in Training Source: https://wagenaartje.github.io/neataptic/docs/methods/cost Apply a specific cost method during the network training process. ```javascript myNetwork.train(trainingData, { log: 1, iterations: 500, error: 0.03, rate: 0.05, cost: methods.cost.METHOD }); ``` -------------------------------- ### Create a Random Neural Network Source: https://wagenaartje.github.io/neataptic/docs/tutorials/visualization Instantiate a neural network with a random architecture. Specify the number of input nodes, hidden nodes, and output nodes. ```javascript var network = architect.Random(2, 20, 2, 2); ``` -------------------------------- ### HTML Template for Neataptic Visualization Source: https://wagenaartje.github.io/neataptic/docs/tutorials/visualization A basic HTML structure to include Neataptic, graph visualization scripts, and a container for the SVG drawing. Ensure 'yourscript.js' contains your network logic. ```html
``` -------------------------------- ### Configure STEP Rate Policy Source: https://wagenaartje.github.io/neataptic/docs/methods/rate Reduces the learning rate by a factor of gamma every stepSize iterations. ```javascript methods.rate.STEP(gamma, stepSize) // default gamma: 0.9 // default stepSize: 100 ``` -------------------------------- ### Connect Nodes Source: https://wagenaartje.github.io/neataptic/docs/architecture/network Establishes a connection between two nodes in the network. ```javascript myNetwork.connect(myNetwork.nodes[4], myNetwork.nodes[5]); ``` -------------------------------- ### Configure mutation methods for evolution Source: https://wagenaartje.github.io/neataptic/docs/methods/mutation Specify a list of mutation methods or predefined groups when using evolve or neat options. ```javascript network.evolve(trainingset, { mutation: [methods.mutation.MOD_BIAS, methods.mutation.ADD_NODE] } ``` ```javascript network.evolve(trainingset, { mutation: methods.mutation.ALL // all mutation methods } network.evolve(trainingset, { mutation: methods.mutation.FFW// all feedforward mutation methods } ``` -------------------------------- ### activate Source: https://wagenaartje.github.io/neataptic/docs/architecture/network Activates the network by processing inputs through all nodes in activation order to produce an output. ```APIDOC ## activate ### Description Activates the network. It will activate all the nodes in activation order and produce an output. ### Request Example myNetwork.activate([0.8, 1, 0.21]); ``` -------------------------------- ### Create a New Node Source: https://wagenaartje.github.io/neataptic/docs/architecture/node Instantiate a new Node object. This is the basic unit for building neural networks. ```javascript var node = new Node(); ``` -------------------------------- ### Node Mutation Usage Source: https://wagenaartje.github.io/neataptic/docs/methods/mutation Shows how to apply mutation methods directly to a specific node. ```APIDOC ## Node Mutation ### Description Applies a specified mutation method to a single node. This is applicable to methods that modify node properties. ### Method `node.mutate(mutationMethod)` ### Parameters - **mutationMethod** (function) - Required - The mutation method to apply (e.g., `methods.mutation.MOD_BIAS`). ### Request Example ```javascript myNode.mutate(methods.mutation.MOD_BIAS); ``` ``` -------------------------------- ### Activate network Source: https://wagenaartje.github.io/neataptic/docs/tutorials/evolution Testing the trained network by activating it with input values. ```javascript myNetwork.activate([0,0]); // [0] myNetwork.activate([0,1]); // [1] myNetwork.activate([1,0]); // [1] myNetwork.activate([1,1]); // [0] ``` -------------------------------- ### network.train(trainingSet, options) Source: https://wagenaartje.github.io/neataptic/docs/important/train Trains the neural network with a given set of input/output pairs and optional configuration parameters. ```APIDOC ## network.train(trainingSet, options) ### Description Trains the network using backpropagation. The process continues until the target error is reached or the maximum number of iterations is met. ### Parameters #### Request Body - **trainingSet** (Array) - Required - An array of objects: { input: [numbers], output: [numbers] } - **options** (Object) - Optional - Configuration for the training process: - **log** (number) - Frequency of logging training status. - **error** (number) - Target error threshold. Default: 0.03. - **cost** (function) - Cost function to use. Default: methods.cost.MSE. - **rate** (number) - Learning rate. Default: 0.3. - **dropout** (number) - Dropout rate for hidden nodes. Default: 0. - **shuffle** (boolean) - Whether to shuffle data every iteration. Default: false. - **iterations** (number) - Maximum iterations to run. - **schedule** (object) - Tasks to run every n iterations. - **clear** (boolean) - Clear network after every activation. Default: false. - **momentum** (number) - Momentum of weight change. Default: 0. - **ratePolicy** (object) - Dynamic rate policy. Default: methods.rate.FIXED(). - **batchSize** (number) - Mini-batch size. Default: 1. - **crossValidate** (object) - Cross-validation settings: - **testSize** (number) - Percentage of set to use for validation. - **testError** (number) - Target error for validation set. ### Request Example { "trainingSet": [{ "input": [0,0], "output": [1] }], "options": { "log": 1, "iterations": 1000, "error": 0.0001, "rate": 0.2 } } ``` -------------------------------- ### Construct a Custom Neural Network Source: https://wagenaartje.github.io/neataptic/docs/tutorials/visualization Manually construct a neural network by defining nodes and their connections. This allows for complex, non-standard architectures. ```javascript var A = new Node(); var B = new Node(); var C = new Node(); var D = new Node(); var E = new Node(); var F = new Node(); var nodes = [A, B, C, D, E, F]; for(var i = 0; i < nodes.length-1; i++){ node = nodes[i]; for(var j = 0; j < 2; j++){ var connectTo = nodes[Math.floor(Math.random() * (nodes.length - i) + i)]; node.connect(connectTo); } } var network = architect.Construct(nodes); ``` -------------------------------- ### Instantiate Layer.Memory Source: https://wagenaartje.github.io/neataptic/docs/architecture/layer Creates a Memory layer to store a specified number of previous inputs. The input layer size must match the memory size, and the output will be size * memory. ```javascript var layer = new Layer.Memory(size, memory); ``` -------------------------------- ### Serialization Source: https://wagenaartje.github.io/neataptic/docs/architecture/node Explains how to serialize and deserialize Node objects to JSON. ```APIDOC ## Serialization (toJSON/fromJSON) ### Description Nodes can be converted into a JSON format for storage or transmission, and then restored back into Node objects. The `fromJSON` method is typically part of a `Network` class to reconstruct the network structure. ### toJSON Serializes the node's state and connections into a JSON object. ### fromJSON Restores a node (or network) from a JSON object. This is usually a static method on a parent class like `Network`. ### Request Example ```javascript var myNode = new Node(); // ... configure myNode ... // Serialize the node var exportedJSON = myNode.toJSON(); // To restore, you would typically use a Network class method: // var importedNode = Network.fromJSON(exportedJSON); // Note: The example implies Network.fromJSON can restore a single node, // but it's more commonly used for entire networks. // Example of restoring (conceptual, assuming Network class exists) // var imported = Network.fromJSON(exportedJSON); ``` ``` -------------------------------- ### Train a GRU Network on Sequences Source: https://wagenaartje.github.io/neataptic/docs/builtins/gru Configure training for a sequence XOR task, ensuring the clear option is enabled and a lower learning rate is applied. ```javascript var trainingSet = [ { input: [0], output: [0]}, { input: [1], output: [1]}, { input: [1], output: [0]}, { input: [0], output: [1]}, { input: [0], output: [0]} ]; var network = new architect.GRU(1,1,1); // Train a sequence: 00100100.. network.train(trainingSet, { log: 1, rate: 0.1, error: 0.005, iterations: 3000, clear: true }); ``` -------------------------------- ### Propagate Network Training Source: https://wagenaartje.github.io/neataptic/docs/architecture/network Teaches the network using specific parameters. Use update: true to apply weight changes. ```javascript myNetwork.propagate(rate, momentum, update, target); ``` ```javascript 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]); } ``` ```javascript network.activate(input); network.propagate(learning_rate, momentum, update_weights, desired_output); ``` -------------------------------- ### Connection Gating Source: https://wagenaartje.github.io/neataptic/docs/architecture/node Explains how to use `gate` and `ungate` to control connection strength based on another node's activation. ```APIDOC ## Connection Gating ### Description Gating allows a node's activation to influence the weight of a connection. `gate` applies a gate, and `ungate` removes it. This can be applied to single connections or arrays of connections. ### gate Applies a gate to one or more connections, making their weights dependent on the gating node's activation. ### ungate Removes a gate from one or more connections. ### Request Example ```javascript var A = new Node(); var B = new Node(); var C = new Node(); // This node will act as the gate var connection = A.connect(B); // Gate the connection from A to B using node C's activation C.gate(connection); // To remove the gate later C.ungate(connection); ``` ``` -------------------------------- ### Crossover Networks Source: https://wagenaartje.github.io/neataptic/docs/architecture/network Creates an offspring network from two parent networks. ```javascript // 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); ``` -------------------------------- ### Activate Node without Trace Source: https://wagenaartje.github.io/neataptic/docs/architecture/node Activates the node without calculating traces, making it faster than regular `activate`. This method cannot be used for backpropagation. ```javascript var node = new Node(); node.noTraceActivate(); // 0.4923128591923 ``` -------------------------------- ### toJSON / fromJSON Source: https://wagenaartje.github.io/neataptic/docs/architecture/network Methods for serializing a network to a JSON object and restoring it back into a new instance. ```APIDOC ## toJSON / fromJSON ### Description Networks can be stored as JSON's and then restored back. ### Request Example var exported = myNetwork.toJSON(); var imported = Network.fromJSON(exported); ``` -------------------------------- ### crossOver Source: https://wagenaartje.github.io/neataptic/docs/architecture/network Creates a new offspring network from two parent networks. ```APIDOC ## crossOver ### Description 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. ### Request Example var network3 = Network.crossOver(network1, network2); ``` -------------------------------- ### Instantiate a Multi-layer GRU Network Source: https://wagenaartje.github.io/neataptic/docs/builtins/gru Create a deep GRU network by specifying multiple hidden layers with a defined number of assemblies. ```javascript var myLSTM = new architect.GRU(2, 4, 4, 4, 1); ``` -------------------------------- ### Initiate Network Evolution Source: https://wagenaartje.github.io/neataptic/docs/important/evolve The evolve function is asynchronous and must be awaited within an async function. ```javascript await myNetwork.evolve(trainingSet, options); ``` -------------------------------- ### Evolve Network with Selection Method Source: https://wagenaartje.github.io/neataptic/docs/methods/selection Specify a selection method when evolving a neural network with training data. Ensure the selection method is available in `methods.selection`. ```javascript var myNetwork = new architect.Perceptron(1,1,1); var myTrainingSet = [{ input:[0], output:[1]}, { input:[1], output:[0]}]; myNetwork.evolve(myTrainingSet, { generations: 10, selection: methods.selection.POWER // eg. }); ``` -------------------------------- ### Connection Management Source: https://wagenaartje.github.io/neataptic/docs/architecture/node Covers methods for establishing and removing connections between nodes. ```APIDOC ## Connection Management ### Description These methods manage the connections between nodes, allowing nodes to send signals to each other. You can connect nodes to other nodes, groups, or even themselves. ### connect Projects a connection from the current node to a target node or group. ### disconnect Removes a projected connection from the current node to a target node. If `force` is true, it also removes the reciprocal connection. ### Request Example ```javascript var A = new Node(); var B = new Node(); var C = new Group(4); // Connect node A to node B A.connect(B); // Connect node B to all nodes in group C B.connect(C); // Connect node A to itself A.connect(A); // Disconnect A from B A.disconnect(B); // Disconnect A from B and B from A A.disconnect(B, true); ``` ``` -------------------------------- ### Instantiate Layer.GRU Source: https://wagenaartje.github.io/neataptic/docs/architecture/layer Creates a GRU layer, similar to LSTM but with fewer gates and no memory cell, excellent for timeseries prediction. ```javascript var layer = new Layer.GRU(size); ``` -------------------------------- ### propagate Source: https://wagenaartje.github.io/neataptic/docs/architecture/network Teaches the network by adjusting weights based on the provided learning rate, momentum, and target output. ```APIDOC ## propagate ### Description This function allows you to teach the network. If you run propagation without setting update to true, then the weights won't update. ### Parameters - **rate** (number) - Required - Learning rate - **momentum** (number) - Required - Momentum value - **update** (boolean) - Required - Whether to update weights - **target** (array) - Optional - Desired output ### Request Example network.propagate(0.2, 0, true, [1]); ``` -------------------------------- ### Activate Network Source: https://wagenaartje.github.io/neataptic/docs/architecture/network Activates the network to produce an output based on input values. ```javascript // Create a network var myNetwork = new Network(3, 2); myNetwork.activate([0.8, 1, 0.21]); // gives: [0.49, 0.51] ``` -------------------------------- ### Import and Export Source: https://wagenaartje.github.io/neataptic/docs/neat Functions for persisting and reloading population states using JSON. ```APIDOC ## export() ### Description Exports the current population to a list containing json objects of the networks. ### Method Function ## import(json) ### Description Imports population from a json array of networks. ### Parameters - **json** (Array) - Required - An array of networks converted to json. ``` -------------------------------- ### Serialize Network to JSON Source: https://wagenaartje.github.io/neataptic/docs/architecture/network Exports and imports a network using JSON format. ```javascript var exported = myNetwork.toJSON(); var imported = Network.fromJSON(exported); ``` -------------------------------- ### Neataptic Input Array Format Source: https://wagenaartje.github.io/neataptic/docs/tutorials/normalization Illustrates the final format for normalized data when used as input for Neataptic neural networks, requiring values to be in arrays. ```javascript [ 0.4665, 0.352, 0.00317, 1, 0, 0, 1, 0, 0 ] [ 0.77, 0.103, 0.01733, 0, 1, 0, 0, 1, 0 ] [ 0.0115, 0.005, 0.40433, 0, 0, 1, 0, 0, 1 ] ``` -------------------------------- ### Group.connect() Source: https://wagenaartje.github.io/neataptic/docs/architecture/group Creates connections between this group and another group or node. ```APIDOC ## Group.connect() Creates connections between this group and another group or node. There are different connection methods for groups, check them out here. ### Usage ```javascript var A = new Group(4); var B = new Group(5); A.connect(B, methods.connection.ALL_TO_ALL); // specifying a method is optional ``` ```