Example #1
0
def test_hopfield_capacity_n(n, k=1, iters=100):
    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)
        # create hopfield net
        mem = hop.hopnet(vecs)
        # read the items backout
        r = mem.readM(vecs, 1000)
        # find the largest fraction of corrupted bits
        corruption[i] = np.mean(r ^ vecs, axis=0)

    return corruption
Example #2
0
def test_hopfield_capacity_n(n, k=1, iters=100):
    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)
        # create hopfield net
        mem = hop.hopnet(vecs)
        # read the items backout
        r = mem.readM(vecs, 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_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 #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_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 #7
0
    return newarr


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])
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)
    newarr = arr[:, 0].reshape((n, n), order='F').ravel()[:, None]
    return newarr

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])
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)
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)

plt.clf()
util.plot_io(ex.reshape((16, 16), order="F"), r1.reshape((16, 16), order="F"))
save("vi_sdm")

plt.clf()
util.plot_io(ex.reshape((16, 16), order="F"), 1 - r2.reshape((16, 16), order="F"))
Example #10
0
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)

plt.clf()
util.plot_io(ex.reshape((16, 16), order='F'), r1.reshape((16, 16), order='F'))