Ejemplo n.º 1
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)
Ejemplo n.º 2
0
    def testStatistics(self):
        p = ScalarEncoderParameters()
        p.size = 100
        p.activeBits = 10
        p.minimum = 0
        p.maximum = 20
        p.clipInput = True
        enc = ScalarEncoder(p)
        del p
        out = SDR(enc.parameters.size)
        mtr = Metrics(out, 9999)

        # The activation frequency of bits near the endpoints of the range is a
        # little weird, because the bits at the very end are not used as often
        # as the ones in the middle of the range, unless clipInputs is enabled.
        # If clipInputs is enabled then the bits 1 radius from the end get used
        # twice as often as the should because they respond to inputs off
        # outside of the valid range as well as inputs inside of the range.
        for i in np.linspace(
                enc.parameters.minimum - enc.parameters.radius / 2,
                enc.parameters.maximum + enc.parameters.radius / 2, 100 + 10):
            enc.encode(i, out)
            # print( i, out.sparse )

        print(str(mtr))
        assert (mtr.sparsity.min() > .95 * .10)
        assert (mtr.sparsity.max() < 1.05 * .10)
        assert (mtr.activationFrequency.min() > .50 * .10)
        assert (mtr.activationFrequency.max() < 1.75 * .10)
        assert (mtr.overlap.min() > .85)
Ejemplo n.º 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)
Ejemplo n.º 4
0
    def testStatistics(self):
        gc = GridCellEncoder(size=200,
                             sparsity=.25,
                             periods=[6, 8.5, 12, 17, 24],
                             seed=42)
        sdr = SDR(gc.dimensions)
        M = Metrics(sdr, 999999)
        for x in range(1000):
            gc.encode([-x, 0], sdr)
        print(M)
        assert (M.sparsity.min() > .25 - .02)
        assert (M.sparsity.max() < .25 + .02)
        assert (M.activationFrequency.min() > .25 - .05)
        assert (M.activationFrequency.max() < .25 + .05)

        # These are approximate...
        assert (M.overlap.min() > .5)
        assert (M.overlap.max() < .9)
        assert (M.overlap.mean() > .7)
        assert (M.overlap.mean() < .8)
Ejemplo n.º 5
0
    def testPeriodic(self):
        p = ScalarEncoderParameters()
        p.size = 100
        p.activeBits = 10
        p.minimum = 0
        p.maximum = 20
        p.periodic = True
        enc = ScalarEncoder(p)
        out = SDR(enc.parameters.size)
        mtr = Metrics(out, 9999)

        for i in range(201 * 10 + 1):
            x = (i % 201) / 10.
            enc.encode(x, out)
            # print( x, out.sparse )

        print(str(mtr))
        assert (mtr.sparsity.min() > .95 * .10)
        assert (mtr.sparsity.max() < 1.05 * .10)
        assert (mtr.activationFrequency.min() > .9 * .10)
        assert (mtr.activationFrequency.max() < 1.1 * .10)
        assert (mtr.overlap.min() > .85)
Ejemplo n.º 6
0
parameters.sparsity = args.sparsity

enc = ScalarEncoder(parameters)

#
# Run the encoder and measure some statistics about its output.
#
sdrs = []
n_samples = (enc.parameters.maximum -
             enc.parameters.minimum) / enc.parameters.resolution
n_samples = int(round(5 * n_samples))
for i in np.linspace(enc.parameters.minimum, enc.parameters.maximum,
                     n_samples):
    sdrs.append(enc.encode(i))

M = Metrics([enc.size], len(sdrs) + 1)
for s in sdrs:
    M.addData(s)
print("Statistics:")
print("Encoded %d inputs." % len(sdrs))
print("Output " + str(M))

#
# Plot the Receptive Field of each bit in the encoder.
#
rf = np.zeros([enc.size, len(sdrs)], dtype=np.uint8)
for i in range(len(sdrs)):
    rf[:, i] = sdrs[i].dense
plt.imshow(rf, interpolation='nearest')
plt.title("ScalarEncoder Receptive Fields")
plt.ylabel("Cell Number")
Ejemplo n.º 7
0
                        help='')
    parser.add_argument('--periods', type=float, nargs='+',
                        default = [6 * (2**.5)**i for i in range(5)],
                        help='')

    args = parser.parse_args()
    print('Module Periods', args.periods)

    gc = GridCellEncoder(
        size     = 100,
        sparsity = args.sparsity,
        periods  = args.periods,)

    gc_sdr = SDR( gc.dimensions )

    gc_statistics = Metrics(gc_sdr, args.arena_size ** 2)

    assert( args.arena_size >= 10 )
    rf = np.empty((gc.size, args.arena_size, args.arena_size))
    for x in range(args.arena_size):
        for y in range(args.arena_size):
            gc.encode([x, y], gc_sdr)
            rf[:, x, y] = gc_sdr.dense.ravel()

    print(gc_statistics)

    rows       = 5
    cols       = 6
    n_subplots = rows * cols
    assert(gc.size > n_subplots)
    samples    = np.linspace( 0, gc.size-1, n_subplots, dtype=np.int )