Sobes.tech
Middle — Senior

How to convert items into embeddings and recommend based on them?

sobes.tech AI

Answer from AI

To convert items (such as products, articles, movies) into embeddings and recommend based on them, the following steps are usually performed:

  1. Feature selection: gather data about items — text descriptions, categories, characteristics, images, etc.

  2. Conversion to vector space:

    • For textual data, models like Word2Vec, FastText, BERT, Sentence Transformers can be used to convert text into fixed-dimensional vectors.
    • For images — use pre-trained CNNs (e.g., ResNet) to extract features.
    • For categorical and numerical features — apply one-hot encoding, normalization, and/or train embeddings.
  3. Feature merging: if there are multiple data types, combine them into a single embedding vector.

  4. Training an embedding model (optional): train a model that considers interactions between users and items (e.g., matrix factorization, neural networks with triplet loss or contrastive loss).

  5. Recommendations:

    • For a new user or query, find embeddings of the items of interest.
    • Use similarity metrics (cosine similarity, Euclidean distance) to find nearest neighbors in the embedding space.
    • Recommend items with the closest embeddings.

Example in Python using the sentence-transformers library for text descriptions:

from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np

items = ["Red cotton T-shirt", "Blue jeans", "Black leather boots"]
model = SentenceTransformer('all-MiniLM-L6-v2')
embeddings = model.encode(items)

# Find an item similar to "Blue T-shirt"
query = "Blue T-shirt"
query_emb = model.encode([query])

similarities = cosine_similarity(query_emb, embeddings)[0]
most_similar_idx = np.argmax(similarities)
print(f"Recommended item: {items[most_similar_idx]}")

Thus, embeddings allow representing items in a convenient form for comparison and building recommendations based on proximity in vector space.