patgen

package
v2.0.0-dev0.0.14 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: BSD-3-Clause Imports: 13 Imported by: 9

README

Docs: GoDoc

Package patgen contains functions that generate patterns, typically based on various constrained-forms of random patterns, e.g., permuted binary random patterns.

Pattern Generator

Pattern generator is capable of flexibly making patterns for models. To configure your own patterns, follow these four steps:

  1. make your vocabulary as a pool name -- tensor map;
  2. use your vocabulary to initialize a big pattern (e.g., TrainAB), in which you later use to store input & output patterns;
  3. mix different pools in the vocabulary into one pattern (e.g., A+B+context-->input pattern), which is stored in the big pattern;
  4. repeat 3) for the output pattern.

Example code could be found in ConfigPats() in hip bench.

Documentation

Overview

Package patgen contains functions that generate patterns, typically based on various constrained-forms of random patterns

Index

Constants

This section is empty.

Variables

View Source
var (
	// RandSource is a random source to use for all random numbers used in patgen
	// By default it just uses the standard Go math/rand source.
	// If initialized, e.g., by calling NewRand(seed), then a separate stream of
	// random numbers will be generated for all patgen calls, and the
	// seed is saved as RandSeed -- it can be reinstated by calling RestoreSeed.
	// Can also set RandSource to another existing erand.Rand source to use it.
	RandSource = &erand.SysRand{}

	// Random seed last set by NewRand or SetRandSeed.
	RandSeed int64
)
View Source
var MinDiffPrintIters = false

MinDiffPrintIters set this to true to see the iteration stats for PermutedBinaryMinDiff -- for large, long-running cases.

Functions

func AddVocabClone

func AddVocabClone(mp Vocab, name string, copyFrom string) (*etensor.Float32, error)

AddVocabClone clones an existing pool in the vocabulary to make a new one.

func AddVocabDrift

func AddVocabDrift(mp Vocab, name string, rows int, pctDrift float32, copyFrom string, copyRow int) (*etensor.Float32, error)

AddVocabDrift adds a row-by-row drifting pool to the vocabulary, starting from the given row in existing vocabulary item (which becomes starting row in this one -- drift starts in second row). The current row patterns are generated by taking the previous row pattern and flipping pctDrift percent of active bits (min of 1 bit).

func AddVocabEmpty

func AddVocabEmpty(mp Vocab, name string, rows, poolY, poolX int) (*etensor.Float32, error)

AddVocabEmpty adds an empty pool to the vocabulary. This can be used to make test cases with missing pools.

func AddVocabPermutedBinary

func AddVocabPermutedBinary(mp Vocab, name string, rows, poolY, poolX int, pctAct, minPctDiff float32) (*etensor.Float32, error)

AddVocabPermutedBinary adds a permuted binary pool to the vocabulary. This is a good source of random patterns with no systematic similarity. pctAct = proportion (0-1) bits turned on for a pool. minPctDiff = proportion of pctAct (0-1) for minimum difference between patterns -- e.g., .5 = each pattern must have half of its bits different from each other. This constraint can be hard to meet depending on the number of rows, amount of activity, and minPctDiff level -- an error will be returned and printed if it cannot be satisfied in a reasonable amount of time.

func AddVocabRepeat

func AddVocabRepeat(mp Vocab, name string, rows int, copyFrom string, copyRow int) (*etensor.Float32, error)

AddVocabRepeat adds a repeated pool to the vocabulary, copying from given row in existing vocabulary item .

func FlipBits

func FlipBits(tsr etensor.Tensor, nOff, nOn int, onVal, offVal float64)

FlipBits turns nOff bits that are currently On to Off and nOn bits that are currently Off to On, using permuted lists.

func FlipBitsRows

func FlipBitsRows(tsr etensor.Tensor, nOff, nOn int, onVal, offVal float64)

FlipBitsRows turns nOff bits that are currently On to Off and nOn bits that are currently Off to On, using permuted lists. Iterates over the outer-most tensor dimension as rows.

func InitPats

func InitPats(dt *etable.Table, name, desc, inputName, outputName string, listSize, ySize, xSize, poolY, poolX int)

InitPats initiates patterns to be used in MixPats

func MixPats

func MixPats(dt *etable.Table, mp Vocab, colName string, poolSource []string) error

MixPats mixes patterns using first listSize rows in the vocabulary map poolSource order: left right, bottom up

func MixPatsN

