network

package
Version: v2.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2022 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

Package network provides data structures and utilities to describe Artificial Neural Network and network solvers.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNetExceededMaxActivationAttempts The error to be raised when maximal number of network activation attempts exceeded
	ErrNetExceededMaxActivationAttempts = errors.New("maximal network activation attempts exceeded")
	// ErrNetUnsupportedSensorsArraySize The error to be raised when unsupported size of the sensor data array provided
	ErrNetUnsupportedSensorsArraySize = errors.New("the sensors array size is unsupported by network solver")
	// ErrMaximalNetDepthExceeded The error to be raised when depth of the network exceeds maximal allowed
	ErrMaximalNetDepthExceeded = errors.New("depth of the network exceeds maximum allowed, fallback to maximal")
	// ErrZeroActivationStepsRequested the error to be raised when zero activation steps requested
	ErrZeroActivationStepsRequested = errors.New("zero activation steps requested")
)

Functions

func ActivateModule

func ActivateModule(module *NNode, a *neatmath.NodeActivatorsFactory) error

ActivateModule Method to activate neuron module presented by provided node. As a result of execution the activation values of all input nodes will be processed by corresponding activation function and corresponding activation values of output nodes will be set. Will panic if unsupported activation type requested.

func ActivateNode

func ActivateNode(node *NNode, a *neatmath.NodeActivatorsFactory) error

ActivateNode Method to calculate activation for specified neuron node based on it's ActivationType field value. Will return error and set -0.0 activation if unsupported activation type requested.

func NeuronTypeName

func NeuronTypeName(neuronType NodeNeuronType) string

NeuronTypeName Returns human-readable neuron type name for given constant

func NodeTypeName

func NodeTypeName(nType NodeType) string

NodeTypeName Returns human-readable NNode type name for given constant value

func PrintAllActivationDepthPaths added in v2.9.0

func PrintAllActivationDepthPaths(n *Network, w io.Writer) error

PrintAllActivationDepthPaths is to print all paths used to find the maximal activation depth of the network

func PrintPath added in v2.9.0

func PrintPath(w io.Writer, paths [][]graph.Node) (err error)

PrintPath is to print the given paths into specified writer

Types

type FastControlNode

type FastControlNode struct {
	// The activation function for control node
	ActivationType neatmath.NodeActivationType
	// The indexes of the input nodes
	InputIndexes []int
	// The indexes of the output nodes
	OutputIndexes []int
}

FastControlNode The module relay (control node) descriptor for fast network

type FastModularNetworkSolver

type FastModularNetworkSolver struct {
	// A network id
	Id int
	// Is a name of this network */
	Name string
	// contains filtered or unexported fields
}

FastModularNetworkSolver is the network solver implementation to be used for large neural networks simulation.

func NewFastModularNetworkSolver

func NewFastModularNetworkSolver(biasNeuronCount, inputNeuronCount, outputNeuronCount, totalNeuronCount int,
	activationFunctions []neatmath.NodeActivationType, connections []*FastNetworkLink,
	biasList []float64, modules []*FastControlNode) *FastModularNetworkSolver

NewFastModularNetworkSolver Creates new fast modular network solver

func (*FastModularNetworkSolver) Flush

func (s *FastModularNetworkSolver) Flush() (bool, error)

Flush Flushes network state by removing all current activations. Returns true if network flushed successfully or false in case of error.

func (*FastModularNetworkSolver) ForwardSteps

func (s *FastModularNetworkSolver) ForwardSteps(steps int) (res bool, err error)

ForwardSteps Propagates activation wave through all network nodes provided number of steps in forward direction. Returns true if activation wave passed from all inputs to the outputs.

func (*FastModularNetworkSolver) LinkCount

func (s *FastModularNetworkSolver) LinkCount() int

LinkCount Returns the total number of links between nodes in the network

func (*FastModularNetworkSolver) LoadSensors

func (s *FastModularNetworkSolver) LoadSensors(inputs []float64) error

LoadSensors Set sensors values to the input nodes of the network

func (*FastModularNetworkSolver) NodeCount

func (s *FastModularNetworkSolver) NodeCount() int

NodeCount Returns the total number of neural units in the network

func (*FastModularNetworkSolver) ReadOutputs

func (s *FastModularNetworkSolver) ReadOutputs() []float64

