ChanceNa commited on
Commit
1cc439e
·
verified ·
1 Parent(s): 2192fff

Upload train.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train.py +18 -29
train.py CHANGED
@@ -108,7 +108,7 @@ BENCHMARK = [
108
 
109
  # -- Hyperparameters --
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
@@ -745,36 +745,25 @@ def train():
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)
 
108
 
109
  # -- Hyperparameters --
110
  BASE_MODEL = "nomic-ai/nomic-embed-text-v1.5"
111
+ EPOCHS = 10
112
  BATCH_SIZE = 64
113
  LEARNING_RATE = 1e-4
114
  WARMUP_RATIO = 0.1
 
745
  if baseline["failures"]:
746
  print(f" Failures ({len(baseline['failures'])}): {baseline['failures']}")
747
 
748
+ # Build training objectives — MNRL (no Matryoshka) + hard negative TripletLoss
749
+ pos_examples = [InputExample(texts=[p["query"], p["positive"]]) for p in positive_pairs]
750
+ trip_examples = [InputExample(texts=[t["query"], t["positive"], t["negative"]]) for t in triplets]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
751
 
752
  train_objectives = []
753
+
754
+ if pos_examples:
755
+ pos_loader = DataLoader(pos_examples, shuffle=True, batch_size=BATCH_SIZE)
756
+ mnrl_loss = losses.MultipleNegativesRankingLoss(model)
757
+ if MATRYOSHKA_DIMS:
758
+ mnrl_loss = losses.MatryoshkaLoss(model, mnrl_loss, matryoshka_dims=MATRYOSHKA_DIMS)
759
+ train_objectives.append((pos_loader, mnrl_loss))
760
+
761
+ if trip_examples:
762
+ trip_loader = DataLoader(trip_examples, shuffle=True, batch_size=BATCH_SIZE)
763
+ triplet_loss = losses.TripletLoss(model)
764
+ if MATRYOSHKA_DIMS:
765
+ triplet_loss = losses.MatryoshkaLoss(model, triplet_loss, matryoshka_dims=MATRYOSHKA_DIMS)
766
+ train_objectives.append((trip_loader, triplet_loss))
767
 
768
  total_steps = sum(len(loader) for loader, _ in train_objectives) * EPOCHS
769
  warmup_steps = int(total_steps * WARMUP_RATIO)