func MixPatsN(dt *etable.Table, mp Vocab, colName string, poolSource []string, targRow, vocabStart, vocabN int) error

MixPatsN mixes patterns using specified startVocab and vocabN numbers of vocabulary patterns, inserting starting at specified targRow in table. poolSource order: left right, bottom up

func NFromPct

func NFromPct(pct float32, n int) int

NFromPct returns the number of bits for given pct (proportion 0-1), relative to total n. uses math.Round.

func NOnInTensor

func NOnInTensor(trow *etensor.Float32) int

NOnInTensor returns the number of bits active in given tensor

func NewRand

func NewRand(seed int64)

NewRand sets RandSource to a new separate random number stream using given seed, which is saved as RandSeed -- see RestoreSeed.

func PctActInTensor

func PctActInTensor(trow *etensor.Float32) float32

PctActInTensor returns the percent activity in given tensor (NOn / size)

func PermutedBinary

func PermutedBinary(tsr etensor.Tensor, nOn int, onVal, offVal float64)

PermutedBinary sets the given tensor to contain nOn onVal values and the remainder are offVal values, using a permuted order of tensor elements (i.e., randomly shuffled or permuted).

func PermutedBinaryMinDiff

func PermutedBinaryMinDiff(tsr *etensor.Float32, nOn int, onVal, offVal float32, minDiff int) error

PermutedBinaryMinDiff treats the tensor as a column of rows as in a etable.Table and sets each row to contain nOn onVal values and the remainder are offVal values, using a permuted order of tensor elements (i.e., randomly shuffled or permuted). This version ensures that all patterns have at least a given minimum distance from each other, expressed using minDiff = number of bits that must be different (can't be > nOn). If the mindiff constraint cannot be met within a reasonable number of iterations, then an error is returned.

func PermutedBinaryRows

func PermutedBinaryRows(tsr etensor.Tensor, nOn int, onVal, offVal float64)

PermutedBinaryRows treats the tensor as a column of rows as in a etable.Table and sets each row to contain nOn onVal values and the remainder are offVal values, using a permuted order of tensor elements (i.e., randomly shuffled or permuted).

func ReshapeCpp

func ReshapeCpp(dt *etable.Table)

ReshapeCpp fixes C++ emergent table shape which is reversed from Go. it switches the dimension order in the given table, for all columns that are float 2D or 4D columns -- assumes these are layer patterns and names dimensions accordingly.

func ReshapeCppFile

func ReshapeCppFile(dt *etable.Table, fname, fixnm string)

ReshapeCppFile fixes C++ emergent table shape which is reversed from Go. It loads file from fname and saves to fixnm

func RestoreSeed

func RestoreSeed()

RestoreSeed restores the random seed last used -- random number sequence will repeat what was generated from that point onward.

func RowVsPrevDist32

func RowVsPrevDist32(tsr *etensor.Float32, row int, fun metric.Func32) (min, max float32)

RowVsPrevDist32 returns the minimum and maximum distance between the given row in tensor and all previous rows. Row must be >= 1 and < total rows. (outer-most dimension is row, as in columns of etable.Table).

func SetRandSeed

func SetRandSeed(seed int64)

SetRandSeed sets existing random number stream to use given random seed, starting from the next call. Saves the seed in RandSeed -- see RestoreSeed.

func Shuffle

func Shuffle(dt *etable.Table, rows []int, colNames []string, colIndependent bool)

Shuffle shuffles rows in specified columns in the table independently

func VocabConcat

func VocabConcat(mp Vocab, newPool string, frmPools []string) error

VocabConcat contatenates several pools in the vocabulary and store it into newPool (could be one of the previous pools).

func VocabShuffle

func VocabShuffle(mp Vocab, shufflePools []string)

VocabShuffle shuffles a pool in the vocabulary on its first dimension (row).

func VocabSlice

func VocabSlice(mp Vocab, frmPool string, newPools []string, sliceOffs []int) error

VocabSlice slices a pool in the vocabulary into new ones. SliceOffs is the cutoff points in the original pool, should have one more element than newPools.

Types

type Vocab

type Vocab map[string]*etensor.Float32

Vocab is a map of named tensors that contain patterns used for creating larger patterns by mixing together.

func (Vocab) ByNameTry

func (vc Vocab) ByNameTry(name string) (*etensor.Float32, error)

ByNameTry looks for vocabulary item of given name, and returns (and logs) error message if not found

Jump to

Keyboard shortcuts

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