Skip to content

Instantly share code, notes, and snippets.

View ShairozS's full-sized avatar

Shairoz Sohail ShairozS

View GitHub Profile
@ShairozS
ShairozS / losses.py
Created November 25, 2021 22:22
Contrastive Loss (pairs only)
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
@ShairozS
ShairozS / BertEmbedder.py
Created November 23, 2021 00:16
Use a pre-trained BeRT model to create sentence embeddings
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/
@ShairozS
ShairozS / train_mnist_triplet.ipynb
Created May 12, 2021 20:50
Train_MNIST_TripletLoss
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ShairozS
ShairozS / form_triplets.py
Created April 19, 2021 16:44
Form triplets from a batch of Pytorch tensor embeddings
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
@ShairozS
ShairozS / form_pairs.py
Created April 19, 2021 16:42
Form all pairwise examples from two Pytorch Tensors, along with a similarity label
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
'''
@ShairozS
ShairozS / train_mnist.ipynb
Created April 15, 2021 22:18
Train MNIST (Contrastive Learning)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ShairozS
ShairozS / losses.py
Created April 13, 2021 19:52
Contrastive loss
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
@ShairozS
ShairozS / trainers.py
Created April 13, 2021 19:30
Trainer for Pytorch contrastive model
import torch
import numpy as np
class Trainer:
##########################
# #
# Initialization #
@ShairozS
ShairozS / trainers.py
Created April 13, 2021 19:03
Generic trainer for Pytorch model
import torch
import numpy as np
class Trainer:
##########################
# #
# Initialization #
# #
##########################
@ShairozS
ShairozS / labeled_contrastive_dataset.py
Created April 13, 2021 01:31
Labeled Contrastive Dataset (PyTorch)
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)