env

package
v2.0.0-dev0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: BSD-3-Clause Imports: 13 Imported by: 27

README

Docs: GoDoc

See Wiki Env page for detailed docs.

Package env defines the Env interface for environments, which determine the nature and sequence of States that can be used as inputs to a model and it can also accept Action responses from the model that affect how the environment evolves in the future.

By adhering to this interface, it is then easier to mix-and-match environments with models.

Env / Agent

The overall division of labor is that the model keeps track of the outer-most Run time-scale depending on its own parameters and learning trajectory and the environment is responsible for generating patterns for each run.

Multiple different environments will typically be used in a model, e.g., one for training and other(s) for testing. Even if these envs all share a common database of patterns, a different Env should be used for each case where different counters and sequences of events etc are presented, which keeps them from interfering with each other. Also, the table.IndexView can be used to allow multiple different Env's to all present different indexed views into a shared common table.Table (e.g., train / test splits). The basic FixedTable env implementation uses this.

Thus, the Env encapsulates all of the counter management logic for each aspect of model training and testing, so that the model itself just needs to manage which Env to use, when, and manage the connection of the Env States as inputs to the model, and vice-versa for Actions on the Env coming from the model.

With the newer looper framework, the counters are managed by looper independent of the env.

There is also an Envs map that provides a basic container for managing multiple Envs -- the key is typically an etime.Modes e.g., etime.Train or etime.Test.

The EnvDesc interface provides additional methods (originally included in Env) that describe the Counters, States, and Actions, of the Env. Each Element of the overall State allows annotation about the different elements of state that are available in general.

The Step should update all relevant state elements as appropriate, so these can be queried by the user. Particular paradigms of environments must establish naming conventions for these state elements which then allow the model to use the information appropriately -- the Env interface only provides the most basic framework for establishing these paradigms, and ultimately a given model will only work within a particular paradigm of environments following specific conventions.

See e.g., env.FixedTable for particular implementation of a fixed Table of patterns, for one example of a widely used paradigm.

Typically each specific implementation of this Env interface will have multiple parameters etc that can be modified to control env behavior -- all of this is paradigm-specific and outside the scope of this basic interface.

Documentation

Overview

Package env defines an interface for environments, which determine the nature and sequence of States that can be used as inputs to a model and it can also accept Action responses from the model that affect how the enviroment evolves in the future.

By adhering to this interface, it is then easier to mix-and-match environments with models.

The overall division of labor is that the model keeps track of the outer-most Run time-scale depending on its own parameters and learning trajectory and the environment is responsible for generating patterns for each run.

Multiple different environments will typically be used in a model, e.g., one for training and other(s) for testing. Even if these envs all share a common database of patterns, a different Env should be used for each case where different counters and sequences of events etc are presented, which keeps them from interfering with each other. Also, the table.IndexView can be used to allow multiple different Env's to all present different indexed views into a shared common table.Table (e.g., train / test splits). The basic FixedTable env implementation uses this.

Thus, the Env encapsulates all of the counter management logic for each aspect of model training and testing, so that the model itself just needs to manange which Env to use, when, and manage the connection of the Env States as inputs to the model, and vice-versa for Actions on the Env coming from the model.

Each Element of the overall State allows annotation about the different elements of state that are available in general, and the `Step` should update all relevant state elements as appropriate, so these can be queried by the user. Particular paradigms of environments must establish naming conventions for these state elements which then allow the model to use the information appropriately -- the Env interface only provides the most basic framework for establishing these paradigms, and ultimately a given model will only work within a particular paradigm of environments following specific conventions.

See e.g., env.FixedTable for particular implementation of a fixed Table of patterns, for one example of a widely used paradigm.

Typically each specific implementation of this Env interface will have multiple parameters etc that can be modified to control env behavior -- all of this is paradigm-specific and outside the scope of this basic interface.

See the emergent github wiki for more info: https://github.com/emer/emergent/v2/wiki/Env

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ModeDi

func ModeDi(mode etime.Modes, di int) string

ModeDi returns the string of the given mode appended with _di data index with leading zero.

Types

type Counter

