Embeddingy pro klasifikační úlohy

Introduction to Embeddings with the OpenAI API

Emmanuel Pire

Senior Software Engineer, DataCamp

Klasifikační úlohy

 

Přiřazování názvětek položkám

  • Kategorizace
    • Příklad: přiřazení nadpisů do témat
  • Analýza sentimentu

 

Tabulka témat pro kategorizaci článků.

Introduction to Embeddings with the OpenAI API

Klasifikační úlohy

 

Přiřazování názvětek položkám

  • Kategorizace
    • Příklad: přiřazení nadpisů do témat
  • Analýza sentimentu
    • Příklad: klasifikace recenzí jako pozitivních nebo negativních

 

Embeddingy zachycují sémantický význam

 

Tabulka se smajlíkem a smutným obličejem pro kategorizaci podle sentimentu.

Introduction to Embeddings with the OpenAI API

Klasifikace pomocí embeddingů

  • Zero-shot klasifikace:
    • Bez použití označených dat

 

Postup:

  1. Embedujte popisy tříd

Embeddované popisy tříd ve vektorovém prostoru.

Introduction to Embeddings with the OpenAI API

Klasifikace pomocí embeddingů

  • Zero-shot klasifikace:
    • Bez použití označených dat

 

Postup:

  1. Embedujte popisy tříd
  2. Embedujte klasifikovanou položku
  3. Vypočítejte kosinové vzdálenosti

Embeddované popisy tříd ve vektorovém prostoru s neznámým vektorem.

Introduction to Embeddings with the OpenAI API

Klasifikace pomocí embeddingů

  • Zero-shot klasifikace:
    • Bez použití označených dat

 

Postup:

  1. Embedujte popisy tříd
  2. Embedujte klasifikovanou položku
  3. Vypočítejte kosinové vzdálenosti
  4. Přiřaďte nejpodobnější názvětek

Embeddované popisy tříd ve vektorovém prostoru s neznámým vektorem přiřazeným ke kategorii Tech.

Introduction to Embeddings with the OpenAI API

Embedování popisů tříd

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

class_descriptions = [topic['label'] for topic in topics]
class_embeddings = create_embeddings(class_descriptions)
Introduction to Embeddings with the OpenAI API

Embedování klasifikované položky

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]
Introduction to Embeddings with the OpenAI API

Výpočet kosinových vzdáleností

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)
Introduction to Embeddings with the OpenAI API

Extrakce nejpodobnější názvětky

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

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

Omezení:

  • Popisy tříd nebyly dostatečně podrobné
Introduction to Embeddings with the OpenAI API

Podrobnější popisy

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
Introduction to Embeddings with the OpenAI API

Pojďme si procvičit!

Introduction to Embeddings with the OpenAI API

Preparing Video For Download...