# Few Shot Learning – Siamese Network

https://miro.medium.com/max/1200/0*AuZLKEVsQpsTD3Dt

Original Source Here

# Few Shot Learning – Siamese Network

Few shot learning 算是 meta-learning 的其中一塊，核心概念是讓模型學會學習(learn to learn)。這樣說有點懸，我們可以把它理解成: few shot learning 是要讓模型學會區分事物的差異。一個學會區分事物差異的模型，我們可以把它用在訓練集從未見過的新類別，並且可以只透過很少的樣本(few shot) 就學會區別此事物。

# Siamese Network

Siamese 這個詞是孿生、連體嬰的意思，表示兩個人身體相連且共享部分的器官。而siamese network 是只有兩個架構權重都相同的類神經網路組合在一起(如下右圖)

`class siameseNet(nn.Module):    def __init__(self, embedding_net):        super(siameseNet, self).__init__()        self.embedding_net = embedding_net    def forward(self, x1, x2):        output1 = self.embedding_net(x1)        output2 = self.embedding_net(x2)        return output1, output2    def get_embedding(self, x):        return self.embedding_net(x)`

Contrastive loss

`class ContrastiveLoss(nn.Module):   def __init__(self, margin):        super(ContrastiveLoss, self).__init__()        self.margin = margin        self.eps = 1e-9   def forward(self, output1, output2, target, size_average=True):        distances = (output2 - output1).pow(2).sum(1)  # squared distances        losses = 0.5 * (target.float() * distances +                        (1 + -1 * target).float() * F.relu(self.margin - (distances + self.eps).sqrt()).pow(2))        return losses.mean() if size_average else losses.sum()`

Triplet loss

Triplet Loss是Google 在 2015 年發表的 FaceNet 論文中提出。可視為Contrastive loss 的改良。

triplet loss 必須建構在三元的image pair 下才能計算，搭配的網路架構如下

`class TripletLoss(nn.Module):   def __init__(self, margin):        super(TripletLoss, self).__init__()        self.margin = margin   def forward(self, anchor, positive, negative, size_average=True):        distance_positive = (anchor - positive).pow(2).sum(1)          distance_negative = (anchor - negative).pow(2).sum(1)          losses = F.relu(distance_positive - distance_negative + self.margin)        return losses.mean() if size_average else losses.sum()`

`class TripletNet(nn.Module):    def __init__(self, embedding_net):        super(TripletNet, self).__init__()        self.embedding_net = embedding_net    def forward(self, x1, x2, x3):        output1 = self.embedding_net(x1)        output2 = self.embedding_net(x2)        output3 = self.embedding_net(x3)        return output1, output2, output3    def get_embedding(self, x):        return self.embedding_net(x)`

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot