emer

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2019 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package emer provides minimal interfaces for the basic structural elements of neural networks including: * emer.Network, emer.Layer, emer.Unit, emer.Prjn (projection that interconnects layers)

These interfaces are intended to be just sufficient to support visualization and generic analysis kinds of functions, but explicitly avoid exposing ANY of the algorithmic aspects, so that those can be purely encoded in the implementation structs.

At this point, given the extra complexity it would require, these interfaces do not support the ability to build or modify networks.

Index

Constants

This section is empty.

Variables

View Source
var KiT_LayerType = kit.Enums.AddEnum(LayerTypeN, false, nil)
View Source
var KiT_PrjnType = kit.Enums.AddEnum(PrjnTypeN, false, nil)

Functions

This section is empty.

Types

type Layer

type Layer interface {
	params.Styler // TypeName, Name, and Class methods for parameter styling

	// InitName MUST be called to initialize the layer's pointer to itself as an emer.Layer
	// which enables the proper interface methods to be called.  Also sets the name.
	InitName(lay Layer, name string)

	// Label satisfies the gi.Labeler interface for getting the name of objects generically
	Label() string

	// SetClass sets CSS-style class name(s) for this layer (space-separated if multiple)
	SetClass(cls string)

	// IsOff returns true if layer has been turned Off (lesioned) -- for experimentation
	IsOff() bool

	// SetOff sets the "off" (lesioned) status of layer
	SetOff(off bool)

	// Shape returns the organization of units in the layer, in terms of an array of dimensions.
	// Row-major ordering is default (Y then X), outer-most to inner-most.
	// if 2D, then it is a simple Y,X layer with no sub-structure (pools).
	// If 4D, then it number of pools Y, X and then number of units per pool Y, X
	Shape() *etensor.Shape

	// Is2D() returns true if this is a 2D layer (no Pools)
	Is2D() bool

	// Is4D() returns true if this is a 4D layer (has Pools as inner 2 dimensions)
	Is4D() bool

	// Type returns the functional type of layer according to LayerType (extensible in
	// more specialized algorithms)
	Type() LayerType

	// SetType sets the functional type of layer
	SetType(typ LayerType)

	// Config configures the basic parameters of the layer
	Config(shape []int, typ LayerType)

	// Thread() returns the thread number (go worker thread) to use in updating this layer.
	// The user is responsible for allocating layers to threads, trying to maintain an even
	// distribution across layers and establishing good break-points.
	Thread() int

	// SetThread sets the thread number (go worker thread) to use in updating this layer.
	SetThread(thr int)

	// RelPos returns the relative 3D position specification for this layer
	// for display in the 3D NetView -- see Pos() for display conventions.
	RelPos() relpos.Rel

	// SetRelPos sets the the relative 3D position specification for this layer
	SetRelPos(r relpos.Rel)

	// Pos returns the 3D position of the lower-left-hand corner of the layer.
	// The 3D view has layers arranged in X-Y planes stacked vertically along the Z axis.
	// Somewhat confusingly, this differs from the standard 3D graphics convention,
	// where the vertical dimension is Y and Z is the depth dimension.  However, in the
	// more "layer-centric" way of thinking about it, it is natural for the width & height
	// to map onto X and Y, and then Z is left over for stacking vertically.
	Pos() mat32.Vec3

	// SetPos sets the 3D position of this layer -- will generally be overwritten by
	// automatic RelPos setting, unless that doesn't specify a valid relative position.
	SetPos(pos mat32.Vec3)

	// Size returns the display size of this layer for the 3D view -- see Pos() for general info.
	// This is multiplied by the RelPos.Scale factor to rescale layer sizes, and takes
	// into account 2D and 4D layer structures.
	Size() mat32.Vec2

	// Index returns a 0..n-1 index of the position of the layer within list of layers
	// in the network.  For backprop networks, index position has computational significance.
	// For Leabra networks, it only has significance in determining who gets which weights for
	// enforcing initial weight symmetry -- higher layers get weights from lower layers.
	Index() int

	// SetIndex sets the layer index
	SetIndex(idx int)

	// UnitVarNames returns a list of variable names available on the units in this layer.
	// this is a global list so do not modify!
	UnitVarNames() []string

	// UnitVals returns values of given variable name on unit,
	// for each unit in the layer, as a float32 slice (which is created de-novo).
	// returns nil on invalid var name -- see Try version for error message.
	UnitVals(varnm string) []float32

	// UnitValsTry returns values of given variable name on unit,
	// for each unit in the layer, as a float32 slice (which is created de-novo).
	// returns error message if var name not found.
	UnitValsTry(varnm string) ([]float32, error)

	// UnitValsTensor returns values of given variable name on unit
	// for each unit in the layer, as a float32 tensor in same shape as layer units.
	// returns nil on invalid var name -- see Try version for error message.
	UnitValsTensor(varnm string) etensor.Tensor

	// UnitValsTensorTry returns values of given variable name on unit
	// for each unit in the layer, as a float32 tensor in same shape as layer units.
	// returns error message if var name not found.
	UnitValsTensorTry(varnm string) (etensor.Tensor, error)

	// UnitVal returns value of given variable name on given unit,
	// using shape-based dimensional index.
	// returns nil on invalid var name or index -- see Try version for error message.
	UnitVal(varnm string, idx []int) float32

	// UnitValTry returns value of given variable name on given unit,
	// using shape-based dimensional index.
	// returns error message if var name not found or invalid index.
	UnitValTry(varnm string, idx []int) (float32, error)

	// UnitVal1D returns value of given variable name on given unit,
	// using 1-dimensional index.
	// returns nil on invalid var name or index -- see Try version for error message.
	UnitVal1D(varnm string, idx int) float32

	// UnitVal1DTry returns value of given variable name on given unit,
	// using 1-dimensional index.
	// returns error message if var name not found or invalid index.
	UnitVal1DTry(varnm string, idx int) (float32, error)

	// RecvPrjns returns the full list of receiving projections
	RecvPrjns() *Prjns

	// NRecvPrjns returns the number of receiving projections
	NRecvPrjns() int

	// RecvPrjn returns a specific receiving projection
	RecvPrjn(idx int) Prjn

	// SendPrjns returns the full list of sending projections
	SendPrjns() *Prjns

	// NSendPrjns returns the number of sending projections
	NSendPrjns() int

	// SendPrjn returns a specific sending projection
	SendPrjn(idx int) Prjn

	// Defaults sets default parameter values for all Layer and recv projection parameters
	Defaults()

	// UpdateParams() updates parameter values for all Layer and recv projection parameters,
	// based on any other params that might have changed.
	UpdateParams()

	// ApplyParams applies given parameter style Sheet to this layer and its recv projections.
	// Calls UpdateParams on anything set to ensure derived parameters are all updated.
	// If setMsg is true, then a message is printed to confirm each parameter that is set.
	// it always prints a message if a parameter fails to be set.
	// returns true if any params were set, and error if there were any errors.
	ApplyParams(pars *params.Sheet, setMsg bool) (bool, error)

	// NonDefaultParams returns a listing of all parameters in the Layer that
	// are not at their default values -- useful for setting param styles etc.
	NonDefaultParams() string

	// AllParams returns a listing of all parameters in the Layer
	AllParams() string

	// WriteWtsJSON writes the weights from this layer from the receiver-side perspective
	// in a JSON text format.  We build in the indentation logic to make it much faster and
	// more efficient.
	WriteWtsJSON(w io.Writer, depth int)

	// ReadWtsJSON reads the weights from this layer from the receiver-side perspective
	// in a JSON text format.
	ReadWtsJSON(r io.Reader) error

	// Build constructs the layer and projection state based on the layer shapes
	// and patterns of interconnectivity
	Build() error

	// VarRange returns the min / max values for given variable
	// over the layer
	VarRange(varNm string) (min, max float32, err error)
}