type Counter struct {

	// current counter value
	Cur int

	// previous counter value, prior to last Incr() call (init to -1)
	Prv int `display:"-"`

	// did this change on the last Step() call or not?
	Chg bool `display:"-"`

	// where relevant, this is a fixed maximum counter value, above which the counter will reset back to 0 -- only used if > 0
	Max int

	// the unit of time scale represented by this counter (just FYI)
	Scale etime.Times `display:"-"`
}

Counter is a counter that counts increments at a given time scale. It keeps track of when it has been incremented or not, and retains the previous value.

func (*Counter) Incr

func (ct *Counter) Incr() bool

Incr increments the counter by 1. If Max > 0 then if Incr >= Max the counter is reset to 0 and true is returned. Otherwise false.

func (*Counter) Init

func (ct *Counter) Init()

Init initializes counter -- Cur = 0, Prv = -1

func (*Counter) Query

func (ct *Counter) Query() (cur, prv int, chg bool)

Query returns the current, previous and changed values for this counter

func (*Counter) Same

func (ct *Counter) Same()

Same resets Chg = false -- good idea to call this on all counters at start of Step or can put in an else statement, but that is more error-prone.

func (*Counter) Set

func (ct *Counter) Set(cur int) bool

Set sets the Cur value if different from Cur, while preserving previous value and setting Chg appropriately. Returns true if changed. does NOT check Cur vs. Max.

type Counters

type Counters struct {

	// ordered list of the counter timescales, from outer-most (highest) to inner-most (lowest)
	Order []etime.Times

	// map of the counters by timescale
	Counters map[etime.Times]*Counter
}

Counters contains an ordered slice of timescales, and a lookup map of counters by timescale used to manage counters in the Env.

func (*Counters) ByScope

func (cs *Counters) ByScope(tm etime.Times) *Counter

ByTime returns counter by timescale key -- nil if not found

func (*Counters) ByTime

func (cs *Counters) ByTime(tm etime.Times) (*Counter, error)

ByTime returns counter by timescale key. returns nil, error if not found.

func (*Counters) CountersToStats

func (cs *Counters) CountersToStats(mode string, stats *estats.Stats)

CountersToStats sets the current counter values to estats Int values by their time names only (no eval Mode).

func (*Counters) Init

func (cs *Counters) Init()

Init does Init on all the counters

func (*Counters) SetTimes

func (cs *Counters) SetTimes(mode string, times ...etime.Times)

SetTimes initializes Counters for given mode and list of times ordered from highest to lowest

type CurPrvF32

type CurPrvF32 struct {

	// current value
	Cur float32

	// previous value
	Prv float32
}

CurPrvF32 is basic state management for current and previous values, float32 values

func (*CurPrvF32) Diff

func (cv *CurPrvF32) Diff() float32

Diff returns the difference between current and previous values

func (*CurPrvF32) Incr

func (cv *CurPrvF32) Incr()

Incr increments Cur by 1

func (*CurPrvF32) Set

func (cv *CurPrvF32) Set(cur float32)

Set sets the new current value, copying Cur to Prv

type CurPrvInt

type CurPrvInt struct {

	// current value
	Cur int

	// previous value
	Prv int
}

CurPrvInt is basic state management for current and previous values, int values

func (*CurPrvInt) Diff

func (cv *CurPrvInt) Diff() int

Diff returns the difference between current and previous values

func (*CurPrvInt) Incr

func (cv *CurPrvInt) Incr()

Incr increments Cur by 1

func (*CurPrvInt) Set

func (cv *CurPrvInt) Set(cur int)

Set sets the new current value, copying Cur to Prv

type CurPrvString

type CurPrvString struct {

	// current value
	Cur string

	// previous value
	Prv string
}

CurPrvString is basic state management for current and previous values, string values

func (*CurPrvString) Set

func (cv *CurPrvString) Set(cur string)

Set sets the new current value, copying Cur to Prv

type Element

type Element struct {

	// name of this element -- must be unique
	Name string

	// shape of the tensor for this element -- each element should generally have a well-defined consistent shape to enable the model to process it consistently
	Shape []int

	// names of the dimensions within the Shape -- optional but useful for ensuring correct usage
	DimNames []string
}

