بتل کد Battle Codeدارد باز می‌شود…

جواب «More Data, Smaller Gap» چالش 5 از جلسه‌ی 48

سؤال

حلقه را خودت بنویس: برای copies در [1, 2, 5, 20] داده‌ی make_digits(copies, 2, 3) بساز، train_net(Xs, ys, 64, 200) را آموزش بده و مثل جلسه یک خط چاپ کن. کوچک‌ترین فاصله را هم نگه دار (در تساوی، اولی). خروجی: 10 pictures: train 100% test 67% gap 33، 20 pictures: train 100% test 87% gap 13، 50 pictures: train 100% test 87% gap 13، 200 pictures: train 100% test 92% gap 8 و smallest gap with 20 copies of each digit.

راهنما

دقت آزمون همیشه روی Xt, yt است، نه روی Xs, ys. برای کوچک‌ترین: if gap < best_gap:.

یک جواب درست

import random
import numpy as np

SHAPES = '''
.###. ..#.. .###. ####. #..#. ##### .###. ##### .###. .###.
#...# .##.. #...# ....# #..#. #.... #.... ....# #...# #...#
#...# ..#.. ..##. .###. ##### ####. ####. ...#. .###. .####
#...# ..#.. .#... ....# ...#. ....# #...# ..#.. #...# ....#
.###. .###. ##### ####. ...#. ####. .###. ..#.. .###. .###.
'''


def make_digits(copies, noise, seed):
    random.seed(seed)
    rows = SHAPES.strip().splitlines()
    X, y = [], []
    for c in range(copies):
        for d in range(10):
            pixels = [1.0 if ch == "#" else 0.0 for ch in "".join(r.split()[d] for r in rows)]
            for i in random.sample(range(25), noise):
                pixels[i] = 1.0 - pixels[i]
            X.append(pixels)
            y.append(d)
    return np.array(X), np.array(y)


X, y = make_digits(20, 2, 1)     # 200 training digits
Xt, yt = make_digits(10, 2, 2)   # 100 new test digits



def softmax(z):
    e = np.exp(z - z.max(axis=1, keepdims=True))
    return e / e.sum(axis=1, keepdims=True)


def rand_matrix(rows, cols):
    return np.array([[random.uniform(-0.5, 0.5) for c in range(cols)] for r in range(rows)])


def train_net(Xs, ys, hidden, epochs, lr=0.5):
    random.seed(0)
    net = {"W1": rand_matrix(25, hidden), "b1": np.zeros(hidden),
           "W2": rand_matrix(hidden, 10), "b2": np.zeros(10)}
    Y = np.eye(10)[ys]
    for epoch in range(epochs):          # few pictures: one batch = all of them
        h = np.maximum(0, Xs @ net["W1"] + net["b1"])
        p = softmax(h @ net["W2"] + net["b2"])
        dz2 = (p - Y) / len(Xs)
        dh = dz2 @ net["W2"].T * (h > 0)
        net["W2"] -= lr * (h.T @ dz2)
        net["b2"] -= lr * dz2.sum(axis=0)
        net["W1"] -= lr * (Xs.T @ dh)
        net["b1"] -= lr * dh.sum(axis=0)
    return net


def accuracy(net, Xs, ys):
    h = np.maximum(0, Xs @ net["W1"] + net["b1"])
    p = softmax(h @ net["W2"] + net["b2"])
    return round(float((p.argmax(axis=1) == ys).mean()) * 100)



best_gap = 100
best_copies = 0
for copies in [1, 2, 5, 20]:
    Xs, ys = make_digits(copies, 2, 3)
    net = train_net(Xs, ys, 64, 200)
    train = accuracy(net, Xs, ys)
    test = accuracy(net, Xt, yt)
    gap = train - test
    print(f"{len(Xs)} pictures: train {train}% test {test}% gap {gap}")
    if gap < best_gap:
        best_gap = gap
        best_copies = copies
print("smallest gap with", best_copies, "copies of each digit")

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن