How to implement siamese network?

Below is my network:

class Siamese(nn.Module):
    def __init__(self):
        super(Siamese, self).__init__()
        self.cnn1 = nn.Sequential(
            nn.Conv2d(1, 20, kernel_size=5),
            nn.MaxPool2d(2, stride=2),
            nn.Conv2d(20, 50, kernel_size=5),
            nn.MaxPool2d(2, stride=2))

        self.fc1 = nn.Sequential(
            nn.Linear(50 * 4 * 4, 500),
            nn.Linear(500, 10),
            nn.Linear(10, 2))

        # self.cnn2 = self.cnn1
        # self.fc2= self.fc1

        # print self.cnn1 is self.cnn2

    def forward(self, input1, input2):
        output1 = self.cnn1(input1)
        output1 = output1.view(output1.size()[0], -1)
        output1 = self.fc1(output1)

        output2 = self.cnn1(input2)
        output2 = output2.view(output2.size()[0], -1)
        output2 = self.fc1(output2)
        # print output1 - output2
        output = torch.sqrt(torch.sum((output1 - output2) * (output1 - output2), 1))

        return output1, output2, output

But it crashed when training with the error:

*** Error in `python': free(): invalid next size (fast): 0x00000000042b8d50 ***
Aborted (core dumped)

I have no idea what happens. It seems my network definition is not correct.

1 Like

It’s a bug. Can you please tell me what input sizes are you using?

Input size is (28L, 28L) since I am using MNIST dataset.
I have to fix the seed of random number generator to reproduce.

BTW, I use

criterion = nn.HingeEmbeddingLoss()

as the loss function.

Can you please upload your sript in a GitHub gist, so I could try to take a look and reproduce the issue? Thanks!

After upgrading to 0.1.12_1, I got new error.

Segmentation fault (core dumped)

The previous error disappeared.

Please check for the code.

Can you please give an example that use Siamese network ? I googled pytorch Siamese but got no worked examples.


@melody it is because of NaN values appearing in your inputs to MaxPooling. If your training is stable and you dont get nans, then it will work fine. Regardless, I have fixed this in master via:


@li_bo you can find an example here:

Did you fix the problem in the end? BTW, I try to change your code with cuda. But I am not able to reproduce the invalid next size error or the segmentation fault. The code in the repo seems fine to me except with unstable training, which makes senses. I guess @smth have fixed the stability issue. But current whl on does not have this fix, isn’t it @smth?

I’ve met something similar. Could you take a look at this Possible data parallel memory leak for siamese network ?

You can find a short tutorial here: