network

package
v0.0.0-...-01f9d02 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2021 License: MIT Imports: 6 Imported by: 2

Documentation

Overview

The package network provides data holders and utilities to describe Artificial Neural Network

Index

Constants

This section is empty.

Variables

View Source
var (
	// The error to be raised when maximal number of network activation attempts exceeded
	NetErrExceededMaxActivationAttempts = errors.New("maximal network activation attempts exceeded.")
	// The error to be raised when unsupported sensors data array size provided
	NetErrUnsupportedSensorsArraySize = errors.New("the sensors array size is unsupported by network solver")
	// The error to be raised when depth calculation failed due to the loop in network
	NetErrDepthCalculationFailedLoopDetected = errors.New("depth can not be determined for network with loop")
)

Functions

func ActivateModule

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

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 *utils.NodeActivatorsFactory) (err error)

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(nlayer NodeNeuronType) string

Returns human readable neuron type name for given constant

func NodeTypeName

func NodeTypeName(ntype NodeType) string

Returns human readable NNode type name for given constant value

Types

type FastControlNode

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

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
}

The fast modular network solver implementation to be used for big neural networks simulation.

func NewFastModularNetworkSolver

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

Creates new fast modular network solver

func (*FastModularNetworkSolver) Flush

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

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

func (*FastModularNetworkSolver) ForwardSteps

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

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 (fmm *FastModularNetworkSolver) LinkCount() int

Returns the total number of links between nodes in the network

func (*FastModularNetworkSolver) LoadSensors

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

Set sensors values to the input nodes of the network

func (*FastModularNetworkSolver) NodeCount

func (fmm *FastModularNetworkSolver) NodeCount() int

Returns the total number of neural units in the network

func (*FastModularNetworkSolver) ReadOutputs

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

Read output values from the output nodes of the network

func (*FastModularNetworkSolver) RecursiveSteps

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

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 (fmm *FastModularNetworkSolver) Relax(maxSteps int, maxAllowedSignalDelta float64) (relaxed bool, err error)

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 (fmm *FastModularNetworkSolver) String() string

Stringer

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

The connection descriptor for fast network

type Link struct {
	// Weight of connection
	Weight 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
	// The amount of weight adjustment
	AddedWeight float64
}

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

func NewLink(weight float64, innode, outnode *NNode, recurrent bool) *Link

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

func NewLinkCopy

func NewLinkCopy(l *Link, innode, outnode *NNode) *Link

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, innode, outnode *NNode, recurrent bool) *Link

Creates new Link with specified Trait

func (*Link) IsEqualGenetically

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

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) String

func (l *Link) String() string

The Link methods implementation

type NNode

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

	// The type of node activation function (SIGMOID, ...)
	ActivationType utils.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
}

A NODE 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

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

func NewNNodeCopy

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

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

func NewNetworkNode

func NewNetworkNode() *NNode

The default constructor

func (*NNode) Depth

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

Find the greatest depth starting from this neuron at depth d

func (*NNode) Flushback

func (n *NNode) Flushback()

Recursively deactivate backwards through the network

func (*NNode) FlushbackCheck

func (n *NNode) FlushbackCheck() error

Verify flushing for debuging

func (*NNode) GetActiveOut

func (n *NNode) GetActiveOut() float64

Returns activation for a current step

func (*NNode) GetActiveOutTd

func (n *NNode) GetActiveOutTd() float64

Returns activation from PREVIOUS time step

func (*NNode) IsNeuron

func (n *NNode) IsNeuron() bool

returns true if this node is NEURON

func (*NNode) IsSensor

func (n *NNode) IsSensor() bool

Returns true if this node is SENSOR

func (*NNode) NodeType

func (n *NNode) NodeType() NodeType

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

func (*NNode) Print

func (n *NNode) Print() string

Prints all node's fields to the string

func (*NNode) SensorLoad

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

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
}

A NETWORK is a LIST of input NODEs and a LIST of output NODEs. 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, net_id int) *Network

Creates new modular network with control nodes

func NewNetwork

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

Creates new network

func (*Network) Activate

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

Activates the net such that all outputs are active

func (*Network) ActivateSteps

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

Attempts to activate the network given number of steps before returning error.

func (*Network) AllNodes

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

Returns all nodes in the network

func (*Network) Complexity

func (n *Network) Complexity() int

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

func (*Network) FastNetworkSolver

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

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)

Puts the network back into an initial state

func (*Network) ForwardSteps

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

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

func (*Network) IsRecurrent

func (n *Network) IsRecurrent(in_node, out_node *NNode, count *int, thresh int) bool

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

Counts the number of links in the net

func (*Network) LoadSensors

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

Takes an array of sensor values and loads it into SENSOR inputs ONLY

func (*Network) MaxDepth

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

Find the maximum number of neurons between an output and an input

func (*Network) NodeCount

func (n *Network) NodeCount() int

Counts the number of nodes in the net

func (*Network) OutputIsOff

func (n *Network) OutputIsOff() bool

If at least one output is not active then return true

func (*Network) PrintActivation

func (n *Network) PrintActivation() string

Prints the values of network outputs to the console

func (*Network) PrintInput

func (n *Network) PrintInput() string

Print the values of network inputs to the console

func (*Network) ReadOutputs

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

Read output values from the output nodes of the network

func (*Network) RecursiveSteps

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

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 outputs.

func (*Network) Relax

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

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.

type NetworkSolver

type NetworkSolver interface {
	// Propagates activation wave through all network nodes provided number of steps in forward direction.
	// Returns true if activation wave passed from all inputs to outputs.
	ForwardSteps(steps int) (bool, error)

	// 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 outputs.
	RecursiveSteps() (bool, error)

	// 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)

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

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

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

Defines network solver interface which describes neural network structures with methods to run activation waves through them.

type NodeNeuronType

type NodeNeuronType byte

NeuronType defines the type of neuron to create

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

These are NNode layer type

func NeuronTypeByName

func NeuronTypeByName(name string) (NodeNeuronType, error)

Returns neuron node type from its name

type NodeType

type NodeType byte

NNodeType defines the type of NNode to create

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

Predefined NNode types

Jump to

Keyboard shortcuts

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