In this lecture we are going to build our model.
The lost function in train our Siamese network.
To start we're going to define our network which actually itself contains an entire neural network.
As usual you can feel free to play around with these hyper parameters and choose your own architecture.
This CNN is just what I happened to copy from the fashion amnesty example earlier in the course
notice that instead of a final soft Max we end with a dense layer that has no activation.
Since we are not doing classification
in the forward function it's very simple.
First as input we accept two batches of images.
I am one and I am too.
We pass both of these through the same CNN to get feet 1 and feet to the embedding for both sets of
images.
Finally we calculate the Euclidean distance between two feet one in feet two for each row.
So if we add any input pairs will have an output distances.
Next we create an instance of our model and set the feature dimension to be 50.
Next we move the model to the GP you
Next we create our custom lost function.
The contrast of loss the first argument is the prediction of Y and the second argument is the target
t inside this function.
We start by calculating the squared distance for the non matching pairs.
This is that funny calculation you saw in the theory lecture where we subtract y from the margin and
so forth.
So here you see we're using a margin value of 1.
This means that for non matching pairs we want the distance to be at least 1.
But as long as it's bigger than that we don't really care how big it is.
Now from the formula you know that we want to take the max between the margin minus Y and 0 but you'll
recognize that this is exactly the value function.
So we can just use the built in real you in PI to urge.
Next we pass this calculation into the rest of the loss which is relatively simple and take the mean
over each sample
next we create our optimizer.
Usually I say loss and optimizer but that is not the case in the script.
Since our loss isn't one of the built in losses
next we have our training function.
So what's different about this.
First you'll notice that in addition to the train generator and the test generator we also pass in the
values for the number of steps per epoch.
This is essential when you use a generator because if you don't know when an epoch is finished you'll
just end up in an infinite loop.
Recall that our generators have infinite while loops inside of them.
This carries over from tends to flow and carries.
So if you've ever used these libraries it should seem familiar
inside the loop.
It's mostly the usual stuff we loop over the train generator and on each iteration we get x1 x2 and
the targets since that's what is yielded by our generator.
We move that to the GP U and so on and so forth.
One minor difference is that I have this variable called steps that counts the number of steps I've
completed so far.
So at the end of our inner loop I incremental steps by 1.
And if it's greater than or equal to the steps per epoch I break out of the inner loop
so it's the same basic idea for the test generator except no gradient descent.
Okay so next we call a training function.
As you can see this train is quite fast.
Pretty good for a CNN
X. We plot the last pre iteration which looks OK
and that's it for now in the next lecture we'll focus on how to evaluate our train to Siamese network.