Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Network

Create a neural network

Networks are easy to create, all you need to specify is an input and an output size.

constructs

Network

Hierarchy

  • Network

Index

Constructors

constructor

  • new Network(inputSize: number, outputSize: number, startsEmpty?: boolean): Network

Properties

adjustedFitness

adjustedFitness: number

connections

connections: Set<Connection>

The connections inside this network.

gates

gates: Set<Connection>

The gates inside this network.

Readonly inputSize

inputSize: number

The input size of this network.

nodes

nodes: Node[]

The nodes inside this network. Stored in activation order.

Readonly outputSize

outputSize: number

The output size of this network.

score

score: number | undefined

The score of this network for evolution.

Methods

activate

  • activate(input: number[], options?: { dropoutRate?: undefined | number; trace?: undefined | false | true }): number[]
  • Activates the network

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

    Parameters

    • input: number[]
    • Default value options: { dropoutRate?: undefined | number; trace?: undefined | false | true } = {}
      • Optional dropoutRate?: undefined | number

        The dropout rate. dropout

      • Optional trace?: undefined | false | true

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

    Returns number[]

    Squashed output values

addGate

clear

  • clear(): void

connect

  • Connects a Node to another Node or Group in the network

    Parameters

    • from: Node

      The source Node

    • to: Node

      The destination Node or Group

    • Default value weight: number = 0

    Returns Connection

    An array of the formed connections

deepCopy

disconnect

distance

  • distance(genome2: Network, c1: number, c2: number, c3: number): number

mutate

  • mutate(method: Mutation, options?: undefined | { allowedActivations?: ActivationType[]; maxConnections?: undefined | number; maxGates?: undefined | number; maxNodes?: undefined | number }): void
  • Mutates the network with the given method.

    Parameters

    • method: Mutation

      (mutation)

    • Optional options: undefined | { allowedActivations?: ActivationType[]; maxConnections?: undefined | number; maxGates?: undefined | number; maxNodes?: undefined | number }

    Returns void

mutateRandom

  • mutateRandom(allowedMethods?: Mutation[], options?: { allowedActivations?: ActivationType[]; maxConnections?: undefined | number; maxGates?: undefined | number; maxNodes?: undefined | number }): void
  • Selects a random mutation method and returns a mutated copy of the network. Warning! Mutates network directly.

    Parameters

    • Default value allowedMethods: Mutation[] = ALL_INSTINCT_MUTATIONS
    • Default value options: { allowedActivations?: ActivationType[]; maxConnections?: undefined | number; maxGates?: undefined | number; maxNodes?: undefined | number } = {}
      • Optional allowedActivations?: ActivationType[]

        All allowed activations

      • Optional maxConnections?: undefined | number

        Maximum amount of Connections a network can grow to

      • Optional maxGates?: undefined | number

        Maximum amount of Gates a network can grow to

      • Optional maxNodes?: undefined | number

        Maximum amount of nodes a network can grow to

    Returns void

propagate

  • propagate(target: number[], options?: { momentum?: undefined | number; rate?: undefined | number; update?: undefined | false | true }): void
  • Backpropagate the network

    This function allows you to teach the network. If you want to do more complex training, use the network.train() function.

    Parameters

    • target: number[]

      Ideal values of the previous activate. Will use the difference to improve the weights

    • Default value options: { momentum?: undefined | number; rate?: undefined | number; update?: undefined | false | true } = {}

      More option for propagation

      • Optional momentum?: undefined | number

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

      • Optional rate?: undefined | number

        Sets the learning rate of the backpropagation process.

      • Optional update?: undefined | false | true

        When set to false weights won't update, but when set to true after being false the last propagation will include the delta weights of the first "update:false" propagation too.

    Returns void

removeGate

removeNode

  • removeNode(node: Node, keepGates?: boolean): void
  • Removes a node from a network, all its connections will be redirected. If it gates a connection, the gate will be removed.

    Parameters

    • node: Node

      Node to remove from the network

    • Default value keepGates: boolean = true

    Returns void

test

  • test(dataset: { input: number[]; output: number[] }[], loss?: lossType): number
  • Tests a set and returns the error and elapsed time

    Parameters

    • dataset: { input: number[]; output: number[] }[]
    • Default value loss: lossType = MSELoss

    Returns number

    A summary object of the network's performance

toJSON

train

  • train(options: TrainOptions): { error: number; iterations: number; time: number }
  • Train the given data to this network

    Parameters

    Returns { error: number; iterations: number; time: number }

    ,iterations:{number},time:{number}}} A summary object of the network's performance

    • error: number

      The loss of the network after training.

    • iterations: number

      The iterations took for training the network.

    • time: number

      The time from begin to end in milliseconds

Private trainEpoch

  • trainEpoch(options: { batchSize: number; dataset: { input: number[]; output: number[] }[]; dropoutRate: number; loss: lossType; momentum: number; trainingRate: number }): number
  • Performs one training epoch and returns the error - this is a private function used in self.train

    Parameters

    • options: { batchSize: number; dataset: { input: number[]; output: number[] }[]; dropoutRate: number; loss: lossType; momentum: number; trainingRate: number }
      • batchSize: number

        The batch size.

      • dataset: { input: number[]; output: number[] }[]

        The dataset.

      • dropoutRate: number

        The dropout rate

      • loss: lossType

        The loss function.

      • momentum: number

        The momentum.

      • trainingRate: number

        The training rate.

    Returns number

Static crossover

  • Create an offspring from two parent networks.

    Networks are not required to have the same size, however input and output size should be the same!

    todo

    Add custom crossover method customization

    Parameters

    • network1: Network

      First parent network

    • network2: Network

      Second parent network

    Returns Network

    New network created from mixing parent networks

Static fromJSON

Generated using TypeDoc