Sentence-transformers: BatchHard triplet loss

Created on 14 May 2020  路  4Comments  路  Source: UKPLab/sentence-transformers

Could you give me an example of how to use BatchHardTripletLoss and which evaluator should we use with it?
I have data in the format [text, label]
I tried to train it without the evaluator and I got this error.
Screenshot 2020-05-14 at 1 49 44 PM

Most helpful comment

Hi @nreimers @zishanmuzeeb,

I was actually able to make this work and could provide a fix with a PR, if there is any interest.

I also implemented batch hard with a soft margin as an option.

All 4 comments

I sadly don't have a current example.

From an really old example, you would need the classes:

hard_triplet_reader = LabelSentenceReader('datasets/hard-wiki')
train_examples = hard_triplet_reader.get_examples('train.csv', 5000)
data_sampler = BatchHardTripletSampler(train_examples, samples_per_label=samples_per_label)
train_dataloader = DataLoader(train_data, sampler=data_sampler, batch_size=train_batch_size, collate_fn=collate)

But as mention, this code is from before the first version was published here. Since then, pretty much all the code has changed.

If you get the example working, would be great to post it here so that it can be shared with others.

Best
Nils Reimers

Hi @nreimers @zishanmuzeeb,

I was actually able to make this work and could provide a fix with a PR, if there is any interest.

I also implemented batch hard with a soft margin as an option.

@cpcdoy Please do!

Just made PR #254 to fix this issue and also added a simple example usage in the examples/ folder

Was this page helpful?
0 / 5 - 0 ratings