ReadOutputs Read output values from the output nodes of the network

func (*FastModularNetworkSolver) RecursiveSteps

func (s *FastModularNetworkSolver) RecursiveSteps() (res bool, err error)

RecursiveSteps Propagates activation wave through all network nodes provided number of steps by recursion from output nodes Returns true if activation wave passed from all inputs to the outputs. This method is preferred method of network activation when number of forward steps can not be easy calculated and no network modules are set.

func (*FastModularNetworkSolver) Relax

func (s *FastModularNetworkSolver) Relax(maxSteps int, maxAllowedSignalDelta float64) (relaxed bool, err error)

Relax Attempts to relax network given amount of steps until giving up. The network considered relaxed when absolute value of the change at any given point is less than maxAllowedSignalDelta during activation waves propagation. If maxAllowedSignalDelta value is less than or equal to 0, the method will return true without checking for relaxation.

func (*FastModularNetworkSolver) String

func (s *FastModularNetworkSolver) String() string

Stringer

type FastNetworkLink struct {
	// The index of source neuron
	SourceIndex int
	// The index of target neuron
	TargetIndex int
	// The weight of this link
	Weight float64
	// The signal relayed by this link
	Signal float64
}

FastNetworkLink The connection descriptor for fast network

type Link struct {
	// weight of connection
	ConnectionWeight float64
	// NNode inputting into the link
	InNode *NNode
	// NNode that the link affects
	OutNode *NNode
	// If TRUE the link is recurrent
	IsRecurrent bool
	// If TRUE the link is time delayed
	IsTimeDelayed bool

	// Points to a trait of parameters for genetic creation
	Trait *neat.Trait

	/* ************ LEARNING PARAMETERS *********** */
	// The following parameters are for use in neurons that learn through habituation,
	// sensitization, or Hebbian-type processes
	Params []float64
}

Link is a connection from one node to another with an associated weight. It can be marked as recurrent.

func NewLink(weight float64, inputNode, outNode *NNode, recurrent bool) *Link

NewLink Creates new link with specified weight, input and output neurons connected recurrently or not.

func NewLinkCopy

func NewLinkCopy(l *Link, inputNode, outNode *NNode) *Link

NewLinkCopy The copy constructor to create new link with parameters taken from provided ones and connecting specified nodes

func NewLinkWithTrait

func NewLinkWithTrait(trait *neat.Trait, weight float64, inputNode, outNode *NNode, recurrent bool) *Link

NewLinkWithTrait Creates new Link with specified Trait

func (*Link) Attributes added in v2.9.0

func (l *Link) Attributes() []encoding.Attribute

Attributes returns list of standard attributes associated with the graph edge

func (*Link) From added in v2.9.0

func (l *Link) From() graph.Node

From returns the from node of the edge. Implements graph.Edge From method

func (*Link) IDString added in v2.9.0

func (l *Link) IDString() string

IDString is to get synthetic ID of this link composed of IDs of connected nodes.

func (*Link) IsEqualGenetically

func (l *Link) IsEqualGenetically(ol *Link) bool

IsEqualGenetically Checks if this link is genetically equal to provided one, i.e. connects nodes with the same IDs and has equal recurrent flag. I.e. if both links represent the same Gene.

func (*Link) ReversedEdge added in v2.9.0

func (l *Link) ReversedEdge() graph.Edge

ReversedEdge returns the edge reversal of the receiver if a reversal is valid for the data type. When a reversal is valid an edge of the same type as the receiver with nodes of the receiver swapped should be returned, otherwise the receiver should be returned unaltered.

func (*Link) String

func (l *Link) String() string

The Link methods implementation

func (*Link) To added in v2.9.0

func (l *Link) To() graph.Node

To returns the to node of the edge. Implements graph.To From method

func (*Link) Weight

func (l *Link) Weight() float64

Weight returns weight of this link

type NNode

type NNode struct {
	// The ID of the node
	Id int

	// The type of node activation function (SIGMOID, ...)
	ActivationType math.NodeActivationType
	// The neuron type for this node (HIDDEN, INPUT, OUTPUT, BIAS)
	NeuronType NodeNeuronType

	// The node's activation value
	Activation float64
	// The number of activations for current node
	ActivationsCount int32
	// The activation sum
	ActivationSum float64

	// The list of all incoming connections
	Incoming []*Link
	// The list of all outgoing connections
	Outgoing []*Link
	// The trait linked to the node
	Trait *neat.Trait

	// Used for Gene decoding by referencing analogue to this node in organism phenotype
	PhenotypeAnalogue *NNode

	/* ************ LEARNING PARAMETERS *********** */
	// The following parameters are for use in neurons that learn through habituation,
	// sensitization, or Hebbian-type processes  */
	Params []float64
	// contains filtered or unexported fields
}

