Skip to content

Instantly share code, notes, and snippets.

@zacg
Created July 7, 2014 19:27
Show Gist options
  • Save zacg/d1640e7346e9747562e7 to your computer and use it in GitHub Desktop.
Save zacg/d1640e7346e9747562e7 to your computer and use it in GitHub Desktop.
HTM Temporal Pooler Example
package main
import (
"fmt"
"github.com/zacg/htm"
"github.com/zacg/htm/utils"
)
func main() {
tps := htm.NewTemporalPoolerParams()
tps.Verbosity = 0
tps.NumberOfCols = 50
tps.CellsPerColumn = 2
tps.ActivationThreshold = 8
tps.MinThreshold = 10
tps.InitialPerm = 0.5
tps.ConnectedPerm = 0.5
tps.NewSynapseCount = 10
tps.PermanenceDec = 0.0
tps.PermanenceInc = 0.1
tps.GlobalDecay = 0
tps.BurnIn = 1
tps.PamLength = 10
tps.CollectStats = true
tp := htm.NewTemporalPooler(*tps)
//Mock encoding of ABCDE
inputs := make([][]bool, 5)
inputs[0] = boolRange(0, 9, 50) //bits 0-9 are "on"
inputs[1] = boolRange(10, 19, 50) //bits 10-19 are "on"
inputs[2] = boolRange(20, 29, 50) //bits 20-29 are "on"
inputs[3] = boolRange(30, 39, 50) //bits 30-39 are "on"
inputs[4] = boolRange(40, 49, 50) //bits 40-49 are "on"
//Learn 5 sequences above
for i := 0; i < 10; i++ {
for p := 0; p < 5; p++ {
tp.Compute(inputs[p], true, false)
}
tp.Reset()
}
//Predict sequences
for i := 0; i < 4; i++ {
tp.Compute(inputs[i], false, true)
p := tp.DynamicState.InfPredictedState
fmt.Printf("Predicted: %v From input: %v \n", p.NonZeroRows(), utils.OnIndices(inputs[i]))
}
}
//helper method for creating boolean sequences
func boolRange(start int, end int, length int) []bool {
result := make([]bool, length)
for i := start; i <= end; i++ {
result[i] = true
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment