Esempio n. 1
0
    def testAlgorithm(self):
        sp = poolers.SpatialPooler(algorithm='product',
                                   ignore_background_pattern=False)
        evidence = [
            0.8,
            0.1,  # from node 0
            0.1,
            0.5,  # from node 1
            0.2,
            0.3
        ]  # from node 2
        coincidence = [0, 0, 1]  # 0.8 * 0.1 * 0.3

        product = sp._product_inference(coincidence, evidence)
        self.assertAlmostEqual(product, 0.024)

        sp = poolers.SpatialPooler(algorithm='product',
                                   ignore_background_pattern=False)
        pp = patterns.copy()
        sp.learn(pp)
        sp.finalize_learning()

        coincidence = np.array([0, 2, 2, 0, 0])
        evidence = np.array([
            0.6, 0.1, 0.2, 0.1, 0, 0.1, 0.8, 0.1, 0, 0, 0.4, 0.6, 0.8, 0, 0.2,
            0, 0.5, 0, 0.4, 0.1
        ])
        y = float(sp._product_inference(coincidence, evidence))
        # 0.6 * 0.8 * 0.4 * 0.8 * 0.5
        self.assertAlmostEqual(y, 0.0768)
Esempio n. 2
0
    def runTest(self):
        sp = poolers.SpatialPooler(algorithm='gaussian',
                                   max_distance=0,
                                   rare_coincidence_threshold=0,
                                   ignore_background_pattern=False,
                                   debug=False)

        for p in patterns:
            sp.learn(p)

        sp.finalize_learning()
        self.assertEqual(len(sp.coincidences), num_of_unique_patterns)

        tp = poolers.TemporalPooler(coincidences_stats=sp.coincidences_stats,
                                    transition_memory=2,
                                    algorithm='maxProp',
                                    requested_group_count=4)
        tp.coincidences_count = len(sp.coincidences)

        for p in patterns:
            tp.learn(sp.infer(p))

        desired_TAM = np.array([[2., 4., 3., 5.], [3., 0., 4., 2.],
                                [4., 0., 0., 3.], [0., 0., 0., 0.]])
        #        self.assertTrue(np.array_equal(tp.TAM, desired_TAM))

        tp.finalize_learning(grouping_method='AHC')

        # print tp.temporal_groups
        out = tp.infer(patterns[0])
        self.assertTrue(np.array_equal([0.2, 0.4, 0.4], out))
Esempio n. 3
0
    def testFromChild(self):
        sp = poolers.SpatialPooler(algorithm='product',
                                   ignore_background_pattern=False)

        #        correct_coincidence = np.array([1, 0, 0, 0,
        #                                        0, 0, 1, 0,
        #                                        0, 0, 1, 0,
        #                                        1, 0, 0, 0,
        #                                        1, 0, 0, 0])
        correct_coincidence = np.array([0, 2, 2, 0, 0])

        # patterns are now considered to be the outputs from children nodes
        # so only one concatenated pattern of all the patterns in the array
        # will be stored in the codebook
        pp = patterns.copy()
        sp.learn(pp)
        sp.finalize_learning()

        self.assertEqual(len(sp.coincidences), 1)
        # the sum of ones in the concidence is equal to the number of children
        self.assertEqual(np.sum(sp.coincidences[0]), num_of_unique_patterns)
        self.assertTrue(np.array_equal(sp.coincidences[0],
                                       correct_coincidence))

        pattern = np.random.random((20, 1))
        y = sp.infer(pattern)
        # there is only one coincidence, so the normalized output must be 1
        self.assertEqual(y, [1.0])
Esempio n. 4
0
    def testDistance(self):
        sp = poolers.SpatialPooler()

        x, y = np.asarray([1, 4, 2, 3, 1, 0]), np.asarray([1, 0, 2, 3, 1, 2])
        dist = sp._widx_distance(x, y)
        self.assertEqual(dist, 2)

        y = np.asarray([1, 4, 2, 3, 1, 0])
        dist = sp._widx_distance(x, y)
        self.assertEqual(dist, 0)

        x, y = np.asarray([0.1, 0.55, 0.31]), np.asarray([0.2, 0.55, 0.31])
        dist = sp._widx_distance(x, y)
        self.assertEqual(dist, 1)
Esempio n. 5
0
    def runTest(self):
        sp = poolers.SpatialPooler(algorithm='dot',
                                   ignore_background_pattern=False)

        sp.learn(patterns[0])
        sp.learn(patterns[1])
        sp.finalize_learning()

        self.assertEqual(len(sp.coincidences), 2)
        self.assertTrue(np.array_equal(sp.coincidences[0], patterns[0]))

        y = sp.infer(patterns[0])
        self.assertEqual(y[0], 0.75)

        y = sp.infer(patterns[2])
        self.assertAlmostEqual(y[0], 0.66666666)
Esempio n. 6
0
    def runTest(self):
        sp = poolers.SpatialPooler(algorithm='gaussian',
                                   ignore_background_pattern=False)

        for p in patterns:
            sp.learn(p)
        sp.finalize_learning()

        self.assertEqual(len(sp.coincidences), 4)

        # the infered vector sums to 1 (it's a probability distribution)
        pattern = np.random.random((10, 1))
        y = sp.infer(pattern[0])
        self.assertAlmostEqual(y.sum(), 1.0)

        # numeric test
        y = sp.infer(patterns[0])
        self.assertAlmostEqual(y[0], 0.77580349)