시작하기무료로 시작하기

더 자세한 설명 임베딩하기

마지막에 예측된 레이블 중 하나가 리뷰를 잘 대표하지 못했던 것 같아요. 이는 클래스 레이블만 임베딩할 때 담기는 정보가 부족했기 때문일 수 있습니다. 이번에는 각 클래스의 설명을 임베딩하여, 모델이 여러분이 레스토랑 리뷰를 분류하고 있다는 점을 더 잘 "이해"하도록 해 볼게요.

다음 객체들을 사용할 수 있어요:

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!"]

이 연습은 강의의 일부입니다

OpenAI API로 시작하는 임베딩 Introduction

강의 보기

연습 안내

  • 감성 설명으로 이루어진 리스트를 추출하고, 이를 임베딩하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# 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}')
코드 편집 및 실행