def State(ev, element): if element == "Image": return ev.Vis.ImgTsr if element == "LGN": return ev.Vis.OutTsr if element == "LGNon": return ev.Vis.OutTsr.SubSpace(go.Slice_int([0])) if element == "LGNoff": return ev.Vis.OutTsr.SubSpace(go.Slice_int([1])) return go.nil
def SetN(ev, n): """ SetN initializes env for given number of options, and inits states """ ev.N = n ev.P = [0.0] * n ev.Input.SetShape(go.Slice_int([n]), go.nil, go.Slice_string(["N"])) ev.Reward.SetShape(go.Slice_int([1]), go.nil, go.Slice_string(["1"])) if ev.RewVal == 0: ev.RewVal = 1
def SetNStim(ev, n): """ SetNStim initializes env for given number of stimuli, init states """ ev.NStim = n ev.Input.SetShape(go.Slice_int([Actions.ActionsN.value + n]), go.nil, go.Slice_string(["N"])) ev.Output.SetShape(go.Slice_int([n]), go.nil, go.Slice_string(["N"])) ev.Reward.SetShape(go.Slice_int([1]), go.nil, go.Slice_string(["1"])) if ev.RewVal == 0: ev.RewVal = 1
def PrjnPlot(ss): ss.TestAll() rvec0 = ss.ValsTsr("rvec0") rvec1 = ss.ValsTsr("rvec1") rvec0.SetShape(go.Slice_int([256]), go.nil, go.nil) rvec1.SetShape(go.Slice_int([256]), go.nil, go.nil) for i in range(256): rvec0.Values[i] = .15 * (2*rand.Float32() - 1) rvec1.Values[i] = .15 * (2*rand.Float32() - 1) tst = ss.TstTrlLog nr = tst.Rows dt = ss.PrjnTable ss.ConfigPrjnTable(dt) for r in range(nr): emote = 0.5*tst.CellTensorFloat1D("Emotion", r, 0) + -0.5*tst.CellTensorFloat1D("Emotion", r, 1) emote += .1 * (2*rand.Float64() - 1) gend = 0.5*tst.CellTensorFloat1D("Gender", r, 0) + -0.5*tst.CellTensorFloat1D("Gender", r, 1) gend += .1 * (2*rand.Float64() - 1) # some jitter so labels are readable input = etensor.Float32(tst.CellTensor("Input", r)) rprjn0 = metric.InnerProduct32(rvec0.Values, input.Values) rprjn1 = metric.InnerProduct32(rvec1.Values, input.Values) dt.SetCellFloat("Trial", r, tst.CellFloat("Trial", r)) dt.SetCellString("TrialName", r, tst.CellString("TrialName", r)) dt.SetCellFloat("GendPrjn", r, gend) dt.SetCellFloat("EmotePrjn", r, emote) dt.SetCellFloat("RndPrjn0", r, float(rprjn0)) dt.SetCellFloat("RndPrjn1", r, float(rprjn1)) plt = ss.PrjnRandom plt.InitName(plt, "PrjnRandom") plt.Params.Title = "Face Random Prjn Plot" plt.Params.XAxisCol = "RndPrjn0" plt.SetTable(dt) plt.Params.Lines = False plt.Params.Points = True # order of params: on, fixMin, min, fixMax, max plt.SetColParams("TrialName", eplot.On, eplot.FixMin, 0, eplot.FloatMax, 0) plt.SetColParams("RndPrjn0", eplot.Off, eplot.FixMin, -1, eplot.FixMax, 1) plt.SetColParams("RndPrjn1", eplot.On, eplot.FixMin, -1, eplot.FixMax, 1) plt = ss.PrjnEmoteGend plt.InitName(plt, "PrjnEmoteGend") plt.Params.Title = "Face Emotion / Gender Prjn Plot" plt.Params.XAxisCol = "GendPrjn" plt.SetTable(dt) plt.Params.Lines = False plt.Params.Points = True # order of params: on, fixMin, min, fixMax, max plt.SetColParams("TrialName", eplot.On, eplot.FixMin, 0, eplot.FloatMax, 0) plt.SetColParams("GendPrjn", eplot.Off, eplot.FixMin, -1, eplot.FixMax, 1) plt.SetColParams("EmotePrjn", eplot.On, eplot.FixMin, -1, eplot.FixMax, 1)
def ConfigPats(ss): dt = ss.Pats sch = etable.Schema( [etable.Column("Name", etensor.STRING, go.nil, go.nil), etable.Column("Input", etensor.FLOAT32, go.Slice_int([4, 5]), go.Slice_string(["Y", "X"])), etable.Column("Output", etensor.FLOAT32, go.Slice_int([4, 5]), go.Slice_string(["Y", "X"]))] ) dt.SetFromSchema(sch, 3) ss.Pats.SetMetaData("name", "Pats") ss.Pats.SetMetaData("desc", "Training patterns") patgen.PermutedBinaryRows(dt.Cols[1], 6, 1, 0) patgen.PermutedBinaryRows(dt.Cols[2], 6, 1, 0) cn = etensor.String(dt.Cols[0]) cn.Values.copy(["any", "baker", "cheese"])
def Init(ev, run): ev.Input.SetShape(go.Slice_int([3, ev.TotTime]), go.nil, go.Slice_string(["3", "TotTime"])) ev.Reward.SetShape(go.Slice_int([1]), go.nil, go.Slice_string(["1"])) ev.Run.Scale = env.Run ev.Epoch.Scale = env.Epoch ev.Trial.Scale = env.Trial ev.Event.Scale = env.Event ev.Run.Init() ev.Epoch.Init() ev.Trial.Init() ev.Event.Init() ev.Run.Cur = run ev.Event.Max = ev.TotTime ev.Event.Cur = -1 ev.TrialUpdt()
def Numpy(ss): """ test conversions to / from numpy """ dt = ss.Pats print("\n\n##############################") print("to / from numpy") etf = etensor.Float32(dt.Cols[1]) npf = pyet.etensor_to_numpy(etf) print(npf) ctf = pyet.numpy_to_etensor(npf) print(ctf) etu32 = etensor.NewUint32(go.Slice_int([3,4,5]), go.nil, go.nil) sz = etf.Len() for i in range(sz): etu32.Values[i] = int(etf.Values[i]) print(etu32) npu32 = pyet.etensor_to_numpy(etu32) print(npu32) ctu32 = pyet.numpy_to_etensor(npu32) print(ctu32) pyet.copy_etensor_to_numpy(npu32, etu32) pyet.copy_numpy_to_etensor(etu32, npu32) ets = etensor.String(dt.Cols[0]) nps = pyet.etensor_to_numpy(ets) print(nps) cts = pyet.numpy_to_etensor(nps) print(cts) pyet.copy_etensor_to_numpy(nps, ets) pyet.copy_numpy_to_etensor(ets, nps) etb = etensor.NewBits(go.Slice_int([3,4,5]), go.nil, go.nil) sz = etb.Len() for i in range(sz): etb.Set1D(i, erand.BoolProb(.2, -1)) print(etb) npb = pyet.etensor_to_numpy(etb) print(npb) ctb = pyet.numpy_to_etensor(npb) print(ctb) pyet.copy_etensor_to_numpy(npb, etb) pyet.copy_numpy_to_etensor(etb, npb)
def V1All(vi): """ V1All aggregates all the relevant simple and complex features into the V1AllTsr which is used for input to a network """ ny = vi.V1sPoolTsr.Dim(0) nx = vi.V1sPoolTsr.Dim(1) nang = vi.V1sPoolTsr.Dim(3) nrows = 5 oshp = go.Slice_int([ny, nx, nrows, nang]) if not etensor.EqualInts(oshp, vi.V1AllTsr.Shp): vi.V1AllTsr.SetShape( oshp, go.nil, go.Slice_string(["Y", "X", "Polarity", "Angle"])) vfilter.FeatAgg(go.Slice_int([0]), 0, vi.V1cLenSumTsr, vi.V1AllTsr) vfilter.FeatAgg(go.Slice_int([0, 1]), 1, vi.V1cEndStopTsr, vi.V1AllTsr) vfilter.FeatAgg(go.Slice_int([0, 1]), 3, vi.V1sPoolTsr, vi.V1AllTsr)
def ConfigPats(ss): dt = ss.Pats dt.SetMetaData("name", "TrainPats") dt.SetMetaData("desc", "Training patterns") sch = etable.Schema([ etable.Column("Name", etensor.STRING, go.nil, go.nil), etable.Column("Input", etensor.FLOAT32, go.Slice_int([10, 10]), go.Slice_string(["Y", "X"])) ]) dt.SetFromSchema(sch, 1) patgen.PermutedBinaryRows(dt.Cols[1], int(ss.InputPct), 1, 0)
def Init(ev, run): ev.Draw.Init() ev.Run.Scale = env.Run ev.Epoch.Scale = env.Epoch ev.Trial.Scale = env.Trial ev.Run.Init() ev.Epoch.Init() ev.Trial.Init() ev.Run.Cur = run ev.Trial.Cur = -1 # init state -- key so that first Step() = 0 ev.Output.SetShape(go.Slice_int([4, 5]), go.nil, go.Slice_string(["Y", "X"]))
def ConfigTrnEpcLog(ss, dt): dt.SetMetaData("name", "TrnEpcLog") dt.SetMetaData("desc", "Record of performance over epochs of training") dt.SetMetaData("read-only", "true") dt.SetMetaData("precision", str(LogPrec)) sch = etable.Schema( [etable.Column("Run", etensor.INT64, go.nil, go.nil), etable.Column("Epoch", etensor.INT64, go.nil, go.nil), etable.Column("RewPredInputWts", etensor.FLOAT32, go.Slice_int([6, 1, 1, 6]), go.nil)] ) dt.SetFromSchema(sch, 0) ss.ConfigRewPredInput(ss.RewPredInputWts)
def Init(ev, run): ev.Run.Scale = env.Run ev.Epoch.Scale = env.Epoch ev.Trial.Scale = env.Trial ev.Trial.Max = len(ev.Words) ev.Run.Init() ev.Epoch.Init() ev.Trial.Init() ev.Run.Cur = run ev.Trial.Cur = -1 ev.WordState.SetShape(go.Slice_int([len(ev.Words)]), go.nil, go.Slice_string(["Words"]))
def Init(ev, run): ev.Run.Scale = env.Run ev.Epoch.Scale = env.Epoch ev.Trial.Scale = env.Trial ev.Run.Init() ev.Epoch.Init() ev.Trial.Init() ev.Run.Cur = run ev.InitOrder() nw = len(ev.Words) ev.CurParaState.SetShape(go.Slice_int([nw]), go.nil, go.Slice_string(["Words"]))
def Init(ev, run): ev.Run.Scale = env.Run ev.Epoch.Scale = env.Epoch ev.Seq.Scale = env.Sequence ev.Tick.Scale = env.Tick ev.Trial.Scale = env.Trial ev.Run.Init() ev.Epoch.Init() ev.Seq.Init() ev.Tick.Init() ev.Trial.Init() ev.Run.Cur = run ev.Trial.Cur = -1 # init state -- key so that first Step() = 0 ev.SentIdx.Set(-1) ev.Rules.Init() ev.MapsFmWords() ev.WordState.SetShape(go.Slice_int([len(ev.Words)]), go.nil, go.Slice_string(["Words"])) ev.RoleState.SetShape(go.Slice_int([len(ev.Roles)]), go.nil, go.Slice_string(["Roles"])) ev.FillerState.SetShape(go.Slice_int([len(ev.Fillers)]), go.nil, go.Slice_string(["Fillers"]))
def ConfigTstQuizLog(ss, dt): dt.SetMetaData("name", "TstQuizLog") dt.SetMetaData("desc", "Summary stats for testing trials") dt.SetMetaData("read-only", "true") dt.SetMetaData("precision", str(LogPrec)) sch = etable.Schema( [etable.Column("Run", etensor.INT64, go.nil, go.nil), etable.Column("Epoch", etensor.INT64, go.nil, go.nil), etable.Column("QNo", etensor.INT64, go.nil, go.nil), etable.Column("Resp", etensor.STRING, go.nil, go.nil), etable.Column("Err", etensor.FLOAT64, go.nil, go.nil), etable.Column("Correls", etensor.FLOAT64, go.Slice_int([3]), go.nil)] ) dt.SetFromSchema(sch, 0)
def ApplyInputs(ss): """ ApplyInputs applies input patterns from given envirbonment. It is good practice to have this be a separate method with appropriate args so that it can be used for various different contexts (training, testing, etc). """ ss.Net.InitExt() ly = leabra.Layer(ss.Net.LayerByName("NeckerCube")) tsr = ss.ValsTsr("Inputs") tsr.SetShape(go.Slice_int([16]), go.nil, go.nil) if tsr.FloatVal1D(0) != 1.0: for i in range(16): tsr.SetFloat1D(i, 1) ly.ApplyExt(tsr)
def __init__(self): super(SemEnv, self).__init__() self.Nm = str() self.SetTags("Nm", 'desc:"name of this environment"') self.Dsc = str() self.SetTags("Dsc", 'desc:"description of this environment"') self.Sequential = False self.SetTags( "Sequential", 'desc:"if true, go sequentially through paragraphs -- else permuted"' ) self.Order = go.Slice_int() self.SetTags( "Order", 'desc:"permuted order of paras to present if not sequential -- updated every time through the list"' ) self.TextFiles = [] self.SetTags("TextFiles", 'desc:"paths to text files"') self.Words = go.Slice_string() self.SetTags("Words", 'desc:"list of words, in alpha order"') self.WordMap = {} self.SetTags("WordMap", 'view:"-" desc:"map of words onto index in Words list"') self.CurParaState = etensor.Float32() self.SetTags("CurParaState", 'desc:"current para activation state"') self.Paras = [] self.SetTags("Paras", 'view:"-" desc:"paragraphs"') self.ParaLabels = [] self.SetTags( "ParaLabels", 'view:"-" desc:"special labels for each paragraph (provided in first word of para)"' ) self.Run = env.Ctr() self.SetTags( "Run", 'view:"inline" desc:"current run of model as provided during Init"' ) self.Epoch = env.Ctr() self.SetTags( "Epoch", 'view:"inline" desc:"number of times through Seq.Max number of sequences"' ) self.Trial = env.Ctr() self.SetTags( "Trial", 'view:"inline" desc:"trial is the step counter within epoch -- this is the index into Paras"' )
def LogRun(ss, dt): """ LogRun adds data from current run to the RunLog table. """ run = ss.TrainEnv.Run.Cur # this is NOT triggered by increment yet -- use Cur row = dt.Rows dt.SetNumRows(row + 1) epclog = ss.TstEpcLog epcix = etable.NewIdxView(epclog) # compute mean over last N epochs for run level nlast = 5 if nlast > epcix.Len()-1: nlast = epcix.Len() - 1 epcix.Idxs = go.Slice_int(epcix.Idxs[epcix.Len()-nlast:]) params = ss.Learn.name + "_" + ss.Pats.name dt.SetCellFloat("Run", row, float(run)) dt.SetCellString("Params", row, params) dt.SetCellFloat("FirstZero", row, float(ss.FirstZero)) dt.SetCellFloat("SSE", row, agg.Mean(epcix, "SSE")[0]) dt.SetCellFloat("AvgSSE", row, agg.Mean(epcix, "AvgSSE")[0]) dt.SetCellFloat("PctErr", row, agg.Mean(epcix, "PctErr")[0]) dt.SetCellFloat("PctCor", row, agg.Mean(epcix, "PctCor")[0]) dt.SetCellFloat("CosDiff", row, agg.Mean(epcix, "CosDiff")[0]) runix = etable.NewIdxView(dt) spl = split.GroupBy(runix, go.Slice_string(["Params"])) split.Desc(spl, "FirstZero") split.Desc(spl, "PctCor") split.Desc(spl, "SSE") ss.RunStats = spl.AggsToTable(etable.AddAggName) # note: essential to use Go version of update when called from another goroutine if ss.RunPlot != 0: ss.RunPlot.GoUpdate() if ss.RunFile != 0: if row == 0: dt.WriteCSVHeaders(ss.RunFile, etable.Tab) dt.WriteCSVRow(ss.RunFile, row, etable.Tab)
def UniquePatStat(ss, dt): """ UniquePatStat analyzes the hidden activity patterns for the single-line test inputs to determine how many such lines have a distinct hidden pattern, as computed from the similarity matrix across patterns """ hc = etensor.Float64(dt.ColByName("Hidden")) norm.Binarize64(hc.Values, .5, 1, 0) ix = etable.NewIdxView(dt) ss.SimMat.TableColStd(ix, "Hidden", "TrialName", False, metric.SumSquares) dm = ss.SimMat.Mat nrow = dm.Dim(0) uniq = 0 for row in range(nrow): tsr = etensor.Float64(dm.SubSpace(go.Slice_int([row]))) nzero = 0 for vl in tsr.Values: if vl == 0: nzero += 1 if nzero == 1: uniq += 1 return float(uniq)
def ConfigWts(ss, dt): dt.SetShape(go.Slice_int([14, 14, 12, 12]), go.nil, go.nil) dt.SetMetaData("grid-fill", "1")
def ConfigMtxInput(ss, dt): dt.SetShape(go.Slice_int([6, 1, 1, 6]), go.nil, go.nil)
def ConfigRewPredInput(ss, dt): dt.SetShape(go.Slice_int([1, 1, 3, 20]), go.nil, go.nil)
def numpy_to_etensor(nar): """ returns an etensor.Tensor constructed from the given etensor.Tensor data is copied into the Tensor -- it is not a view. """ et = 0 narf = np.reshape(nar, -1) # flat view if nar.dtype == np.uint8: et = etensor.NewUint8(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype == np.int8: et = etensor.NewInt8(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype == np.uint16: et = etensor.NewUint16(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype == np.int16: et = etensor.NewInt16(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype == np.uint32: et = etensor.NewUint32(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype == np.int32: et = etensor.NewInt32(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype == np.uint64: et = etensor.NewUint64(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype == np.int64: et = etensor.NewInt64(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype == np.float32: et = etensor.NewFloat32(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype == np.float64: et = etensor.NewFloat64(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype.type is np.string_ or nar.dtype.type is np.str_: et = etensor.NewString(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype == np.int_ or nar.dtype == np.intc: et = etensor.NewInt(go.Slice_int(list(nar.shape)), go.nil, go.nil) et.Values.copy(narf) elif nar.dtype == np.bool_: et = etensor.NewBits(go.Slice_int(list(nar.shape)), go.nil, go.nil) rnar = narf sz = len(rnar) for i in range(sz): et.Set1D(i, rnar[i]) else: raise TypeError("numpy ndarray with type %s cannot be converted" % (nar.dtype)) return 0 return et
def ConfigHidFmInput(ss, dt): dt.SetShape(go.Slice_int([4, 5, 5, 5]), go.nil, go.nil)