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 testAccuracy: 73.3% on 30 test digitsWeakest 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 testEthics:- 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
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):
# the title between two lines of 30 "=" signs, then Model, Data,
# Accuracy, Weakest digit, Most common mistake, Limits and Ethics
pass
print_card(card, correct, n, weakest, classes, most)
راهنما و جواب این چالش · برگشت به درس جلسهی 64: ارائه و پورتفولیو
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال