Ejemplo n.º 1
0
func TestUpdateBoostFactors(t *testing.T) {
	sp := SpatialPooler{}
	sp.MaxBoost = 10.0
	sp.numColumns = 6
	sp.minActiveDutyCycles = make([]float64, sp.numColumns)
	for i, _ := range sp.minActiveDutyCycles {
		sp.minActiveDutyCycles[i] = -0.0000001
	}
	sp.activeDutyCycles = []float64{0.1, 0.3, 0.02, 0.04, 0.7, 0.12}
	sp.boostFactors = make([]float64, sp.numColumns)
	trueBoostFactors := []float64{1, 1, 1, 1, 1, 1}
	sp.updateBoostFactors()

	for i, _ := range sp.boostFactors {
		assert.Equal(t, trueBoostFactors[i], sp.boostFactors[i])
	}

	sp.MaxBoost = 10.0
	sp.numColumns = 6
	sp.minActiveDutyCycles = []float64{0.1, 0.3, 0.02, 0.04, 0.7, 0.12}
	sp.activeDutyCycles = []float64{0.1, 0.3, 0.02, 0.04, 0.7, 0.12}
	trueBoostFactors = []float64{1, 1, 1, 1, 1, 1}
	sp.updateBoostFactors()

	for i, _ := range sp.boostFactors {
		diff := math.Abs(trueBoostFactors[i] - sp.boostFactors[i])
		assert.True(t, diff <= 0.0000001)
	}

	sp.MaxBoost = 10.0
	sp.numColumns = 6
	sp.minActiveDutyCycles = []float64{0.1, 0.2, 0.02, 0.03, 0.7, 0.12}
	sp.activeDutyCycles = []float64{0.01, 0.02, 0.002, 0.003, 0.07, 0.012}
	trueBoostFactors = []float64{9.1, 9.1, 9.1, 9.1, 9.1, 9.1}
	sp.updateBoostFactors()
	for i, _ := range sp.boostFactors {
		diff := math.Abs(trueBoostFactors[i] - sp.boostFactors[i])
		assert.True(t, diff <= 0.0000001)
	}

	sp.MaxBoost = 10.0
	sp.numColumns = 6
	sp.minActiveDutyCycles = []float64{0.1, 0.2, 0.02, 0.03, 0.7, 0.12}
	sp.activeDutyCycles = make([]float64, sp.numColumns)
	trueBoostFactors = utils.MakeSliceFloat64(6, sp.MaxBoost)
	sp.updateBoostFactors()
	for i, _ := range sp.boostFactors {
		diff := math.Abs(trueBoostFactors[i] - sp.boostFactors[i])
		assert.True(t, diff <= 0.0000001)
	}

}
Ejemplo n.º 2
0
func TestUpdateMinDutyCyclesGlobal(t *testing.T) {
	sp := SpatialPooler{}
	sp.MinPctActiveDutyCycles = 0.02
	sp.MinPctOverlapDutyCycles = 0.01
	sp.numColumns = 5
	sp.overlapDutyCycles = []float64{0.06, 1, 3, 6, 0.5}
	sp.activeDutyCycles = []float64{0.6, 0.07, 0.5, 0.4, 0.3}
	sp.minOverlapDutyCycles = make([]float64, sp.numColumns)
	sp.minActiveDutyCycles = make([]float64, sp.numColumns)
	sp.updateMinDutyCyclesGlobal()
	trueMinActiveDutyCycles := utils.MakeSliceFloat64(sp.numColumns, 0.02*0.6)
	trueMinOverlapDutyCycles := utils.MakeSliceFloat64(sp.numColumns, 0.01*6)

	assert.Equal(t, 5, len(sp.minActiveDutyCycles))
	assert.Equal(t, 5, len(sp.minOverlapDutyCycles))
	for i := 0; i < sp.numColumns; i++ {
		assert.AlmostEqualFloat(t, trueMinActiveDutyCycles[i], sp.minActiveDutyCycles[i])
		assert.AlmostEqualFloat(t, trueMinOverlapDutyCycles[i], sp.minOverlapDutyCycles[i])
	}

	sp.MinPctOverlapDutyCycles = 0.015
	sp.MinPctActiveDutyCycles = 0.03
	sp.numColumns = 5
	sp.overlapDutyCycles = []float64{0.86, 2.4, 0.03, 1.6, 1.5}
	sp.activeDutyCycles = []float64{0.16, 0.007, 0.15, 0.54, 0.13}
	sp.updateMinDutyCyclesGlobal()
	trueMinOverlapDutyCycles = utils.MakeSliceFloat64(sp.numColumns, 0.015*2.4)
	for i := 0; i < sp.numColumns; i++ {
		assert.AlmostEqualFloat(t, trueMinOverlapDutyCycles[i], sp.minOverlapDutyCycles[i])
	}

	sp.MinPctOverlapDutyCycles = 0.015
	sp.MinPctActiveDutyCycles = 0.03
	sp.numColumns = 5
	sp.overlapDutyCycles = utils.MakeSliceFloat64(sp.numColumns, 0)
	sp.activeDutyCycles = utils.MakeSliceFloat64(sp.numColumns, 0)
	sp.updateMinDutyCyclesGlobal()
	trueMinActiveDutyCycles = utils.MakeSliceFloat64(sp.numColumns, 0)
	trueMinOverlapDutyCycles = utils.MakeSliceFloat64(sp.numColumns, 0)

	assert.Equal(t, 5, len(sp.minActiveDutyCycles))
	assert.Equal(t, 5, len(sp.minOverlapDutyCycles))
	for i := 0; i < sp.numColumns; i++ {
		assert.AlmostEqualFloat(t, trueMinActiveDutyCycles[i], sp.minActiveDutyCycles[i])
		assert.AlmostEqualFloat(t, trueMinOverlapDutyCycles[i], sp.minOverlapDutyCycles[i])
	}

}
Ejemplo n.º 3
0
func TestInhibitColumns(t *testing.T) {
	sp := SpatialPooler{}

	globalValue := []int{1}
	localValue := []int{2}
	lastGlobalDensity := 0.0
	lastLocalDensity := 0.0
	globalFunc := func(overlaps []float64, density float64) []int {
		lastGlobalDensity = density
		return globalValue
	}

	localFunc := func(overlaps []float64, density float64) []int {
		lastLocalDensity = density
		return localValue
	}

	sp.ColumnDimensions = []int{5}
	sp.numColumns = 5
	sp.inhibitionRadius = 10
	sp.tieBreaker = make([]float64, sp.numColumns)
	for i := 0; i < len(sp.tieBreaker); i++ {
		sp.tieBreaker[i] = 0.01 * rand.Float64()
	}

	overlaps := utils.RandomSample(sp.numColumns)

	sp.NumActiveColumnsPerInhArea = 5
	sp.LocalAreaDensity = 0.1
	sp.GlobalInhibition = true
	sp.inhibitionRadius = 5
	trueDensity := sp.LocalAreaDensity
	sp.InhibitColumns(overlaps, globalFunc, localFunc)
	assert.Equal(t, trueDensity, lastGlobalDensity)

	//----- 2
	sp.ColumnDimensions = []int{50, 10}
	sp.numColumns = 500
	sp.tieBreaker = utils.MakeSliceFloat64(500, 0)
	sp.NumActiveColumnsPerInhArea = -1
	sp.LocalAreaDensity = 0.1
	sp.GlobalInhibition = false
	sp.inhibitionRadius = 7
	// 0.1 * (2*9+1)**2 = 22.5
	trueDensity = sp.LocalAreaDensity
	overlaps = utils.RandomSample(sp.numColumns)
	sp.InhibitColumns(overlaps, globalFunc, localFunc)
	assert.Equal(t, trueDensity, lastLocalDensity)

	// Test translation of numColumnsPerInhArea into local area density
	sp.ColumnDimensions = []int{10, 10}
	sp.numColumns = 1000
	sp.tieBreaker = utils.MakeSliceFloat64(1000, 0)
	sp.NumActiveColumnsPerInhArea = 3
	sp.LocalAreaDensity = -1
	sp.GlobalInhibition = false
	sp.inhibitionRadius = 4
	trueDensity = 3.0 / 81.0
	overlaps = utils.RandomSample(sp.numColumns)

	// 3.0 / (((2*4) + 1) ** 2)
	sp.InhibitColumns(overlaps, globalFunc, localFunc)
	assert.Equal(t, trueDensity, lastLocalDensity)

	// Test clipping of local area density to 0.5
	sp.ColumnDimensions = []int{10, 10}
	sp.numColumns = 1000
	sp.tieBreaker = utils.MakeSliceFloat64(1000, 0)
	sp.NumActiveColumnsPerInhArea = 7
	sp.LocalAreaDensity = -1
	sp.GlobalInhibition = false
	sp.inhibitionRadius = 1
	trueDensity = 0.5
	overlaps = utils.RandomSample(sp.numColumns)

	sp.InhibitColumns(overlaps, globalFunc, localFunc)
	assert.Equal(t, trueDensity, lastLocalDensity)

}
Ejemplo n.º 4
0
func TestBumpUpWeakColumns(t *testing.T) {
	sp := SpatialPooler{}
	sp.numInputs = 8
	sp.InputDimensions = []int{8}
	sp.numColumns = 5
	sp.ColumnDimensions = []int{5}
	sp.SynPermBelowStimulusInc = 0.01
	sp.SynPermTrimThreshold = 0.05
	sp.overlapDutyCycles = []float64{0, 0.009, 0.1, 0.001, 0.002}
	sp.minOverlapDutyCycles = utils.MakeSliceFloat64(5, 0.01)
	sp.SynPermInactiveDec = 0.01
	sp.SynPermActiveInc = 0.1
	sp.connectedSynapses = NewDenseBinaryMatrix(sp.numColumns, sp.numInputs)
	sp.connectedCounts = make([]int, sp.numColumns)
	sp.SynPermMax = 1
	sp.SynPermMin = 0
	sp.SynPermConnected = 0.10

	sp.MinPctOverlapDutyCycles = 0.1
	sp.MinPctActiveDutyCycles = 0.1
	sp.DutyCyclePeriod = 10
	sp.MaxBoost = 10.0
	sp.PotentialRadius = 5
	sp.PotentialPct = 0.5

	ints := [][]int{{1, 1, 1, 1, 0, 0, 0, 0},
		{1, 0, 0, 0, 1, 1, 0, 1},
		{0, 0, 1, 0, 1, 1, 1, 0},
		{1, 1, 1, 0, 0, 0, 1, 0},
		{1, 1, 1, 1, 1, 1, 1, 1}}
	sp.potentialPools = NewDenseBinaryMatrixFromInts(ints)

	floats := []float64{0.200, 0.120, 0.090, 0.040, 0.000, 0.000, 0.000, 0.000,
		0.150, 0.000, 0.000, 0.000, 0.180, 0.120, 0.000, 0.450,
		0.000, 0.000, 0.014, 0.000, 0.032, 0.044, 0.110, 0.000,
		0.041, 0.000, 0.000, 0.000, 0.000, 0.000, 0.178, 0.000,
		0.100, 0.738, 0.045, 0.002, 0.050, 0.008, 0.208, 0.034}
	elms := make(map[int]float64, len(floats))
	for i, val := range floats {
		elms[i] = val
	}
	sp.permanences = matrix.MakeSparseMatrix(elms, 5, 8)

	truePermanences := [][]float64{
		{0.210, 0.130, 0.100, 0.000, 0.000, 0.000, 0.000, 0.000},
		// Inc   Inc 	Inc    Trim   - - - -
		{0.160, 0.000, 0.000, 0.000, 0.190, 0.130, 0.000, 0.460},
		// Inc 	  -      -      - 	  Inc 	 Inc 	 - 	   Inc
		{0.000, 0.000, 0.014, 0.000, 0.032, 0.044, 0.110, 0.000}, //unchanged
		// - 	 - 		 - 		- 		- 	 - 		 - 		-
		{0.051, 0.000, 0.000, 0.000, 0.000, 0.000, 0.188, 0.000},
		// Inc 	Trim 	Trim 	- 	   - 	  - 	Inc 	-
		{0.110, 0.748, 0.055, 0.000, 0.060, 0.000, 0.218, 0.000}}

	sp.bumpUpWeakColumns()

	for i := 0; i < sp.numColumns; i++ {
		for j := 0; j < sp.numInputs; j++ {
			assert.AlmostEqualFloat(t, truePermanences[i][j], sp.permanences.Get(i, j), strconv.Itoa(i)+" "+strconv.Itoa(j))
		}
	}

}
Ejemplo n.º 5
0
func TestUpdateMinDutyCyclesLocal(t *testing.T) {
	sp := SpatialPooler{}

	sp.ColumnDimensions = []int{5}
	sp.numColumns = 5

	neighborsValue := [][]int{{0, 1, 2},
		{1, 2, 3},
		{2, 3, 4},
		{0, 2, 4},
		{0, 1, 3},
	}
	callCount := 0
	getNeighborsMock := func(columnIndex int, dimensions []int, radius int, wrapAround bool) []int {
		//t.Logf("called mock")
		result := neighborsValue[callCount]
		callCount++
		return result
	}

	sp.MinPctOverlapDutyCycles = 0.04
	sp.overlapDutyCycles = []float64{1.4, 0.5, 1.2, 0.8, 0.1}
	trueMinOverlapDutyCycles := []float64{0.04 * 1.4, 0.04 * 1.2, 0.04 * 1.2, 0.04 * 1.4, 0.04 * 1.4}

	sp.MinPctActiveDutyCycles = 0.02
	sp.activeDutyCycles = []float64{0.4, 0.5, 0.2, 0.18, 0.1}
	trueMinActiveDutyCycles := []float64{0.02 * 0.5, 0.02 * 0.5, 0.02 * 0.2, 0.02 * 0.4, 0.02 * 0.5}

	sp.minOverlapDutyCycles = utils.MakeSliceFloat64(sp.numColumns, 0)

	sp.minActiveDutyCycles = utils.MakeSliceFloat64(sp.numColumns, 0)
	sp.updateMinDutyCyclesLocal(getNeighborsMock)

	//assert.Equal(t, trueMinOverlapDutyCycles, sp.minOverlapDutyCycles)
	for i, _ := range trueMinOverlapDutyCycles {
		assert.AlmostEqualFloat(t, trueMinOverlapDutyCycles[i], sp.minOverlapDutyCycles[i])
	}
	assert.Equal(t, trueMinActiveDutyCycles, sp.minActiveDutyCycles)

	// 2-----
	sp.ColumnDimensions = []int{8}
	sp.numColumns = 8
	neighborsValue = [][]int{{0, 1, 2, 3, 4},
		{1, 2, 3, 4, 5},
		{2, 3, 4, 6, 7},
		{0, 2, 4, 6},
		{1, 6},
		{3, 5, 7},
		{1, 4, 5, 6},
		{2, 3, 6, 7}}

	sp.MinPctOverlapDutyCycles = 0.01
	sp.overlapDutyCycles = []float64{1.2, 2.7, 0.9, 1.1, 4.3, 7.1, 2.3, 0.0}
	trueMinOverlapDutyCycles = []float64{0.01 * 4.3, 0.01 * 7.1, 0.01 * 4.3, 0.01 * 4.3,
		0.01 * 4.3, 0.01 * 7.1, 0.01 * 7.1, 0.01 * 2.3}

	sp.MinPctActiveDutyCycles = 0.03
	sp.activeDutyCycles = []float64{0.14, 0.25, 0.125, 0.33, 0.27, 0.11, 0.76, 0.31}
	trueMinActiveDutyCycles = []float64{0.03 * 0.33, 0.03 * 0.33, 0.03 * 0.76, 0.03 * 0.76,
		0.03 * 0.76, 0.03 * 0.33, 0.03 * 0.76, 0.03 * 0.76}

	sp.minOverlapDutyCycles = utils.MakeSliceFloat64(sp.numColumns, 0)
	sp.minActiveDutyCycles = utils.MakeSliceFloat64(sp.numColumns, 0)
	callCount = 0
	sp.updateMinDutyCyclesLocal(getNeighborsMock)
	assert.Equal(t, trueMinOverlapDutyCycles, sp.minOverlapDutyCycles)
	assert.Equal(t, trueMinActiveDutyCycles, sp.minActiveDutyCycles)

}