Example #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
Example #2
0
def test_hopfield_prototype_n(n, kp=1, ke=1, noise=0, iters=100):
    corruption = np.empty((iters, kp))
    bits = int(n * noise)

    # store the same number of items multiple times
    for i in xrange(iters):
        # generate random inputs
        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)
        # create hopfield net
        mem = hop.hopnet(cvecs)
        # read the items backout
        r = mem.readM(ex, 1000)
        # find the largest fraction of corrupted bits
        corruption[i] = np.mean(r ^ vecs, axis=0)

    return corruption
Example #3
0
def test_hopfield_prototype_n(n, kp=1, ke=1, noise=0, iters=100):
    corruption = np.empty((iters, kp))
    bits = int(n * noise)

    # store the same number of items multiple times
    for i in xrange(iters):
        # generate random inputs
        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)
        # create hopfield net
        mem = hop.hopnet(cvecs)
        # read the items backout
        r = mem.readM(ex, 1000)
        # find the largest fraction of corrupted bits
        corruption[i] = np.mean(r ^ vecs, axis=0)

    return corruption
Example #4
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
Example #5
0
def test_hopfield_noise_tolerance_n(n, k=1, noise=0, iters=100):
    if noise == 0:
        return test_hopfield_capacity_n(n, k=k, iters=iters)
    
    corruption = np.empty((iters, k))
    bits = int(n * noise)

    # store the same number of items multiple times
    for i in xrange(iters):
        # generate random inputs
        vecs = util.random_input(n, k)
        cvecs = util.corrupt(vecs, bits)
        # create hopfield net
        mem = hop.hopnet(vecs)
        # read the items backout
        r = mem.readM(cvecs, 1000)
        # find the largest fraction of corrupted bits
        corruption[i] = np.mean(r ^ vecs, axis=0)

    return corruption
Example #6
0
def test_hopfield_noise_tolerance_n(n, k=1, noise=0, iters=100):
    if noise == 0:
        return test_hopfield_capacity_n(n, k=k, iters=iters)

    corruption = np.empty((iters, k))
    bits = int(n * noise)

    # store the same number of items multiple times
    for i in xrange(iters):
        # generate random inputs
        vecs = util.random_input(n, k)
        cvecs = util.corrupt(vecs, bits)
        # create hopfield net
        mem = hop.hopnet(vecs)
        # read the items backout
        r = mem.readM(cvecs, 1000)
        # find the largest fraction of corrupted bits
        corruption[i] = np.mean(r ^ vecs, axis=0)

    return corruption
Example #7
0
def test_sdm_noise_tolerance_n(params, k=1, noise=0, iters=100):
    if noise == 0:
        return test_sdm_capacity_n(params, k=k, iters=iters)
    
    n, m, D = params
    mem = sdm.SDM(n, m, D)
    corruption = np.empty((iters, k))
    bits = int(n * noise)
    
    # store the same number of items multiple times
    for i in xrange(iters):
        # generate random inputs
        vecs = util.random_input(n, k)
        cvecs = util.corrupt(vecs, bits)
        # 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(cvecs)
        # find the largest fraction of corrupted bits
        corruption[i] = np.mean(r ^ vecs, axis=0)
        
    return corruption
Example #8
0
def test_sdm_noise_tolerance_n(params, k=1, noise=0, iters=100):
    if noise == 0:
        return test_sdm_capacity_n(params, k=k, iters=iters)

    n, m, D = params
    mem = sdm.SDM(n, m, D)
    corruption = np.empty((iters, k))
    bits = int(n * noise)

    # store the same number of items multiple times
    for i in xrange(iters):
        # generate random inputs
        vecs = util.random_input(n, k)
        cvecs = util.corrupt(vecs, bits)
        # 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(cvecs)
        # find the largest fraction of corrupted bits
        corruption[i] = np.mean(r ^ vecs, axis=0)

    return corruption
Example #9
0
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])
inputs = np.hstack([face, X, hi, num1, num2])

hop = hopfield.hopnet(inputs)
addresses = inputs.copy()
numNeurons, numPatterns = addresses.shape

numIters = 1000

print "uncorrupted tests"
for i in xrange(numPatterns):
    a = addresses[:, i]
    d = hop.read(a, numIters).reshape((10, 10), order='F')
    plt.figure(i)
    plot_io(a.reshape((10, 10), order='F'), d)

print "now with corruption"
for i in xrange(numPatterns):
    a = corrupt(addresses[:, i], 10)
    d = hop.read(a, numIters).reshape((10, 10), order='F')
    plt.figure(i + numPatterns)
    plot_io(a.reshape((10, 10), order='F'), d)
Example #10
0
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
n = 15 # number of exemplars

exemplars = np.empty((100, n), dtype='i4')
plt.figure(1)
plt.clf()
for i in xrange(n):
    e = corrupt(face, c)
    exemplars[:, i] = e[:, 0]

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
    data = np.empty((lenPatterns, numPatterns)).astype('i4')
    data[:, :-1] = addresses[:, 1:]
    data[:, -1] = addresses[:, -1]
        
    mem.writeM(addresses, data)

    # plot what we just wrote
    for i in xrange(numPatterns):
        plt.figure(50+curSeq)
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])
inputs = np.hstack([face, X, hi, num1, num2])

hop = hopfield.hopnet(inputs)
addresses = inputs.copy()
numNeurons, numPatterns = addresses.shape

numIters = 1000

print "uncorrupted tests"
for i in xrange(numPatterns):
    a = addresses[:, i]
    d = hop.read(a, numIters).reshape((10, 10), order='F')
    plt.figure(i)
    plot_io(a.reshape((10, 10), order='F'), d)

print "now with corruption"
for i in xrange(numPatterns):
    a = corrupt(addresses[:, i], 10)
    d = hop.read(a, numIters).reshape((10, 10), order='F')
    plt.figure(i + numPatterns)
    plot_io(a.reshape((10, 10), order='F'), d)
# 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
    data = np.empty((lenPatterns, numPatterns)).astype('i4')
    data[:, :-1] = addresses[:, 1:]
    data[:, -1] = addresses[:, -1]

    mem.writeM(addresses, data)

    # plot what we just wrote
    for i in xrange(numPatterns):
        plt.figure(50 + curSeq)
        plt.subplot(1, numPatterns, i + 1)
Example #14
0
m = M[-1]
nex = 9

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([], [])
    save("vi_ex%d" % i)
Example #15
0
from storage import SecureStorage
from util import corrupt

K = int(input("Enter value of k: "))
E = int(input("Enter maximum redundance (value of e): "))

message = input("Enter your message here: ")
length = len(message)

storage = SecureStorage(k=K, e=E)

print("\nGenerating message shares now")
m_enc = storage.encode(message)

print("Generated shares: ", m_enc)

print("\nCorrupting {0} random blocks of encoded message".format(E))
m_corr = corrupt(m_enc, e=E)
print("Corrupted message", m_corr)

print("\nDecoding message now")
m_dec = storage.decode(m_corr)

print("Final decoded message: ", m_dec[::-1][:length][::-1])
Example #16
0
m = M[-1]
nex = 9

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,