Element specifies one element of State or Action in an environment

type Elements

type Elements []Element

Elements is a list of Element info

type Env

type Env interface {
	labels.Labeler

	// Init initializes the environment for a given run of the model.
	// The environment may not care about the run number, but may implement
	// different parameterizations for different runs (e.g., between-subject
	// manipulations).  In general the Env can expect that the model will likely
	// have established a different random seed per run, prior to calling this
	// method, and that may be sufficient to enable different run-level behavior.
	// All other initialization / updating beyond this outer-most Run level must
	// be managed internally by the Env itself, and the model can query the
	// Counter state information to determine when things have updated at different
	// time scales.  See Step() for important info about state of env after Init
	// but prior to first Step() call.
	Init(run int)

	// Step generates the next step of environment state.
	// This is the main API for how the model interacts with the environment.
	// The env should update all other levels of state internally over
	// repeated calls to the Step method.
	// If there are no further inputs available, it returns false (most envs
	// typically only return true and just continue running as long as needed).
	//
	// The Env thus always reflects the *current* state of things, and this
	// call increments that current state, such that subsequent calls to
	// State() will return this current state.
	//
	// This implies that the state just after Init and prior to first Step
	// call should be an *initialized* state that then allows the first Step
	// call to establish the proper *first* state.  Typically this means that
	// one or more counters will be set to -1 during Init and then get incremented
	// to 0 on the first Step call.
	Step() bool

	// State returns the given element's worth of tensor data from the environment
	// based on the current state of the env, as a function of having called Step().
	// If no output is available on that element, then nil is returned.
	// The returned tensor must be treated as read-only as it likely points to original
	// source data -- please make a copy before modifying (e.g., Clone() methdod).
	State(element string) tensor.Tensor

	// Action sends tensor data about e.g., responses from model back to act
	// on the environment and influence its subsequent evolution.
	// The nature and timing of this input is paradigm dependent.
	Action(element string, input tensor.Tensor)
}

Env defines an interface for environments, which determine the nature and sequence of States that can be used as inputs to a model, and the Env also can accept Action responses from the model that affect state evolution.

The Env manages Counter values to advance the temporal state of the environment, using etime.Times standard intervals.

State is comprised of one or more Elements, each of which consists of an tensor.Tensor chunk of values that can be obtained by the model. Likewise, Actions can also have Elements. The Step method is the main interface for advancing the Env state. Counters should be queried after calling Step to see if any relevant values have changed, to trigger functions in the model (e.g., logging of prior statistics, etc).

Typically each specific implementation of this Env interface will have multiple parameters etc that can be modified to control env behavior -- all of this is paradigm-specific and outside the scope of this basic interface.

type Envs

type Envs map[string]Env

Envs is a map of environments organized according to the evaluation mode string (recommended key value)

func (*Envs) Add

func (es *Envs) Add(evs ...Env)

Add adds Env(s), using its Label as the key

func (*Envs) ByMode

func (es *Envs) ByMode(mode etime.Modes) Env

ByMode returns env by etime.Modes evaluation mode as the map key. returns nil if not found

func (*Envs) ByModeDi

func (es *Envs) ByModeDi(mode etime.Modes, di int) Env

ByModeDi returns env by etime.Modes evaluation mode and data parallel index as the map key, using ModeDi function. returns nil if not found

func (*Envs) Init

func (es *Envs) Init()

Init initializes the map if not yet

type FixedTable

type FixedTable struct {
	// name of this environment, usually Train vs. Test.
	Name string

	// this is an indexed view of the table with the set of patterns to output.
	// The indexes are used for the *sequential* view so you can easily
	// sort / split / filter the patterns to be presented using this view.
	// This adds the random permuted Order on top of those if !sequential.
	Table *table.IndexView

	// present items from the table in sequential order (i.e., according to
	// the indexed view on the Table)?  otherwise permuted random order.
	Sequential bool

	// permuted order of items to present if not sequential.
	// updated every time through the list.
	Order []int

	// current ordinal item in Table. if Sequential then = row number in table,
	// otherwise is index in Order list that then gives row number in Table.
	Trial Counter `display:"inline"`

	// if Table has a Name column, this is the contents of that.
	TrialName CurPrvString

	// if Table has a Group column, this is contents of that.
	GroupName CurPrvString

	// name of the Name column -- defaults to 'Name'.
	NameCol string

	// name of the Group column -- defaults to 'Group'.
	GroupCol string
}