Layer defines the basic interface for neural network layers, used for managing the structural elements of a network, and for visualization, I/O, etc. Interfaces are automatically pointers -- think of this as a pointer to your specific layer type, with a very basic interface for accessing general structural properties. Nothing algorithm-specific is implemented here -- all of that goes in your specific layer struct.

type LayerType

type LayerType int32

LayerType is the type of the layer: Input, Hidden, Target, Compare. Class parameter styles automatically key off of these types. Specialized algorithms can extend this to other types, but these types encompass most standard neural network models.

const (
	// Hidden is an internal representational layer that does not receive direct input / targets
	Hidden LayerType = iota

	// Input is a layer that receives direct external input in its Ext inputs
	Input

	// Target is a layer that receives direct external target inputs used for driving plus-phase learning
	Target

	// Compare is a layer that receives external comparison inputs, which drive statistics but
	// do NOT drive activation or learning directly
	Compare

	LayerTypeN
)

The layer types

func (*LayerType) FromString

func (i *LayerType) FromString(s string) error

func (LayerType) MarshalJSON

func (ev LayerType) MarshalJSON() ([]byte, error)

func (LayerType) String

func (i LayerType) String() string

func (*LayerType) UnmarshalJSON

func (ev *LayerType) UnmarshalJSON(b []byte) error

type Layers

type Layers []Layer

Layers is a slice of layers

func (*Layers) ElemLabel

func (ls *Layers) ElemLabel(idx int) string

ElemLabel satisfies the gi.SliceLabeler interface to provide labels for slice elements

type Network

