0

I am implementing a Siamese neural network with triplet loss.

The model, trained_model, is fit on three inputs: anchor, positive, and negative.

However, my SNN aims to determine similarity between two texts. So I want to feed 2 inputs into the model and get a 0 if the documents are similar, and a 1 otherwise.

I know that I should save my weights from trained_model and load them onto a new model. However, how do I actually do this?

I asked about this in another post, but am approaching this question from another angle (tensorflow weights).

Is there a way to load weights from the three input model to a two input model?

EDIT**:

I have seen this done:

eval_model = Model(inputs=anchor_input, outputs=encoded_anchor)
eval_model.load_weights('weights.hdf5')

But not sure how to use this to test the similarity between two inputs

maxipod
  • 79
  • 6

1 Answers1

0

From what I've understood of Siamese networks and the Triplet Loss function, what you get as an output is a sort of similarity score between two input things. More precisely, once trained with an anchor, you should be able to obtain a similarity score between any input object (of similar type as the anchor) and the said anchor. As far as I get it, it won't give you a way to build a similarity function between any two objects. So if you want a similarity score between object A and object B, define one as the anchor, apply some training to the SN (using the TL function, though not using B as a negative example) and once the training is done, you should be able to get what you want.

GregoirePelegrin
  • 1,206
  • 2
  • 7
  • 23
  • 1
    Thanks for your thoughts! Could you please expand on how object B is used in the testing stage? (I understand feeding in object A as the anchor) – maxipod Apr 23 '22 at 17:53
  • I wouldn't use B in the testing phase as you're supposed to know the ground truth for this dataset too. I would use B only when the training AND testing phase are completed and you're satisfied with the performances of your model. – GregoirePelegrin Apr 23 '22 at 20:02
  • hmm I think I'm still not following, would you mind writing what you mean in code? I included an edit in my post which is similar to what I think you're suggesting. Except I don't see how to test the model after loading weights onto it (I'm not interested in prediction, but ```model.evaluate```) – maxipod Apr 23 '22 at 22:57
  • What do you mean, you're not interested in prediction but in `model.evaluate`? The `evaluate` method does the prediction... – GregoirePelegrin Apr 24 '22 at 07:33
  • My apologies, I meant that I want to do ```model.evaluate``` rather than ```model.predict``` as I want to get a final loss + accuracy from the model on validation data – maxipod Apr 24 '22 at 11:45
  • From what I can see, a SNN should be composed of 2 identical NN, producing embeddings for 2 inputs, and outputing the similarity between these embeddings. As far as I can tell, the triplet loss only is here for the training. So in theory, the core of the SNN is a single input/single output NN, which, once trained, should allow you to compute the similarity between any number of objects you want, as you only need to pass them through the NN to obtain their embeddings, and thus their distance (using the same distance function as in the triplet loss) – GregoirePelegrin Apr 24 '22 at 19:58