How exactly do you calculate the similarity/distance in a Siamese network, and classify them after?
This is my current attempt
class SiameseNetwork(nn.Module):
def __init__(self) -> None:
super().__init__()
self.resnet = torchvision.models.resnet18(num_classes=5)
def forward_once(self, item):
output = self.resnet(item)
return output
def forward(self, anchor, positive, negative):
output1 = self.forward_once(anchor)
output2 = self.forward_once(positive)
output3 = self.forward_once(negative)
return output1, output2, output3
I use resnet and TripletMarginLoss as the loss function but i’m confused on how to calculate similarity and classify the output after
also if you find anything wrong about the code please tell me.
Mitutoyo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
TripletMarginLoss
uses an underlying p-norm distance. If you are using the default parameter for p=2
, then you should use euclidean distance to compute the similarity of embeddings.
Classification requires a labeled classification dataset and a a separate loss for classification, the same as any standard classification problem. Similarity loss does not give you classification.
1