type Network interface {
	// InitName MUST be called to initialize the network's pointer to itself as an emer.Network
	// which enables the proper interface methods to be called.  Also sets the name.
	InitName(net Network, name string)

	// Name() returns name of the network
	Name() string

	// Label satisfies the gi.Labeler interface for getting the name of objects generically
	Label() string

	// NLayers returns the number of layers in the network
	NLayers() int

	// Layer returns layer (as emer.Layer interface) at given index -- does not
	// do extra bounds checking
	Layer(idx int) Layer

	// LayerByName returns layer of given name, nil if not found
	LayerByName(name string) Layer

	// LayerByNameTry returns layer of given name, returns error and emits a log message
	// if not found
	LayerByNameTry(name string) (Layer, error)

	// Defaults sets default parameter values for everything in the Network
	Defaults()

	// UpdateParams() updates parameter values for all Network parameters,
	// based on any other params that might have changed.
	UpdateParams()

	// ApplyParams applies given parameter style Sheet to layers and prjns in this network.
	// Calls UpdateParams on anything set to ensure derived parameters are all updated.
	// If setMsg is true, then a message is printed to confirm each parameter that is set.
	// it always prints a message if a parameter fails to be set.
	// returns true if any params were set, and error if there were any errors.
	ApplyParams(pars *params.Sheet, setMsg bool) (bool, error)

	// NonDefaultParams returns a listing of all parameters in the Network that
	// are not at their default values -- useful for setting param styles etc.
	NonDefaultParams() string

	// AllParams returns a listing of all parameters in the Network
	AllParams() string

	// WriteWtsJSON writes network weights (and any other state that adapts with learning)
	// to JSON-formatted output.
	WriteWtsJSON(w io.Writer)

	// ReadWtsJSON reads network weights (and any other state that adapts with learning)
	// from JSON-formatted input.
	ReadWtsJSON(r io.Reader) error

	// SaveWtsJSON saves network weights (and any other state that adapts with learning)
	// to a JSON-formatted file.
	SaveWtsJSON(filename gi.FileName) error

	// OpenWtsJSON opens network weights (and any other state that adapts with learning)
	// from a JSON-formatted file.
	OpenWtsJSON(filename gi.FileName) error

	// NewLayer creates a new concrete layer of appropriate type for this network
	NewLayer() Layer

	// NewPrjn creates a new concrete projection of appropriate type for this network
	NewPrjn() Prjn

	// ConnectLayerNames establishes a projection between two layers, referenced by name
	// adding to the recv and send projection lists on each side of the connection.
	// Returns error if not successful.
	// Does not yet actually connect the units within the layers -- that requires Build.
	ConnectLayerNames(send, recv string, pat prjn.Pattern, typ PrjnType) (rlay, slay Layer, pj Prjn, err error)

	// ConnectLayers establishes a projection between two layers,
	// adding to the recv and send projection lists on each side of the connection.
	// Returns false if not successful. Does not yet actually connect the units within the layers -- that
	// requires Build.
	ConnectLayers(send, recv Layer, pat prjn.Pattern, typ PrjnType) Prjn

	// Bounds returns the minimum and maximum display coordinates of the network for 3D display
	Bounds() (min, max mat32.Vec3)

	// VarRange returns the min / max values for given variable
	VarRange(varNm string) (min, max float32, err error)
}

Network defines the basic interface for a neural network, used for managing the structural elements of a network, and for visualization, I/O, etc

type Prjn