FixedTable is a basic Env that manages patterns from an table.Table, with either sequential or permuted random ordering, with the Trial counters to record progress and iterations through the table. It uses an IndexView indexed view of the Table, so a single shared table can be used across different environments, with each having its own unique view.

func (*FixedTable) Action

func (ft *FixedTable) Action(element string, input tensor.Tensor)

func (*FixedTable) Config

func (ft *FixedTable) Config(tbl *table.IndexView)

Config configures the environment to use given table IndexView and evaluation mode (e.g., etime.Train.String()). If mode is Train then a Run counter is added, otherwise just Epoch and Trial. NameCol and GroupCol are initialized to "Name" and "Group" so set these to something else after this if needed.

func (*FixedTable) Init

func (ft *FixedTable) Init(run int)

func (*FixedTable) Label

func (ft *FixedTable) Label() string

func (*FixedTable) NewOrder

func (ft *FixedTable) NewOrder()

NewOrder sets a new random Order based on number of rows in the table.

func (*FixedTable) PermuteOrder

func (ft *FixedTable) PermuteOrder()

PermuteOrder permutes the existing order table to get a new random sequence of inputs just calls: randx.PermuteInts(ft.Order)

func (*FixedTable) Row

func (ft *FixedTable) Row() int

Row returns the current row number in table, based on Sequential / perumuted Order and already de-referenced through the IndexView's indexes to get the actual row in the table.

func (*FixedTable) SetGroupName

func (ft *FixedTable) SetGroupName()

func (*FixedTable) SetTrialName

func (ft *FixedTable) SetTrialName()

func (*FixedTable) State

func (ft *FixedTable) State(element string) tensor.Tensor

func (*FixedTable) Step

func (ft *FixedTable) Step() bool

func (*FixedTable) Validate

func (ft *FixedTable) Validate() error

type FreqTable

type FreqTable struct {

	// name of this environment
	Name string

	// this is an indexed view of the table with the set of patterns to output -- the indexes are used for the *sequential* view so you can easily sort / split / filter the patterns to be presented using this view -- we then add the random permuted Order on top of those if !sequential
	Table *table.IndexView

	// number of samples to use in constructing the list of items to present according to frequency -- number per epoch ~ NSamples * Freq -- see RandSamp option
	NSamples float64

	// if true, use random sampling of items NSamples times according to given Freq probability value -- otherwise just directly add NSamples * Freq items to the list
	RandSamp bool

	// present items from the table in sequential order (i.e., according to the indexed view on the Table)?  otherwise permuted random order.  All repetitions of given item will be sequential if Sequential
	Sequential bool

	// list of items to present, with repetitions -- updated every time through the list
	Order []int

	// current ordinal item in Table -- if Sequential then = row number in table, otherwise is index in Order list that then gives row number in Table
	Trial Counter `display:"inline"`

	// if Table has a Name column, this is the contents of that
	TrialName CurPrvString

	// if Table has a Group column, this is contents of that
	GroupName CurPrvString

	// name of the Name column -- defaults to 'Name'
	NameCol string

	// name of the Group column -- defaults to 'Group'
	GroupCol string

	// name of the Freq column -- defaults to 'Freq'
	FreqCol string
}

FreqTable is an Env that manages patterns from an table.Table with frequency information so that items are presented according to their associated frequencies which are effectively probabilities of presenting any given input -- must have a Freq column with these numbers in the table (actual col name in FreqCol). Either sequential or permuted random ordering is supported, with std Trial / Epoch TimeScale counters to record progress and iterations through the table. It also records the outer loop of Run as provided by the model. It uses an IndexView indexed view of the Table, so a single shared table can be used across different environments, with each having its own unique view.

