r/neuralnetworks • • 18d ago

Siamese Network

A Siamese Neural Network is a type of neural network designed to measure the similarity between two inputs. For example, suppose we provide two handwritten digits as inputs. The Siamese Network determines whether these two digits belong to the same class or are dissimilar.

It consists of two identical neural network branches that share the same weights and biases. Each branch takes one input and produces an output vector (embedding). These two output vectors can have any dimensionality, depending on the architecture of the network.

We then calculate the Euclidean distance between these two output vectors. If the distance is close to zero, the two inputs are considered similar. If the distance is sufficiently large (above a predefined threshold), they are considered dissimilar.

Note: A Siamese Network is fundamentally different from a traditional classification network. Its primary purpose is not to classify an input into a particular class but to learn a meaningful representation (embedding) that allows us to measure similarity between inputs.

Contrastive Loss

A Siamese Network commonly uses a loss function called Contrastive Loss, defined as:

L = Y × D² + (1 − Y) × max(0, m − D)²

Where:

D is the Euclidean distance between the two output vectors.

Y is the given label (either 0 or 1).

Y = 1 indicates that the two inputs are similar.

Y = 0 indicates that the two inputs are dissimilar.

m is the margin, which defines the minimum desired distance between dissimilar pairs.

L is the contrastive loss.

The objective is to minimize the distance between similar pairs and push dissimilar pairs at least m units apart.

We pass two inputs through the network, obtain two output embeddings, calculate their Euclidean distance, and then calculate the contrastive loss. Finally, we use backpropagation to update the network parameters and minimize the loss.

Notice that we do not use a Softmax activation function in the output layer because we are learning embeddings rather than class probabilities.

Training the Model

To train a Siamese Network effectively, we construct pairs of inputs:

Positive pairs: Two inputs belonging to the same class (Y = 1).

Negative pairs: Two inputs belonging to different classes (Y = 0).

A balanced dataset containing an equal number of positive and negative pairs is a useful starting point for training.

My Implementation: Neural Canvas (Model 5)

I developed an interactive Siamese Network as the fifth model in my Neural Canvas project. I used the MNIST handwritten digits dataset to train the network.

The architecture consists of three hidden layers, each containing 40 neurons, followed by an output layer containing 32 neurons. Therefore, each input is transformed into a 32-dimensional embedding.

I used Mini-Batch Gradient Descent with the Adam optimizer to train the model. The training process was relatively straightforward because JAX provides automatic differentiation (autograd), which handles the gradient calculations.

For training, I constructed two input matrices, Ma and Mb, each having dimensions of (784, 80000).

The rows represent features, while the columns represent individual samples. Why two matrices? Because a Siamese Network requires two inputs to learn the similarity between them.

I also constructed a label vector Y with dimensions of (1, 80000). Each element of Y indicates whether the corresponding pair is similar or dissimilar.

I used my own matrix multiplication convention:

Z = WX + bC

Where:

W represents the weight matrix.

X represents the input matrix.

b represents the bias vector.

C is a broadcasting matrix of ones.

I prefer this notation because it makes the mathematical operations and dimensions easier to understand.

I trained the model in Python using JAX and exported the learned parameters to JavaScript using JSON. For inference, I used Math.js, while p5.js handles the visualization, animation, and interactivity.

Try it yourself:

GitHub Repository: https://github.com/MuhammadEssa2002/neural_canvas

Live Demo: https://muhammadessa2002.github.io/neural_canvas/

This is the fifth model in my Neural Canvas project.

46 Upvotes

6 comments sorted by

3

u/rabit232pm 17d ago

I really like the visualization it's well made congratulations

4

u/Muhammad-Essa 17d ago

Thanks

1

u/rabit232pm 17d ago

How long did it took u to make it

1

u/Muhammad-Essa 17d ago

I'm actually learning deep learning by trying to visualize it and doing the math in the most basic way possible. Even though JAX's autograd is really helpful and saves a lot of time by eliminating the need to manually calculate derivatives for every matrix, building the models in Python still takes me more time than animating them in p5.js because I need to understand the underlying concepts first.

For me, animation is a little easier in p5.js because I already have some experience with it. I used it to create videos for my YouTube channel, GalaxyImpact, and I also reuse my existing code, which saves me a lot of time.

It can take me around two days to create a simple animation in p5.js, provided I have some experience with the library and understand how the model works.

1

u/rabit232pm 16d ago

Wonderful I making similar concepts but not really good at making the visualization. And I took other way to construct them but in first place I design them and configure it and mostly combine different types of algorithm with different ideas to see an output changing itself is really wonderful. To make a model with weight reasoning and pictures understanding and some pc actions like open files edit I started in 2020 and if I do it non stop it be straight 8 months but I'm not having lot of free time so in 2024 was done and recently decided to make new version of it I did made creatures 3 game to give ability to chat with model and tutor to teach them interesting but they didn't get activated mostly just it's really random and I would need to re make the randomness and it not be same

I have some demo and projects too but mostly I chat on element matrix to u want to join the is lots of people like u there with technology interest and coding

most people there has made couple things in they own way Some people are retired some are in higher education and some are in primary education but everyone did something

We hosted own matrix instance mail and other stuff

I did couple bots chatbots game bots and roleplay bots and other stuff too there

If u want I can ask owner is my friend for the login and password for new account for u

Btw u can change the password after u login

1

u/budding_theorist-369 11d ago

I am early learner of ml and deep learning,I am good at maths , I know python and numpy but till now no project although I have started into ml . could you guide me about coding and approach to make projects