NNode is either a NEURON or a SENSOR.

  • If it's a sensor, it can be loaded with a value for output
  • If it's a neuron, it has a list of its incoming input signals ([]*Link is used)

Use an activation count to avoid flushing

func NewNNode

func NewNNode(nodeId int, neuronType NodeNeuronType) *NNode

NewNNode Creates new node with specified ID and neuron type associated (INPUT, HIDDEN, OUTPUT, BIAS)

func NewNNodeCopy

func NewNNodeCopy(n *NNode, t *neat.Trait) *NNode

NewNNodeCopy Construct a NNode off another NNode with given trait for genome purposes

func NewNetworkNode

func NewNetworkNode() *NNode

NewNetworkNode The default constructor

func (*NNode) AddIncoming added in v2.9.0

func (n *NNode) AddIncoming(in *NNode, weight float64) *Link

AddIncoming adds a non-recurrent incoming link to this node. You should use this with caution because this doesn't create full duplex link needed for proper network activation. This method only intended for linking the control nodes. For all other needs use ConnectFrom which properly creates all needed links.

func (*NNode) AddOutgoing added in v2.9.0

func (n *NNode) AddOutgoing(out *NNode, weight float64) *Link

AddOutgoing adds a non-recurrent outgoing link to this node. You should use this with caution because this doesn't create full duplex link needed for proper network activation. This method only intended for linking the control nodes. For all other needs use ConnectFrom which properly creates all needed links.

func (*NNode) Attributes added in v2.9.0

func (n *NNode) Attributes() []encoding.Attribute

Attributes returns list of standard attributes associated with the graph node

func (*NNode) ConnectFrom added in v2.9.0

func (n *NNode) ConnectFrom(in *NNode, weight float64) *Link

ConnectFrom is to create link between two nodes. The incoming links of current node and outgoing links of in node would be updated to have reference to the new link.

func (*NNode) Depth

func (n *NNode) Depth(d int, maxDepth int) (int, error)

Depth Find the greatest depth starting from this neuron at depth d. If maxDepth > 0 it can be used to stop early in case if very deep network detected

func (*NNode) Flushback

func (n *NNode) Flushback()

Flushback Recursively deactivate backwards through the network

func (*NNode) FlushbackCheck

func (n *NNode) FlushbackCheck() error

FlushbackCheck is to verify flushing for debugging

func (*NNode) GetActiveOut

func (n *NNode) GetActiveOut() float64

GetActiveOut Returns activation for a current step

func (*NNode) GetActiveOutTd

func (n *NNode) GetActiveOutTd() float64

GetActiveOutTd Returns activation from PREVIOUS time step

func (*NNode) ID added in v2.9.0

func (n *NNode) ID() int64

ID is to get ID of the node. Implements graph.Node ID method.

func (*NNode) IsNeuron

func (n *NNode) IsNeuron() bool

IsNeuron returns true if this node is NEURON

func (*NNode) IsSensor

func (n *NNode) IsSensor() bool

IsSensor Returns true if this node is SENSOR

func (*NNode) NodeType

func (n *NNode) NodeType() NodeType

NodeType Convenient method to check network's node type (SENSOR, NEURON)

func (*NNode) PrintDebug added in v2.9.0

func (n *NNode) PrintDebug() string

PrintDebug is to print all fields of the node to the string

func (*NNode) SensorLoad

func (n *NNode) SensorLoad(load float64) bool

SensorLoad If the node is a SENSOR, returns TRUE and loads the value

func (*NNode) String

func (n *NNode) String() string

type Network

type Network struct {
	// A network id
	Id int
	// Is a name of this network
	Name string
	// NNodes that output from the network
	Outputs []*NNode
	// contains filtered or unexported fields
}

Network is a collection of all nodes within an organism's phenotype, which effectively defines Neural Network topology. The point of the network is to define a single entity which can evolve or learn on its own, even though it may be part of a larger framework.

