Embedding podrobnějších popisů
Jedno z posledních předpovězených označení zřejmě neodpovídalo recenzi – pravděpodobně proto, že při embeddingu samotných názvů tříd se zachytí jen málo informací. Tentokrát proto místo nich použijeme popisy jednotlivých tříd, aby model lépe „pochopil", že klasifikuješ recenze restaurací.
Máš k dispozici následující objekty:
sentiments = [{'label': 'Positive',
'description': 'A positive restaurant review'},
{'label': 'Neutral',
'description':'A neutral restaurant review'},
{'label': 'Negative',
'description': 'A negative restaurant review'}]
reviews = ["The food was delicious!",
"The service was a bit slow but the food was good",
"The food was cold, really disappointing!"]
Toto cvičení je součástí kurzu
Introduction to Embeddings with the OpenAI API
Pokyny k cvičení
- Extrahuj seznam obsahující popisy sentimentů a vytvoř z nich embeddings.
Interaktivní cvičení na vyzkoušení si v praxi
Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.
# Extract and embed the descriptions from sentiments
class_descriptions = ____
class_embeddings = ____
review_embeddings = create_embeddings(reviews)
def find_closest(query_vector, embeddings):
distances = []
for index, embedding in enumerate(embeddings):
dist = distance.cosine(query_vector, embedding)
distances.append({"distance": dist, "index": index})
return min(distances, key=lambda x: x["distance"])
for index, review in enumerate(reviews):
closest = find_closest(review_embeddings[index], class_embeddings)
label = sentiments[closest['index']]['label']
print(f'"{review}" was classified as {label}')