Answers.Body: <p>In addition to @Omg's answer note that Siamese networks are typically used in situations where applying <code>(A,B)</code> to the inputs must generate the same output as applying <code>(B,A)</code> (i.e. the similarity measure of <code>A</code> to <code>B</code> is the same as the similarity of <code>B</code> to <code>A</code>).</p>
  <p>With a network with separate weights, this is not guaranteed. One way to get close to this is to not only use samples <code>(A,B)</code> as training input but also (equally often) <code>(B,A)</code>. Effectively this doubles the number of training steps (and therefore training time) and the network output is still not guaranteed to be symmetric.</p>
  <p>By sharing weights, the symmetry of the response of the network (<code>(A,B)</code> gives the same output as <code>(B,A)</code>) is guaranteed by design.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-16T08:10:23.260Z
Answers.Text: You say "With a network with separate weights, this is not guaranteed". But what do you mean by "separate weights"? It seems that the OP is suggesting to use a neural network that receives as input the 2 objects that need to be compared, so the weights of this neural network would, in a way, be shared among these 2 objects, so not separate. So, can you clarify this?
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-17T10:43:36.137Z
Answers.Text: Good point -- OP mentions "why not using instead a single neural network that receives as input the two objects". I implicitly assumed that this would be two subnetworks of identical structure (at least for the first few layers). So effectively I had 'two subnetworks with separate weights' in mind.
Answers.UserDisplayName: Andre Holzner
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-18T15:10:19.403Z
Answers.Text: Indeed, I was thinking of a single NN in which the input are the two images concatenated. Anyway, at least in the first layer the weights will be different and what [Andre Holzner](https://ai.stackexchange.com/users/18159/andre-holzner) has answered applies.
Answers.UserDisplayName: IgnacioGaBo
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-21T15:44:23.943Z
Answers.Text: Fantastic answer. What do you mean by the symmetry of the network and how Siamese offers that mathematically please (source if possible)?
Answers.UserDisplayName: Avv
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-21T15:45:28.027Z
Answers.Text: @nbro. Are there other networks for measuring similarity between a pair rather than Siamese architecture pleae?
Answers.UserDisplayName: Avv
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-16T07:28:48.713Z
Answers.LastActivityDate: 2022-04-16T07:28:48.713Z
Answers.UserDisplayName: Andre Holzner
Answers.Body: <p>Assuming you want to train an object detector to detect people in an image.</p>
  <p>The detector's neural networks learn to identify features that help them detect people. These features range from low-level aspects like edges to high-level characteristics such as the presence of feet, hands, and more.</p>
  <p>If you use the features from this network for person re-identification, you won't obtain good results because the learned features do not focus on extracting differences between individuals.</p>
  <p>However, if you employ a Siamese neural network with a loss function like Hinge, triplet,..., which involves comparing predictions between positive and negative matches, the network learns to extract features that highlight the differences between individuals, such as clothing color and more.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-21T01:49:14.497Z
Answers.LastActivityDate: 2023-08-21T01:49:14.497Z
Answers.UserDisplayName: CVDE
Answers.Body: <p>I come up with multiple advantages for siamese against a single neural network for similarity measuring:</p>
  <p><strong>Training Phase.</strong> If using a single network to replace Siamese, it might be required a double number of parameters (weights) for learning. Hence, training the network will likely converge slower and the network will be more volatile to noise.</p>
  <p><strong>Testing Phase.</strong> Note that these similarity measurements are used in the applications like face recognition. Now, suppose we are going to use the model in such a system. If we have implemented the model by the Siamese, we would only need to compute the output of the model for the input once, and then use the cached results for the existing images in the database, and eventually fasly compute the similarity measures. <em>On the other hand</em>, if we have implemented the measurement by a single neural network, we should compute per query the result for all combination of the input and images in the background. Hence, in the latter, we cannot cache the results for the existing data in the database. <em>Therefore, single neural network implementation will have much more intensive query time for massin dataset than Siamese implementation.</em></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-16T08:06:49.060Z
Answers.Text: Why using a **single** NN to replace siamese neural networks, which the OP describes as being composed of **2  neural networks**, would _necessarily_ double the number of parameters? I am not currently familiar with siamese neural networks, so maybe that's why I have this question. Moreover, regarding your second point, I think I understand what you mean, but note that you could also cache the results of the comparison itself. Of course, for objects that you haven't yet compared, you wouldn't have anything in the database.
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-16T19:03:05.637Z
Answers.Text: @nbro Not necessarily, double. So, I updated the post to `might be`.  For the second part, you're right. We can cache all queries; however, the space complexity of the cache will be the `query size` that can rapidly grow. But for the Siamese network, it's constant to the number of training data and does depend on the `query size.` So, for the latter, the cache size is manageable.
Answers.UserDisplayName: OmG
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-21T15:25:52.913Z
Answers.Text: Thank you. What are the alternatives to the Siamese network if we want to measure the similarity between 2 inputs, please?
Answers.UserDisplayName: Avv
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-12T20:11:00.647Z
Answers.LastActivityDate: 2022-04-16T18:57:02.353Z
Answers.LastEditDate: 2022-04-16T18:57:02.353Z
Answers.UserDisplayName: OmG
Body: <p><a href="https://www.cs.cmu.edu/%7Ersalakhu/papers/oneshot1.pdf" rel="nofollow noreferrer">Siamese Neural Networks</a> are a type of neural network used to compare two instances and infer if they belong to the same object. They are composed by two parallel identical neural networks, whose output is a vector of features. This vector of features is then used to infer the similarity between the two instances by measuring a distance metric.</p>
  <p>I was wondering, why not using instead a single neural network that receives as input the two objects that are being compared (e.g. two images) and directly outputs the similarity score? Wouldn't it be better to let the model compare some features of the intermediate layers? Why the Siamese Neural Networks are used for this task and what are the benefits of a Siamese Neural Network over a single neural network that receives as input two instances (e.g. two images) and directly outputs the distance score?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-03-21T15:25:43.407Z
Comments.Text: This is a very good question. What are the alternatives to the Siamese network if we want to measure the similarity between 2 inputs, please?
Comments.UserDisplayName: Avv
Tags: <neural-networks><machine-learning><comparison><similarity><siamese-neural-network>
Title: Why are Siamese Neural Networks used instead of a single neural network?
UserDisplayName: IgnacioGaBo
ThreadText: # Why are Siamese Neural Networks used instead of a single neural network?
  
  (Asked by: username_0 on 2022-04-11)
  
  [Siamese Neural Networks](https://www.cs.cmu.edu/%7Ersalakhu/papers/oneshot1.pdf) are a type of neural network used to compare two instances and infer if they belong to the same object. They are composed by two parallel identical neural networks, whose output is a vector of features. This vector of features is then used to infer the similarity between the two instances by measuring a distance metric.
  
  I was wondering, why not using instead a single neural network that receives as input the two objects that are being compared (e.g. two images) and directly outputs the similarity score? Wouldn't it be better to let the model compare some features of the intermediate layers? Why the Siamese Neural Networks are used for this task and what are the benefits of a Siamese Neural Network over a single neural network that receives as input two instances (e.g. two images) and directly outputs the distance score?
  
  **Tags:** neural-networks, machine-learning, comparison, similarity, siamese-neural-network
  
  > **Comments:**
  > **username_1**: This is a very good question. What are the alternatives to the Siamese network if we want to measure the similarity between 2 inputs, please?  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_5 on 2022-04-12. Score: 4 | Accepted answer
  
  I come up with multiple advantages for siamese against a single neural network for similarity measuring:
  
  **Training Phase.** If using a single network to replace Siamese, it might be required a double number of parameters (weights) for learning. Hence, training the network will likely converge slower and the network will be more volatile to noise.
  
  **Testing Phase.** Note that these similarity measurements are used in the applications like face recognition. Now, suppose we are going to use the model in such a system. If we have implemented the model by the Siamese, we would only need to compute the output of the model for the input once, and then use the cached results for the existing images in the database, and eventually fasly compute the similarity measures. *On the other hand*, if we have implemented the measurement by a single neural network, we should compute per query the result for all combination of the input and images in the background. Hence, in the latter, we cannot cache the results for the existing data in the database. *Therefore, single neural network implementation will have much more intensive query time for massin dataset than Siamese implementation.*
  
  > **Comments:**
  > **username_3**: Why using a **single** NN to replace siamese neural networks, which the OP describes as being composed of **2  neural networks**, would _necessarily_ double the number of parameters? I am not currently familiar with siamese neural networks, so maybe that's why I have this question. Moreover, regarding your second point, I think I understand what you mean, but note that you could also cache the results of the comparison itself. Of course, for objects that you haven't yet compared, you wouldn't have anything in the database.  
  > **username_5**: @username_3 Not necessarily, double. So, I updated the post to `might be`.  For the second part, you're right. We can cache all queries; however, the space complexity of the cache will be the `query size` that can rapidly grow. But for the Siamese network, it's constant to the number of training data and does depend on the `query size.` So, for the latter, the cache size is manageable.  
  > **username_1**: Thank you. What are the alternatives to the Siamese network if we want to measure the similarity between 2 inputs, please?  
  
  ---
  
  ### Answer by username_2 on 2022-04-16. Score: 3
  
  In addition to @Omg's answer note that Siamese networks are typically used in situations where applying `(A,B)` to the inputs must generate the same output as applying `(B,A)` (i.e. the similarity measure of `A` to `B` is the same as the similarity of `B` to `A`).
  
  With a network with separate weights, this is not guaranteed. One way to get close to this is to not only use samples `(A,B)` as training input but also (equally often) `(B,A)`. Effectively this doubles the number of training steps (and therefore training time) and the network output is still not guaranteed to be symmetric.
  
  By sharing weights, the symmetry of the response of the network (`(A,B)` gives the same output as `(B,A)`) is guaranteed by design.
  
  > **Comments:**
  > **username_3**: You say "With a network with separate weights, this is not guaranteed". But what do you mean by "separate weights"? It seems that the OP is suggesting to use a neural network that receives as input the 2 objects that need to be compared, so the weights of this neural network would, in a way, be shared among these 2 objects, so not separate. So, can you clarify this?  
  > **username_2**: Good point -- OP mentions "why not using instead a single neural network that receives as input the two objects". I implicitly assumed that this would be two subnetworks of identical structure (at least for the first few layers). So effectively I had 'two subnetworks with separate weights' in mind.  
  > **username_0**: Indeed, I was thinking of a single NN in which the input are the two images concatenated. Anyway, at least in the first layer the weights will be different and what [username_2 username_2](https://ai.stackexchange.com/users/18159/username_2-username_2) has answered applies.  
  > **username_1**: Fantastic answer. What do you mean by the symmetry of the network and how Siamese offers that mathematically please (source if possible)?  
  > **username_1**: @username_3. Are there other networks for measuring similarity between a pair rather than Siamese architecture pleae?  
  
  ---
  
  ### Answer by username_4 on 2023-08-21. Score: 0
  
  Assuming you want to train an object detector to detect people in an image.
  
  The detector's neural networks learn to identify features that help them detect people. These features range from low-level aspects like edges to high-level characteristics such as the presence of feet, hands, and more.
  
  If you use the features from this network for person re-identification, you won't obtain good results because the learned features do not focus on extracting differences between individuals.
  
  However, if you employ a Siamese neural network with a loss function like Hinge, triplet,..., which involves comparing predictions between positive and negative matches, the network learns to extract features that highlight the differences between individuals, such as clothing color and more.
  
  
  ---
  
  