func (s ScalarSensor) Decode(bits data.Bitset) interface{} { found := bits.Any(func(int) bool { return true }) return (0.5+float64(found))*s.BucketSize + s.MinValue }
func (c *Column) LearnSequence(learnState data.Bitset) { logEnabled := log.HtmLogger.Enabled() if learnState.IsZero() { // Select the learning cell, but don't increment the target. c.learning = c.learningTarget if logEnabled { log.HtmLogger.Printf("\t(%d, %d)=%04d: skip learning empty sequence", c.Index, c.learning, c.CellId(c.learning)) } return } cell, sIndex, _ := c.FindBestSegment(learnState, 1, true) if sIndex >= 0 { if logEnabled { log.HtmLogger.Printf("\t(%d, %d)=%04d: Will reinforce segment: %v", c.Index, cell, c.CellId(cell), c.distal[cell].Segment(sIndex)) } } else { if logEnabled { log.HtmLogger.Printf("\t(%d, %d)=%04d: Will learn a new segment.", c.Index, c.learningTarget, c.CellId(c.learningTarget)) } cell = c.learningTarget c.learningTarget = (c.learningTarget + 1) % c.Height() sIndex = -1 } c.learning = cell update := c.distal[cell].CreateUpdate(sIndex, learnState, 1) if logEnabled { log.HtmLogger.Printf("\tLearning sequence %v => (%d, %d)=%04d", update, c.Index, cell, c.CellId(cell)) } c.distal[cell].Apply(update, true) }
func (c Column) FindBestSegment(state data.Bitset, minOverlap int, weak bool) (bestCell, bestSegment, bestOverlap int) { bestCell = -1 bestSegment = -1 bestOverlap = minOverlap - 1 maxOverlap := state.NumSetBits() for i := 0; i < c.Height(); i++ { sIndex, sOverlap := c.distal[i].ComputeActive(state, minOverlap, weak) if sOverlap > bestOverlap { bestCell = i bestSegment = sIndex bestOverlap = sOverlap if bestOverlap == maxOverlap { break } } } if log.HtmLogger.Enabled() { if bestSegment >= 0 { s := c.distal[bestCell].Segment(bestSegment) log.HtmLogger.Printf("\t\tFind best segment for column=%d, state=%v, minOverlap=%d, weak=%t: (%d, %d)=%04d <= %v (overlap=%d)", c.Index, state, minOverlap, weak, c.Index, bestCell, c.CellId(bestCell), s.Connected(), bestOverlap) } } return }
func (pm *PermanenceMap) weaken(input data.Bitset) { for k, v := range pm.permanence { if input.IsSet(k) { v -= pm.config.Decrement } pm.Set(k, v) } }
func PermanenceMapFromBits(bits data.Bitset) (pm *PermanenceMap) { pm = NewPermanenceMap(bits.Len()) bits.Foreach(func(k int) { pm.permanence[k] = pm.config.Initial }) if pm.config.Initial > pm.config.Threshold { pm.synapses.Or(bits) } return pm }
func (pm *PermanenceMap) narrow(input data.Bitset) { for k, v := range pm.permanence { if input.IsSet(k) { v += pm.config.Increment } else { v -= pm.config.Decrement } pm.Set(k, v) } }
func (c *Column) ConfirmPrediction(state data.Bitset) bool { if c.learning >= 0 && state.IsSet(c.CellId(c.learning)) { if log.HtmLogger.Enabled() { log.HtmLogger.Printf("\t(%d, %d)=%04d: Confirmed prediction.", c.Index, c.learning, c.CellId(c.learning)) } return true } return false }
func (l *Region) FeedBack(output data.Bitset) *data.Bitset { dest := data.NewBitset(l.InputLength) output.Foreach(func(cellId int) { col := l.columns[cellId/l.Height()] cell := cellId % l.Height() if !col.Distal(cell).HasActiveSegment(output, l.MinimumInputOverlap) { // Not a predicted cell, must be active from fast-forward. dest.Or(col.Connected()) } }) return dest }
func (g *DistalSegmentGroup) CreateUpdate(sIndex int, activeState data.Bitset, minSynapses int) *SegmentUpdate { state := data.NewBitset(activeState.Len()) if sIndex >= 0 { s := g.segments[sIndex] state.ResetTo(s.Connected()) } state.Or(activeState) for num := state.NumSetBits(); num < minSynapses; num = state.NumSetBits() { // TODO(tms): optimize. indices := segmentRand.Perm(state.Len())[num:minSynapses] state.Set(indices...) } return NewSegmentUpdate(sIndex, state) }
func (ds *DendriteSegment) broaden(input data.Bitset, minOverlap int) (overlapCount int) { overlapCount = input.Overlap(ds.Connected()) threshold := ds.Config().Threshold newPermanence := ds.Config().Minimum + ds.Boost if newPermanence > threshold { newPermanence = threshold } newSynapses := input.Clone() newSynapses.AndNot(ds.ReceptiveField()) newSynapses.Foreach(func(k int) { v := ds.Get(k) if v < newPermanence { ds.Set(k, newPermanence) } }) ds.overlapHistory.Record(overlapCount >= minOverlap) if avg, ok := ds.overlapHistory.Average(); ok && avg < ds.MinActivityRatio { for k, v := range ds.permanence { ds.Set(k, v*1.01) } } return }
func (s *PeriodicSensor) Decode(bits data.Bitset) interface{} { found := bits.Any(func(int) bool { return true }) if bits.IsSet(found + 2) { return found + 1 + s.First } else if bits.IsSet(found + 1) { // found is the center bit, so first. return s.First } else { // found is center bit + 1, so last. return s.Last } }
func (s *CategorySensor) Decode(bits data.Bitset) interface{} { found := bits.Any(func(int) bool { return true }) return s.reverse[found/s.W] }