示例#1
0
def test_sdm_prototype_n(params, kp=1, ke=1, noise=0, iters=100):
    n, m, D = params
    mem = sdm.SDM(n, m, D)
    corruption = np.empty((iters, kp))
    bits = int(n * noise)
    
    # store the same number of items multiple times
    for i in xrange(iters):
        # generate random prototype and exemplars
        vecs = util.random_input(n, kp)
        cvecs = vecs[..., None] * np.ones((n, kp, ke), dtype='i4')
        cvecs = util.corrupt(
            cvecs.reshape((n, kp*ke)),
            bits, with_replacement=True)
        ex = util.corrupt(vecs, bits)
        # reset the memory to its original state
        mem.reset()
        # write random inputs to memory
        mem.writeM(cvecs, cvecs)
        # read the items back out
        r = mem.readM(ex)
        # find the fraction of corrupted bits
        corruption[i] = np.mean(r ^ vecs, axis=0)

    return corruption
示例#2
0
def get_model(addresses_path) -> sdm_lib.SDM:
    scanner_type = sdm_lib.SDM_SCANNER_OPENCL
    address_space = sdm_lib.AddressSpace.init_from_b64_file(addresses_path.encode("utf-8"))
    counter = sdm_lib.Counter.init_zero(600, 3_000_000)

    sdm = sdm_lib.SDM(address_space, counter, 8, scanner_type)
    return sdm
示例#3
0
def test_sdm_capacity_n(params, k=1, iters=100):
    n, m, D = params
    mem = sdm.SDM(n, m, D)
    corruption = np.empty((iters, k))
    
    # store the same number of items multiple times
    for i in xrange(iters):
        # generate random inputs
        vecs = util.random_input(n, k)
        # reset the memory to its original state
        mem.reset()
        # write random inputs to memory
        mem.writeM(vecs, vecs)
        # read the items back out
        r = mem.readM(vecs)
        # find the largest fraction of corrupted bits
        corruption[i] = np.mean(r ^ vecs, axis=0)
        
    return corruption
示例#4
0
scanner_type = sdmlib.SDM_SCANNER_OPENCL

# Generate an address space with 1,000,000 random 1,000-bit bitstrings.
address_space = sdmlib.AddressSpace.init_random(bits, sample)

# Generate 1,000,000 counters initialized with value zero in the RAM memory.
counter = sdmlib.Counter.init_zero(bits, sample)

# Create a file to store the 1,000,000 counters initialized with value zero.
# You do not need to provide file extension, because it will generate two files
# and automatically included the extension for you.
#counter = sdmlib.Counter.create_file('sdm-10w', bits, sample)

# Create an SDM with the generated address space and counter.
# The scans will be performed using the OpenCL scanner.
sdm = sdmlib.SDM(address_space, counter, 451, scanner_type)

v = []
for i in range(10):
    print(i)
    # Generate a random 1,000-bit bitstring.
    b = sdmlib.Bitstring.init_random(1000)

    # Write the bitstring to the SDM.
    sdm.write(b, b)
    v.append(b)
print(len(v), "bitstring wrote into memory.")

# Copy the bitstring from v[0].
# We have to make a copy because the flip_random_bits function changes the bitstring itself.
b = sdmlib.Bitstring.init_from_bitstring(v[0])
示例#5
0
data = np.load('patterns.npz')
X = data['X']
hi = data['hi']
face = data['face']
num1 = data['num1']
num2 = data['num2']
num3 = data['num3']
num4 = data['num4']
data.close()
inputs = np.hstack([face, X, hi, num1, num2, num3, num4])

# <codecell>

reload(sdm)
mem = sdm.SDM(100, 10000, 40)
print "Addresses in hamming radius:", mem._select(inputs).sum(axis=0)
addresses = inputs.copy()
mem.writeM(addresses, inputs)

# <codecell>

for i in xrange(7):
    a = corrupt(addresses[:, i], 10)
    d = mem.read(a).reshape((10, 10), order='F')
    plt.figure(i)
    plot_io(a.reshape((10, 10), order='F'), d)

# <codecell>

c = 5 # amount of bits to corrupt
figSize = (np.sqrt(lenPatterns)).astype('i4')

# number of sequences ( this is similar to # of exemplars )
numSequences = 10

# percentage of bits to corrupt
numCorrupt = 25

# number of addresses in SDM
numStorage = 10000

# hamming radius
D = 112

reload(sdm)
mem = sdm.SDM(lenPatterns, numStorage, D)
print "Addresses in hamming radius:", mem._select(inputs).sum(axis=0)

for curSeq in xrange(numSequences):
    # store copy of inputs in addresses
    addresses = inputs.copy()

    # corrupt the patterns
    for curPat in xrange(numPatterns):
        a = addresses[:, curPat].copy()
        d = corrupt(addresses[:, curPat], numCorrupt)
        addresses[:, curPat] = d
#        plt.figure(curPat)
#        plot_io(a.reshape((figSize, figSize), order='F'), d.reshape((figSize,figSize), order='F'))

# write data to address curPat with address curPat+1
示例#7
0
data = io.loadmat('numbers.mat')
zero = data['zero']
one = data['one']
two = data['two']
three = data['three']
four = data['four']
five = data['five']
six = data['six']

inputs = np.hstack([zero, one, two, three, four, five, six])

vec = six.copy()
cvecs = util.corrupt(vec * np.ones((n, nex), dtype='i4'), b)
ex = util.corrupt(vec.copy(), b)

mem1 = sdm.SDM(n, m, D)
mem1.writeM(cvecs, cvecs)
r1 = mem1.readM(ex)

mem2 = hop.hopnet(cvecs)
r2 = mem2.readM(ex, 1000)

for i in xrange(nex):
    plt.clf()
    plt.imshow(cvecs[:, i].reshape((16, 16), order='F'),
               cmap='gray',
               vmin=0,
               vmax=1,
               interpolation='nearest')
    plt.xticks([], [])
    plt.yticks([], [])