Learning the Metric Itself
What makes two pictures similar depends on the task. By training on examples of useful matches, we can learn a representation in which the differences we care about affect the distance.
16 sections · 14 questions · 4 coding challenges
The arrow keys move between sections too.
Could Examples Teach Us What Similar Should Mean?
BOTIE
Hadsell, Chopra and LeCun's 2006 work learned a mapping from examples of relationships between inputs. Its contrastive objective encouraged related observations to be nearby while separating observations marked as dissimilar.
A shared encoder maps both members of a pair into the same space. Training pulls specified positive pairs together and penalises negative pairs that are too close under the chosen margin. This makes the desired relationship part of the learning task. The labels and sampling policy therefore define the notion of similarity, and the resulting distances still need evaluation on pairs the model did not see.
Sources for this history
Teach the Model What Should Count as Similar
A classifier learns to name categories, but its internal distances were not necessarily optimized for finding matches. If matching is the goal, we can train from pairs that explicitly say what should count as similar.
Metric learning adjusts a representation so the pairwise comparisons serve that task.
Define similarity with labeled pairs
A matching pair says these two observations should be near under the intended representation. A nonmatching pair says they should be distinguishable. These pair labels provide the supervision.
Pair one
- Two crosses
- Match
Pair two
- Cross and square
- Do not match
The training target is the relationship between inputs
The detailed contrastive calculation shows how matching and nonmatching pairs contribute differently and where the margin acts.
Shared weights and held-out evaluation are essential to interpreting the learned distance.
Explore the full example
Compare the distances of matching and nonmatching pairs. Change the margin and inspect both groups, since pulling everything together would make matching distances small too.
…
Part 1. The Problem With A Borrowed Ruler
1. Four kinds of picture, and a question about sameness
Every picture on this page is sixteen pixels on a side with one brightness per pixel, and holds one of four shapes, a cross, the outline of a square, a filled disc or a diagonal bar, drawn at a random position and size against a random background with a little noise on every pixel. A fifth kind, a ring, is drawn only when asked for, as a kind no network here is ever shown.
We want a distance between two pictures that is small when they are the same kind and large when they are not, and the method on this page trains that distance directly from pairs, where the usual route borrows it from a network trained to name the kinds.
The page asks six questions, in order. What goes wrong with the distance a classifier leaves behind, and why train a distance from pairs at all? What does the contrastive loss ask of one pair, worked by hand? How does one network serve both pictures of a pair, and is the slope of a weight used twice really the sum of its two uses?
Trained on pairs of three kinds, does the distance carry to the fourth kind and to rings, against the vector borrowed from a classifier? What does the margin do, and what happens with no pairs of different kinds at all? And what does the method cost in pairs, before the last Part asks where it stops being defined.
| the collection | on this page |
|---|---|
| side of one picture | 16 pixels, one brightness each |
| kinds | cross, square, disc, bar |
| training pictures per kind | 60 |
| held-out pictures per kind | 60 |
| a fifth kind, never trained on | ring, 60 drawn |
| pictures every distance is measured among | 300, the 240 held out and the 60 rings |
The same drawn collection the other picture pages use. Every nearest neighbour below is searched among the 300 evaluation pictures unless a step says otherwise.
Keep in mind
2. The ruler a classifier leaves behind
The page on a vector for a picture trained a network to name the four kinds and kept the sixteen numbers of the layer before its answer as a picture’s position. It found that a held-out picture’s nearest neighbour there is its own kind for 230 to 235 of 240 across three weight seeds, which is a very good ruler for the kinds the network was taught. It also found that the ruler is worse at the one thing it was never asked about, since 43 to 45 of 60 rings find another ring by it where the raw pixels manage 46.
Searched among all 300 pictures by straight-line distance, the same network’s layer gives a nearest neighbour of the right kind to 56 crosses, 58 discs and 59 bars of 60, and to only 40 squares, because the rings it was never shown land among the squares and take their places as nearest neighbours. The pixels give 45, 38, 50 and 54, and 44 rings of 60.
Keep in mind
3. What a pair asks for that a label does not
A classifier needs every example named, and it can only ever answer with one of the names it was given. A great many real problems do not come like that. A bank verifying signatures has pairs known to be by one person and pairs known not to be, and a photograph collection has pictures known to show one face, and in both the people who matter tomorrow are not among the names today. What they have is sameness, and sameness is exactly what a distance is supposed to encode.
So the method asks for it directly. Show the network two pictures and a single bit, one kind or two, and adjust the weights so that the distance between the two answers agrees with the bit.
d(a, b) as small as it can be, when a and b are one kind
d(a, b) ≥ m, when they are two kindsThe hope, and the claim the method is usually sold on, is that a distance trained this way describes what makes pictures alike rather than which name to give them, and so should serve kinds the training never contained. Part 4 tests exactly that.
Keep in mind
Part 2. The Contrastive Loss
4. A network whose answer is a position
The network is the shared picture network with its last layer replaced. Where the classifier ended in four scores, one per kind, this one ends in eight numbers with no nonlinear activation applied, and those eight numbers are the picture’s position. Every starting weight below that last layer is the reference network’s own, so the two networks this page compares begin from the same place.
p(x) = W₂ tanh(W₁ f(x) + b₁) + b₂ f(x) the 128 numbers the convolutions hand on
W₁ is 16 × 128, W₂ is 8 × 16
d(a, b) = ‖p(a) − p(b)‖ = √( Σᵢ (p(a)ᵢ − p(b)ᵢ)² )Before any training the mean distance between two of the 300 evaluation pictures’ positions is 0.7325. The margin used throughout is 1, chosen before any margin was compared because it is a distance the untrained network nearly reaches already, so training does not have to drag the positions to a new scale before it can start arranging them.
Keep in mind
5. A pair of one kind is pulled together
For two pictures of one kind the loss is half their squared distance. It is zero only when the two positions coincide, and its slope at either position points along the line to the other, growing with the distance, so a far-apart pair is pulled hard and a close pair gently, the way a spring behaves.
L_same(a, b) = ½ d(a, b)²
∂L_same / ∂p(a) = p(a) − p(b)A step of descent moves p(a) against that slope, which is towards p(b), and moves p(b) by the opposite amount, towards p(a). The half is there only so the slope comes out as the gap itself.
Keep in mind
6. A pair of two kinds is pushed to the margin and no further
For two pictures of different kinds the loss is half the squared amount by which they fall short of the margin. Closer than the margin they are pushed apart, hardest when they are closest; once they are a margin apart the loss is zero and so is its slope, and they are left alone.
L_different(a, b) = ½ max(0, m − d)²
∂L_different / ∂p(a) = −((m − d) / d) (p(a) − p(b)) when d < m, and 0 when d ≥ mTogether, with y equal to 1 for a pair of one kind and 0 for a pair of two, the contrastive loss of Hadsell, Chopra and LeCun is the following, averaged over the pairs.
L = y · ½ d² + (1 − y) · ½ max(0, m − d)²›Why the push stops at a margin
The obvious alternative is to reward distance without limit, a loss of minus the squared distance for a pair of two kinds. That loss has no minimum. The network can always lower it by making every position larger, so the weights grow without end and the pulls, which are bounded by how close a pair can get, are swamped. The margin turns the push into a demand that can be met, after which a pair contributes nothing, and it is what makes the loss bounded below by zero.
Keep in mind
7. One pair checked by hand
Two positions in a plane are enough to check every piece of the loss. Put the first at the origin and the second at 0.36 and 0.48, which is 0.6 away, and take the margin as 1.
| step | what it is |
|---|---|
| d = √(0.36² + 0.48²) = √0.36 = 0.6 | the distance between the two positions. |
| one kind: ½ × 0.6² = 0.18 | the pull. Its slope at the first position is the gap, (0 − 0.36, 0 − 0.48) = (−0.36, −0.48), so a step moves the first point towards the second. |
| two kinds: ½ × (1 − 0.6)² = 0.08 | the push, since 0.6 is short of the margin by 0.4. |
| −(0.4 / 0.6) × (−0.36, −0.48) = (0.24, 0.32) | its slope at the first position, pointing towards the second point, so a step moves the first point away. |
| second at (1.2, 1.6): d = 2, ½ × max(0, 1 − 2)² = 0 | past the margin a pair of two kinds costs nothing, where as one kind it would cost ½ × 2² = 2. |
…
Drag either point and switch the label. The curve on the right is the loss over distance for each label, with the margin dashed; the dot is the answer for the pair on the left.
Keep in mind
8. Two real pairs through the trained network
The same arithmetic on real pictures, placed by the network once it has been trained on pairs. Two held-out crosses land close together and a cross and a square land far apart, and the loss treats each pair the way its label says.
…
Both pairs share the same first cross. Each pair’s eight numbers are drawn side by side, and the table gives the gap the loss reads.
The two pairs, by hand
The two cross embeddings are about 0.1536 apart. With margin one, compare the losses for a same-kind label and a different-kind label.
same-kind loss ≈ ½ × 0.1536² ≈ 0.0118
different-kind shortfall ≈ 1 − 0.1536 = 0.8464
different-kind loss ≈ ½ × 0.8464² ≈ 0.3582The cross and square are about 1.5737 apart, beyond the margin. Their different-kind loss is zero; calling them the same kind would cost about 1.2382. The learned positions separate these two comparisons much more than raw pixel distance does.
Keep in mind
Part 3. One Network, Two Pictures
9. Both pictures go through the same weights
The loss needs a position for each picture of a pair, so each picture is run through the network, and it is the same network both times. Drawn out, that looks like two identical towers joined at the top by the distance, which is why the arrangement is called a Siamese network, though there is only one set of weights and nothing about it is doubled except the drawing.
Using one set of weights is forced by what a distance has to be. If the two pictures went through different networks, the distance from a to b would differ from the distance from b to a, and a picture’s position would depend on which side of a pair it happened to be shown on, so there would be no single place to put it when it is later searched for.
Keep in mind
10. Walking the pair loss down each picture’s copy
The loss of one pair is a function of two positions, and each position is a function of the same weights. Training needs the slope of the loss with respect to each weight, which the chain rule gives as one term through each picture.
∂L/∂W = (∂L/∂p(a)) · (∂p(a)/∂W) + (∂L/∂p(b)) · (∂p(b)/∂W)Each term is an ordinary backward pass. The slope of the loss at one picture’s position arrives at the top layer, each layer turns the slope at its answer into slopes for its own weights and a slope at its input, and hands the second down to the layer below, exactly as backpropagation does for one picture. The layers themselves only know losses that score one answer against a target, so the pair loss and its slope at the two positions are computed outside the layers, and the walk down each picture’s copy is done by hand from there.
Keep in mind
11. A weight used twice has two slopes, and they add
That the two slopes should be added is the whole point of the arrangement, and it is easy to get wrong, by stepping each copy on its own slope and ending up with two copies that no longer agree. The check is to nudge a weight up and down by a tiny amount, run both pictures through the nudged network again, and measure how much the pair loss changed. That measurement knows nothing about copies.
…
Four pairs through the untrained network. For each of five weights, the slope through each picture’s copy, their sum, and the slope measured by nudging.
On four pairs through the untrained network, with a mean loss of 0.2056, the summed slope and the measured one agree on every weight checked, in both convolutions, the hidden layer and the last layer, to within 1.1 × 10⁻¹¹, with the weight nudged by 10⁻⁵ each way. Either copy’s slope alone is wrong by between 0.0136 and 0.1462. Here one network is run twice and stepped once, with the sum, so there is only ever one copy of every weight to step.
Keep in mind
12. The last layer’s biases learn nothing
The last row of the table above is a bias of the layer that produces the position, and its summed slope is exactly zero, not merely small. Through the first pictures’ copy its slope is 0.1462 and through the second it is −0.1462. The reason is that a bias adds the same amount to every position, and the loss only ever reads the difference between two positions.
(p(a) + c) − (p(b) + c) = p(a) − p(b)So a shift applied to every position changes no distance and no loss, and nothing in training can decide where the collection as a whole sits. The same is true of turning or reflecting every position together. This is why the drawing at the top of the page moves the middle of the collection to the origin before it measures any direction, and it comes back in the last Part.
Keep in mind
13. Every pair in a batch at once
Walking two copies for every pair would cost two forward and two backward passes per pair. Training here takes eight pictures of each of three kinds, 24 pictures in a batch, which hold 276 pairs, 84 of them of one kind and 192 of two, and runs the 24 pictures through once.
∂(Σ over pairs Lᵢⱼ) / ∂p(k) = Σ over the pairs that contain k of ∂Lᵢⱼ / ∂p(k)Because the slope of a sum is the sum of the slopes, a picture’s slope from every pair it belongs to can be added at its position first, and then one backward pass over the 24 pictures gives the same gradient that 276 pairs of separate walks would have summed to. Each picture appears in 23 pairs and is walked down once. That is the two copies’ sum from step 11 again, computed once per picture instead of once per pair.
Keep in mind
Check yourself
Questions on Parts 1 to 3
Question 1 of 5
Searched among all 300 pictures, the reference classifier’s layer finds the right kind for far fewer squares than for crosses, discs or bars.
Part 4. Does The Distance Reach A Kind It Never Saw?
14. Training on three kinds and holding the bar back
To ask whether a distance learned from sameness reaches new kinds, the network is trained on pairs of crosses, squares and discs only, 180 training pictures, and the bar is held back along with the ring. For a fair comparison a classifier of the reference design is trained on the same 180 pictures to name the same three kinds, from the same starting weights, and its layer of sixteen is read the way the neighbouring page reads it.
Both train properly. The pair loss falls from 0.1351 in the first epoch to 0.0122 in the fortieth, and the three-kind classifier names 0.9778 of the held-out pictures of its three kinds correctly. So four spaces are compared, the pixels, the reference classifier’s layer (which was shown bars), the three-kind classifier’s layer and the positions trained on pairs, and every number depending on training is given for weight seeds 0, 1 and 2. The widgets draw seed 0 unless a button says otherwise.
Keep in mind
15. Among the kinds it was trained on, it keeps up
First the easy question. Searching only among the 180 held-out pictures of crosses, squares and discs, so that no picture of an unseen kind can be anyone’s nearest, the positions trained on pairs do about as well as the classifier’s layer.
… training two networks for this study, a few seconds the first time
Nearest neighbours of the right kind among the held-out pictures of the three trained kinds only, for each seed.
On seed 0 the positions give 171 of 180 pictures a nearest neighbour of their own kind against the three-kind classifier’s 179, and on seed 2 they are ahead, 178 against 176. Seed 1 is the weak one, 157 against 174. The pixels manage 144 and the reference classifier 174.
Keep in mind
16. The held-back bar lands among the crosses
Now the bars and rings join the search, 300 pictures in all. This is where the claim for the method is made, and on these pictures it did not hold. A held-back bar finds another bar as its nearest picture far less often in the positions trained on pairs than in the three-kind classifier’s layer, and the bars also spoil the crosses.
… training two networks for this study, a few seconds the first time
Every kind in every space, searched among all 300. Amber cells are kinds that space’s network never saw. The reading buttons change what each cell measures.
In the positions trained on pairs 33, 27 and 34 bars of 60 find a bar across the three seeds, where the three-kind classifier’s layer gives 52, 51 and 53 and the raw pixels 54. The crosses suffer too. Searched among their own three kinds 56 crosses found a cross, and with the bars and rings added only 39 do, because the network, having never been asked where a bar should go, puts many of them where the crosses are.
The playground at the top shows it; take a bar and look at its five nearest.
Keep in mind
17. The rings do worse than by pixels
The ring is the stricter test, since no network on this page saw one, the classifiers included. It is also the place where the method is plainly worse than doing nothing, which is worth stating as found rather than explaining away.
In the positions trained on pairs 33, 33 and 39 rings of 60 find another ring across the three seeds. The three-kind classifier’s layer gives 46, 51 and 43, the reference classifier 43, and the raw pixels 44. So on every seed the distance trained directly for sameness gathers the unseen kind less well than comparing the pictures pixel by pixel.
Keep in mind
18. Whichever kind is held back
The bar might simply be a hard kind to hold back. It is not the reason. Holding back the cross, the disc or the square instead, each on seed 0, gives the same picture, and a wider answer does not rescue it either.
| held back | finds its own kind, pairs | finds its own kind, classifier | rings, pairs | rings, classifier |
|---|---|---|---|---|
| bar | 33 of 60 | 52 of 60 | 33 of 60 | 46 of 60 |
| cross | 21 of 60 | 44 of 60 | 38 of 60 | 43 of 60 |
| disc | 29 of 60 | 47 of 60 | 31 of 60 | 47 of 60 |
| square | 30 of 60 | 46 of 60 | 35 of 60 | 44 of 60 |
Seed 0, searched among all 300. The classifier is the one trained to name the same three kinds. The study buttons on the table in step 16 show each row in full.
The positions have eight numbers where the classifier’s layer has sixteen, so I retrained the pair network on seed 0 answering with sixteen. Its bars find a bar 31 times of 60 where the eight-number version managed 33, and its crosses 37 times where it managed 39. Its rings do better, 43 of 60 against 33, which matches the reference classifier and is still below the pixels. So the narrower answer costs the rings something and is not what goes wrong with the held-back kind.
Keep in mind
19. A reading on which the pairs win, and why it misleads
There is a way of reading the same positions that makes the method look as advertised. Take a kind’s mean distance between its own pictures and divide it by its mean distance to every other picture. Lower is tighter, and on that reading the rings are tighter in the positions trained on pairs than in the three-kind classifier’s layer in all six studies.
ratio(kind) = mean d inside the kind / mean d from the kind to everything elseOn seed 0 the ratio for rings is 0.5230 against 0.5984, on seed 1 0.4431 against 0.6945, and on seed 2 0.3532 against 0.7923, the opposite verdict to the nearest-neighbour count. The ratio has a confound. The pair loss pushes the three trained kinds a margin apart from each other, which makes the whole collection larger, and that enlarges every kind’s distance to everything else, so the ratio falls for a kind that did not gather at all.
The nearest-neighbour count cannot be moved that way, and it is the question a search actually asks, which is why this page reads it first. The reading buttons on the table in step 16 show all three.
Keep in mind
Part 5. What The Margin Does
20. The margin is a unit of length
The margin looks like a setting that says how far apart kinds should be, and it is less than that. Nothing about a position has a natural size, so a network trained at a margin of 2 can reach exactly the arrangement it would reach at a margin of 1, with every position doubled.
| expression | what changed |
|---|---|
| d(c·p(a), c·p(b)) = c · d(p(a), p(b)) | multiply every position by c and every distance is multiplied by c. |
| ½ (c d)² = c² · ½ d² | the pull at the scaled positions is c² times the pull at the old ones. |
| ½ max(0, c m − c d)² = c² · ½ max(0, m − d)² | and the push at margin c m is c² times the push at margin m. |
| L(c·p; c·m) = c² · L(p; m) | so every arrangement costs the same at margin c m as it did at m, up to the one factor c². |
The loss surface at a margin of 2 is the one at a margin of 1, stretched. What does not stretch is everything else. The starting positions are the same size whatever the margin, and the slopes reaching the lower layers grow with the margin, so at one step size a larger margin takes larger steps. Dividing the step size by the square of the margin undoes the c² and is the fair way to compare margins.
Keep in mind
21. At one step size, a larger margin freezes everything onto one point
Ignoring that has a striking consequence. Trained at the step size that suits a margin of 1, a margin of 2 or 4 does not give a wider arrangement. It gives none at all, because every picture’s position lands on the same point.
… training this setting, a few seconds the first time
The widget opens on a margin of 2 at the unchanged step size. The loss stops falling and the mean distance between positions drops to almost nothing; try a margin of 4, then switch the step size to scaled.
With margin two, all embedded positions collapse to nearly one point by epoch fifteen. With margin four, they do so after the first epoch. Once every distance is zero, same-kind pairs cost nothing and different-kind pairs cost half the squared margin. There are 192 different-kind pairs among 276 pairs.
mean loss at margin 2 = (192 / 276) × ½ × 2² ≈ 1.3913
mean loss at margin 4 = (192 / 276) × ½ × 4² ≈ 5.5652Those are the losses where the runs stall. At exactly zero distance, this implementation returns a zero positional gradient because the distance has no unique direction there. The same failure occurs with margin one half on seed one when the step size is raised to four.
collapsed loss at margin 0.5 = (192 / 276) × ½ × 0.5² ≈ 0.0870Keep in mind
22. With the step size scaled, the margin changes little that holds up
With the step size divided by the margin squared, every margin trains. The arrangement then really does scale with the margin, and whatever else changes is small and changes its direction from one seed to the next.
| margin, seed 0 | step size | mean distance, one kind | mean distance, two kinds | rings finding a ring |
|---|---|---|---|---|
| 0.5 | 4 | 0.2267 | 0.6474 | 32 of 60 |
| 2 | 0.25 | 0.5982 | 2.8644 | 44 of 60 |
| 4 | 0.0625 | 1.1180 | 5.7965 | 49 of 60 |
Distances among held-out pictures of the three trained kinds; rings counted among all 300. The margin of 1 is the main study, where 33 rings find a ring.
On seed 0 the rings improve steadily with the margin, from 32 of 60 to 49, and at a margin of 4 they beat the pixels’ 44. It is tempting to conclude that a wide margin keeps room for the unseen. The seed buttons on the widget above say otherwise, since on seeds 1 and 2 the rings at a margin of 4 do no better than at 2, and on seed 1 a margin of 0.5 freezes. I would not trust any trend in the margin from three seeds of this size.
Keep in mind
Part 6. Why The Push Is Needed
23. With pairs of one kind only, everything falls onto one point
Pairs of one kind are the ones that say what sameness is, so it is natural to wonder whether the pairs of two kinds are needed at all. They are, and the measurement is immediate. Trained on the same batches with every two-kind pair left out, from the same starting weights, the network does the one thing that satisfies every pull at once, which is to give every picture the same position.
…
The two runs share their starting weights and their batches. Only the pairs of two kinds differ.
The mean distance between positions starts at 0.7325 in both runs. With pulls only it is 0.0393 after one epoch and 0.0018 after forty, while the ordinary run ends at 0.8284. The collapsed positions still differ in their last digits, so a nearest neighbour can still be found, and it means almost nothing. The share of each picture’s ten nearest that are its own kind falls to 0.2917 for crosses, 0.3000 for squares and 0.2633 for discs, close to the one in five that a picture chosen at random from the other 299 would give.
Keep in mind
24. The loss calls the collapse a success
The alarming part is what the loss says while this happens. The run with pulls only reaches a mean loss of 0.0000078 by the fortieth epoch, where the ordinary run ends at 0.0122, so by its own measure the collapsed network is more than a thousand times better trained.
›Why every constant answer is a perfect answer
With pulls only, the loss is a sum of squared distances, so it is never negative, and it is exactly zero for any network that gives every picture one and the same position, whatever that position is. Such a network exists in this design without any help, since setting the last layer’s weights to zero does it, and descent finds its way towards one.
The push is the only term that a constant answer fails, since it charges half the margin squared for every pair of two kinds sitting on top of each other. Hadsell, Chopra and LeCun gave exactly this argument for including it.
Keep in mind
Check yourself
Questions on Parts 4 to 6
Question 1 of 5
Trained on pairs of crosses, squares and discs, how did the learned distance do on the held-back bars once all 300 pictures were searched?
Part 7. What The Pairs Cost
25. Pairs grow with the square of the pictures
A classifier learns from each picture once per epoch. A loss over pairs has one term for every two pictures, and the number of pairs grows with the square of the number of pictures, which is harmless at the scale of this page and decisive at the scale of a real collection.
pairs among n pictures = n (n − 1) / 2…
Left, the pairs among this page’s training pictures and what one training actually computed. Right, how the count grows.
The 180 training pictures make 16,110 pairs, of which 5,310 are of one kind and 10,800 of two. A thousand pictures make 499,500 and a million make 499,999,500,000, so at any real size most pairs are never looked at, and which pairs are shown becomes a decision rather than a detail.
Keep in mind
26. What one training computed
At this size the training can afford to visit almost every pair. Forty epochs of seven batches of 276 pairs compute 77,280 pair losses, and those cover 16,015 of the 16,110 different pairs, 0.9941 of them. In the run I measured the forty epochs took about 1.4 seconds against about 2.3 for the three-kind classifier, since batching every pair makes each step one pass over 24 pictures; the widget above reports the times on whatever machine answers, and they will differ.
Keep in mind
27. Most pairs of two kinds stop teaching
Of the pairs that are computed, most soon contribute nothing. A pair of two kinds past the margin has a loss of zero and a slope of zero, and once training has done its work that describes most of them. At the end, 0.7909 of all two-kind pairs among the training pictures are past the margin, and 0.6620 among the held-out ones, so roughly four in five of the pushes each batch computes are multiplications by zero.
That is the problem FaceNet met at scale and answered with a change of loss and a search. Its loss takes a triplet, a picture, another of the same kind and one of a different kind, and asks that the first two be nearer than the first and third by a margin α.
L = max(0, ‖p(a) − p(same)‖² − ‖p(a) − p(different)‖² + α)Its authors then chose, within each batch, the different-kind pictures that were close enough to still violate the margin but not closer than the same-kind one, because random triplets were almost all satisfied already. None of that is built on this page; with 180 pictures every pair can simply be visited.
Keep in mind
Part 8. Where The Method Stops Being Defined
28. What the loss leaves undecided
A loss that reads only distances cannot tell apart two arrangements that have the same distances. Shifting every position by one amount, turning the whole arrangement, or reflecting it leaves every distance unchanged and so every loss, and step 12 measured the first of these as a slope of exactly zero on the last layer’s biases. A trained position is therefore defined only up to a rigid motion, and a single coordinate of it means nothing.
Two consequences follow. Positions from two separate trainings cannot be compared with each other at all, since each training settles on its own orientation, and a direction measured from the origin is an accident of the starting weights, which is why the drawing at the top measures directions from the middle of the collection instead.
Keep in mind
29. The degenerate cases, one by one
The method asks for two things of its data, pairs of both labels, and labels that some arrangement of points could satisfy. Where either fails, the loss does not become an approximation. It becomes a quantity with no single minimiser, or a slope that is not defined, and the table says which and what has to be decided.
| the case | what the method says |
|---|---|
| two kinds at distance zero | the loss is ½ m², and its slope, −((m − d)/d) times a gap of zero, is a zero over a zero. The push has a size and no direction, and by symmetry any function of distance alone has none there. The choices are to return no slope, which leaves a frozen arrangement frozen, as Part 5 measured, or to pick a direction at random, which breaks the tie and makes the step depend on the draw. |
| a margin of zero | max(0, 0 − d) is zero for every pair, so the push term vanishes and only the pulls are left, which is the next row. |
| no pairs of two kinds | every constant answer has a loss of exactly zero, so the minimiser is not unique and none of the minimisers is a distance. Measured here, the positions fell from 0.7325 apart to 0.0018 in forty epochs. |
| no pairs of one kind | the loss is zero as soon as every pair of two kinds is a margin apart, and nothing asks pictures of one kind to be near each other, so a kind can be spread anywhere that keeps its distance from the others. Many arrangements are perfect, and most of them are useless for search. |
| a kind with one picture | that picture has no pair of one kind, so only pushes act on it. It is kept a margin from everything else and never gathered with anything, which is the previous row for a single kind. |
| sameness that is not transitive | if a and b are judged one kind, b and c one kind, and a and c two kinds, the triangle inequality gives d(a, c) ≤ d(a, b) + d(b, c). Pulling both pairs to zero puts a and c on one point, which the margin forbids, so no arrangement has a loss of zero, and how the conflict is split depends on how many such pairs there are. |
| a margin without a scale | the loss at margin c m is c² times the loss at m for positions scaled by c, so a margin has no meaning apart from the scale of the starting weights and the step size. Quoting a margin alone says nothing about how far apart kinds end up. |
| a kind that appears in no pair | its positions are defined, since the network answers any picture, and the loss never said anything about them. Measured here, held-back bars found a bar 27 to 34 times of 60, where the pixels manage 54. |
| positions from two trainings | undefined as a comparison, since each training fixes its arrangement only up to a rigid motion of its own. |
| two pictures exactly as near | a nearest neighbour is not unique, so a rule must choose, and any rule is arbitrary. In a collapsed arrangement nearly every nearest neighbour is such a tie. |
Keep in mind
Check yourself
Questions on Parts 7 and 8
Question 1 of 4
By the end of training, 0.7909 of the two-kind pairs among the training pictures are past the margin. What follows?
Try it with Botie
Build with Botie
Write and run Python