Sentence transformer target encoding

From wenmin-wu/ds-skills · View interactive version

Sentence transformer target encoding is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Encode text prompts into fixed-length dense vectors using SentenceTransformer for cosine-similarity evaluation in image-to-text retrieval tasks

How to use this skill

Skill file

SentenceTransformer Target Encoding

Overview

In image-to-prompt competitions, the target is not raw text but its SentenceTransformer embedding. Models predict embedding vectors and are scored by cosine similarity against the ground-truth prompt embeddings. Understanding this encoding step is essential: predictions must live in the same embedding space as the evaluation targets.

Quick Start

from sentence_transformers import SentenceTransformer
import numpy as np

st_model = SentenceTransformer("all-MiniLM-L6-v2")

prompts = ["a painting of a sunset over mountains", "cyberpunk city at night"]
embeddings = st_model.encode(prompts, normalize_embeddings=True)
# embeddings.shape: (2, 384)

# Cosine similarity between two prompt embeddings
similarity = np.dot(embeddings[0], embeddings[1])

Workflow

  1. Load a SentenceTransformer model matching the competition's evaluation model
  2. Encode all ground-truth prompts into dense vectors (the target)
  3. Any prediction pipeline must output vectors in this same space
  4. Score predictions via cosine similarity: np.dot(pred, target) / (norm(pred) * norm(target))
  5. For submission, flatten the embedding matrix row-wise into a single column

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.