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

جواب «Step 4: The Model Card» چالش 4 از جلسه‌ی 64

سؤال

قدم ۴: لیست‌های limits و ethics به کارت اضافه شده‌اند. تابع print_card را خودت بنویس تا کارت کامل را چاپ کند: خط ۳۰ تایی =، عنوان، دوباره خط =، بعد مدل، داده، دقت (با یک رقم اعشار، حساب‌شده از correct و n)، ضعیف‌ترین رقم، رایج‌ترین اشتباه، و فهرست Limits: و Ethics: که هر مورد با - شروع می‌شود. خروجی، بعد از خط‌های قدم‌های قبل، باید این باشد:
==============================
MODEL CARD: Digit Reader
==============================
Model: neural network 25-16-10 (sessions 41-47)
Data: 5x5 digits: 200 noisy copies to train, 30 new ones drawn by classmates to test
Accuracy: 73.3% on 30 test digits
Weakest digit: 8 (0%)
Most common mistake: 8 read as 3 (3 times)
Limits:
- only 5x5 pictures with one digit in the middle
- small test set: one more mistake changes the accuracy by 3.3%
- it never read an 8 correctly in the test
Ethics:
- bias: all digits were drawn by one class; other handwriting may do worse
- privacy: no names or photos of students are stored
- must not be used for: marking exams or any decision about a person

راهنما

print("=" * 30) برای خط‌ها؛ دقت: round(100 * correct / n, 1)؛ برای فهرست‌ها for item in card["limits"]: print("-", item) و همین برای ethics.

یک جواب درست

card = {
    "name": "Digit Reader",
    "kind": "neural network 25-16-10 (sessions 41-47)",
    "data": "5x5 digits: 200 noisy copies to train, 30 new ones drawn by classmates to test",
    "train": 200,
    "test": 30,
}
total = card["train"] + card["test"]
print("Model:", card["name"], "-", card["kind"])
print("Data:", card["data"])
print(f"Split: {card['train']} train / {card['test']} test ({round(100 * card['test'] / total)}% test)")


true = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] * 3
pred = [0, 1, 2, 3, 4, 5, 6, 7, 3, 9,
        0, 7, 2, 3, 4, 6, 6, 7, 3, 8,
        0, 1, 2, 8, 4, 6, 6, 7, 3, 9]


def accuracy(true, pred):
    correct = 0
    for t, p in zip(true, pred):
        if t == p:
            correct += 1
    return correct, len(true)


def per_class(true, pred):
    result = {}
    for d in sorted(set(true)):
        seen = 0
        right = 0
        for t, p in zip(true, pred):
            if t == d:
                seen += 1
                if p == d:
                    right += 1
        result[d] = round(100 * right / seen)
    return result


correct, n = accuracy(true, pred)
print(f"Accuracy: {correct}/{n} = {round(100 * correct / n, 1)}%")
classes = per_class(true, pred)
print("Per digit:", " ".join(f"{d}:{p}%" for d, p in classes.items()))
weakest = min(classes, key=classes.get)
print(f"Weakest digit: {weakest} ({classes[weakest]}%)")


def mistakes(true, pred):
    counts = {}
    for t, p in zip(true, pred):
        if t != p:
            counts[(t, p)] = counts.get((t, p), 0) + 1
    return counts


errors = mistakes(true, pred)
ranked = sorted(errors.items(), key=lambda pair: -pair[1])
for (t, p), c in ranked[:3]:
    print(f"{t} read as {p}: {c}x")
(t, p), c = ranked[0]
most = f"{t} read as {p} ({c} times)"
print("Most common mistake:", most)


card["limits"] = [
    "only 5x5 pictures with one digit in the middle",
    "small test set: one more mistake changes the accuracy by 3.3%",
    "it never read an 8 correctly in the test",
]
card["ethics"] = [
    "bias: all digits were drawn by one class; other handwriting may do worse",
    "privacy: no names or photos of students are stored",
    "must not be used for: marking exams or any decision about a person",
]


def print_card(card, correct, n, weakest, classes, most):
    print("=" * 30)
    print("MODEL CARD:", card["name"])
    print("=" * 30)
    print("Model:", card["kind"])
    print("Data:", card["data"])
    print(f"Accuracy: {round(100 * correct / n, 1)}% on {n} test digits")
    print(f"Weakest digit: {weakest} ({classes[weakest]}%)")
    print("Most common mistake:", most)
    print("Limits:")
    for item in card["limits"]:
        print("-", item)
    print("Ethics:")
    for item in card["ethics"]:
        print("-", item)


print_card(card, correct, n, weakest, classes, most)

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