Spaces:
Runtime error
Runtime error
felix
commited on
Commit
·
04c8a7b
1
Parent(s):
3004443
training
Browse files- data_set_training.csv +1 -0
- dev_set_training.csv +5 -0
- train.py +170 -1
data_set_training.csv
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
101 Crawfords Corner Road, Suite 2511-W, Holmdel NJ 07734|101 Crawfords Corner Rd, Ste 2511-W, Holmdel NJ 07734|1
|
| 2 |
101 Crawfords Corner Road, Suite 2511-W, Holmdel NJ 07734|101 Crawfords Corner Road, Ste 2511 W, Holmdel NJ 07734|1
|
| 3 |
101 Crawfords Corner Road, Suite 2511-W, Holmdel NJ 07734|101 Crawfords Corner Rd, Suite 2511-W, Holmdel NJ 07734|1
|
|
|
|
| 1 |
+
address1|address2|are_same
|
| 2 |
101 Crawfords Corner Road, Suite 2511-W, Holmdel NJ 07734|101 Crawfords Corner Rd, Ste 2511-W, Holmdel NJ 07734|1
|
| 3 |
101 Crawfords Corner Road, Suite 2511-W, Holmdel NJ 07734|101 Crawfords Corner Road, Ste 2511 W, Holmdel NJ 07734|1
|
| 4 |
101 Crawfords Corner Road, Suite 2511-W, Holmdel NJ 07734|101 Crawfords Corner Rd, Suite 2511-W, Holmdel NJ 07734|1
|
dev_set_training.csv
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
address1|address2|are_same
|
| 2 |
+
1555 Ruth Rd STE 5, North Brunswick Township, NJ 08902|1555 Ruth Road Suit 5, North Brunswick Township, NJ 08902|1
|
| 3 |
+
1555 Ruth Rd STE 5, North Brunswick Township, NJ 08902|1558 Ruth Road STE 5, North Brunswick Township, NJ 08902|0
|
| 4 |
+
1061 Schmidt Ln, North Brunswick Township, NJ 08902|1061 Schmidt Lane, North Brunswick Township, NJ 08902|1
|
| 5 |
+
1061 Schmidt Ln, North Brunswick Township, NJ 08902|934 Schmidt Ln, North Brunswick Township, NJ 08902|0
|
train.py
CHANGED
|
@@ -1,4 +1,173 @@
|
|
| 1 |
# Will be based on
|
| 2 |
# ConstructiveLoss function.
|
| 3 |
#
|
| 4 |
-
# https://github.com/UKPLab/sentence-transformers/blob/master/examples/training/quora_duplicate_questions/training_OnlineContrastiveLoss.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Will be based on
|
| 2 |
# ConstructiveLoss function.
|
| 3 |
#
|
| 4 |
+
# https://github.com/UKPLab/sentence-transformers/blob/master/examples/training/quora_duplicate_questions/training_OnlineContrastiveLoss.py
|
| 5 |
+
|
| 6 |
+
from torch.utils.data import DataLoader
|
| 7 |
+
from sentence_transformers import losses, util
|
| 8 |
+
from sentence_transformers import LoggingHandler, SentenceTransformer, evaluation
|
| 9 |
+
from sentence_transformers.readers import InputExample
|
| 10 |
+
import logging
|
| 11 |
+
from datetime import datetime
|
| 12 |
+
import csv
|
| 13 |
+
import os
|
| 14 |
+
from zipfile import ZipFile
|
| 15 |
+
import random
|
| 16 |
+
|
| 17 |
+
#### Just some code to print debug information to stdout
|
| 18 |
+
logging.basicConfig(format='%(asctime)s - %(message)s',
|
| 19 |
+
datefmt='%Y-%m-%d %H:%M:%S',
|
| 20 |
+
level=logging.INFO,
|
| 21 |
+
handlers=[LoggingHandler()])
|
| 22 |
+
logger = logging.getLogger(__name__)
|
| 23 |
+
#### /print debug information to stdout
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
#As base model, we use DistilBERT-base that was pre-trained on NLI and STSb data
|
| 27 |
+
model = SentenceTransformer('albert-base-v2')
|
| 28 |
+
num_epochs = 10
|
| 29 |
+
train_batch_size = 8
|
| 30 |
+
|
| 31 |
+
#As distance metric, we use cosine distance (cosine_distance = 1-cosine_similarity)
|
| 32 |
+
distance_metric = losses.SiameseDistanceMetric.COSINE_DISTANCE
|
| 33 |
+
|
| 34 |
+
#Negative pairs should have a distance of at least 0.5
|
| 35 |
+
margin = 0.5
|
| 36 |
+
|
| 37 |
+
dataset_path = "data_set_training.csv"
|
| 38 |
+
model_save_path = 'output/training_OnlineConstrativeLoss-'+datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 39 |
+
|
| 40 |
+
os.makedirs(model_save_path, exist_ok=True)
|
| 41 |
+
|
| 42 |
+
######### Read train data ##########
|
| 43 |
+
# Read train data
|
| 44 |
+
train_samples = []
|
| 45 |
+
with open(dataset_path, encoding='utf8') as fIn:
|
| 46 |
+
reader = csv.DictReader(fIn, delimiter='|', quoting=csv.QUOTE_NONE)
|
| 47 |
+
for row in reader:
|
| 48 |
+
sample = InputExample(texts=[row['address1'], row['address2']], label=int(row['are_same']))
|
| 49 |
+
train_samples.append(sample)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
train_dataloader = DataLoader(train_samples, shuffle=True, batch_size=train_batch_size)
|
| 53 |
+
train_loss = losses.OnlineContrastiveLoss(model=model, distance_metric=distance_metric, margin=margin)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
################### Development Evaluators ##################
|
| 57 |
+
# We add 3 evaluators, that evaluate the model on Duplicate Questions pair classification,
|
| 58 |
+
# Duplicate Questions Mining, and Duplicate Questions Information Retrieval
|
| 59 |
+
#evaluators = []
|
| 60 |
+
|
| 61 |
+
###### Classification ######
|
| 62 |
+
# Given (quesiton1, question2), is this a duplicate or not?
|
| 63 |
+
# The evaluator will compute the embeddings for both questions and then compute
|
| 64 |
+
# a cosine similarity. If the similarity is above a threshold, we have a duplicate.
|
| 65 |
+
dev_sentences1 = []
|
| 66 |
+
dev_sentences2 = []
|
| 67 |
+
dev_labels = []
|
| 68 |
+
with open( "dev_set_training.csv", encoding='utf8') as fIn:
|
| 69 |
+
reader = csv.DictReader(fIn, delimiter='|', quoting=csv.QUOTE_NONE)
|
| 70 |
+
for row in reader:
|
| 71 |
+
dev_sentences1.append(row['address1'])
|
| 72 |
+
dev_sentences2.append(row['address2'])
|
| 73 |
+
dev_labels.append(int(row['are_same']))
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
binary_acc_evaluator = evaluation.BinaryClassificationEvaluator(dev_sentences1, dev_sentences2, dev_labels)
|
| 77 |
+
#evaluators.append(binary_acc_evaluator)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
###### Duplicate Questions Mining ######
|
| 82 |
+
# Given a large corpus of questions, identify all duplicates in that corpus.
|
| 83 |
+
|
| 84 |
+
# For faster processing, we limit the development corpus to only 10,000 sentences.
|
| 85 |
+
#max_dev_samples = 10000
|
| 86 |
+
#dev_sentences = {}
|
| 87 |
+
#dev_duplicates = []
|
| 88 |
+
#with open("dev_corpus.csv", encoding='utf8') as fIn:
|
| 89 |
+
# reader = csv.DictReader(fIn, delimiter='|', quoting=csv.QUOTE_NONE)
|
| 90 |
+
# for row in reader:
|
| 91 |
+
# dev_sentences[row['qid']] = row['question']
|
| 92 |
+
#
|
| 93 |
+
# if len(dev_sentences) >= max_dev_samples:
|
| 94 |
+
# break
|
| 95 |
+
#
|
| 96 |
+
#with open(os.path.join(dataset_path, "duplicate-mining/dev_duplicates.tsv"), encoding='utf8') as fIn:
|
| 97 |
+
# reader = csv.DictReader(fIn, delimiter='\t', quoting=csv.QUOTE_NONE)
|
| 98 |
+
# for row in reader:
|
| 99 |
+
# if row['qid1'] in dev_sentences and row['qid2'] in dev_sentences:
|
| 100 |
+
# dev_duplicates.append([row['qid1'], row['qid2']])
|
| 101 |
+
#
|
| 102 |
+
#
|
| 103 |
+
## The ParaphraseMiningEvaluator computes the cosine similarity between all sentences and
|
| 104 |
+
## extracts a list with the pairs that have the highest similarity. Given the duplicate
|
| 105 |
+
## information in dev_duplicates, it then computes and F1 score how well our duplicate mining worked
|
| 106 |
+
#paraphrase_mining_evaluator = evaluation.ParaphraseMiningEvaluator(dev_sentences, dev_duplicates, name='dev')
|
| 107 |
+
#evaluators.append(paraphrase_mining_evaluator)
|
| 108 |
+
#
|
| 109 |
+
#
|
| 110 |
+
####### Duplicate Questions Information Retrieval ######
|
| 111 |
+
## Given a question and a large corpus of thousands questions, find the most relevant (i.e. duplicate) question
|
| 112 |
+
## in that corpus.
|
| 113 |
+
#
|
| 114 |
+
## For faster processing, we limit the development corpus to only 10,000 sentences.
|
| 115 |
+
#max_corpus_size = 100000
|
| 116 |
+
#
|
| 117 |
+
#ir_queries = {} #Our queries (qid => question)
|
| 118 |
+
#ir_needed_qids = set() #QIDs we need in the corpus
|
| 119 |
+
#ir_corpus = {} #Our corpus (qid => question)
|
| 120 |
+
#ir_relevant_docs = {} #Mapping of relevant documents for a given query (qid => set([relevant_question_ids])
|
| 121 |
+
#
|
| 122 |
+
#with open(os.path.join(dataset_path, 'information-retrieval/dev-queries.tsv'), encoding='utf8') as fIn:
|
| 123 |
+
# next(fIn) #Skip header
|
| 124 |
+
# for line in fIn:
|
| 125 |
+
# qid, query, duplicate_ids = line.strip().split('\t')
|
| 126 |
+
# duplicate_ids = duplicate_ids.split(',')
|
| 127 |
+
# ir_queries[qid] = query
|
| 128 |
+
# ir_relevant_docs[qid] = set(duplicate_ids)
|
| 129 |
+
#
|
| 130 |
+
# for qid in duplicate_ids:
|
| 131 |
+
# ir_needed_qids.add(qid)
|
| 132 |
+
#
|
| 133 |
+
## First get all needed relevant documents (i.e., we must ensure, that the relevant questions are actually in the corpus
|
| 134 |
+
#distraction_questions = {}
|
| 135 |
+
#with open(os.path.join(dataset_path, 'information-retrieval/corpus.tsv'), encoding='utf8') as fIn:
|
| 136 |
+
# next(fIn) #Skip header
|
| 137 |
+
# for line in fIn:
|
| 138 |
+
# qid, question = line.strip().split('\t')
|
| 139 |
+
#
|
| 140 |
+
# if qid in ir_needed_qids:
|
| 141 |
+
# ir_corpus[qid] = question
|
| 142 |
+
# else:
|
| 143 |
+
# distraction_questions[qid] = question
|
| 144 |
+
#
|
| 145 |
+
## Now, also add some irrelevant questions to fill our corpus
|
| 146 |
+
#other_qid_list = list(distraction_questions.keys())
|
| 147 |
+
#random.shuffle(other_qid_list)
|
| 148 |
+
#
|
| 149 |
+
#for qid in other_qid_list[0:max(0, max_corpus_size-len(ir_corpus))]:
|
| 150 |
+
# ir_corpus[qid] = distraction_questions[qid]
|
| 151 |
+
#
|
| 152 |
+
##Given queries, a corpus and a mapping with relevant documents, the InformationRetrievalEvaluator computes different IR
|
| 153 |
+
## metrices. For our use case MRR@k and Accuracy@k are relevant.
|
| 154 |
+
#ir_evaluator = evaluation.InformationRetrievalEvaluator(ir_queries, ir_corpus, ir_relevant_docs)
|
| 155 |
+
#
|
| 156 |
+
#evaluators.append(ir_evaluator)
|
| 157 |
+
#
|
| 158 |
+
## Create a SequentialEvaluator. This SequentialEvaluator runs all three evaluators in a sequential order.
|
| 159 |
+
## We optimize the model with respect to the score from the last evaluator (scores[-1])
|
| 160 |
+
#seq_evaluator = evaluation.SequentialEvaluator(evaluators, main_score_function=lambda scores: scores[-1])
|
| 161 |
+
#
|
| 162 |
+
#
|
| 163 |
+
#logger.info("Evaluate model without training")
|
| 164 |
+
#seq_evaluator(model, epoch=0, steps=0, output_path=model_save_path)
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
# Train the model
|
| 168 |
+
model.fit(train_objectives=[(train_dataloader, train_loss)],
|
| 169 |
+
evaluator=binary_acc_evaluator,
|
| 170 |
+
epochs=num_epochs,
|
| 171 |
+
warmup_steps=5,
|
| 172 |
+
output_path=model_save_path
|
| 173 |
+
)
|