Example #1
0
    def testSeed(self):
        P = RDSE_Parameters()
        P.size     = 1000
        P.sparsity = .08
        P.radius   = 12
        P.seed     = 98
        R = RDSE( P )
        A = R.encode( 987654 )

        P.seed = 99
        R = RDSE( P )
        B = R.encode( 987654 )
        assert( A != B )
Example #2
0
 def testRandomOverlap(self):
     """ Verify that distant values have little to no semantic similarity.
     Also measure sparsity & activation frequency. """
     P = RDSE_Parameters()
     P.size     = 2000
     P.sparsity = .08
     P.radius   = 12
     P.seed     = 42
     R = RDSE( P )
     num_samples = 1000
     A = SDR( R.parameters.size )
     M = Metrics( A, num_samples + 1 )
     for i in range( num_samples ):
         X = i * R.parameters.radius
         R.encode( X, A )
     print( M )
     assert(M.overlap.max()  < .15 )
     assert(M.overlap.mean() < .10 )
     assert(M.sparsity.min()  > R.parameters.sparsity - .01 )
     assert(M.sparsity.max()  < R.parameters.sparsity + .01 )
     assert(M.sparsity.mean() > R.parameters.sparsity - .005 )
     assert(M.sparsity.mean() < R.parameters.sparsity + .005 )
     assert(M.activationFrequency.min()  > R.parameters.sparsity - .05 )
     assert(M.activationFrequency.max()  < R.parameters.sparsity + .05 )
     assert(M.activationFrequency.mean() > R.parameters.sparsity - .005 )
     assert(M.activationFrequency.mean() < R.parameters.sparsity + .005 )
     assert(M.activationFrequency.entropy() > .99 )
Example #3
0
 def testAverageOverlap(self):
     """ Verify that nearby values have the correct amount of semantic
     similarity. Also measure sparsity & activation frequency. """
     P = RDSE_Parameters()
     P.size     = 2000
     P.sparsity = .08
     P.radius   = 12
     P.seed     = 42
     R = RDSE( P )
     A = SDR( R.parameters.size )
     num_samples = 10000
     M = Metrics( A, num_samples + 1 )
     for i in range( num_samples ):
         R.encode( i, A )
     print( M )
     assert(M.overlap.min()  > (1 - 1. / R.parameters.radius) - .04 )
     assert(M.overlap.max()  < (1 - 1. / R.parameters.radius) + .04 )
     assert(M.overlap.mean() > (1 - 1. / R.parameters.radius) - .001 )
     assert(M.overlap.mean() < (1 - 1. / R.parameters.radius) + .001 )
     assert(M.sparsity.min()  > R.parameters.sparsity - .01 )
     assert(M.sparsity.max()  < R.parameters.sparsity + .01 )
     assert(M.sparsity.mean() > R.parameters.sparsity - .005 )
     assert(M.sparsity.mean() < R.parameters.sparsity + .005 )
     assert(M.activationFrequency.min()  > R.parameters.sparsity - .05 )
     assert(M.activationFrequency.max()  < R.parameters.sparsity + .05 )
     assert(M.activationFrequency.mean() > R.parameters.sparsity - .005 )
     assert(M.activationFrequency.mean() < R.parameters.sparsity + .005 )
     assert(M.activationFrequency.entropy() > .99 )
Example #4
0
    def testDeterminism(self):
        """ Verify that the same seed always gets the same results. """
        GOLD = SDR( 1000 )
        GOLD.sparse = [
            28, 47, 63, 93, 123, 124, 129, 131, 136, 140, 196, 205, 213, 239,
            258, 275, 276, 286, 305, 339, 345, 350, 372, 394, 395, 443, 449,
            462, 468, 471, 484, 514, 525, 557, 565, 570, 576, 585, 600, 609,
            631, 632, 635, 642, 651, 683, 693, 694, 696, 699, 721, 734, 772,
            790, 792, 795, 805, 806, 833, 836, 842, 846, 892, 896, 911, 914,
            927, 936, 947, 953, 955, 962, 965, 989, 990, 996]

        P = RDSE_Parameters()
        P.size     = GOLD.size
        P.sparsity = .08
        P.radius   = 12
        P.seed     = 42
        R = RDSE( P )
        A = R.encode( 987654 )
        print( A )
        assert( A == GOLD )
Example #5
0
    def testPickle(self):
        """
        The pickling is successfull if pickle serializes and de-serialize the
        RDSE object. 
        Moreover, the de-serialized object shall give the same SDR than the 
        original encoder given the same scalar value to encode.
        """        
        rdse_params = RDSE_Parameters()
        rdse_params.sparsity = 0.1
        rdse_params.size = 100
        rdse_params.resolution = 0.1
        rdse_params.seed = 1997

        rdse = RDSE(rdse_params)
        filename = "RDSE_testPickle"

        try:
            with open(filename, "wb") as f:
                pickle.dump(rdse, f)
        except:
            dump_success = False
        else:
            dump_success = True

        assert(dump_success)

        try:
            with open(filename, "rb") as f:
                rdse_loaded = pickle.load(f)
        except:
            read_success = False
        else:
            read_success = True

        assert(read_success)
        value_to_encode = 69003        
        SDR_original = rdse.encode(value_to_encode)
        SDR_loaded = rdse_loaded.encode(value_to_encode)

        assert(SDR_original == SDR_loaded)
Example #6
0
    def testRadiusResolution(self):
        """ Check that these arguments are equivalent. """
        # radius -> resolution
        P = RDSE_Parameters()
        P.size       = 2000
        P.activeBits = 100
        P.radius     = 12
        R = RDSE( P )
        self.assertAlmostEqual( R.parameters.resolution, 12./100, places=5)

        # resolution -> radius
        P = RDSE_Parameters()
        P.size       = 2000
        P.activeBits = 100
        P.resolution = 12
        R = RDSE( P )
        self.assertAlmostEqual( R.parameters.radius, 12*100, places=5)

        # Moving 1 resolution moves 1 bit (usually)
        P = RDSE_Parameters()
        P.size       = 2000
        P.activeBits = 100
        P.resolution = 3.33
        P.seed       = 42
        R = RDSE( P )
        sdrs = []
        for i in range(100):
            X = i * ( R.parameters.resolution )
            sdrs.append( R.encode( X ) )
            print("X", X, sdrs[-1])
        moved_one = 0
        moved_one_samples = 0
        for A, B in zip(sdrs, sdrs[1:]):
            delta = A.getSum() - A.getOverlap( B )
            if A.getSum() == B.getSum():
                assert( delta < 2 )
                moved_one += delta
                moved_one_samples += 1
        assert( moved_one >= .9 * moved_one_samples )
Example #7
0
    def testJSONSerialization(self):
        """
        This test is to insure that Python can access the C++ serialization functions.
        Serialization is tested more completely in C++ unit tests. Just checking 
        that Python can access it.
        """
        rdse_params = RDSE_Parameters()
        rdse_params.sparsity = 0.1
        rdse_params.size = 1000
        rdse_params.resolution = 0.1
        rdse_params.seed = 1997

        rdse = RDSE(rdse_params)
        filename = 'RDSE_testPickle'
        rdse.saveToFile(filename, 'JSON')

        rdse_loaded = RDSE()
        rdse_loaded.loadFromFile(filename, 'JSON')

        value_to_encode = 69003
        SDR_original = rdse.encode(value_to_encode)
        SDR_loaded = rdse_loaded.encode(value_to_encode)

        assert (SDR_original == SDR_loaded)