Welcome to Sparse Distributed Memory Framework’s documentation!

Welcome! This is the documentation for Sparse Distributed Memory Framework.

Parts of the documentation

The documentation is organized in two parts: C API and Python Library.

It is suggested to use the Python Library. It is a facade which simplifies the creation and manipulation of SDM instances. It also works in the Jupyter Notebook.

The C API is the low level module which may be extended to add new operations to the SDM.

SDM Library

Bitstring

AddressSpace

Counter

SDM

C API

Bitstring’s functions

The following functions are used to create, destroy, and manipulate bitstrings. Many of them have a parameter len, which is the number of bytes of the bitstring. It was a design choice which saves computing it everytime.

typedef uint64_t bitstring_t
void bs_init_bitcount_table()

Initialize a 64kb in RAM which is used to improve performance when calculating the distance between two bitstrings.

bitstring_t* bs_alloc(const unsigned int len)

Allocate memory for a bitstring with len bytes.

void bs_free(bitstring_t *bs)

Free the memory of a bitstring.

void bs_copy(bitstring_t *dst, const bitstring_t *src, unsigned int len)

Copy one bitstring into another.

void bs_init_zeros(bitstring_t *bs, unsigned int len, unsigned int bits_remaining)

Initialize a bitstring with all bits equal to zero. The bitstring’s memory must have already been allocated.

void bs_init_ones(bitstring_t *bs, unsigned int len, unsigned int bits_remaining)

Initialize a bitstring with all bits equal to one. The bitstring’s memory must have already been allocated.

void bs_init_random(bitstring_t *bs, unsigned int len, unsigned int bits_remaining)

Initialize a bitstring with random bits. Each bit is sampled from Bernoulli trial with p=0.5. The bitstring’s memory must have already been allocated.

void bs_init_hex(bitstring_t *bs, unsigned int len, char *hex)

Initialize a bitstring with random bits. Each bit is sampled from Bernoulli trial with p=0.5. The bitstring’s memory must have already been allocated.

void bs_init_b64(bitstring_t *bs, char *b64)

Initialize a bitstring from a base64 string. The bitstring’s memory must have already been allocated.

void bs_to_hex(char *buf, bitstring_t *bs, unsigned int len)

Initialize a bitstring from a hexadecimal string. The bitstring’s memory must have already been allocated.

void bs_to_b64(char *buf, bitstring_t *bs, unsigned int len)

Generate the base64 string representation of the bitstring.

int bs_distance(const bitstring_t *bs1, const bitstring_t *bs2, const unsigned int len)

Calculate the hamming distance between two bitstrings.

unsigned int bs_get_bit(bitstring_t *bs, unsigned int bit)

Return a specific bit from a bitstring.

void bs_set_bit(bitstring_t *bs, unsigned int bit, unsigned int value)

Change the value of a specific bit from a bitstring.

void bs_flip_bit(bitstring_t *bs, unsigned int bit)

Flip a specific bit from a bitstring.

int bs_flip_random_bits(bitstring_t *bs, unsigned int bits, unsigned int flips)

Randomly choose flips bits of the bitstring. It is used to generate a random bitstring with a given distance from another bitstring.

void bs_xor(bitstring_t *bs1, const bitstring_t *bs2, const unsigned int len)

Calculate the XOR bitwise operation between two bitstrings. The result is stored in bs1.

void bs_and(bitstring_t *bs1, const bitstring_t *bs2, const unsigned int len)

Calculate the AND bitwise operation between two bitstrings. The result is stored in bs1.

void bs_or(bitstring_t *bs1, const bitstring_t *bs2, const unsigned int len)

Calculate the OR bitwise operation between two bitstrings. The result is stored in bs1.

void bs_average(bitstring_t *bs1, const bitstring_t *bs2, const unsigned int len)

Calculate average between the bitstrings. The result is stored in bs1.

Address Space’s functions

struct address_space_s()
unsigned int bits

SDM dimension.

unsigned int sample

Number of hard-locations.

bitstring_t **addresses

This approach allocates a continuous chunk of memory for all bitstring addresses. The addresses allows the use of array notation: addresses[0], addresses[1], …

Let a be addresses. Then:

          a[0]   a[1]   a[2]   a[3]   a[4]
          |      |      |      |      |
          v      v      v      v      v
bs_data = xxxxxx|xxxxxx|xxxxxx|xxxxxx|xxxxxx
unsigned int bs_len;
unsigned int bs_bits_remaining;
bitstring_t *bs_data;
int as_init(struct address_space_s *this, unsigned int bits, unsigned int sample)

Testing…

int as_init_random(struct address_space_s *this, unsigned int bits, unsigned int sample)

Testing again..

int as_init_from_b64_file(struct address_space_s *this, char *filename)
int as_free(struct address_space_s *this)
int as_save_b64_file(const struct address_space_s *this, char *filename)
int as_scan_linear(const struct address_space_s *this, const bitstring_t *bs, unsigned int radius, uint8_t *buf)
int as_scan_thread(const struct address_space_s *this, const bitstring_t *bs, unsigned int radius, uint8_t *buf, unsigned int thread_count)
void as_print_summary(struct address_space_s *this)
void as_print_addresses_b64(struct address_space_s *this)
void as_print_addresses_hex(struct address_space_s *this)

OpenCL Scanner

int as_scanner_opencl_init(struct opencl_scanner_s *this, struct address_space_s *as, char *opencl_source)
void as_scanner_opencl_free(struct opencl_scanner_s *this)
int as_scan_opencl(struct opencl_scanner_s *this, bitstring_t *bs, unsigned int radius, uint8_t *result)

Counter’s functions

typedef int counter_t
struct counter_s
unsigned int bits
unsigned int sample
int fd
char *filename
counter_t **counter
counter_t *data
int counter_init(struct counter_s *this, unsigned int bits, unsigned int sample)
int counter_init_file(char *filename, struct counter_s *this)
void counter_free(struct counter_s *this)
void counter_print_summary(struct counter_s *this)
void counter_print(struct counter_s *this, unsigned int index)
int counter_add_bitstring(struct counter_s *this, unsigned int index, bitstring_t *bs)
int counter_add_counter(struct counter_s *c1, unsigned int idx1, struct counter_s *c2, unsigned int idx2)
int counter_to_bitstring(struct counter_s *this, unsigned int index, bitstring_t *bs)
int counter_create_file(char *filename, unsigned int bits, unsigned int sample)

SDM’s functions

struct sdm_s
unsigned int bits
unsinged int sample
unsinged int scanner_type
SDM_SCANNER_LINEAR
SDM_SCANNER_THREAD
SDM_SCANNER_OPENCL
struct opencl_scanner_s *opencl_opts
unsinged int thread_count
struct address_space_s *address_space
struct counter_s *counter
int sdm_init_linear(struct sdm_s *sdm, struct address_space_s *address_space, struct counter_s *counter)
int sdm_init_thread(struct sdm_s *sdm, struct address_space_s *address_space, struct counter_s *counter, unsigned int thread_count)
int sdm_init_opencl(struct sdm_s *sdm, struct address_space_s *address_space, struct counter_s *counter, char *opencl_source)
void sdm_free(struct sdm_s *sdm)
int sdm_write(struct sdm_s *sdm, bitstring_t *addr, unsigned int radius, bitstring_t *datum)
int sdm_read(struct sdm_s *sdm, bitstring_t *addr, unsigned int radius, bitstring_t *output)
int sdm_iter_read(struct sdm_s *sdm, bitstring_t *addr, unsigned int radius, unsigned int max_iter, bitstring_t *output)

Examples

Distance between bitstrings

Here we will draw the histogram of the distance between two random bitstrings.

In [1]:
import sdm as sdmlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
#%matplotlib inline
In [2]:
distances = []
for i in xrange(10000):
    b1 = sdmlib.Bitstring.init_random(1000)
    b2 = sdmlib.Bitstring.init_random(1000)
    distances.append(b1.distance_to(b2))