func NewModularNetwork

func NewModularNetwork(in, out, all, control []*NNode, netId int) *Network

NewModularNetwork Creates new modular network with control nodes

func NewNetwork

func NewNetwork(in, out, all []*NNode, netId int) *Network

NewNetwork Creates new network

func (*Network) Activate

func (n *Network) Activate() (bool, error)

Activate is to activate the network such that all outputs are active

func (*Network) ActivateSteps

func (n *Network) ActivateSteps(maxSteps int) (bool, error)

ActivateSteps Attempts to activate the network given number of steps before returning error. Normally the maxSteps should be equal to the maximal activation depth of the network as returned by MaxActivationDepth() or MaxActivationDepthFast()

func (*Network) AllNodes

func (n *Network) AllNodes() []*NNode

AllNodes Returns all network nodes including MIMO control nodes: base nodes + control nodes

func (*Network) BaseNodes added in v2.9.0

func (n *Network) BaseNodes() []*NNode

BaseNodes returns all nodes in this network excluding MIMO control nodes

func (*Network) Complexity

func (n *Network) Complexity() int

Complexity Returns complexity of this network which is sum of nodes count and links count

func (*Network) ControlNodes added in v2.9.0

func (n *Network) ControlNodes() []*NNode

ControlNodes returns all control nodes of this network.

func (*Network) Edge added in v2.9.0

func (n *Network) Edge(uid, vid int64) graph.Edge

Edge returns the edge from u to v, with IDs uid and vid, if such an edge exists and nil otherwise. The node v must be directly reachable from u as defined by the From method.

func (*Network) FastNetworkSolver

func (n *Network) FastNetworkSolver() (Solver, error)

FastNetworkSolver Creates fast network solver based on the architecture of this network. It's primarily aimed for big networks to improve processing speed.

func (*Network) Flush

func (n *Network) Flush() (res bool, err error)

func (*Network) ForwardSteps

func (n *Network) ForwardSteps(steps int) (res bool, err error)

func (*Network) From added in v2.9.0

func (n *Network) From(id int64) graph.Nodes

From returns all nodes that can be reached directly from the node with the given ID.

From must not return nil.

func (*Network) HasEdgeBetween added in v2.9.0

func (n *Network) HasEdgeBetween(xid, yid int64) bool

HasEdgeBetween returns whether an edge exists between nodes with IDs xid and yid without considering direction.

func (*Network) HasEdgeFromTo added in v2.9.0

func (n *Network) HasEdgeFromTo(uid, vid int64) bool

HasEdgeFromTo returns whether an edge exists in the graph from u to v with IDs uid and vid.

func (*Network) IsControlNode added in v2.9.0

func (n *Network) IsControlNode(nid int) bool

IsControlNode is to check if specified node ID is a control node

func (*Network) IsRecurrent

func (n *Network) IsRecurrent(inNode, outNode *NNode, count *int, thresh int) bool

IsRecurrent This checks a POTENTIAL link between a potential in_node and potential out_node to see if it must be recurrent. Use count and thresh to jump out in the case of an infinite loop.

func (*Network) LinkCount

func (n *Network) LinkCount() int

func (*Network) LoadSensors

func (n *Network) LoadSensors(sensors []float64) error

func (*Network) MaxActivationDepth added in v2.9.0

func (n *Network) MaxActivationDepth() (int, error)

MaxActivationDepth is to find the maximum number of neuron layers to be activated between an output and an input layers.

func (*Network) MaxActivationDepthFast added in v2.9.0

func (n *Network) MaxActivationDepthFast(maxDepth int) (int, error)

MaxActivationDepthFast is to find the maximum number of neuron layers to be activated between an output and an input layers. This is the fastest version of depth calculation but only suitable for simple networks. If current network is modular the error will be raised. It is possible to limit the maximal depth value by setting the maxDepth value greater than zero. If network depth exceeds provided maxDepth value this value will be returned along with ErrMaximalNetDepthExceeded to indicate that calculation stopped. If maxDepth is less or equal to zero no maximal depth limitation will be set.

func (*Network) Node added in v2.9.0

func (n *Network) Node(id int64) graph.Node

Node returns the node with the given ID if it exists in the graph, and nil otherwise.

func (*Network) NodeCount

func (n *Network) NodeCount() int

func (*Network) Nodes added in v2.9.0

