분류 작업을 위한 임베딩

OpenAI API로 시작하는 임베딩 Introduction

Emmanuel Pire

Senior Software Engineer, DataCamp

분류 작업

 

항목에 레이블 할당

  • 범주화
    • 예시: 헤드라인을 주제로 분류
  • 감성 분석

 

기사 분류에 사용할 수 있는 주제 테이블.

OpenAI API로 시작하는 임베딩 Introduction

분류 작업

 

항목에 레이블 할당

  • 범주화
    • 예시: 헤드라인을 주제로 분류
  • 감성 분석
    • 예시: 리뷰를 긍정 또는 부정으로 분류

 

임베딩의미론적 의미를 포착

 

감성에 따라 분류할 수 있는 웃는 얼굴과 슬픈 얼굴이 담긴 테이블.

OpenAI API로 시작하는 임베딩 Introduction

임베딩을 활용한 분류

  • 제로샷 분류:
    • 레이블 데이터 미사용

 

프로세스:

  1. 클래스 설명 임베딩

벡터 공간에 임베딩된 클래스 설명.

OpenAI API로 시작하는 임베딩 Introduction

임베딩을 활용한 분류

  • 제로샷 분류:
    • 레이블 데이터 미사용

 

프로세스:

  1. 클래스 설명 임베딩
  2. 분류할 항목 임베딩
  3. 코사인 거리 계산

벡터 공간에 임베딩된 클래스 설명과 미지의 벡터.

OpenAI API로 시작하는 임베딩 Introduction

임베딩을 활용한 분류

  • 제로샷 분류:
    • 레이블 데이터 미사용

 

프로세스:

  1. 클래스 설명 임베딩
  2. 분류할 항목 임베딩
  3. 코사인 거리 계산
  4. 가장 유사한 레이블 할당

벡터 공간에 임베딩된 클래스 설명과 Tech 레이블이 할당된 미지의 벡터.

OpenAI API로 시작하는 임베딩 Introduction

클래스 설명 임베딩

topics = [
  {'label': 'Tech'},
  {'label': 'Science'},
  {'label': 'Sport'},
  {'label': 'Business'},
]

class_descriptions = [topic['label'] for topic in topics]
class_embeddings = create_embeddings(class_descriptions)
OpenAI API로 시작하는 임베딩 Introduction

분류할 항목 임베딩

article = {"headline": "How NVIDIA GPUs Could Decide Who Wins the AI Race",
           "keywords": ["ai", "business", "computers"]}

def create_article_text(article): return f"""Headline: {article['headline']} Keywords: {', '.join(article['keywords'])}""" article_text = create_article_text(article)
article_embeddings = create_embeddings(article_text)[0]
OpenAI API로 시작하는 임베딩 Introduction

코사인 거리 계산

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

closest = find_closest(article_embeddings, class_embeddings)
OpenAI API로 시작하는 임베딩 Introduction

가장 유사한 레이블 추출

label = topics[closest['index']]['label']

print(label)
Business
article = {"headline": "How NVIDIA GPUs Could Decide Who Wins the AI Race",
           "keywords": ["ai", "business", "computers"]}

한계:

  • 클래스 설명의 세부 정보 부족
OpenAI API로 시작하는 임베딩 Introduction

더 상세한 설명

topics = [
  {'label': 'Tech', 'description': 'A news article about technology'},
  {'label': 'Science', 'description': 'A news article about science'},
  {'label': 'Sport', 'description': 'A news article about sports'},
  {'label': 'Business', 'description': 'A news article about business'},
]

class_descriptions = [topic['description'] for topic in topics] class_embeddings = create_embeddings(class_descriptions)
[...] label = topics[closest['index']]['label'] print(label)
Tech
OpenAI API로 시작하는 임베딩 Introduction

연습해 봅시다!

OpenAI API로 시작하는 임베딩 Introduction

Preparing Video For Download...