Ifi6057w11

Allikas: Lambda

11. Nädal

Perceptron. Närvivõrgud. AIMA 18.6.3-18.7.4

Lineaarne klassifitseerija Pythonis.

Loenguslaidid: PDF


Harjutustund

bmp laadimine (paint.net 8-bit grayscale peal VIST töötab)

import struct
def load_bmp(fn):
    with open(fn, "rb") as f:
        header = f.read(54)
        offset = header[10:14]
        offs = struct.unpack("@L", offset)
        f.read(offs[0]-54)
        data = list(f.read(28*28))
        inv_data = []
        for i in range(27, -1, -1):
            inv_data += data[i*28: (i+1)*28]
        return inv_data


import learning
import random

def load_mnist_img(fn):
    img_list = []
    with open(fn, "rb") as f:
        f.read(16)
        chunk_size = 28*28
        chunk = f.read(chunk_size)
        while len(chunk) == chunk_size:
            img_list.append([px/256 for px in (list(chunk))])
            chunk = f.read(chunk_size)
    return img_list

def load_mnist_label(fn):
    label_list = []
    with open(fn, "rb") as f:
        f.read(8)
        chunk = f.read(1)
        while chunk:
            label_list.append((list(chunk)))
            chunk = f.read(1)
    return label_list


train_img = load_mnist_img("aima-data\\MNIST\\train-images-idx3-ubyte")
print(len(train_img))
train_labels = load_mnist_label("aima-data\\MNIST\\train-labels-idx1-ubyte")
print(len(train_labels))
#print(train_labels[:20])

train_data = [pixels+classif
              for pixels, classif
              in zip(train_img, train_labels)]

train_ds = learning.DataSet(random.sample(train_data, 1000))
print("training...")
nn = learning.NeuralNetLearner(train_ds,
                    hidden_layer_sizes=[20],
                               epochs=10)

test_img = load_mnist_img("aima-data\\MNIST\\t10k-images-idx3-ubyte")
print(len(test_img))
test_labels = load_mnist_label("aima-data\\MNIST\\t10k-labels-idx1-ubyte")
print(len(test_labels))

test_data = [pixels+classif
              for pixels, classif
              in zip(test_img, test_labels)]

test_ds = learning.DataSet(random.sample(test_data, 100))

for i in range(5):
    print("tegelikult", test_labels[i], "narvivork arvab", nn(test_img[i]))

print(learning.err_ratio(nn, test_ds))