func (n *Network) Nodes() graph.Nodes

Nodes returns all the nodes in the graph.

Nodes must not return nil.

func (*Network) OutputIsOff

func (n *Network) OutputIsOff() bool

OutputIsOff If at least one output is not active then return true

func (*Network) PrintActivation

func (n *Network) PrintActivation() string

PrintActivation Prints the values of network outputs to the console

func (*Network) PrintInput

func (n *Network) PrintInput() string

PrintInput Print the values of network inputs to the console

func (*Network) ReadOutputs

func (n *Network) ReadOutputs() []float64

func (*Network) RecursiveSteps

func (n *Network) RecursiveSteps() (bool, error)

func (*Network) Relax

func (n *Network) Relax(_ int, _ float64) (bool, error)

func (*Network) To added in v2.9.0

func (n *Network) To(id int64) graph.Nodes

To returns all nodes that can reach directly to the node with the given ID.

To must not return nil.

func (*Network) Weight added in v2.9.0

func (n *Network) Weight(xid, yid int64) (w float64, ok bool)

Weight returns the weight for the edge between x and y with IDs xid and yid if Edge(xid, yid) returns a non-nil Edge. If x and y are the same node or there is no joining edge between the two nodes the weight value returned is implementation dependent. Weight returns true if an edge exists between x and y or if x and y have the same ID, false otherwise.

func (*Network) WeightedEdge added in v2.9.0

func (n *Network) WeightedEdge(uid, vid int64) graph.WeightedEdge

WeightedEdge returns the weighted edge from u to v with IDs uid and vid if such an edge exists and nil otherwise. The node v must be directly reachable from u as defined by the From method.

type NodeIdGenerator

type NodeIdGenerator interface {
	// NextNodeId is to get next unique node ID
	NextNodeId() int
}

NodeIdGenerator definition of the unique IDs generator for network nodes.

type NodeNeuronType

type NodeNeuronType byte

NodeNeuronType NeuronType defines the type of neuron to create

const (
	// HiddenNeuron The node is in hidden layer
	HiddenNeuron NodeNeuronType = iota
	// InputNeuron The node is in input layer
	InputNeuron
	// OutputNeuron The node is in output layer
	OutputNeuron
	// BiasNeuron The node is bias
	BiasNeuron
)

These are NNode layer type

func NeuronTypeByName

func NeuronTypeByName(name string) (NodeNeuronType, error)

NeuronTypeByName Returns neuron node type from its name

type NodeType

type NodeType byte

NodeType NNodeType defines the type of NNode to create

const (
	// NeuronNode The neuron type
	NeuronNode NodeType = iota
	// SensorNode The sensor type
	SensorNode
)

Predefined NNode types

type Solver

type Solver interface {
	// ForwardSteps Propagates activation wave through all network nodes provided number of steps in forward direction.
	// Normally the number of steps should be equal to the activation depth of the network.
	// Returns true if activation wave passed from all inputs to the output nodes.
	ForwardSteps(steps int) (bool, error)

	// RecursiveSteps Propagates activation wave through all network nodes provided number of steps by recursion from output nodes
	// Returns true if activation wave passed from all inputs to the output nodes.
	RecursiveSteps() (bool, error)

	// Relax Attempts to relax network given amount of steps until giving up. The network considered relaxed when absolute
	// value of the change at any given point is less than maxAllowedSignalDelta during activation waves propagation.
	// If maxAllowedSignalDelta value is less than or equal to 0, the method will return true without checking for relaxation.
	Relax(maxSteps int, maxAllowedSignalDelta float64) (bool, error)

	// Flush Flushes network state by removing all current activations. Returns true if network flushed successfully or
	// false in case of error.
	Flush() (bool, error)

	// LoadSensors Set sensors values to the input nodes of the network
	LoadSensors(inputs []float64) error
	// ReadOutputs Read output values from the output nodes of the network
	ReadOutputs() []float64

	// NodeCount Returns the total number of neural units in the network
	NodeCount() int
	// LinkCount Returns the total number of links between nodes in the network
	LinkCount() int
}

Solver defines network solver interface, which allows propagation of the activation waves through the underlying network graph.

Directories

Path Synopsis
Package formats defines the serialization formats which can be used for network graph persistence
Package formats defines the serialization formats which can be used for network graph persistence

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL