جواب «Step 6: Cite Your Sources» چالش 6 از جلسهی 58
سؤال
قدم آخر: تابع top_sources(question, k) را بنویس که حداکثر k درس با بیشترین امتیاز را (فقط امتیازهای بزرگتر از صفر، از بیشتر به کمتر) به شکل (title, source, score) برگرداند. بعد برنامه یک پرسش با input() میگیرد، Bot: ... را چاپ میکند و بعد Sources: و منبعها را شمارهدار (1. overfitting (session 48) 0.8، امتیاز با ۲ رقم)، یا اگر منبعی نبود Sources: none. برای پرسش my model is great on training data but bad on test data خروجی: Bot: Overfitting means ...، Sources:، 1. overfitting (session 48) 0.8، 2. validation (session 49) 0.4، 3. decision tree (sessions 50-51) 0.24.
راهنما
order = sorted(range(len(LESSONS)), key=lambda i: s[i], reverse=True)، بعد روی order[:k] بچرخ و فقط s[i] > 0 را نگه دار. برای شمارهها enumerate(sources, 1).
یک جواب درست
import numpy as np
LESSONS = [
{"title": "overfitting", "source": "session 48",
"text": "overfitting is when a model memorises the training data and does badly on new test data",
"answer": "Overfitting means the model memorised its training examples and fails on new ones."},
{"title": "validation", "source": "session 49",
"text": "a validation set is data the model never trains on so we can check it fairly",
"answer": "A validation set is kept apart from training so you can measure the model fairly."},
{"title": "relu", "source": "session 43",
"text": "relu is an activation that keeps positive numbers and turns negative numbers into zero",
"answer": "ReLU keeps a positive number and turns a negative one into 0, like max(0, x)."},
{"title": "softmax", "source": "session 44",
"text": "softmax turns a list of scores into probabilities that are positive and add up to one",
"answer": "Softmax turns scores into probabilities that add up to 1."},
{"title": "mini-batch", "source": "session 45",
"text": "a mini batch is a small group of training examples used for one step of gradient descent",
"answer": "A mini-batch is a small group of examples used for one gradient descent step."},
{"title": "decision tree", "source": "sessions 50-51",
"text": "a decision tree asks yes or no questions about the features and splits the data at every node",
"answer": "A decision tree asks one yes/no question per node and splits the data until a leaf answers."},
{"title": "convolution", "source": "session 53",
"text": "convolution slides a small filter over the image and adds up each patch times the filter to find edges",
"answer": "Convolution slides a small filter across the picture and adds up patch * filter at every spot."},
{"title": "embedding", "source": "session 55",
"text": "a word embedding is a vector of numbers for a word so that similar words get close vectors",
"answer": "A word embedding is a vector for a word; similar words point in similar directions."},
{"title": "attention", "source": "session 57",
"text": "attention lets every word look at the other words of the sentence with a query a key and a value",
"answer": "In attention every word scores the others (query times key), softmax gives weights, and it mixes their values."},
]
def show_lessons():
print(len(LESSONS), "lessons")
for lesson in LESSONS:
print(f"{lesson['title']} ({lesson['source']})")
STOP = {"a", "an", "the", "is", "are", "what", "how", "does", "do", "of", "to", "and", "in", "on",
"it", "that", "for", "with", "at", "so", "by", "i", "my", "we", "can", "you", "why", "which",
"into", "its", "be", "this", "about", "or", "when", "me", "every", "each"}
def tokenize(text):
clean = "".join(ch if ch.isalpha() else " " for ch in text.lower())
return [w for w in clean.split() if w not in STOP]
VOCAB = sorted(set(w for lesson in LESSONS for w in tokenize(lesson["text"])))
def vectorize(text):
v = np.zeros(len(VOCAB))
for w in tokenize(text):
if w in VOCAB:
v[VOCAB.index(w)] += 1
return v
LESSON_VECTORS = [vectorize(lesson["text"]) for lesson in LESSONS]
def cosine(a, b):
na = np.linalg.norm(a)
nb = np.linalg.norm(b)
if na == 0 or nb == 0:
return 0.0
return float(a @ b / (na * nb))
def scores(question):
q = vectorize(question)
return [cosine(q, v) for v in LESSON_VECTORS]
def best_match(question):
s = scores(question)
best = 0
for i in range(len(s)):
if s[i] > s[best]:
best = i
return best, s[best]
def answer(question):
i, score = best_match(question)
return LESSONS[i]["answer"]
THRESHOLD = 0.25
def reply(question):
i, score = best_match(question)
if score < THRESHOLD:
return "I don't know, ask your teacher."
return LESSONS[i]["answer"]
def top_sources(question, k):
s = scores(question)
order = sorted(range(len(LESSONS)), key=lambda i: s[i], reverse=True)
result = []
for i in order[:k]:
if s[i] > 0:
result.append((LESSONS[i]["title"], LESSONS[i]["source"], s[i]))
return result
question = input()
print("Bot:", reply(question))
sources = top_sources(question, 3)
if sources:
print("Sources:")
for n, (title, source, score) in enumerate(sources, 1):
print(f"{n}. {title} ({source}) {round(score, 2)}")
else:
print("Sources: none")
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن