neataptic

repository·master·Indexed 22 days ago

https://github.com/wagenaartje/neataptic

An architecture-free neural network library supporting both neuro-evolution via the Instinct algorithm and traditional backpropagation. It features dynamic architectures where neurons and synapses can be added or removed, built-in network types like LSTM and GRU, and tools for visualizing networks using D3.js and WebCola. Version 1.4.7 requires Node.js v7.6 or higher.

Tokens
25.4K
Snippets
80
Records
154
Agent score
72%

What's inside neataptic

  1. Prepare training data for classification

    master

    When training a network for classification, inputs should be normalized (e.g., RGB values scaled between 0 and 1) and outputs should be represented using one-hot encoding.

    In one-hot encoding, the output is an array of zeros with a 1 at the index corresponding to the correct class. For example, if you have 3 possible colors and the target is the second color, the output array would be [0, 1, 0].

  2. Building custom neural networks in Neataptic

    master

    Neataptic allows for the construction of custom neural networks using two primary approaches:

    1. Low-level construction: Building the network from the bottom up by manually defining individual Node and Connection entities.
    2. High-level construction: Using abstractions like Group and Layer to ease the process of organizing complex architectures.

    All components are ultimately organized into a Network object.

  3. Understand the three gating methods in Neataptic

    master

    Gating makes network weights more dynamic by adapting them to a gating node. Neataptic provides three distinct gating methods that define how nodes in a gating group influence connections between emitting and receiving groups:

    1. methods.gating.OUTPUT: Every node in the gating group gates at least one node in the emitting group and all its connections to the receiving group.
    2. methods.gating.INPUT: Every node in the gating group gates at least one node in the receiving group and all its connections from the emitting group.
    3. methods.gating.SELF: Every node in the gating group gates at least one self-connection within the emitting/receiving group.
  4. How the Perceptron architecture works

    master

    The Perceptron is a feed-forward neural network architecture. It is structured as a sequence of layers where every neuron in one layer is connected to every neuron in the subsequent layer.

    When defining a Perceptron, the arguments passed to the constructor represent the number of neurons in each layer in order: (input_neurons, hidden_layer_1_neurons, ..., hidden_layer_n_neurons, output_neurons).

  5. Understand the Target Seeking AI simulation logic

    master

    The target seeking simulation uses neuro-evolution to train neural networks to follow a moving target.

    Agent Inputs and Outputs

    Each agent receives the following inputs to its neural network:

    • Own speed in the x-axis
    • Own speed in the y-axis
    • Target's speed in the x-axis
    • Target's speed in the y-axis
    • Angle towards the target
    • Distance to the target

    The output of the agent is the desired movement direction.

    Scoring and Fitness

    Agents are scored based on their proximity to the target. When an agent is within a specific vicinity (e.g., 100 pixels), its score increases proportionally to the distance: (100 - dist).

    To prevent overfitting and reduce computational requirements, a penalty is applied based on the network complexity: the score is decreased for every node in the agent's network.

  6. Use the Gated Recurrent Unit (GRU) architecture

    master

    The Gated Recurrent Unit (GRU) is a recurrent neural network architecture similar to LSTM, but with one fewer gate and no self-connections. It is well-suited for classifying, processing, and predicting time series data where long time lags exist between important events.

    Note: GRU is currently considered experimental and may not work for all datasets.

    To use this architecture, you must define at least one input node, one gated recurrent unit assembly, and one output node. A single GRU assembly consists of seven internal nodes: input, update gate, inverse update gate, reset gate, memorycell, output, and previous output memory.

  7. Getting started with Neataptic tutorials

    master

    To begin using Neataptic for creating, training, and evolving neural networks, it is recommended to follow the core tutorial series. The learning path covers the following fundamental concepts:

    • Training: How to train your networks.
    • Evolution: How to evolve your networks.
    • Normalization: Techniques for data or weight normalization.
    • Visualization: How to visualize your networks and their processes.
  8. Normalize numerical data for Neataptic networks

    master

    While Neataptic networks accept non-normalized values, normalizing your input data to a range between 0 and 1 helps the network converge faster.

    For numerical values (where the distance between values matters, e.g., price), you should normalize by dividing the input by a chosen maximum value. This maximum value should be greater than or equal to any current or expected future values to avoid the need for re-normalization and retraining.

    Example: If your stock values are 933, 154, and 23, and you assume the maximum possible stock is 2000, you normalize as follows:

    • 933 / 2000 = 0.4665
    • 154 / 2000 = 0.077
    • 23 / 2000 = 0.0115
    // 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
  9. Classify colors using genetic algorithms in Neataptic

    master

    You can use Neataptic to evolve a neural network that learns to classify inputs (such as RGB color values) into specific categories using a genetic algorithm.

    To achieve this, you must:

    1. Prepare a training set: Create an array of objects where each object contains an input array (normalized values between 0 and 1) and an output array (using one-hot encoding to represent the target category).
    2. Configure the evolution process: Use the network.evolve() method with parameters for iterations, mutation rate, elitism, population size, mutation method, and cost function.
    3. Define the fitness function: The algorithm uses a cost function (like Mean Squared Error) to calculate fitness. Neataptic also uses a default growth parameter to penalize networks that become excessively large.
    network.evolve(set, {
      iterations: 1,
      mutationRate: 0.6,
      elisitm: 5,
      popSize: 100,
      mutation: methods.mutation.FFW,
      cost: methods.cost.MSE
    });
  10. Set up the environment for drawing graphs

    master

    To visualize neural networks using Neataptic, your HTML file must include the following dependencies and local files:

    1. d3v3: d3.v3.min.js (D3.js version 3)
    2. webcola: cola.v3.min.js (WebCola layout engine)
    3. neataptic.js: The core Neataptic library.
    4. graph.js: The graph drawing logic.
    5. graph.css: Styles for connections and nodes.

    Example HTML structure:

    <html
      <head>
        <script src="libs/d3v3.js"></script>
        <script src="libs/webcola.js"></script>
    
        <script src="/libs/neataptic.js"></script>
        <script src="/libs/graph.js"></script>
     
        <script src="script.js"></script>
        
        <link rel="stylesheet" type="text/css" href="/libs/graph.css">
      </head>
      <body
        <div class="container">
          <div class="row">
            <svg class="draw" width="1000px" height="1000px"/>
          </div
        </div
      </body
    </html
    <html
      <head>
        <script src="libs/d3v3.js"></script>
        <script src="libs/webcola.js"></script>
    
        <script src="/libs/neataptic.js"></script>
        <script src="/libs/graph.js"></script>
     
        <script src="script.js"></script>
        
        <link rel="stylesheet" type="text/css" href="/libs/graph.css">
      </head>
      <body
        <div class="container">
          <div class="row">
            <svg class="draw" width="1000px" height="1000px"/>
          </div
        </div
      </body
    </html
  11. Set up Neataptic for neural network visualization

    master

    To visualize neural networks, you need to include the Neataptic library, the graph visualization scripts, and their associated CSS. You also need D3.js and WebCola for the graph rendering.

    Create an HTML file with the following structure to provide a container for the SVG graph:

    <html
      <head>
        <script src="http://d3js.org/d3.v3.min.js"></script>
        <script src="http://marvl.infotech.monash.edu/webcola/cola.v3.min.js"></script>
    
        <script src="https://rawgit.com/wagenaartje/neataptic/master/dist/neataptic.js"></script>
        <script src="https://rawgit.com/wagenaartje/neataptic/master/graph/graph.js"></script>
        <link rel="stylesheet" type="text/css" href="https://rawgit.com/wagenaartje/neataptic/master/graph/graph.css">
      </head>
      <body
        <div class="container">
          <div class="row">
            <svg class="draw" width="1000px" height="1000px"/>
          </div
        </div
        <script src="yourscript.js"></script>
      </body
    </html>