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 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) }
func (tp *TrivialPredictor) reset() { for _, method := range tp.Methods { utils.FillSliceBool(tp.State[method].ActiveState, false) utils.FillSliceBool(tp.State[method].ActiveStateLast, false) utils.FillSliceBool(tp.State[method].PredictedState, false) utils.FillSliceBool(tp.State[method].PredictedStateLast, false) utils.FillSliceFloat64(tp.State[method].Confidence, 0.0) utils.FillSliceFloat64(tp.State[method].ConfidenceLast, 0.0) stats := tp.InternalStats[method] stats.NInfersSinceReset = 0 stats.CurPredictionScore = 0.0 stats.CurPredictionScore2 = 0.0 stats.FalseNegativeScoreTotal = 0.0 stats.FalsePositiveScoreTotal = 0.0 stats.CurExtra = 0.0 stats.CurMissing = 0.0 tp.InternalStats[method] = stats } }
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") } }
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) }
func phase3(t *testing.T, bt *boostTest) { //Do two more training batches through the input patterns y := make([]bool, bt.sp.numColumns) for i := 0; i < 2; 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 that just won should be at 1. for idx, val := range y { if val { if bt.sp.boostFactors[idx] != 1 { assert.Fail(t, "Boost factors of winning columns not 1") } } } // By now, every column should have been sufficiently boosted to win at least // once. The number of columns that have never won should now be 0 for _, val := range bt.winningIteration { if val == 0 { assert.Fail(t, "Expected all columns to have won atleast once.") } } // Because of the artificially induced thrashing, even the first two patterns // should have low overlap. Verify that the first two SDR's now have little // overlap overlap := computeOverlap(bt.lastSDR[0], bt.lastSDR[1]) assert.True(t, overlap < 7, "First two SDR's overlap significantly when they should not") }
func (tp *TrivialPredictor) infer(activeColumns []int) { numColsToPredict := int(0.5 + tp.AverageDensity*float64(tp.NumOfCols)) //for method in self.methods: for _, method := range tp.Methods { // Copy t-1 into t copy(tp.State[method].ActiveStateLast, tp.State[method].ActiveState) copy(tp.State[method].PredictedStateLast, tp.State[method].PredictedState) copy(tp.State[method].ConfidenceLast, tp.State[method].Confidence) utils.FillSliceBool(tp.State[method].ActiveState, false) utils.FillSliceBool(tp.State[method].PredictedState, false) utils.FillSliceFloat64(tp.State[method].Confidence, 0.0) for _, val := range activeColumns { tp.State[method].ActiveState[val] = true } var predictedCols []int switch method { case Random: // Randomly predict N columns //predictedCols = RandomInts(numColsToPredict, tp.NumOfCols) break case Zeroth: // Always predict the top N most frequent columns var inds []int ints.Argsort(tp.ColumnCount, inds) predictedCols = inds[len(inds)-numColsToPredict:] break case Last: // Always predict the last input for idx, val := range tp.State[method].ActiveState { if val { predictedCols = append(predictedCols, idx) } } break case All: // Always predict all columns for i := 0; i < tp.NumOfCols; i++ { predictedCols = append(predictedCols, i) } break case Lots: // Always predict 2 * the top N most frequent columns numColsToPredict := mathutil.Min(2*numColsToPredict, tp.NumOfCols) var inds []int ints.Argsort(tp.ColumnCount, inds) predictedCols = inds[len(inds)-numColsToPredict:] break default: panic("prediction method not implemented") } for _, val := range predictedCols { tp.State[method].PredictedState[val] = true tp.State[method].Confidence[val] = 1.0 } if tp.Verbosity > 1 { fmt.Println("Random prediction:", method) fmt.Println(" numColsToPredict:", numColsToPredict) fmt.Println(predictedCols) } } }
//Clears all entries func (sm *DenseBinaryMatrix) Clear() { utils.FillSliceBool(sm.entries, false) }