type Prjn interface {
	params.Styler // TypeName, Name, and Class methods for parameter styling

	// Init MUST be called to initialize the prjn's pointer to itself as an emer.Prjn
	// which enables the proper interface methods to be called.
	Init(prjn Prjn)

	// RecvLay returns the receiving layer for this projection
	RecvLay() Layer

	// SendLay returns the sending layer for this projection
	SendLay() Layer

	// Pattern returns the pattern of connectivity for interconnecting the layers
	Pattern() prjn.Pattern

	// Type returns the functional type of projection according to PrjnType (extensible in
	// more specialized algorithms)
	Type() PrjnType

	// SetType sets the functional type of projection according to PrjnType
	SetType(typ PrjnType)

	// Connect sets the basic connection parameters for this projection (send, recv, pattern, and type)
	Connect(send, recv Layer, pat prjn.Pattern, typ PrjnType)

	// SetClass sets CSS-style class name(s) for this projection (space-separated if multiple)
	SetClass(cls string)

	// Label satisfies the gi.Labeler interface for getting the name of objects generically
	Label() string

	// IsOff returns true if projection or either send or recv layer has been turned Off.
	// Useful for experimentation
	IsOff() bool

	// SetOff sets the projection Off status (i.e., lesioned)
	SetOff(off bool)

	// SynVarNames returns the names of all the variables on the synapse
	SynVarNames() []string

	// SynVals returns values of given variable name on synapses
	// for each synapse in the projection using the natural ordering
	// of the synapses (sender based for Leabra).
	// returns nil if variable name invalid -- see also Try version.
	SynVals(varNm string) []float32

	// SynValsTry returns values of given variable name on synapses
	// for each synapse in the projection using the natural ordering
	// of the synapses (sender based for Leabra).
	// returns error message for invalid variable name
	SynValsTry(varNm string) ([]float32, error)

	// SynVal returns value of given variable name on the synapse
	// between given send, recv unit indexes (1D, flat indexes)
	// returns nil if variable name or indexes invalid -- see also Try version.
	SynVal(varnm string, sidx, ridx int) float32

	// SynValTry returns value of given variable name on the synapse
	// between given send, recv unit indexes (1D, flat indexes)
	// returns error for access errors.
	SynValTry(varnm string, sidx, ridx int) (float32, error)

	// SetSynVal sets value of given variable name on the synapse
	// between given send, recv unit indexes (1D, flat indexes)
	// returns error for access errors.
	SetSynVal(varnm string, sidx, ridx int, val float32) error

	// Defaults sets default parameter values for all Prjn parameters
	Defaults()

	// UpdateParams() updates parameter values for all Prjn parameters,
	// based on any other params that might have changed.
	UpdateParams()

	// ApplyParams applies given parameter style Sheet to this projection.
	// Calls UpdateParams if anything set to ensure derived parameters are all updated.
	// If setMsg is true, then a message is printed to confirm each parameter that is set.
	// it always prints a message if a parameter fails to be set.
	// returns true if any params were set, and error if there were any errors.
	ApplyParams(pars *params.Sheet, setMsg bool) (bool, error)

	// NonDefaultParams returns a listing of all parameters in the Projection that
	// are not at their default values -- useful for setting param styles etc.
	NonDefaultParams() string

	// AllParams returns a listing of all parameters in the Projection
	AllParams() string

	// WriteWtsJSON writes the weights from this projection from the receiver-side perspective
	// in a JSON text format.  We build in the indentation logic to make it much faster and
	// more efficient.
	WriteWtsJSON(w io.Writer, depth int)

	// ReadWtsJSON reads the weights from this projection from the receiver-side perspective
	// in a JSON text format.
	ReadWtsJSON(r io.Reader) error

	// Build constructs the full connectivity among the layers as specified in this projection.
	Build() error
}

Prjn defines the basic interface for a projection which connects two layers. Name is set automatically to: SendLay().Name() + "To" + RecvLay().Name()

type PrjnType

type PrjnType int32

PrjnType is the type of the projection (extensible for more specialized algorithms). Class parameter styles automatically key off of these types.

const (
	// Forward is a feedforward, bottom-up projection from sensory inputs to higher layers
	Forward PrjnType = iota

	// Back is a feedback, top-down projection from higher layers back to lower layers
	Back

	// Lateral is a lateral projection within the same layer / area
	Lateral

	// Inhib is an inhibitory projection that drives inhibitory synaptic inputs instead of excitatory
	Inhib

	PrjnTypeN
)

The projection types

func (*PrjnType) FromString

func (i *PrjnType) FromString(s string) error

func (PrjnType) MarshalJSON

func (ev PrjnType) MarshalJSON() ([]byte, error)

func (PrjnType) String

func (i PrjnType) String() string

func (*PrjnType) UnmarshalJSON

func (ev *PrjnType) UnmarshalJSON(b []byte) error

type Prjns

type Prjns []Prjn

Prjns is a slice of projections

func (*Prjns) Add

func (pl *Prjns) Add(p Prjn)

Add adds a projection to the list

func (*Prjns) ElemLabel

func (pl *Prjns) ElemLabel(idx int) string

ElemLabel satisfies the gi.SliceLabeler interface to provide labels for slice elements

func (*Prjns) Recv

func (pl *Prjns) Recv(recv Layer) (Prjn, bool)

Recv finds the projection with given recv layer

func (*Prjns) RecvName

func (pl *Prjns) RecvName(recv string) Prjn

RecvName finds the projection with given recv layer name, nil if not found see Try version for error checking.

func (*Prjns) RecvNameTry

func (pl *Prjns) RecvNameTry(recv string) (Prjn, error)

RecvNameTry finds the projection with given recv layer name. returns error message if not found

func (*Prjns) Send

func (pl *Prjns) Send(send Layer) (Prjn, bool)

Send finds the projection with given send layer

func (*Prjns) SendName

func (pl *Prjns) SendName(sender string) Prjn

SendName finds the projection with given send layer name, nil if not found see Try version for error checking.

func (*Prjns) SendNameTry

func (pl *Prjns) SendNameTry(sender string) (Prjn, error)

SendNameTry finds the projection with given send layer name. returns error message if not found

Jump to

Keyboard shortcuts

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