Upload train.py with huggingface_hub
Browse files
train.py
CHANGED
|
@@ -110,9 +110,9 @@ BENCHMARK = [
|
|
| 110 |
BASE_MODEL = "nomic-ai/nomic-embed-text-v1.5"
|
| 111 |
EPOCHS = 30
|
| 112 |
BATCH_SIZE = 64
|
| 113 |
-
LEARNING_RATE =
|
| 114 |
WARMUP_RATIO = 0.1
|
| 115 |
-
MATRYOSHKA_DIMS = [
|
| 116 |
GARBAGE_THRESHOLD = 0.3
|
| 117 |
|
| 118 |
# -- Training Data --
|
|
@@ -745,25 +745,36 @@ def train():
|
|
| 745 |
if baseline["failures"]:
|
| 746 |
print(f" Failures ({len(baseline['failures'])}): {baseline['failures']}")
|
| 747 |
|
| 748 |
-
# Build training objectives
|
| 749 |
-
|
| 750 |
-
|
|
|
|
|
|
|
| 751 |
|
| 752 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 753 |
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 767 |
|
| 768 |
total_steps = sum(len(loader) for loader, _ in train_objectives) * EPOCHS
|
| 769 |
warmup_steps = int(total_steps * WARMUP_RATIO)
|
|
|
|
| 110 |
BASE_MODEL = "nomic-ai/nomic-embed-text-v1.5"
|
| 111 |
EPOCHS = 30
|
| 112 |
BATCH_SIZE = 64
|
| 113 |
+
LEARNING_RATE = 1e-4
|
| 114 |
WARMUP_RATIO = 0.1
|
| 115 |
+
MATRYOSHKA_DIMS = [] # Disabled — dilutes gradient across dims
|
| 116 |
GARBAGE_THRESHOLD = 0.3
|
| 117 |
|
| 118 |
# -- Training Data --
|
|
|
|
| 745 |
if baseline["failures"]:
|
| 746 |
print(f" Failures ({len(baseline['failures'])}): {baseline['failures']}")
|
| 747 |
|
| 748 |
+
# Build training objectives — TripletLoss only
|
| 749 |
+
# Convert positive pairs into triplets by sampling random negatives from corpus
|
| 750 |
+
qp = "search_query: "
|
| 751 |
+
dp = "search_document: "
|
| 752 |
+
corpus_docs = [f"{dp}{name}" for name in corpus_names]
|
| 753 |
|
| 754 |
+
all_triplet_examples = []
|
| 755 |
+
|
| 756 |
+
# Hard negatives first (highest value)
|
| 757 |
+
for t in triplets:
|
| 758 |
+
all_triplet_examples.append(InputExample(texts=[t["query"], t["positive"], t["negative"]]))
|
| 759 |
|
| 760 |
+
# Convert positive pairs to triplets with random corpus negatives
|
| 761 |
+
for p in positive_pairs:
|
| 762 |
+
pos_subcat = strip_prefix(p["positive"])
|
| 763 |
+
# Pick a random negative that isn't the positive
|
| 764 |
+
neg_candidates = [d for d in corpus_docs if strip_prefix(d) != pos_subcat]
|
| 765 |
+
if neg_candidates:
|
| 766 |
+
neg = random.choice(neg_candidates)
|
| 767 |
+
all_triplet_examples.append(InputExample(texts=[p["query"], p["positive"], neg]))
|
| 768 |
+
|
| 769 |
+
random.shuffle(all_triplet_examples)
|
| 770 |
+
print(f" Total triplets (hard + random neg): {len(all_triplet_examples)}")
|
| 771 |
+
|
| 772 |
+
train_objectives = []
|
| 773 |
+
trip_loader = DataLoader(all_triplet_examples, shuffle=True, batch_size=BATCH_SIZE)
|
| 774 |
+
triplet_loss = losses.TripletLoss(model)
|
| 775 |
+
if MATRYOSHKA_DIMS:
|
| 776 |
+
triplet_loss = losses.MatryoshkaLoss(model, triplet_loss, matryoshka_dims=MATRYOSHKA_DIMS)
|
| 777 |
+
train_objectives.append((trip_loader, triplet_loss))
|
| 778 |
|
| 779 |
total_steps = sum(len(loader) for loader, _ in train_objectives) * EPOCHS
|
| 780 |
warmup_steps = int(total_steps * WARMUP_RATIO)
|