In [3]:
mu = 500
sigma = (1000**(0.5))/2.0
x = np.linspace(0, 1000, 1000)
y = mlab.normpdf(x, mu, sigma)
In [4]:
plt.hist(distances, bins=range(1001), density=True)
plt.plot(x, y, 'r', linewidth=2.0)
plt.xlim(0, 1000)
plt.show()
_images/notebooks_Distance_between_bitstrings_4_0.png
In [5]:
plt.hist(distances, bins=range(1001), density=True)
plt.plot(x, y, 'r', linewidth=2.0)
plt.xlim(400, 600)
plt.show()
_images/notebooks_Distance_between_bitstrings_5_0.png

Generates a new SDM

In [1]:
import sdm as sdmlib
In [2]:
bits = 1000
sample = 1000000
scanner_type = sdmlib.SDM_SCANNER_OPENCL
In [3]:
# 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)
In [4]:
v = []
for i in xrange(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 ''
print '{} bitstring wrote into memory.'.format(len(v))
0 1 2 3 4 5 6 7 8 9
10 bitstring wrote into memory.
In [5]:
# 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])
b.flip_random_bits(400)

# Read the bitstring from the SDM and checks the distance from the retrieved bitstring and v[0].
c = sdm.read(b)
print 'Distance', c.distance_to(v[0])
Distance 134
In [6]:
# Save the address space into the file 'sdm-10w.as'.
# The recommended extension for an address space is '.as'.

# Although we have used 10w as an indication of 10 writes to the memory, the
# address space is not affected by the writes. It is just a reference to help us
# remeber that this address space has been used together with the counters.
#address_space.save('sdm-10w.as');

Kanerva’s Figure 1.2 (page 25)

In [1]:
import sdm as sdmlib
import matplotlib.pyplot as plt
from IPython.display import clear_output
%matplotlib inline
In [2]:
bits = 1000
sample = 1000000

address_space = sdmlib.AddressSpace.init_random(bits, sample)
In [3]:
def calculate_probabilities():
    from math import factorial
    comb = lambda a, b: factorial(a)/factorial(b)/factorial(a-b)
    acc = [0]
    for i in xrange(1001):
        acc.append(acc[-1] + comb(1000, i))
    x = range(0, 1001)
    y = [acc[i]/float(2**1000) for i in xrange(1001)]
    return x, y
In [4]:
x, y = calculate_probabilities()
plt.plot(x, y);
_images/notebooks_Kanerva's_Figure_1.2_4_0.png
In [5]:
def run(radius):
    x = range(0, 1001)
    y = []
    for i, dist in enumerate(x):
        clear_output(wait=True)
        print 'Distance: {:4d} ({:.2f}%)'.format(dist, 100.*(i+1)/len(x))

        b1 = sdmlib.Bitstring.init_random(bits)
        b2 = sdmlib.Bitstring.init_from_bitstring(b1)
        b2.flip_random_bits(dist)
        assert b1.distance_to(b2) == dist

        h1 = set(address_space.scan_thread(b1, radius, 4))
        h2 = set(address_space.scan_thread(b2, radius, 4))

        y.append(len(h1&h2))
    return x, y
In [6]:
v = []
In [7]:
v.append((451, run(451)))
Distance: 1000 (100.00%)
In [10]:
v.append((480, run(480)))
Distance: 1000 (100.00%)
In [9]:
v.append((500, run(500)))
Distance: 1000 (100.00%)
In [14]:
plt.figure(figsize=(8, 6), dpi=100)
plt.hold(True)
for d, points in v[:-1]:
    x, y = points
    ymax = max(y)
    y = [float(a)/ymax for a in y]
    plt.plot(x, y, label='Radius: {}'.format(d))
plt.xlabel('Distance between two random bitstrings')
plt.ylabel('Intersection of their circles')
plt.legend()
plt.hold(False)
/Library/Python/2.7/site-packages/ipykernel_launcher.py:2: MatplotlibDeprecationWarning: pyplot.hold is deprecated.
    Future behavior will be consistent with the long-time default:
    plot commands add elements without first clearing the
    Axes and/or Figure.

/Library/Python/2.7/site-packages/ipykernel_launcher.py:11: MatplotlibDeprecationWarning: pyplot.hold is deprecated.
    Future behavior will be consistent with the long-time default:
    plot commands add elements without first clearing the
    Axes and/or Figure.
  # This is added back by InteractiveShellApp.init_path()
_images/notebooks_Kanerva's_Figure_1.2_10_1.png

Kanerva’s Table 7.3 (page 70)

In [1]:
import sdm as sdmlib
import matplotlib.pyplot as plt
from IPython.display import clear_output
%matplotlib inline
In [2]:
bits = 1000
sample = 1000000
radius = 451
scanner_type = sdmlib.SDM_SCANNER_OPENCL
In [3]:
#address_space = sdmlib.AddressSpace.init_from_b64_file('sdm-10000w.as')
#counter = sdmlib.Counter.load_file('sdm-10000w')
address_space = sdmlib.AddressSpace.init_random(bits, sample)
counter = sdmlib.Counter.init_zero(bits, sample)
sdm = sdmlib.SDM(address_space, counter, radius, scanner_type)
In [4]:
for i in range(10000):
    clear_output(wait=True)
    print i
    bs = sdmlib.Bitstring.init_random(1000)
    sdm.write(bs, bs)
9999
In [5]:
b = sdmlib.Bitstring.init_random(1000)
sdm.write(b, b)
In [8]:
from IPython.display import clear_output

max_iter = 10
samples = 1

distances = []
x = range(0, 1001)
for i, dist in enumerate(x):
    clear_output(wait=True)
    print 'Distance: {:4d} ({:.2f}%)'.format(dist, 100.*(i+1)/len(x))
    v = []
    for j in range(samples):
        c = sdmlib.Bitstring.init_from_bitstring(b)
        c.flip_random_bits(dist)
        assert c.distance_to(b) == dist
        d = sdm.iter_read(c, max_iter=max_iter)
        v.append(d.distance_to(b))
    distances.append(1.0*sum(v)/len(v))
print 'Done!'
Distance: 1000 (100.00%)
Done!
In [9]:
plt.figure(figsize=(8, 6), dpi=100)
plt.plot(x, distances)
plt.plot(x, x, 'k')
plt.plot(x, [500]*len(x), 'k:')
#plt.title('Kanerva\'s Figure 7.3')
if max_iter == 1:
    plt.ylabel('New distance (after a single read)')
else:
    plt.ylabel('New distance (after {} iterative reads)'.format(max_iter))
plt.xlabel('Old distance')
plt.grid()
#plt.axis([0, 1000, 0, 1000]);
plt.axis([x[0], x[-1], x[0], x[-1]]);
_images/notebooks_Kanerva-Table-7.3_7_0.png
In [ ]:
c = sdmlib.Bitstring.init_from_bitstring(b)
c.flip_random_bits(1000)
d = c
print 0, b.distance_to(d)
for i in xrange(10):
    d = sdm.read(d)
    print i+1, b.distance_to(d)

Noise filter

In [1]:
import sdm as sdmlib
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
import urllib, cStringIO
import random
from IPython.core.display import display, Image as IPythonImage
%matplotlib inline
In [2]:
width = 30
height = 30
noise_flip = True
In [3]:
def gen_img(letter='A'):
    img = Image.new('RGBA', (30, 30), (255, 255, 255))
    font = ImageFont.truetype('Arial.ttf', 30)
    draw = ImageDraw.Draw(img)
    draw.text((5, 0), letter, (0, 0, 0), font=font)
    return img
In [4]:
def gen_noise_add(img, p=0.15, flip=False):
    img2 = img.copy()
    draw = ImageDraw.Draw(img2)
    for py in xrange(height):
        for px in xrange(width):
            if random.random() < p:
                if flip:
                    pixel = img.getpixel((px, py))
                    value = sum([int(x/255+0.5) for x in pixel[:3]])//3
                    assert value == 0 or value == 1
                    value = (1 - value)*255
                    draw.point((px, py), fill=(value, value, value))
                else:
                    draw.point((px, py), fill=(0, 0, 0))
    return img2
In [5]:
img = gen_img();
img2 = gen_noise_add(img, flip=noise_flip)
plt.subplot(1, 2, 1)
plt.imshow(img)
plt.subplot(1, 2, 2)
plt.imshow(img2);
_images/notebooks_Noise_filter_5_0.png
In [6]:
def to_bitstring(img):
    v = []
    bs = sdmlib.Bitstring.init_ones(1000)
    for py in xrange(height):
        for px in xrange(width):
            pixel = img.getpixel((px, py))
            value = sum([int(x/255+0.5) for x in pixel[:3]])//3
            assert value == 0 or value == 1
            idx = px+width*py
            assert idx >= 0 and idx < 1000, 'Ops {} {} {}'.format(x, y, idx)
            bs.set_bit(idx, value)
            v.append(value)
    v2 = [bs.get_bit(i) for i in xrange(height*width)]
    assert v == v2
    return bs
In [7]:
def to_img(bs):
    img = Image.new('RGBA', (30, 30), (255, 255, 255))
    draw = ImageDraw.Draw(img)
    for py in xrange(height):
        for px in xrange(width):
            idx = px+width*py
            assert idx >= 0 and idx < 1000, 'Ops {} {} {}'.format(x, y, idx)
            x = 255*bs.get_bit(idx)
            draw.point((px, py), fill=(x, x, x))
    return img
In [8]:
bits = 1000
sample = 1000000
scanner_type = sdmlib.SDM_SCANNER_OPENCL
In [9]:
address_space = sdmlib.AddressSpace.init_random(bits, sample)
counter = sdmlib.Counter.init_zero(bits, sample)
sdm = sdmlib.SDM(address_space, counter, 451, scanner_type)
In [10]:
def fill_memory(letter, p=0.15, n=100):
    cols = 15
    rows = n//cols + 1
    plt.figure(figsize=(20,10))
    for i in xrange(n):
        img = gen_img(letter=letter);
        img2 = gen_noise_add(img, flip=noise_flip)
        #display(img2)
        plt.subplot(rows, cols, i+1)
        plt.imshow(img2)
        bs = to_bitstring(img2)
        sdm.write(bs, bs)
In [11]:
fill_memory('T')
_images/notebooks_Noise_filter_11_0.png
In [12]:
fill_memory('I')
_images/notebooks_Noise_filter_12_0.png
In [13]:
def read(letter, n=6, p=0.25):
    n = 6
    cols = 7
    rows = n//cols + 1
    plt.figure(figsize=(20,10))

    img = gen_img(letter=letter);
    img2 = gen_noise_add(img, p=p, flip=noise_flip)
    plt.subplot(rows, cols, 1)
    plt.imshow(img2)

    for i in xrange(n):
        bs2 = to_bitstring(img2)
        bs3 = sdm.read(bs2)
        img3 = to_img(bs3)
        plt.subplot(rows, cols, i+2)
        plt.imshow(img3)
        img2 = img3
In [20]:
read('T', p=0.42)
_images/notebooks_Noise_filter_14_0.png
In [15]:
read('I', p=0.42)
_images/notebooks_Noise_filter_15_0.png

Classification Test #1

In [1]:
import sdm as sdmlib
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
import urllib, cStringIO
import random
from IPython.core.display import display, Image as IPythonImage
%matplotlib inline
In [2]:
width = 30
height = 30
noise_flip = True
In [3]:
def gen_img(letter='A'):
    img = Image.new('RGBA', (30, 30), (255, 255, 255))
    font = ImageFont.truetype('Arial.ttf', 30)
    draw = ImageDraw.Draw(img)
    draw.text((5, 0), letter, (0, 0, 0), font=font)
    return img
In [4]:
def gen_noise_add(img, p=0.15, flip=False):
    img2 = img.copy()
    draw = ImageDraw.Draw(img2)
    for py in xrange(height):
        for px in xrange(width):
            if random.random() < p:
                if flip:
                    pixel = img.getpixel((px, py))
                    value = sum([int(x/255+0.5) for x in pixel[:3]])//3
                    assert value == 0 or value == 1
                    value = (1 - value)*255
                    draw.point((px, py), fill=(value, value, value))
                else:
                    draw.point((px, py), fill=(0, 0, 0))
    return img2
In [5]:
img = gen_img();
img2 = gen_noise_add(img, p=0.05, flip=noise_flip)
plt.subplot(1, 2, 1)
plt.imshow(img)
plt.subplot(1, 2, 2)
plt.imshow(img2);
_images/notebooks_Classification_Test_1_5_0.png
In [6]:
def to_bitstring(img):
    v = []
    bs = sdmlib.Bitstring.init_ones(1000)
    for py in xrange(height):
        for px in xrange(width):
            pixel = img.getpixel((px, py))
            value = sum([int(x/255+0.5) for x in pixel[:3]])//3
            assert value == 0 or value == 1
            idx = px+width*py
            assert idx >= 0 and idx < 1000, 'Ops {} {} {}'.format(x, y, idx)
            bs.set_bit(idx, value)
            v.append(value)
    v2 = [bs.get_bit(i) for i in xrange(height*width)]
    assert v == v2
    return bs
In [7]:
def to_img(bs):
    img = Image.new('RGBA', (30, 30), (255, 255, 255))
    draw = ImageDraw.Draw(img)
    for py in xrange(height):
        for px in xrange(width):
            idx = px+width*py
            assert idx >= 0 and idx < 1000, 'Ops {} {} {}'.format(x, y, idx)
            x = 255*bs.get_bit(idx)
            draw.point((px, py), fill=(x, x, x))
    return img
In [8]:
bits = 1000
sample = 1000000
scanner_type = sdmlib.SDM_SCANNER_THREAD
In [9]:
address_space = sdmlib.AddressSpace.init_from_b64_file('sdm-letters.as')
counter = sdmlib.Counter.create_file('sdm-classification', bits, sample)
sdm = sdmlib.SDM(address_space, counter, 451, scanner_type)
In [10]:
for i in xrange(100):
    print i,
    b = sdmlib.Bitstring.init_random(1000)
    sdm.write(b, b)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
In [11]:
def fill_memory(letter, label_bs, p=0.1, n=100):
    cols = 15
    rows = n//cols + 1
    plt.figure(figsize=(20,10))
    for i in xrange(n):
        img = gen_img(letter=letter);
        img2 = gen_noise_add(img, p=p, flip=noise_flip)
        #display(img2)
        plt.subplot(rows, cols, i+1)
        plt.imshow(img2)
        bs = to_bitstring(img2)
        bs.xor(label_bs)
        sdm.write(bs, bs)
    plt.show()
In [12]:
def read(letter, label_bs, n=6, p=0.2, radius=None):
    n = 7
    cols = 15
    rows = n//cols + 1
    plt.figure(figsize=(20,10))

    img = gen_img(letter=letter);
    img2 = gen_noise_add(img, p=p, flip=noise_flip)
    plt.subplot(rows, cols, 1)
    plt.imshow(img2)

    for i in xrange(n):
        bs2 = to_bitstring(img2)
        bs2.xor(label_bs)
        bs3 = sdm.read(bs2, radius=radius)
        if bs3 == bs2:
            break
        bs3.xor(label_bs)
        img3 = to_img(bs3)
        plt.subplot(rows, cols, i+2)
        plt.imshow(img3)
        img2 = img3
In [13]:
labels = list('ABCD8OQ')
label_to_bs = {}
for x in labels:
    label_to_bs[x] = sdmlib.Bitstring.init_random(1000)
In [14]:
for x in labels:
    print 'Training for label {}...'.format(x)
    fill_memory(x, label_to_bs[x])
Training for label A...
_images/notebooks_Classification_Test_1_14_1.png
Training for label B...
_images/notebooks_Classification_Test_1_14_3.png
Training for label C...
_images/notebooks_Classification_Test_1_14_5.png
Training for label D...
_images/notebooks_Classification_Test_1_14_7.png
Training for label 8...
_images/notebooks_Classification_Test_1_14_9.png
Training for label O...
_images/notebooks_Classification_Test_1_14_11.png
Training for label Q...
_images/notebooks_Classification_Test_1_14_13.png
In [15]:
read('C', label_to_bs['C'])
read('C', label_to_bs['D'])
read('C', label_to_bs['O'])
_images/notebooks_Classification_Test_1_15_0.png
_images/notebooks_Classification_Test_1_15_1.png
_images/notebooks_Classification_Test_1_15_2.png
In [16]:
read('A', label_to_bs['C'], p=0.1)
_images/notebooks_Classification_Test_1_16_0.png
In [17]:
read('A', sdmlib.Bitstring.init_random(1000), p=0)
_images/notebooks_Classification_Test_1_17_0.png
In [18]:
def intersection(a, b):
    bs1 = to_bitstring(gen_img(letter=a))
    bs2 = to_bitstring(gen_img(letter=b))
    hl1 = set(address_space.scan_thread(bs1, 451, 4))
    hl2 = set(address_space.scan_thread(bs2, 451, 4))
    return len(hl1 & hl2)
print intersection('B', '8')
200

Classification Test #2

In [1]:
import sdm as sdmlib
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
import urllib, cStringIO
import random
from IPython.core.display import display, Image as IPythonImage
%matplotlib inline
In [2]:
width = 30
height = 30
noise_flip = True
In [3]:
def gen_img(letter='A'):
    img = Image.new('RGBA', (30, 30), (255, 255, 255))
    font = ImageFont.truetype('Arial.ttf', 30)
    draw = ImageDraw.Draw(img)
    draw.text((5, 0), letter, (0, 0, 0), font=font)
    return img
In [4]:
def gen_noise_add(img, p=0.15, flip=False):
    img2 = img.copy()
    draw = ImageDraw.Draw(img2)
    for py in xrange(height):
        for px in xrange(width):
            if random.random() < p:
                if flip:
                    pixel = img.getpixel((px, py))
                    value = sum([int(x/255+0.5) for x in pixel[:3]])//3
                    assert value == 0 or value == 1
                    value = (1 - value)*255
                    draw.point((px, py), fill=(value, value, value))
                else:
                    draw.point((px, py), fill=(0, 0, 0))
    return img2
In [5]:
img = gen_img();
img2 = gen_noise_add(img, p=0.05, flip=noise_flip)
plt.subplot(1, 2, 1)
plt.imshow(img)
plt.subplot(1, 2, 2)
plt.imshow(img2);
_images/notebooks_Classification_Test_2_5_0.png
In [6]:
def to_bitstring(img):
    v = []
    bs = sdmlib.Bitstring.init_ones(1000)
    for py in xrange(height):
        for px in xrange(width):
            pixel = img.getpixel((px, py))
            value = sum([int(x/255+0.5) for x in pixel[:3]])//3
            assert value == 0 or value == 1
            idx = px+width*py
            assert idx >= 0 and idx < 1000, 'Ops {} {} {}'.format(x, y, idx)
            bs.set_bit(idx, value)
            v.append(value)
    v2 = [bs.get_bit(i) for i in xrange(height*width)]
    assert v == v2
    return bs
In [7]:
def to_img(bs):
    img = Image.new('RGBA', (30, 30), (255, 255, 255))
    draw = ImageDraw.Draw(img)
    for py in xrange(height):
        for px in xrange(width):
            idx = px+width*py
            assert idx >= 0 and idx < 1000, 'Ops {} {} {}'.format(x, y, idx)
            x = 255*bs.get_bit(idx)
            draw.point((px, py), fill=(x, x, x))
    return img
In [8]:
bits = 1000
sample = 1000000
scanner_type = sdmlib.SDM_SCANNER_OPENCL
In [9]:
address_space = sdmlib.AddressSpace.init_from_b64_file('sdm-letters.as')
counter = sdmlib.Counter.create_file('sdm-classification-2', bits, sample)
sdm = sdmlib.SDM(address_space, counter, 451, scanner_type)
In [10]:
def fill_memory(letter, label_bs, p=0.1, n=100):
    cols = 15
    rows = n//cols + 1
    plt.figure(figsize=(20,10))
    for i in xrange(n):
        img = gen_img(letter=letter);
        img2 = gen_noise_add(img, p=p, flip=noise_flip)
        #display(img2)
        plt.subplot(rows, cols, i+1)
        plt.imshow(img2)
        bs = to_bitstring(img2)
        sdm.write(bs, label_bs)
    plt.show()
In [11]:
def read(letter, n=6, p=0.2, radius=None):
    n = 7
    cols = 15
    rows = n//cols + 1
    plt.figure(figsize=(20,10))

    img = gen_img(letter=letter);
    img2 = gen_noise_add(img, p=p, flip=noise_flip)
    plt.subplot(rows, cols, 1)
    plt.imshow(img2)

    bs2 = to_bitstring(img2)
    bs3 = sdm.read(bs2, radius=radius)

    label = min(label_to_bs.items(), key=lambda v: bs3.distance_to(v[1]))
    return label[0]
In [12]:
labels = list('ABCD8OQ0')
label_to_bs = {}
for x in labels:
    label_to_bs[x] = sdmlib.Bitstring.init_random(1000)
In [13]:
for x in labels:
    print 'Training for label {}...'.format(x)
    fill_memory(x, label_to_bs[x])
Training for label A...
_images/notebooks_Classification_Test_2_13_1.png
Training for label B...
_images/notebooks_Classification_Test_2_13_3.png
Training for label C...
_images/notebooks_Classification_Test_2_13_5.png
Training for label D...
_images/notebooks_Classification_Test_2_13_7.png
Training for label 8...
_images/notebooks_Classification_Test_2_13_9.png
Training for label O...
_images/notebooks_Classification_Test_2_13_11.png
Training for label Q...
_images/notebooks_Classification_Test_2_13_13.png
Training for label 0...
_images/notebooks_Classification_Test_2_13_15.png
In [14]:
read('C')
Out[14]:
'C'
_images/notebooks_Classification_Test_2_14_1.png
In [15]:
read('A')
Out[15]:
'A'
_images/notebooks_Classification_Test_2_15_1.png
In [16]:
read('D')
Out[16]:
'D'
_images/notebooks_Classification_Test_2_16_1.png
In [17]:
read('8')
Out[17]:
'8'
_images/notebooks_Classification_Test_2_17_1.png
In [18]:
read('B')
Out[18]:
'B'
_images/notebooks_Classification_Test_2_18_1.png
In [19]:
read('O')
Out[19]:
'O'
_images/notebooks_Classification_Test_2_19_1.png
In [35]:
read('Q')
Out[35]:
'Q'
_images/notebooks_Classification_Test_2_20_1.png
In [25]:
read('0')
Out[25]:
'0'
_images/notebooks_Classification_Test_2_21_1.png
In [45]:
read('0', p=0.3)
Out[45]:
'0'
_images/notebooks_Classification_Test_2_22_1.png

Classification Test #3

In [1]:
import sdm as sdmlib
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
import urllib, cStringIO
import random
from IPython.core.display import display, Image as IPythonImage
%matplotlib inline
In [2]:
width = 31
height = 32
noise_flip = True
In [3]:
def gen_img(letter='A'):
    img = Image.new('RGBA', (width, height), (255, 255, 255))
    font = ImageFont.truetype('Arial.ttf', 30)
    draw = ImageDraw.Draw(img)
    w, h = draw.textsize(letter, font=font)
    top = (height-w)//2
    left = (width-h)//2 if h <= 30 else 30-2-h
    draw.text((top, left), letter, (0, 0, 0), font=font)
    return img
In [4]:
def gen_noise_add(img, p=0.15, flip=False):
    img2 = img.copy()
    draw = ImageDraw.Draw(img2)
    for py in xrange(height):
        for px in xrange(width):
            if random.random() < p:
                if flip:
                    pixel = img.getpixel((px, py))
                    value = sum([int(x/255+0.5) for x in pixel[:3]])//3
                    assert value == 0 or value == 1
                    value = (1 - value)*255
                    draw.point((px, py), fill=(value, value, value))
                else:
                    draw.point((px, py), fill=(0, 0, 0))
    return img2
In [5]:
img = gen_img(letter='R');
img2 = gen_noise_add(img, p=0.05, flip=noise_flip)
plt.subplot(1, 2, 1)
plt.imshow(img)
plt.subplot(1, 2, 2)
plt.imshow(img2);
_images/notebooks_Classification_Test_3_5_0.png
In [6]:
def to_bitstring(img):
    v = []
    bs = sdmlib.Bitstring.init_ones(1000)
    for py in xrange(height):
        for px in xrange(width):
            pixel = img.getpixel((px, py))
            value = sum([int(x/255+0.5) for x in pixel[:3]])//3
            assert value == 0 or value == 1
            idx = px+width*py
            assert idx >= 0 and idx < 1000, 'Ops {} {} {}'.format(x, y, idx)
            bs.set_bit(idx, value)
            v.append(value)
    v2 = [bs.get_bit(i) for i in xrange(height*width)]
    assert v == v2
    return bs
In [7]:
def to_img(bs):
    img = Image.new('RGBA', (30, 30), (255, 255, 255))
    draw = ImageDraw.Draw(img)
    for py in xrange(height):
        for px in xrange(width):
            idx = px+width*py
            assert idx >= 0 and idx < 1000, 'Ops {} {} {}'.format(x, y, idx)
            x = 255*bs.get_bit(idx)
            draw.point((px, py), fill=(x, x, x))
    return img
In [8]:
bits = 1000
sample = 1000000
scanner_type = sdmlib.SDM_SCANNER_OPENCL
In [9]:
address_space = sdmlib.AddressSpace.init_from_b64_file('sdm-letters.as')
In [10]:
counter = sdmlib.Counter.create_file('sdm-classification-3', bits, sample)
sdm = sdmlib.SDM(address_space, counter, 451, scanner_type)
In [11]:
def fill_memory(letter, label_bs, p=0.1, n=100):
    cols = 15
    rows = n//cols + 1
    plt.figure(figsize=(20,10))
    for i in xrange(n):
        img = gen_img(letter=letter);
        img2 = gen_noise_add(img, p=p, flip=noise_flip)
        #display(img2)
        plt.subplot(rows, cols, i+1)
        plt.imshow(img2)
        bs = to_bitstring(img2)
        sdm.write(bs, label_bs)
    plt.show()
In [12]:
def read(letter, n=6, p=0.2, radius=None):
    img = gen_img(letter=letter);
    img2 = gen_noise_add(img, p=p, flip=noise_flip)
    plt.imshow(img2)

    bs2 = to_bitstring(img2)
    bs3 = sdm.read(bs2, radius=radius)

    label = min(label_to_bs.items(), key=lambda v: bs3.distance_to(v[1]))
    return label[0]
In [13]:
def iter_read(letter, n=10, p=0.2):
    cols = 15
    rows = n//cols + 1
    plt.figure(figsize=(20,10))

    wrong = 0
    correct = 0
    answers = []
    for i in xrange(n):
        plt.subplot(rows, cols, i+1)
        y = read(x, p=p)
        answers.append(y)
        if x == y:
            correct += 1
        else:
            wrong += 1
    plt.show()
    print '!! {} correct={:2d} wrong={:2d} answers={}'.format(x, correct, wrong, answers)
Generating labels
In [14]:
labels = list('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
label_to_bs = {}
for x in labels:
    label_to_bs[x] = sdmlib.Bitstring.init_random(1000)
Training the SDM
In [15]:
for x in labels:
    print 'Training for label {}...'.format(x)
    fill_memory(x, label_to_bs[x])
Training for label A...
_images/notebooks_Classification_Test_3_17_1.png
Training for label B...
_images/notebooks_Classification_Test_3_17_3.png
Training for label C...
_images/notebooks_Classification_Test_3_17_5.png
Training for label D...
_images/notebooks_Classification_Test_3_17_7.png
Training for label E...
_images/notebooks_Classification_Test_3_17_9.png
Training for label F...
_images/notebooks_Classification_Test_3_17_11.png
Training for label G...
_images/notebooks_Classification_Test_3_17_13.png
Training for label H...
_images/notebooks_Classification_Test_3_17_15.png
Training for label I...
_images/notebooks_Classification_Test_3_17_17.png
Training for label J...
_images/notebooks_Classification_Test_3_17_19.png
Training for label K...
_images/notebooks_Classification_Test_3_17_21.png
Training for label L...
_images/notebooks_Classification_Test_3_17_23.png
Training for label M...
_images/notebooks_Classification_Test_3_17_25.png
Training for label N...
_images/notebooks_Classification_Test_3_17_27.png
Training for label O...
_images/notebooks_Classification_Test_3_17_29.png
Training for label P...
_images/notebooks_Classification_Test_3_17_31.png
Training for label Q...
_images/notebooks_Classification_Test_3_17_33.png
Training for label R...
_images/notebooks_Classification_Test_3_17_35.png
Training for label S...
_images/notebooks_Classification_Test_3_17_37.png
Training for label T...
_images/notebooks_Classification_Test_3_17_39.png
Training for label U...
_images/notebooks_Classification_Test_3_17_41.png
Training for label V...
_images/notebooks_Classification_Test_3_17_43.png
Training for label W...
_images/notebooks_Classification_Test_3_17_45.png
Training for label X...
_images/notebooks_Classification_Test_3_17_47.png
Training for label Y...
_images/notebooks_Classification_Test_3_17_49.png
Training for label Z...
_images/notebooks_Classification_Test_3_17_51.png
Training for label a...
_images/notebooks_Classification_Test_3_17_53.png
Training for label b...
_images/notebooks_Classification_Test_3_17_55.png
Training for label c...
_images/notebooks_Classification_Test_3_17_57.png
Training for label d...
_images/notebooks_Classification_Test_3_17_59.png
Training for label e...
_images/notebooks_Classification_Test_3_17_61.png
Training for label f...
_images/notebooks_Classification_Test_3_17_63.png
Training for label g...
_images/notebooks_Classification_Test_3_17_65.png
Training for label h...
_images/notebooks_Classification_Test_3_17_67.png
Training for label i...
_images/notebooks_Classification_Test_3_17_69.png
Training for label j...
_images/notebooks_Classification_Test_3_17_71.png
Training for label k...
_images/notebooks_Classification_Test_3_17_73.png
Training for label l...
_images/notebooks_Classification_Test_3_17_75.png
Training for label m...
_images/notebooks_Classification_Test_3_17_77.png
Training for label n...
_images/notebooks_Classification_Test_3_17_79.png
Training for label o...
_images/notebooks_Classification_Test_3_17_81.png
Training for label p...
_images/notebooks_Classification_Test_3_17_83.png
Training for label q...
_images/notebooks_Classification_Test_3_17_85.png
Training for label r...
_images/notebooks_Classification_Test_3_17_87.png
Training for label s...
_images/notebooks_Classification_Test_3_17_89.png
Training for label t...
_images/notebooks_Classification_Test_3_17_91.png
Training for label u...
_images/notebooks_Classification_Test_3_17_93.png
Training for label v...
_images/notebooks_Classification_Test_3_17_95.png
Training for label w...
_images/notebooks_Classification_Test_3_17_97.png
Training for label x...
_images/notebooks_Classification_Test_3_17_99.png
Training for label y...
_images/notebooks_Classification_Test_3_17_101.png
Training for label z...
_images/notebooks_Classification_Test_3_17_103.png
Training for label 0...
_images/notebooks_Classification_Test_3_17_105.png
Training for label 1...
_images/notebooks_Classification_Test_3_17_107.png
Training for label 2...
_images/notebooks_Classification_Test_3_17_109.png
Training for label 3...
_images/notebooks_Classification_Test_3_17_111.png
Training for label 4...
_images/notebooks_Classification_Test_3_17_113.png
Training for label 5...
_images/notebooks_Classification_Test_3_17_115.png
Training for label 6...
_images/notebooks_Classification_Test_3_17_117.png
Training for label 7...
_images/notebooks_Classification_Test_3_17_119.png
Training for label 8...
_images/notebooks_Classification_Test_3_17_121.png
Training for label 9...
_images/notebooks_Classification_Test_3_17_123.png
Testing with high noise
In [16]:
for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789':
    iter_read(x)
_images/notebooks_Classification_Test_3_19_0.png
!! A correct=10 wrong= 0 answers=['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A']
_images/notebooks_Classification_Test_3_19_2.png
!! B correct= 9 wrong= 1 answers=['S', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B']
_images/notebooks_Classification_Test_3_19_4.png
!! C correct=10 wrong= 0 answers=['C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']
_images/notebooks_Classification_Test_3_19_6.png
!! D correct=10 wrong= 0 answers=['D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D']
_images/notebooks_Classification_Test_3_19_8.png
!! E correct=10 wrong= 0 answers=['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E']
_images/notebooks_Classification_Test_3_19_10.png
!! F correct=10 wrong= 0 answers=['F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F']
_images/notebooks_Classification_Test_3_19_12.png
!! G correct=10 wrong= 0 answers=['G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G']
_images/notebooks_Classification_Test_3_19_14.png
!! H correct=10 wrong= 0 answers=['H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H']
_images/notebooks_Classification_Test_3_19_16.png
!! I correct=10 wrong= 0 answers=['I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I']
_images/notebooks_Classification_Test_3_19_18.png
!! J correct=10 wrong= 0 answers=['J', 'J', 'J', 'J', 'J', 'J', 'J', 'J', 'J', 'J']
_images/notebooks_Classification_Test_3_19_20.png
!! K correct=10 wrong= 0 answers=['K', 'K', 'K', 'K', 'K', 'K', 'K', 'K', 'K', 'K']
_images/notebooks_Classification_Test_3_19_22.png
!! L correct=10 wrong= 0 answers=['L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L']
_images/notebooks_Classification_Test_3_19_24.png
!! M correct=10 wrong= 0 answers=['M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M']
_images/notebooks_Classification_Test_3_19_26.png
!! N correct=10 wrong= 0 answers=['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N']
_images/notebooks_Classification_Test_3_19_28.png
!! O correct= 8 wrong= 2 answers=['G', 'G', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
_images/notebooks_Classification_Test_3_19_30.png
!! P correct=10 wrong= 0 answers=['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P']
_images/notebooks_Classification_Test_3_19_32.png
!! Q correct=10 wrong= 0 answers=['Q', 'Q', 'Q', 'Q', 'Q', 'Q', 'Q', 'Q', 'Q', 'Q']
_images/notebooks_Classification_Test_3_19_34.png
!! R correct=10 wrong= 0 answers=['R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R']
_images/notebooks_Classification_Test_3_19_36.png
!! S correct=10 wrong= 0 answers=['S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S']
_images/notebooks_Classification_Test_3_19_38.png
!! T correct= 9 wrong= 1 answers=['T', 'T', 'T', 'T', 'T', 'I', 'T', 'T', 'T', 'T']
_images/notebooks_Classification_Test_3_19_40.png
!! U correct=10 wrong= 0 answers=['U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U']
_images/notebooks_Classification_Test_3_19_42.png
!! V correct=10 wrong= 0 answers=['V', 'V', 'V', 'V', 'V', 'V', 'V', 'V', 'V', 'V']
_images/notebooks_Classification_Test_3_19_44.png
!! W correct=10 wrong= 0 answers=['W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W']
_images/notebooks_Classification_Test_3_19_46.png
!! X correct=10 wrong= 0 answers=['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X']
_images/notebooks_Classification_Test_3_19_48.png
!! Y correct= 9 wrong= 1 answers=['Y', 'I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y']
_images/notebooks_Classification_Test_3_19_50.png
!! Z correct=10 wrong= 0 answers=['Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z']
_images/notebooks_Classification_Test_3_19_52.png
!! a correct=10 wrong= 0 answers=['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
_images/notebooks_Classification_Test_3_19_54.png
!! b correct= 3 wrong= 7 answers=['o', 'o', 'o', 'b', 'o', 'h', 'h', 'b', 'b', 'o']
_images/notebooks_Classification_Test_3_19_56.png
!! c correct= 8 wrong= 2 answers=['c', 'c', 'c', 'c', 'c', 'o', 'c', 'c', 'c', 'o']
_images/notebooks_Classification_Test_3_19_58.png
!! d correct=10 wrong= 0 answers=['d', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd']
_images/notebooks_Classification_Test_3_19_60.png
!! e correct= 4 wrong= 6 answers=['e', 'o', 'e', 'o', 'o', 'o', 'e', 'o', 'o', 'e']
_images/notebooks_Classification_Test_3_19_62.png
!! f correct= 0 wrong=10 answers=['I', 'I', 'I', 'I', 'i', 'I', 'I', 'I', 'I', 'I']
_images/notebooks_Classification_Test_3_19_64.png
!! g correct=10 wrong= 0 answers=['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g']
_images/notebooks_Classification_Test_3_19_66.png
!! h correct=10 wrong= 0 answers=['h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h']
_images/notebooks_Classification_Test_3_19_68.png
!! i correct= 8 wrong= 2 answers=['i', 'i', 'i', 'I', 'i', 'i', 'i', 'i', 'I', 'i']
_images/notebooks_Classification_Test_3_19_70.png
!! j correct= 7 wrong= 3 answers=['j', 'j', 'j', 'I', 'I', 'j', 'j', 'j', 'j', 'I']
_images/notebooks_Classification_Test_3_19_72.png
!! k correct=10 wrong= 0 answers=['k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k']
_images/notebooks_Classification_Test_3_19_74.png
!! l correct= 0 wrong=10 answers=['I', 'i', 'I', 'I', 'I', 'I', 'i', 'I', 'I', 'i']
_images/notebooks_Classification_Test_3_19_76.png
!! m correct=10 wrong= 0 answers=['m', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm']
_images/notebooks_Classification_Test_3_19_78.png
!! n correct= 5 wrong= 5 answers=['u', 'n', 'n', 'n', 'n', 'n', 'u', 'u', 'u', 'h']
_images/notebooks_Classification_Test_3_19_80.png
!! o correct=10 wrong= 0 answers=['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o']
_images/notebooks_Classification_Test_3_19_82.png
!! p correct=10 wrong= 0 answers=['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p']
_images/notebooks_Classification_Test_3_19_84.png
!! q correct= 9 wrong= 1 answers=['q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'g']
_images/notebooks_Classification_Test_3_19_86.png
!! r correct=10 wrong= 0 answers=['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r']
_images/notebooks_Classification_Test_3_19_88.png
!! s correct=10 wrong= 0 answers=['s', 's', 's', 's', 's', 's', 's', 's', 's', 's']
_images/notebooks_Classification_Test_3_19_90.png
!! t correct= 0 wrong=10 answers=['I', 'r', 'I', 'i', 'I', 'i', 'i', 'i', 'I', 'i']
_images/notebooks_Classification_Test_3_19_92.png
!! u correct=10 wrong= 0 answers=['u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u']
_images/notebooks_Classification_Test_3_19_94.png
!! v correct=10 wrong= 0 answers=['v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v']
_images/notebooks_Classification_Test_3_19_96.png
!! w correct=10 wrong= 0 answers=['w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w']
_images/notebooks_Classification_Test_3_19_98.png
!! x correct=10 wrong= 0 answers=['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x']
_images/notebooks_Classification_Test_3_19_100.png
!! y correct=10 wrong= 0 answers=['y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y']
_images/notebooks_Classification_Test_3_19_102.png
!! z correct=10 wrong= 0 answers=['z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z']
_images/notebooks_Classification_Test_3_19_104.png
!! 0 correct=10 wrong= 0 answers=['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
_images/notebooks_Classification_Test_3_19_106.png
!! 1 correct= 5 wrong= 5 answers=['1', 'I', '1', 'I', '1', '1', 'I', 'I', '1', 'I']
_images/notebooks_Classification_Test_3_19_108.png
!! 2 correct=10 wrong= 0 answers=['2', '2', '2', '2', '2', '2', '2', '2', '2', '2']
_images/notebooks_Classification_Test_3_19_110.png
!! 3 correct=10 wrong= 0 answers=['3', '3', '3', '3', '3', '3', '3', '3', '3', '3']
_images/notebooks_Classification_Test_3_19_112.png
!! 4 correct=10 wrong= 0 answers=['4', '4', '4', '4', '4', '4', '4', '4', '4', '4']
_images/notebooks_Classification_Test_3_19_114.png
!! 5 correct=10 wrong= 0 answers=['5', '5', '5', '5', '5', '5', '5', '5', '5', '5']
_images/notebooks_Classification_Test_3_19_116.png
!! 6 correct=10 wrong= 0 answers=['6', '6', '6', '6', '6', '6', '6', '6', '6', '6']
_images/notebooks_Classification_Test_3_19_118.png
!! 7 correct= 7 wrong= 3 answers=['7', '7', '7', 'I', '7', 'I', 'I', '7', '7', '7']
_images/notebooks_Classification_Test_3_19_120.png
!! 8 correct= 4 wrong= 6 answers=['8', '6', '6', '6', '8', 'd', '8', '8', 'd', '6']
_images/notebooks_Classification_Test_3_19_122.png
!! 9 correct= 3 wrong= 7 answers=['9', '0', '6', '0', '9', '0', '0', '9', '0', '0']
Testing with low noise
In [17]:
for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789':
    iter_read(x, p=0.05)
_images/notebooks_Classification_Test_3_21_0.png
!! A correct=10 wrong= 0 answers=['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A']
_images/notebooks_Classification_Test_3_21_2.png
!! B correct=10 wrong= 0 answers=['B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B']
_images/notebooks_Classification_Test_3_21_4.png
!! C correct=10 wrong= 0 answers=['C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']
_images/notebooks_Classification_Test_3_21_6.png
!! D correct=10 wrong= 0 answers=['D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D']
_images/notebooks_Classification_Test_3_21_8.png
!! E correct=10 wrong= 0 answers=['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E']
_images/notebooks_Classification_Test_3_21_10.png
!! F correct=10 wrong= 0 answers=['F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F']
_images/notebooks_Classification_Test_3_21_12.png
!! G correct=10 wrong= 0 answers=['G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G']
_images/notebooks_Classification_Test_3_21_14.png
!! H correct=10 wrong= 0 answers=['H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H']
_images/notebooks_Classification_Test_3_21_16.png
!! I correct=10 wrong= 0 answers=['I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I']
_images/notebooks_Classification_Test_3_21_18.png
!! J correct=10 wrong= 0 answers=['J', 'J', 'J', 'J', 'J', 'J', 'J', 'J', 'J', 'J']
_images/notebooks_Classification_Test_3_21_20.png
!! K correct=10 wrong= 0 answers=['K', 'K', 'K', 'K', 'K', 'K', 'K', 'K', 'K', 'K']
_images/notebooks_Classification_Test_3_21_22.png
!! L correct=10 wrong= 0 answers=['L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L']
_images/notebooks_Classification_Test_3_21_24.png
!! M correct=10 wrong= 0 answers=['M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M']
_images/notebooks_Classification_Test_3_21_26.png
!! N correct=10 wrong= 0 answers=['N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N']
_images/notebooks_Classification_Test_3_21_28.png
!! O correct=10 wrong= 0 answers=['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']
_images/notebooks_Classification_Test_3_21_30.png
!! P correct=10 wrong= 0 answers=['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P']
_images/notebooks_Classification_Test_3_21_32.png
!! Q correct=10 wrong= 0 answers=['Q', 'Q', 'Q', 'Q', 'Q', 'Q', 'Q', 'Q', 'Q', 'Q']
_images/notebooks_Classification_Test_3_21_34.png
!! R correct=10 wrong= 0 answers=['R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R']
_images/notebooks_Classification_Test_3_21_36.png
!! S correct=10 wrong= 0 answers=['S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S']
_images/notebooks_Classification_Test_3_21_38.png
!! T correct=10 wrong= 0 answers=['T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T']
_images/notebooks_Classification_Test_3_21_40.png
!! U correct=10 wrong= 0 answers=['U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U']
_images/notebooks_Classification_Test_3_21_42.png
!! V correct=10 wrong= 0 answers=['V', 'V', 'V', 'V', 'V', 'V', 'V', 'V', 'V', 'V']
_images/notebooks_Classification_Test_3_21_44.png
!! W correct=10 wrong= 0 answers=['W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W']
_images/notebooks_Classification_Test_3_21_46.png
!! X correct=10 wrong= 0 answers=['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X']
_images/notebooks_Classification_Test_3_21_48.png
!! Y correct=10 wrong= 0 answers=['Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y']
_images/notebooks_Classification_Test_3_21_50.png
!! Z correct=10 wrong= 0 answers=['Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z']
_images/notebooks_Classification_Test_3_21_52.png
!! a correct=10 wrong= 0 answers=['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
_images/notebooks_Classification_Test_3_21_54.png
!! b correct= 7 wrong= 3 answers=['b', 'b', 'b', 'h', 'b', 'o', 'b', 'h', 'b', 'b']
_images/notebooks_Classification_Test_3_21_56.png
!! c correct=10 wrong= 0 answers=['c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c']
_images/notebooks_Classification_Test_3_21_58.png
!! d correct=10 wrong= 0 answers=['d', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd']
_images/notebooks_Classification_Test_3_21_60.png
!! e correct= 9 wrong= 1 answers=['e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'o', 'e']
_images/notebooks_Classification_Test_3_21_62.png
!! f correct= 6 wrong= 4 answers=['i', 'f', 'f', 'I', 'I', 'I', 'f', 'f', 'f', 'f']
_images/notebooks_Classification_Test_3_21_64.png
!! g correct=10 wrong= 0 answers=['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g']
_images/notebooks_Classification_Test_3_21_66.png
!! h correct=10 wrong= 0 answers=['h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h']
_images/notebooks_Classification_Test_3_21_68.png
!! i correct=10 wrong= 0 answers=['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i']
_images/notebooks_Classification_Test_3_21_70.png
!! j correct=10 wrong= 0 answers=['j', 'j', 'j', 'j', 'j', 'j', 'j', 'j', 'j', 'j']
_images/notebooks_Classification_Test_3_21_72.png
!! k correct=10 wrong= 0 answers=['k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k']
_images/notebooks_Classification_Test_3_21_74.png
!! l correct= 0 wrong=10 answers=['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i']
_images/notebooks_Classification_Test_3_21_76.png
!! m correct=10 wrong= 0 answers=['m', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm', 'm']
_images/notebooks_Classification_Test_3_21_78.png
!! n correct=10 wrong= 0 answers=['n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n']
_images/notebooks_Classification_Test_3_21_80.png
!! o correct=10 wrong= 0 answers=['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o']
_images/notebooks_Classification_Test_3_21_82.png
!! p correct=10 wrong= 0 answers=['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p']
_images/notebooks_Classification_Test_3_21_84.png
!! q correct=10 wrong= 0 answers=['q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q']
_images/notebooks_Classification_Test_3_21_86.png
!! r correct=10 wrong= 0 answers=['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r']
_images/notebooks_Classification_Test_3_21_88.png
!! s correct=10 wrong= 0 answers=['s', 's', 's', 's', 's', 's', 's', 's', 's', 's']
_images/notebooks_Classification_Test_3_21_90.png
!! t correct= 9 wrong= 1 answers=['t', 't', 't', 't', 't', 't', 't', 'i', 't', 't']
_images/notebooks_Classification_Test_3_21_92.png
!! u correct=10 wrong= 0 answers=['u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u']
_images/notebooks_Classification_Test_3_21_94.png
!! v correct=10 wrong= 0 answers=['v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v']
_images/notebooks_Classification_Test_3_21_96.png
!! w correct=10 wrong= 0 answers=['w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w']
_images/notebooks_Classification_Test_3_21_98.png
!! x correct=10 wrong= 0 answers=['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x']
_images/notebooks_Classification_Test_3_21_100.png
!! y correct=10 wrong= 0 answers=['y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y']
_images/notebooks_Classification_Test_3_21_102.png
!! z correct=10 wrong= 0 answers=['z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z']
_images/notebooks_Classification_Test_3_21_104.png
!! 0 correct=10 wrong= 0 answers=['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
_images/notebooks_Classification_Test_3_21_106.png
!! 1 correct=10 wrong= 0 answers=['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']
_images/notebooks_Classification_Test_3_21_108.png
!! 2 correct=10 wrong= 0 answers=['2', '2', '2', '2', '2', '2', '2', '2', '2', '2']
_images/notebooks_Classification_Test_3_21_110.png
!! 3 correct=10 wrong= 0 answers=['3', '3', '3', '3', '3', '3', '3', '3', '3', '3']
_images/notebooks_Classification_Test_3_21_112.png
!! 4 correct=10 wrong= 0 answers=['4', '4', '4', '4', '4', '4', '4', '4', '4', '4']
_images/notebooks_Classification_Test_3_21_114.png
!! 5 correct=10 wrong= 0 answers=['5', '5', '5', '5', '5', '5', '5', '5', '5', '5']
_images/notebooks_Classification_Test_3_21_116.png
!! 6 correct=10 wrong= 0 answers=['6', '6', '6', '6', '6', '6', '6', '6', '6', '6']
_images/notebooks_Classification_Test_3_21_118.png
!! 7 correct=10 wrong= 0 answers=['7', '7', '7', '7', '7', '7', '7', '7', '7', '7']
_images/notebooks_Classification_Test_3_21_120.png
!! 8 correct= 9 wrong= 1 answers=['8', '6', '8', '8', '8', '8', '8', '8', '8', '8']
_images/notebooks_Classification_Test_3_21_122.png
!! 9 correct= 7 wrong= 3 answers=['9', '9', '0', '9', '9', '9', '0', '0', '9', '9']
Testing with no noise
In [18]:
for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789':
    iter_read(x, p=0, n=1)
_images/notebooks_Classification_Test_3_23_0.png
!! A correct= 1 wrong= 0 answers=['A']
_images/notebooks_Classification_Test_3_23_2.png
!! B correct= 1 wrong= 0 answers=['B']
_images/notebooks_Classification_Test_3_23_4.png
!! C correct= 1 wrong= 0 answers=['C']
_images/notebooks_Classification_Test_3_23_6.png
!! D correct= 1 wrong= 0 answers=['D']
_images/notebooks_Classification_Test_3_23_8.png
!! E correct= 1 wrong= 0 answers=['E']
_images/notebooks_Classification_Test_3_23_10.png
!! F correct= 1 wrong= 0 answers=['F']
_images/notebooks_Classification_Test_3_23_12.png
!! G correct= 1 wrong= 0 answers=['G']
_images/notebooks_Classification_Test_3_23_14.png
!! H correct= 1 wrong= 0 answers=['H']
_images/notebooks_Classification_Test_3_23_16.png
!! I correct= 1 wrong= 0 answers=['I']
_images/notebooks_Classification_Test_3_23_18.png
!! J correct= 1 wrong= 0 answers=['J']
_images/notebooks_Classification_Test_3_23_20.png
!! K correct= 1 wrong= 0 answers=['K']
_images/notebooks_Classification_Test_3_23_22.png
!! L correct= 1 wrong= 0 answers=['L']
_images/notebooks_Classification_Test_3_23_24.png
!! M correct= 1 wrong= 0 answers=['M']
_images/notebooks_Classification_Test_3_23_26.png
!! N correct= 1 wrong= 0 answers=['N']
_images/notebooks_Classification_Test_3_23_28.png
!! O correct= 1 wrong= 0 answers=['O']
_images/notebooks_Classification_Test_3_23_30.png
!! P correct= 1 wrong= 0 answers=['P']
_images/notebooks_Classification_Test_3_23_32.png
!! Q correct= 1 wrong= 0 answers=['Q']
_images/notebooks_Classification_Test_3_23_34.png
!! R correct= 1 wrong= 0 answers=['R']
_images/notebooks_Classification_Test_3_23_36.png
!! S correct= 1 wrong= 0 answers=['S']
_images/notebooks_Classification_Test_3_23_38.png
!! T correct= 1 wrong= 0 answers=['T']
_images/notebooks_Classification_Test_3_23_40.png
!! U correct= 1 wrong= 0 answers=['U']
_images/notebooks_Classification_Test_3_23_42.png
!! V correct= 1 wrong= 0 answers=['V']
_images/notebooks_Classification_Test_3_23_44.png
!! W correct= 1 wrong= 0 answers=['W']
_images/notebooks_Classification_Test_3_23_46.png
!! X correct= 1 wrong= 0 answers=['X']
_images/notebooks_Classification_Test_3_23_48.png
!! Y correct= 1 wrong= 0 answers=['Y']
_images/notebooks_Classification_Test_3_23_50.png
!! Z correct= 1 wrong= 0 answers=['Z']
_images/notebooks_Classification_Test_3_23_52.png
!! a correct= 1 wrong= 0 answers=['a']
_images/notebooks_Classification_Test_3_23_54.png
!! b correct= 1 wrong= 0 answers=['b']
_images/notebooks_Classification_Test_3_23_56.png
!! c correct= 1 wrong= 0 answers=['c']
_images/notebooks_Classification_Test_3_23_58.png
!! d correct= 1 wrong= 0 answers=['d']
_images/notebooks_Classification_Test_3_23_60.png
!! e correct= 1 wrong= 0 answers=['e']
_images/notebooks_Classification_Test_3_23_62.png
!! f correct= 1 wrong= 0 answers=['f']
_images/notebooks_Classification_Test_3_23_64.png
!! g correct= 1 wrong= 0 answers=['g']
_images/notebooks_Classification_Test_3_23_66.png
!! h correct= 1 wrong= 0 answers=['h']
_images/notebooks_Classification_Test_3_23_68.png
!! i correct= 1 wrong= 0 answers=['i']
_images/notebooks_Classification_Test_3_23_70.png
!! j correct= 1 wrong= 0 answers=['j']
_images/notebooks_Classification_Test_3_23_72.png
!! k correct= 1 wrong= 0 answers=['k']
_images/notebooks_Classification_Test_3_23_74.png
!! l correct= 0 wrong= 1 answers=['i']
_images/notebooks_Classification_Test_3_23_76.png
!! m correct= 1 wrong= 0 answers=['m']
_images/notebooks_Classification_Test_3_23_78.png
!! n correct= 1 wrong= 0 answers=['n']
_images/notebooks_Classification_Test_3_23_80.png
!! o correct= 1 wrong= 0 answers=['o']
_images/notebooks_Classification_Test_3_23_82.png
!! p correct= 1 wrong= 0 answers=['p']
_images/notebooks_Classification_Test_3_23_84.png
!! q correct= 1 wrong= 0 answers=['q']
_images/notebooks_Classification_Test_3_23_86.png
!! r correct= 1 wrong= 0 answers=['r']
_images/notebooks_Classification_Test_3_23_88.png
!! s correct= 1 wrong= 0 answers=['s']
_images/notebooks_Classification_Test_3_23_90.png
!! t correct= 1 wrong= 0 answers=['t']
_images/notebooks_Classification_Test_3_23_92.png
!! u correct= 1 wrong= 0 answers=['u']
_images/notebooks_Classification_Test_3_23_94.png
!! v correct= 1 wrong= 0 answers=['v']
_images/notebooks_Classification_Test_3_23_96.png
!! w correct= 1 wrong= 0 answers=['w']
_images/notebooks_Classification_Test_3_23_98.png
!! x correct= 1 wrong= 0 answers=['x']
_images/notebooks_Classification_Test_3_23_100.png
!! y correct= 1 wrong= 0 answers=['y']
_images/notebooks_Classification_Test_3_23_102.png
!! z correct= 1 wrong= 0 answers=['z']
_images/notebooks_Classification_Test_3_23_104.png
!! 0 correct= 1 wrong= 0 answers=['0']
_images/notebooks_Classification_Test_3_23_106.png
!! 1 correct= 1 wrong= 0 answers=['1']
_images/notebooks_Classification_Test_3_23_108.png
!! 2 correct= 1 wrong= 0 answers=['2']
_images/notebooks_Classification_Test_3_23_110.png
!! 3 correct= 1 wrong= 0 answers=['3']
_images/notebooks_Classification_Test_3_23_112.png
!! 4 correct= 1 wrong= 0 answers=['4']
_images/notebooks_Classification_Test_3_23_114.png
!! 5 correct= 1 wrong= 0 answers=['5']
_images/notebooks_Classification_Test_3_23_116.png
!! 6 correct= 1 wrong= 0 answers=['6']
_images/notebooks_Classification_Test_3_23_118.png
!! 7 correct= 1 wrong= 0 answers=['7']
_images/notebooks_Classification_Test_3_23_120.png
!! 8 correct= 1 wrong= 0 answers=['8']
_images/notebooks_Classification_Test_3_23_122.png
!! 9 correct= 1 wrong= 0 answers=['9']

Indices and tables