This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| import numpy as np | |
| import torch.nn.functional as F | |
| def form_pairs(inA, inB): | |
| ''' | |
| Form pairs from two tensors of embeddings. It is assumed that the embeddings at corresponding batch positions are similar | |
| and all other batch positions are dissimilar | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BertEmbedder(torch.nn.Module): | |
| def __init__(self, bert_model='bert-base-uncased'): | |
| ''' | |
| Initialize a BeRT model and use it to create tensor sentence embeddings of length 768 | |
| Made from the guide at: https://mccormickml.com/2019/05/14/BERT-word-embeddings-tutorial/ | |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def form_triplets(inA, inB): | |
| ''' | |
| Form triplets from two tensors of embeddings. It is assumed that the embeddings at corresponding batch positions are similar | |
| and all other batch positions are dissimilar | |
| i.e inA[i] ~ inB[i] and inA[i] !~ inB[j] for all i =! j | |
| ''' | |
| b, emb_size = inA.shape |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def form_pairs(inA, inB): | |
| ''' | |
| Form pairs from two tensors of embeddings. It is assumed that the embeddings at corresponding batch positions are similar | |
| and all other batch positions are dissimilar | |
| i.e inA[i] ~ inB[i] and inA[i] !~ inB[j] for all i =! j | |
| ''' | |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ContrastiveLoss(torch.nn.Module): | |
| """ | |
| Contrastive loss function. | |
| Based on: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf | |
| args: | |
| distance (function): A function that returns the distance between two tensors - should be a valid metric over R; default= L2 distance | |
| margin (scalar): The margin value between positive and negative class ; default=1.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| import numpy as np | |
| class Trainer: | |
| ########################## | |
| # # | |
| # Initialization # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| import numpy as np | |
| class Trainer: | |
| ########################## | |
| # # | |
| # Initialization # | |
| # # | |
| ########################## |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LabeledContrastiveDataset(Dataset): | |
| """ | |
| Take a folder containing sub-folders of images, where the sub-folder name is the image class, and generate | |
| pairs of images with the same class label for contrastive learning. This procedure fixes the batch size since | |
| every batch contains all classes and every batch element is a unique pairing of each class. | |
| """ | |
| def __init__(self, folder, transforms=None): | |
| labels = os.listdir(folder) |