การสร้าง Embedding จากคำอธิบายที่ละเอียดขึ้น
ผลลัพธ์ label ที่คาดการณ์ได้ในรายการสุดท้ายดูเหมือนจะไม่สื่อถึงรีวิวนั้นมากนัก ซึ่งน่าจะเกิดจากการที่ข้อมูลที่ดึงมาใช้มีน้อยเกินไปเมื่อ embed เพียงแค่ชื่อ class label คราวนี้จะ embed คำอธิบายของแต่ละ class แทน เพื่อให้โมเดล "เข้าใจ" ได้ดีขึ้นว่ากำลังจำแนกประเภทรีวิวร้านอาหาร
ออบเจกต์ต่อไปนี้พร้อมใช้งานแล้ว:
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!"]
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การสร้าง Embeddings ด้วย OpenAI API
คำแนะนำการฝึกหัด
- ดึงข้อมูลเป็น list ที่มีคำอธิบาย sentiment แล้วสร้าง embedding จากข้อมูลนั้น
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# 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}')