Exemple #1
0
func TestUpdateDutyCycleHelper(t *testing.T) {

	dc := []float64{1000.0, 1000.0, 1000.0, 1000.0, 1000.0}
	period := 1000
	newvals := make([]int, 5)
	actual := updateDutyCyclesHelper(dc, newvals, period)
	expected := []float64{999, 999, 999, 999, 999}
	assert.Equal(t, expected, actual)

	utils.FillSliceInt(newvals, 1000)
	actual = updateDutyCyclesHelper(dc, newvals, period)
	assert.Equal(t, dc, actual)

	newvals = []int{2000, 4000, 5000, 6000, 7000}
	expected = []float64{1001, 1003, 1004, 1005, 1006}
	actual = updateDutyCyclesHelper(dc, newvals, period)
	assert.Equal(t, expected, actual)

	dc = []float64{1000, 800, 600, 400, 2000}
	utils.FillSliceInt(newvals, 0)
	period = 2
	actual = updateDutyCyclesHelper(dc, newvals, period)
	expected = []float64{500, 400, 300, 200, 1000}
	assert.Equal(t, expected, actual)
}
Exemple #2
0
func TestUpdatePermanencesForColumn(t *testing.T) {

	sp := SpatialPooler{}
	sp.InputDimensions = []int{5}
	sp.ColumnDimensions = []int{5}
	sp.numColumns = 5
	sp.numInputs = 5
	sp.SynPermConnected = 0.1
	sp.SynPermTrimThreshold = 0.05
	sp.connectedCounts = make([]int, sp.numColumns)
	elms := make(map[int]float64, 25)
	sp.permanences = matrix.MakeSparseMatrix(elms, sp.numColumns, sp.numInputs)
	sp.potentialPools = NewDenseBinaryMatrix(sp.numColumns, sp.numInputs)
	sp.connectedSynapses = NewDenseBinaryMatrix(sp.numColumns, sp.numInputs)
	sp.SynPermMax = 1
	sp.SynPermMin = 0

	permanences := [][]float64{
		{-0.10, 0.500, 0.400, 0.010, 0.020},
		{0.300, 0.010, 0.020, 0.120, 0.090},
		{0.070, 0.050, 1.030, 0.190, 0.060},
		{0.180, 0.090, 0.110, 0.010, 0.030},
		{0.200, 0.101, 0.050, -0.09, 1.100}}

	/*
	   These are the 'true permanences' reflected in trueConnectedSynapses
	   truePermanences = SparseMatrix(
	   [[0.000, 0.500, 0.400, 0.000, 0.000],
	   Clip - - Trim Trim
	   [0.300, 0.000, 0.000, 0.120, 0.090],
	   - Trim Trim - -
	   [0.070, 0.050, 1.000, 0.190, 0.060],
	   - - Clip - -
	   [0.180, 0.090, 0.110, 0.000, 0.000],
	   - - - Trim Trim
	   [0.200, 0.101, 0.050, 0.000, 1.000]])
	   - - - Clip Clip
	*/

	trueConnectedSynapses := utils.Make2DBool([][]int{{0, 1, 1, 0, 0},
		{1, 0, 0, 1, 0},
		{0, 0, 1, 1, 0},
		{1, 0, 1, 0, 0},
		{1, 1, 0, 0, 1}})

	trueConnectedCounts := []int{2, 2, 2, 2, 3}

	for i := 0; i < sp.numColumns; i++ {
		sp.updatePermanencesForColumn(permanences[i], i, true)
		assert.Equal(t, trueConnectedSynapses[i], sp.connectedSynapses.GetDenseRow(i))
	}

	assert.Equal(t, trueConnectedCounts, sp.connectedCounts)

}
func phase2(t *testing.T, bt *boostTest) {

	y := make([]bool, bt.sp.numColumns)

	// Do 9 training batch through the input patterns
	for i := 0; i < 9; i++ {
		for idx, input := range bt.x {
			utils.FillSliceBool(y, false)
			bt.sp.Compute(input, true, y, bt.sp.InhibitColumns)
			for j, winner := range y {
				if winner {
					bt.winningIteration[j] = bt.sp.IterationLearnNum
				}
			}
			bt.lastSDR[idx] = y
		}
	}

	// The boost factor for all columns should be at 1.
	assert.Equal(t, bt.sp.numColumns, utils.CountFloat64(bt.sp.boostFactors, 1), "Boost factors are not all 1")

	// Roughly half of the columns should have never been active.
	winners := utils.CountInt(bt.winningIteration, 0)
	assert.True(t, winners >= int(0.4*float64(bt.sp.numColumns)), "More than 60% of the columns have been active")

	// All the never-active columns should have duty cycle of 0
	activeSum := 0.0
	for idx, val := range bt.sp.activeDutyCycles {
		if bt.winningIteration[idx] == 0 {
			activeSum += val
		}
	}
	assert.Equal(t, 0, activeSum, "Inactive columns have positive duty cycle.")

	dutyAvg := 0.0
	dutyCount := 0
	for _, val := range bt.sp.activeDutyCycles {
		if val > 0 {
			dutyAvg += val
			dutyCount++
		}
	}

	// The average at-least-once-active columns should have duty cycle >= 0.15
	// and <= 0.25
	dutyAvg = dutyAvg / float64(dutyCount)
	assert.True(t, dutyAvg >= 0.15, "Average on-columns duty cycle is too low.")
	assert.True(t, dutyAvg <= 0.30, "Average on-columns duty cycle is too high.")

	verifySDRProps(t, bt)
}
Exemple #4
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])
	}

}
func TestUpdateSynapsePermanence(t *testing.T) {
	c := NewTemporalMemoryConnections(1000, 32, []int{64, 64})
	c.CreateSegment(0)
	c.CreateSynapse(0, 483, 0.1284)
	c.UpdateSynapsePermanence(0, 0.2496)
	assert.Equal(t, 0.2496, c.DataForSynapse(0).Permanence)
}
Exemple #6
0
func TestInhibitColumnsGlobal(t *testing.T) {
	sp := SpatialPooler{}
	density := 0.3
	sp.numColumns = 10
	overlaps := []float64{1, 2, 1, 4, 8, 3, 12, 5, 4, 1}
	active := sp.inhibitColumnsGlobal(overlaps, density)
	trueActive := []int{4, 6, 7}
	assert.Equal(t, trueActive, active)

	density = 0.5
	overlaps = []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	active = sp.inhibitColumnsGlobal(overlaps, density)
	trueActive = []int{5, 6, 7, 8, 9}
	assert.Equal(t, trueActive, active)

}
Exemple #7
0
func TestAvgConnectedSpanForColumn2D(t *testing.T) {
	sp := SpatialPooler{}
	sp.InputDimensions = []int{8}
	sp.numInputs = 8
	sp.numColumns = 9
	sp.ColumnDimensions = []int{9}

	ints := [][]int{{0, 1, 0, 1, 0, 1, 0, 1},
		{0, 0, 0, 1, 0, 0, 0, 1},
		{0, 0, 0, 0, 0, 0, 1, 0},
		{0, 0, 1, 0, 0, 0, 1, 0},
		{0, 0, 0, 0, 0, 0, 0, 0},
		{0, 1, 1, 0, 0, 0, 0, 0},
		{0, 0, 1, 1, 1, 0, 0, 0},
		{0, 0, 1, 0, 1, 0, 0, 0},
		{1, 1, 1, 1, 1, 1, 1, 1}}

	sp.connectedSynapses = NewDenseBinaryMatrixFromInts(ints)

	trueAvgConnectedSpan := []int{7, 5, 1, 5, 0, 2, 3, 3, 8}

	for i := 0; i < sp.numColumns; i++ {
		connectedSpan := sp.avgConnectedSpanForColumnND(i)
		assert.Equal(t, trueAvgConnectedSpan[i], connectedSpan)
	}

}
Exemple #8
0
func TestParseXmlNodesWithCdataAndComment(t *testing.T) {
	var actuals []string = make([]string, 10)
	var actualsPos int = 0
	emitter := func(ed *contentBuffer.EmitterData) bool {
		actuals[actualsPos] = ed.Content
		actualsPos++
		return false
	}
	reader := bytes.NewReader([]byte("<helloA><!-- test<>--<><--><helloB><helloC><![CDATA[Hello<! World!]]></helloC><helloC>C2</helloC></helloB></helloA>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("helloA/helloB/helloC")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, actuals[0], "<helloC><![CDATA[Hello<! World!]]></helloC>")
	assert.Equal(t, actuals[1], "<helloC>C2</helloC>")
}
Exemple #9
0
func TestParseXmlNodesConstrainedBuffer(t *testing.T) {
	var actuals []string = make([]string, 10)
	var actualsPos int = 0
	emitter := func(ed *contentBuffer.EmitterData) bool {
		actuals[actualsPos] = ed.Content
		actualsPos++
		return false
	}
	reader := bytes.NewReader([]byte("<helloA><helloB><helloC>C1</helloC><helloC>C2</helloC></helloB></helloA>"))
	saxReader := newTestSaxReader(emitter)
	saxReader.ReaderBufferSize = 1
	tm := tagMatcher.NewTagMatcher("helloA/helloB/helloC")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, actuals[0], "<helloC>C1</helloC>")
	assert.Equal(t, actuals[1], "<helloC>C2</helloC>")
}
Exemple #10
0
func TestUpdateInhibitionRadius(t *testing.T) {
	sp := SpatialPooler{}

	// Test global inhibition case
	sp.GlobalInhibition = true
	sp.ColumnDimensions = []int{57, 31, 2}
	sp.numColumns = 3

	sp.updateInhibitionRadius(sp.avgConnectedSpanForColumnND, sp.avgColumnsPerInput)
	expected := 57
	assert.Equal(t, expected, sp.inhibitionRadius)

	sp.GlobalInhibition = false
	avgConnectedSpan := 3.0
	avgColPerInput := 4.0
	var avgConnectedSpanMock = func(i int) float64 {
		return avgConnectedSpan
	}

	var avgColPerInputMock = func() float64 {
		return avgColPerInput
	}

	trueInhibitionRadius := 6
	//((3 * 4) - 1) / 2 => round up
	sp.updateInhibitionRadius(avgConnectedSpanMock, avgColPerInputMock)
	assert.Equal(t, trueInhibitionRadius, sp.inhibitionRadius)

	//Test clipping at 1.false
	sp.GlobalInhibition = false
	trueInhibitionRadius = 1
	avgConnectedSpan = 0.5
	avgColPerInput = 1.2
	sp.updateInhibitionRadius(avgConnectedSpanMock, avgColPerInputMock)
	assert.Equal(t, trueInhibitionRadius, sp.inhibitionRadius)

	// //Test rounding up
	sp.GlobalInhibition = false
	avgConnectedSpan = 2.4
	avgColPerInput = 2
	trueInhibitionRadius = 2
	// ((2 * 2.4) - 1) / 2.false => round up
	sp.updateInhibitionRadius(avgConnectedSpanMock, avgColPerInputMock)
	assert.Equal(t, trueInhibitionRadius, sp.inhibitionRadius)

}
func basicComputeLoop(t *testing.T, spParams SpParams) {
	/*
		 Feed in some vectors and retrieve outputs. Ensure the right number of
		columns win, that we always get binary outputs, and that nothing crashes.
	*/

	sp := NewSpatialPooler(spParams)

	// Create a set of input vectors as well as various numpy vectors we will
	// need to retrieve data from the SP
	numRecords := 100

	inputMatrix := make([][]bool, numRecords)
	for i := range inputMatrix {
		inputMatrix[i] = make([]bool, sp.numInputs)
		for j := range inputMatrix[i] {
			inputMatrix[i][j] = rand.Float64() > 0.8
		}
	}

	// With learning off and no prior training we should get no winners
	y := make([]bool, sp.numColumns)
	for _, input := range inputMatrix {
		utils.FillSliceBool(y, false)
		sp.Compute(input, false, y, sp.InhibitColumns)
		assert.Equal(t, 0, utils.CountTrue(y))
	}

	// With learning on we should get the requested number of winners
	for _, input := range inputMatrix {
		utils.FillSliceBool(y, false)
		sp.Compute(input, true, y, sp.InhibitColumns)
		assert.Equal(t, sp.NumActiveColumnsPerInhArea, utils.CountTrue(y))

	}

	// With learning off and some prior training we should get the requested
	// number of winners
	for _, input := range inputMatrix {
		utils.FillSliceBool(y, false)
		sp.Compute(input, false, y, sp.InhibitColumns)
		assert.Equal(t, sp.NumActiveColumnsPerInhArea, utils.CountTrue(y))
	}

}
func phase1(t *testing.T, bt *boostTest) {
	y := make([]bool, bt.sp.numColumns)
	// Do one training batch through the input patterns
	for idx, input := range bt.x {
		utils.FillSliceBool(y, false)
		bt.sp.Compute(input, true, y, bt.sp.InhibitColumns)
		for j, winner := range y {
			if winner {
				bt.winningIteration[j] = bt.sp.IterationLearnNum
			}
		}
		bt.lastSDR[idx] = y
	}

	//The boost factor for all columns should be at 1.
	assert.Equal(t, bt.sp.numColumns, utils.CountFloat64(bt.sp.boostFactors, 1), "Boost factors are not all 1")

	//At least half of the columns should have never been active.
	winners := utils.CountInt(bt.winningIteration, 0)
	assert.True(t, winners >= bt.sp.numColumns/2, "More than half of the columns have been active")

	//All the never-active columns should have duty cycle of 0
	//All the at-least-once-active columns should have duty cycle >= 0.2
	activeSum := 0.0
	for idx, val := range bt.sp.activeDutyCycles {
		if bt.winningIteration[idx] == 0 {
			//assert.Equal(t, expected, actual, ...)
			activeSum += val
		}
	}
	assert.Equal(t, 0, activeSum, "Inactive columns have positive duty cycle.")

	winningMin := 100000.0
	for idx, val := range bt.sp.activeDutyCycles {
		if bt.winningIteration[idx] > 0 {
			if val < winningMin {
				winningMin = val
			}
		}
	}
	assert.True(t, winningMin >= 0.2, "Active columns have duty cycle that is too low.")

	verifySDRProps(t, bt)
}
Exemple #13
0
func TestLearnPredict(t *testing.T) {
	tps := 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.DoPooling = true

	tps.CollectStats = true
	tp := NewTemporalPooler(*tps)

	inputs := make([][]bool, 5)

	// inputs[0] = GenerateRandSequence(80, 50)
	// inputs[1] = GenerateRandSequence(80, 50)
	// inputs[2] = GenerateRandSequence(80, 50)
	inputs[0] = boolRange(0, 9, 50)
	inputs[1] = boolRange(10, 19, 50)
	inputs[2] = boolRange(20, 29, 50)
	inputs[3] = boolRange(30, 39, 50)
	inputs[4] = boolRange(40, 49, 50)

	//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.Entries()
		fmt.Println(p)
		assert.Equal(t, 10, len(p))
		for _, val := range p {
			next := i + 1
			if next > 4 {
				next = 4
			}
			assert.True(t, inputs[next][val.Row])
		}
	}

}
Exemple #14
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)
	}

}
Exemple #15
0
func TestParseXmlOneNodeOneAttributeSingle(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<hello id='123'>test</hello>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("hello?id=123")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "<hello id='123'>test</hello>")
}
Exemple #16
0
func TestParseXmlTest(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<hello id=\"123\" ref=\"42\"><hello2 idx=\"1234\" refx=\"421\">test</hello2></hello>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("hello?id=123&ref=42/hello2?idx=1234&refx=421")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "<hello2 idx=\"1234\" refx=\"421\">test</hello2>")
}
Exemple #17
0
func TestMapPotential1Column1Input(t *testing.T) {
	sp := SpatialPooler{}
	sp.InputDimensions = []int{1}
	sp.numInputs = 1
	sp.ColumnDimensions = []int{1}
	sp.numColumns = 1
	sp.PotentialRadius = 2
	sp.PotentialPct = 1

	expectedMask := []bool{true}
	mask := sp.mapPotential(0, false)
	assert.Equal(t, expectedMask, mask)
}
Exemple #18
0
func TestParseXmlNodesWithLtEscapeTag(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<helloA><helloB>&lt;helloC>&lt;/helloC></helloB></helloA>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("helloA/helloB")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "<helloB>&lt;helloC>&lt;/helloC></helloB>")
}
Exemple #19
0
func TestParseXmlOneNodeEmptySearch(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<hello>test</hello>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "")
}
Exemple #20
0
func TestInhibitColumnsLocal(t *testing.T) {
	sp := SpatialPooler{}
	density := 0.5
	sp.numColumns = 10
	sp.ColumnDimensions = []int{sp.numColumns}
	sp.inhibitionRadius = 2
	overlaps := []float64{1, 2, 7, 0, 3, 4, 16, 1, 1.5, 1.7}
	// L W  W  L  L  W   W  L   L    W
	trueActive := []int{1, 2, 5, 6, 9}
	active := sp.inhibitColumnsLocal(overlaps, density)
	assert.Equal(t, trueActive, active)

	//Test add to winners
	density = 0.3333
	sp.inhibitionRadius = 3
	overlaps = []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
	// W W L L W W L L L W
	trueActive = []int{0, 1, 4, 5, 8}
	active = sp.inhibitColumnsLocal(overlaps, density)
	assert.Equal(t, trueActive, active)

}
Exemple #21
0
func TestParseXmlTestS2(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<hello><text xml:space=\"preserve\">this</text><hello2>Test2</hello2><hello3>Test3</hello3></hello>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("?xml:space")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "<text xml:space=\"preserve\">this</text>")
}
Exemple #22
0
func TestParseXmlNodeConstrainedBuffer(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<hello>test</hello>"))
	saxReader := newTestSaxReader(emitter)
	saxReader.ReaderBufferSize = 1
	tm := tagMatcher.NewTagMatcher("hello")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "<hello>test</hello>")
}
Exemple #23
0
func TestCompute1(t *testing.T) {
	/*
		Checks that feeding in the same input vector leads to polarized
		permanence values: either zeros or ones, but no fractions
	*/

	spParams := NewSpParams()
	spParams.InputDimensions = []int{9}
	spParams.ColumnDimensions = []int{5}
	spParams.PotentialRadius = 3
	spParams.PotentialPct = 0.5
	spParams.GlobalInhibition = false
	spParams.LocalAreaDensity = -1
	spParams.NumActiveColumnsPerInhArea = 3
	spParams.StimulusThreshold = 1
	spParams.SynPermInactiveDec = 0.01
	spParams.SynPermActiveInc = 0.1
	spParams.SynPermConnected = 0.10
	spParams.MinPctOverlapDutyCycle = 0.1
	spParams.MinPctActiveDutyCycle = 0.1
	spParams.DutyCyclePeriod = 10
	spParams.MaxBoost = 10.0
	sp := NewSpatialPooler(spParams)

	sp.potentialPools = NewDenseBinaryMatrix(sp.numColumns, sp.numInputs)
	for i := 0; i < sp.numColumns; i++ {
		for j := 0; j < sp.numInputs; j++ {
			sp.potentialPools.Set(i, j, true)
		}
	}

	inhibitColumnsMock := func(overlaps []float64, inhibitColumnsGlobal, inhibitColumnsLocal inhibitColumnsFunc) []int {
		return []int{0, 1, 2, 3, 4}
	}

	inputVector := utils.Make1DBool([]int{1, 0, 1, 0, 1, 0, 0, 1, 1})
	activeArray := make([]bool, 5)

	for i := 0; i < 20; i++ {
		sp.Compute(inputVector, true, activeArray, inhibitColumnsMock)
	}

	for i := 0; i < 20; i++ {
		perm := Float64SliceToInt(GetRowFromSM(sp.permanences, i))
		assert.Equal(t, inputVector, utils.Make1DBool(perm))
	}

}
func phase4(t *testing.T, bt *boostTest) {
	//The boost factor for all columns that just won should be at 1.
	boostAtBeg := make([]float64, len(bt.sp.boostFactors))
	copy(bt.sp.boostFactors, boostAtBeg)

	// Do one more iteration through the input patterns with learning OFF
	y := make([]bool, bt.sp.numColumns)
	for _, input := range bt.x {
		utils.FillSliceBool(y, false)
		bt.sp.Compute(input, false, y, bt.sp.InhibitColumns)

		// The boost factor for all columns that just won should be at 1.
		assert.Equal(t, utils.SumSliceFloat64(boostAtBeg), utils.SumSliceFloat64(bt.sp.boostFactors), "Boost factors changed when learning is off")
	}

}
Exemple #25
0
func TestCompute2(t *testing.T) {
	/*
		Checks that columns only change the permanence values for
		inputs that are within their potential pool
	*/

	spParams := NewSpParams()
	spParams.InputDimensions = []int{10}
	spParams.ColumnDimensions = []int{5}
	spParams.PotentialRadius = 3
	spParams.PotentialPct = 0.5
	spParams.GlobalInhibition = false
	spParams.LocalAreaDensity = -1
	spParams.NumActiveColumnsPerInhArea = 3
	spParams.StimulusThreshold = 1
	spParams.SynPermInactiveDec = 0.01
	spParams.SynPermActiveInc = 0.1
	spParams.SynPermConnected = 0.10
	spParams.MinPctOverlapDutyCycle = 0.1
	spParams.MinPctActiveDutyCycle = 0.1
	spParams.DutyCyclePeriod = 10
	spParams.MaxBoost = 10.0
	sp := NewSpatialPooler(spParams)

	inhibitColumnsMock := func(overlaps []float64, inhibitColumnsGlobal, inhibitColumnsLocal inhibitColumnsFunc) []int {
		return []int{0, 1, 2, 3, 4}
	}

	inputVector := utils.Make1DBool([]int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
	activeArray := make([]bool, 5)

	for i := 0; i < 20; i++ {
		sp.Compute(inputVector, true, activeArray, inhibitColumnsMock)
	}

	for i := 0; i < sp.numColumns; i++ {
		perm := Float64SliceToInt(GetRowFromSM(sp.permanences, i))
		potential := sp.potentialPools.GetDenseRow(i)
		assert.Equal(t, potential, utils.Make1DBool(perm))
	}

}
Exemple #26
0
func TestMapPotential1D(t *testing.T) {
	sp := SpatialPooler{}
	sp.InputDimensions = []int{10}
	sp.numInputs = 10
	sp.ColumnDimensions = []int{4}
	sp.numColumns = 4
	sp.PotentialRadius = 2
	sp.PotentialPct = 1

	expectedMask := []bool{true, true, true, false, false, false, false, false, false, false}
	mask := sp.mapPotential(0, false)
	assert.Equal(t, expectedMask, mask)

	expectedMask = []bool{false, false, false, false, true, true, true, true, true, false}
	mask = sp.mapPotential(2, false)
	assert.Equal(t, expectedMask, mask)

	sp.PotentialPct = 1

	expectedMask = []bool{true, true, true, false, false, false, false, false, true, true}
	mask = sp.mapPotential(0, true)
	assert.Equal(t, expectedMask, mask)

	expectedMask = []bool{true, true, false, false, false, false, false, true, true, true}
	mask = sp.mapPotential(3, true)
	assert.Equal(t, expectedMask, mask)

	// Test with potentialPct < 1
	sp.PotentialPct = 0.5

	expectedMask = []bool{true, true, true, false, false, false, false, false, true, true}
	mask = sp.mapPotential(0, true)

	assert.Equal(t, 3, utils.CountTrue(mask))

	unionMask := utils.OrBool(expectedMask, mask)
	assert.Equal(t, expectedMask, unionMask)

}
func TestCellsForColumn1D(t *testing.T) {
	c := NewTemporalMemoryConnections(1000, 5, []int{2048})
	expectedCells := []int{5, 6, 7, 8, 9}
	assert.Equal(t, expectedCells, c.CellsForColumn(1))
}
Exemple #28
0
func TestGetNeighborsND(t *testing.T) {
	sp := SpatialPooler{}

	dimensions := []int{5, 7, 2}
	var layout [5][7][2]int

	counter := 0
	for i := range layout {
		for j := range layout[i] {
			for k := range layout[i][j] {
				layout[i][j][k] = counter
				counter++
			}
		}
	}

	radius := 1
	x := 1
	y := 3
	z := 2

	columnIndex := layout[z][y][x]

	neighbors := sp.getNeighborsND(columnIndex, dimensions, radius, true)

	var expected []int

	for i := radius * -1; i <= radius; i++ {
		for j := radius * -1; j <= radius; j++ {
			for k := radius * -1; k <= radius; k++ {
				zprime := (z + i) % dimensions[0]
				yprime := (y + j) % dimensions[1]
				xprime := (x + k) % dimensions[2]
				if layout[zprime][yprime][xprime] != columnIndex && !utils.ContainsInt(layout[zprime][yprime][xprime], expected) {
					expected = append(expected, layout[zprime][yprime][xprime])
				}

			}
		}
	}

	assert.Equal(t, expected, neighbors)

	dimensions = []int{5, 7, 9}
	var layoutb [5][7][9]int
	counter = 0
	for i := range layoutb {
		for j := range layoutb[i] {
			for k := range layoutb[i][j] {
				layoutb[i][j][k] = counter
				counter++
			}
		}
	}

	radius = 3
	x = 0
	y = 0
	z = 3
	columnIndex = layoutb[z][y][x]
	neighbors = sp.getNeighborsND(columnIndex, dimensions, radius, true)
	expected = []int{}
	for i := radius * -1; i <= radius; i++ {
		for j := radius * -1; j <= radius; j++ {
			for k := radius * -1; k <= radius; k++ {
				zprime := utils.Mod((z + i), (dimensions[0]))
				yprime := utils.Mod((y + j), (dimensions[1]))
				xprime := utils.Mod((x + k), (dimensions[2]))

				if layoutb[zprime][yprime][xprime] != columnIndex && !utils.ContainsInt(layoutb[zprime][yprime][xprime], expected) {
					expected = append(expected, layoutb[zprime][yprime][xprime])
				}

			}
		}
	}

	assert.Equal(t, expected, neighbors)

	dimensions = []int{5, 10, 7, 6}
	var layoutc [5][10][7][6]int
	counter = 0
	for i := range layoutc {
		for j := range layoutc[i] {
			for k := range layoutc[i][j] {
				for m := range layoutc[i][j][k] {
					layoutc[i][j][k][m] = counter
					counter++
				}
			}
		}
	}

	radius = 4
	w := 2
	x = 5
	y = 6
	z = 2
	columnIndex = layoutc[z][y][x][w]
	neighbors = sp.getNeighborsND(columnIndex, dimensions, radius, true)
	expected = []int{}
	for i := radius * -1; i <= radius; i++ {
		for j := radius * -1; j <= radius; j++ {
			for k := radius * -1; k <= radius; k++ {
				for m := radius * -1; m <= radius; m++ {
					zprime := utils.Mod((z + i), (dimensions[0]))
					yprime := utils.Mod((y + j), (dimensions[1]))
					xprime := utils.Mod((x + k), (dimensions[2]))
					wprime := utils.Mod((w + m), (dimensions[3]))

					if layoutc[zprime][yprime][xprime][wprime] != columnIndex && !utils.ContainsInt(layoutc[zprime][yprime][xprime][wprime], expected) {
						expected = append(expected, layoutc[zprime][yprime][xprime][wprime])
					}

				}

			}
		}
	}

	assert.Equal(t, expected, neighbors)

	layoutd := []bool{false, false, true, false, true, false, false, false}
	columnIndex = 3
	dimensions = []int{8}
	radius = 1
	mask := sp.getNeighborsND(columnIndex, dimensions, radius, true)
	t.Log("mask", mask)

	for idx, val := range layoutd {
		if utils.ContainsInt(idx, mask) {
			assert.Equal(t, true, val)
		} else {
			assert.Equal(t, false, val)
		}
	}

	layoute := []bool{false, true, true, false, true, true, false, false}
	columnIndex = 3
	dimensions = []int{8}
	radius = 2

	mask = sp.getNeighborsND(columnIndex, dimensions, radius, true)
	t.Log("mask", mask)

	for idx, val := range layoute {
		if utils.ContainsInt(idx, mask) {
			assert.Equal(t, true, val)
		} else {
			assert.Equal(t, false, val)
		}
	}

	// Wrap around
	layoutf := []bool{false, true, true, false, false, false, true, true}
	columnIndex = 0
	dimensions = []int{8}
	radius = 2

	mask = sp.getNeighborsND(columnIndex, dimensions, radius, true)
	t.Log("mask", mask)

	for idx, val := range layoutf {
		if utils.ContainsInt(idx, mask) {
			assert.Equal(t, true, val)
		} else {
			assert.Equal(t, false, val)
		}
	}

	//Radius too big
	layoutg := []bool{true, true, true, true, true, true, false, true}
	columnIndex = 6
	dimensions = []int{8}
	radius = 20

	mask = sp.getNeighborsND(columnIndex, dimensions, radius, true)
	t.Log("mask", mask)

	for idx, val := range layoutg {
		if utils.ContainsInt(idx, mask) {
			assert.Equal(t, true, val)
		} else {
			assert.Equal(t, false, val)
		}
	}

	//These are all the same tests from 2D
	ints := [][]int{{0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0},
		{0, 1, 1, 1, 0},
		{0, 1, 0, 1, 0},
		{0, 1, 1, 1, 0},
		{0, 0, 0, 0, 0}}

	layouth := NewDenseBinaryMatrixFromInts(ints)
	t.Log(layouth.ToString())

	columnIndex = 3*5 + 2
	dimensions = []int{6, 5}
	radius = 1

	mask = sp.getNeighborsND(columnIndex, dimensions, radius, true)
	t.Log("mask", mask)
	t.Log("1d", layouth.Flatten())
	for idx, val := range layouth.Flatten() {
		if utils.ContainsInt(idx, mask) {
			assert.Equal(t, true, val)
		} else {
			assert.Equal(t, false, val)
		}
	}

	ints = [][]int{{0, 0, 0, 0, 0},
		{1, 1, 1, 1, 1},
		{1, 1, 1, 1, 1},
		{1, 1, 0, 1, 1},
		{1, 1, 1, 1, 1},
		{1, 1, 1, 1, 1}}

	layoutj := NewDenseBinaryMatrixFromInts(ints)
	t.Log(layouth.ToString())

	columnIndex = 3*5 + 2
	radius = 2

	mask = sp.getNeighborsND(columnIndex, dimensions, radius, true)
	t.Log("mask", mask)
	t.Log("1d", layouth.Flatten())
	for idx, val := range layoutj.Flatten() {
		if utils.ContainsInt(idx, mask) {
			assert.Equal(t, true, val)
		} else {
			assert.Equal(t, false, val)
		}
	}

	//Radius too big
	ints = [][]int{{1, 1, 1, 1, 1},
		{1, 1, 1, 1, 1},
		{1, 1, 1, 1, 1},
		{1, 1, 0, 1, 1},
		{1, 1, 1, 1, 1},
		{1, 1, 1, 1, 1}}

	layoutk := NewDenseBinaryMatrixFromInts(ints)
	t.Log(layouth.ToString())

	columnIndex = 3*5 + 2
	radius = 7

	mask = sp.getNeighborsND(columnIndex, dimensions, radius, true)
	t.Log("mask", mask)
	t.Log("1d", layoutk.Flatten())
	for idx, val := range layoutk.Flatten() {
		if utils.ContainsInt(idx, mask) {
			assert.Equal(t, true, val)
		} else {
			assert.Equal(t, false, val)
		}
	}

	//Wrap-around
	ints = [][]int{{1, 0, 0, 1, 1},
		{0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0},
		{1, 0, 0, 1, 1},
		{1, 0, 0, 1, 0}}

	layoutl := NewDenseBinaryMatrixFromInts(ints)
	t.Log(layoutl.ToString())

	columnIndex = 29
	radius = 1

	mask = sp.getNeighborsND(columnIndex, dimensions, radius, true)
	t.Log("mask", mask)
	t.Log("1d", layoutl.Flatten())
	for idx, val := range layoutl.Flatten() {
		if utils.ContainsInt(idx, mask) {
			assert.Equal(t, true, val)
		} else {
			assert.Equal(t, false, val)
		}
	}

	//No wrap around
	columnIndex = 8
	radius = 2
	dimensions = []int{10}
	neighbors = sp.getNeighborsND(columnIndex, dimensions, radius, false)
	expected = []int{6, 7, 9}
	assert.Equal(t, expected, neighbors)

}
func TestCellsForColumn2D(t *testing.T) {
	c := NewTemporalMemoryConnections(1000, 4, []int{64, 64})
	expectedCells := []int{256, 257, 258, 259}
	assert.Equal(t, expectedCells, c.CellsForColumn(64))
}
func TestNumColumns(t *testing.T) {
	c := NewTemporalMemoryConnections(0, 32, []int{64, 64})
	assert.Equal(t, 64*64, c.NumberOfColumns())
}