func (*FreqTable) Action

func (ft *FreqTable) Action(element string, input tensor.Tensor)

func (*FreqTable) Init

func (ft *FreqTable) Init(run int)

func (*FreqTable) Label

func (ft *FreqTable) Label() string

func (*FreqTable) Row

func (ft *FreqTable) Row() int

Row returns the current row number in table, based on Sequential / perumuted Order and already de-referenced through the IndexView's indexes to get the actual row in the table.

func (*FreqTable) Sample

func (ft *FreqTable) Sample()

Sample generates a new sample of items

func (*FreqTable) SetGroupName

func (ft *FreqTable) SetGroupName()

func (*FreqTable) SetTrialName

func (ft *FreqTable) SetTrialName()

func (*FreqTable) State

func (ft *FreqTable) State(element string) tensor.Tensor

func (*FreqTable) Step

func (ft *FreqTable) Step() bool

func (*FreqTable) Validate

func (ft *FreqTable) Validate() error

type MPIFixedTable

type MPIFixedTable struct {

	// name of this environment
	Name string

	// this is an indexed view of the table with the set of patterns to output -- the indexes are used for the *sequential* view so you can easily sort / split / filter the patterns to be presented using this view -- we then add the random permuted Order on top of those if !sequential
	Table *table.IndexView

	// present items from the table in sequential order (i.e., according to the indexed view on the Table)?  otherwise permuted random order
	Sequential bool

	// permuted order of items to present if not sequential -- updated every time through the list
	Order []int

	// current ordinal item in Table -- if Sequential then = row number in table, otherwise is index in Order list that then gives row number in Table
	Trial Counter `display:"inline"`

	// if Table has a Name column, this is the contents of that
	TrialName CurPrvString

	// if Table has a Group column, this is contents of that
	GroupName CurPrvString

	// name of the Name column -- defaults to 'Name'
	NameCol string

	// name of the Group column -- defaults to 'Group'
	GroupCol string

	// for MPI, trial we start each epoch on, as index into Order
	TrialSt int

	// for MPI, trial number we end each epoch before (i.e., when ctr gets to Ed, restarts)
	TrialEd int
}

MPIFixedTable is an MPI-enabled version of the FixedTable, which is a basic Env that manages patterns from an table.Table, with either sequential or permuted random ordering, and uses standard Trial Time counter to record iterations through the table. It uses an IndexView indexed view of the Table, so a single shared table can be used across different environments, with each having its own unique view. The MPI version distributes trials across MPI procs, in the Order list. It is ESSENTIAL that the number of trials (rows) in Table is evenly divisible by number of MPI procs! If all nodes start with the same seed, it should remain synchronized.

func (*MPIFixedTable) Action

func (ft *MPIFixedTable) Action(element string, input tensor.Tensor)

func (*MPIFixedTable) Init

func (ft *MPIFixedTable) Init(run int)

func (*MPIFixedTable) Label

func (ft *MPIFixedTable) Label() string

func (*MPIFixedTable) NewOrder

func (ft *MPIFixedTable) NewOrder()

NewOrder sets a new random Order based on number of rows in the table.

func (*MPIFixedTable) PermuteOrder

func (ft *MPIFixedTable) PermuteOrder()

PermuteOrder permutes the existing order table to get a new random sequence of inputs just calls: randx.PermuteInts(ft.Order)

func (*MPIFixedTable) Row

func (ft *MPIFixedTable) Row() int

Row returns the current row number in table, based on Sequential / perumuted Order and already de-referenced through the IndexView's indexes to get the actual row in the table.

func (*MPIFixedTable) SetGroupName

func (ft *MPIFixedTable) SetGroupName()

func (*MPIFixedTable) SetTrialName

func (ft *MPIFixedTable) SetTrialName()

func (*MPIFixedTable) State

func (ft *MPIFixedTable) State(element string) tensor.Tensor

func (*MPIFixedTable) Step

func (ft *MPIFixedTable) Step() bool

func (*MPIFixedTable) Validate

func (ft *MPIFixedTable) Validate() error

Jump to

Keyboard shortcuts

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