Few-Shot Learning
Few-Shot Learning
Few-Shot Learning(Shusen Wang) YouTube lecture
Basic Concepts
What is Few-Shot Learning?
Few-Shot learning is a learning method for training a model “well” using only a small dataset, where the focus of learning is on finding similarities and differences.
Why Few-Shot Learning?
Training a deep learning model requires an enormous amount of data. However, building this vast amount of data for every task is practically impossible. For example, suppose an image dataset of 1000 classes has been diligently built, but then classification is needed for classes outside those 1000? And what if the images of this class happen to be very hard to collect? In such cases, Few-Shot Learning can be a great help.
In particular, for the Novel View Synthesis task, the model must generate images from a new view that was not among the learned views. In such cases, Few-shot learning is very useful.
Supervised Learning vs. Few-Shot Learning
Learn to Learn
“Learn to learn” is the goal of Meta Learning, which includes Few-Shot Learning. That is, during the training stage the model prepares to “learn well” when it receives an unfamiliar Query. For this, in Few-Shot Learning the dataset is divided differently from Supervised Learning:
Supervised Learning

- Structure: Training Set - Test Set
- The Test Set must be data not in the Training Set, but it must belong to a class that is already known.
Few-Shot Learning

- Structure: Training Set - Support Set - Query Sample
- The Query Sample must be data not in the Training Set, and at the same time must belong to a class that has never been seen before.
kWay nShot
Few-Shot Learning is divided according to the size of the Support Set into
- \(k\)way: the number of classes in the Support Set \(k\)
- \(n\)shot: the number of samples belonging to each class \(n\)
For example,

if it has this kind of Support Set structure, then it is 4-way 2-shot learning.

Prediction Accuracy
Prediction accuracy increases as the number of Shots increases and the number of Ways decreases.
Few-Shot Learning training method
The training process is broadly divided into three stages.

1. Train the model with a large Training Set.
- At this point the similarities and differences between images are learned.
- The Training Set does not have to include the Query’s class.
- Through the Similarity function, a similarity score \(sim(\bold{x_i}, \bold{x_j})\) in the range \([0,1]\) is computed and learned.

2. When a Query image is received, compare the Query with each sample in the Support Set.

3. Through this process, determine the class the Query belongs to.
- Find the class with the highest similarity score.
Siamese Network
Pairwise Similarity Scores

- Positive Samples: randomly sample an image of one class from the training data, and pair it with another image of the same class.
- Negative Samples: likewise randomly sample an image of one class from the training data, and pair it with another image from a class other than that class.
Training
CNN for Feature Extraction


Forward Propagation
- For Positive Samples, the Target of the two images = 0. Here \(f\) is a CNN, and \(x_1\), \(x_2\) share one CNN \(f\).
- After extracting the feature vectors as \(h_1\), \(h_2\), compute the difference of the two to obtain \(z\).
- Pass \(z\) through Dense Layers to derive a single scalar value.
- Pass it through the Sigmoid activation function to obtain a similarity score between \([0,1]\).
- Compare it with the Target value to compute the Loss and update.
- Train the same number of Positive & Negative Sample pairs.
Back Propagation

When backpropagation proceeds, both values are updated:
- The parameters of the CNN \(f\)
- The parameters of the Dense Layers that \(z\) passes through
One-Shot Prediction
Based on the trained model, the Support Set and Query are used to compute similarity scores for verification.
Loss Function
Then how should the Objective Function be defined in a Siamese Network? Representative examples are the Binary Loss used in the Siamese Network training process examined earlier, and the Triplet Loss which computes loss by comparing three images.
Binary Loss
In the preceding Training process, Binary Loss is defined as follows.
- \(\bold{z} = \vert f(\bold{x}_1)-f(\bold{x}_2)\vert\), which is the absolute value of the difference between feature vectors passed through a plain CNN.
- Binary Loss is defined as, after applying a sigmoid transformation to \(\bold{z}\), \(BCE(Binary Cross Entropy)\).
Triplet Loss
Triplet Loss has three inputs.
- Anchor, \(\bold{x}^a\): the training sample that serves as the anchor
- Positive, \(\bold{x}^+\): a random sample of the same class as \(\bold{x}^a\)
- Negative, \(\bold{x}^-\): a random sample drawn from a class other than the class \(\bold{x}^a\) belongs to
Based on the three input images, Triplet Loss is computed as follows.

- Pass the three images through one CNN to perform feature extraction for each.
- Compute \(d^+\), \(d^-\), the \(L_2\) norms between \(f(\bold{x}^a)\) and \(f(\bold{x}^+)\), and \(f(\bold{x}^a)\) and \(f(\bold{x}^-)\), respectively.
- Here, since they are in the same class relationship \(d^+\) should be a small value, and since they are in a different class relationship \(d^-\) should be trained to be a large value: this becomes the goal of training!
- Matching the training objective, the Loss becomes \(L = \max\{0,\ d^+ + \alpha - d^-\}\).
- \(\alpha\) is the margin value, used to sufficiently separate the two distance values when \(d^-\), \(d^+\) are very close.
- If \(d^- \ge d^+ + \alpha\), then loss is set to 0.
Is it really okay that loss = 0 as long as \(d^- \ge d^+ + \alpha\)?
If training samples are chosen by plain random sampling, the loss becomes 0 too easily, which hinders learning. Therefore, in actual model training, the training is made “hard” from the start. That is, images where the difference between \(d^+\) and \(d^-\) is small (= hard to distinguish) are paired together!
Few-Shot Prediction
So far the training process of Few-Shot Learning has been examined. Then how is the prediction stage performed?
Simply, one could encode both the training sample and the samples of the supporting set with a CNN, and apply the same similarity function used during training to compare with (the average of) each class’s images in the Support set. However, because the number of samples in the Support set is very small, several different methods are used.
Making Prediction

- First, pass the Support Set samples through the same CNN \(f\) to obtain feature vectors, and normalize them again to obtain \(\mu\) values. Construct a matrix \(M\) with \(\mu_1, \mu_2, \mu_3\) as rows.
- This can also be expressed as \(M = W\), \(q = f(\bold{x}_i)\).
-
In the same way, embed the Query Set samples into vectors. (\(q\))

- Multiply \(M\) and \(q\) and apply softmax to obtain the probability \(p\).
- Rewritten as \(p = Softmax(W \cdot f(\bold{x}_i)\) (\(bias = 0\)) by 1., it is the familiar probability function.
Cosine Similarity
\[\mathbf{p} = \mathrm{Softmax}\left(\begin{bmatrix} \mathrm{sim}(\mathbf{w}_1, \mathbf{q}) + b_1 \\ \mathrm{sim}(\mathbf{w}_2, \mathbf{q}) + b_2 \\ \mathrm{sim}(\mathbf{w}_3, \mathbf{q}) + b_3 \end{bmatrix}\right),\quad \text{where } \mathrm{sim}(\mathbf{w}, \mathbf{q}) = \frac{\mathbf{w}^T \mathbf{q}}{\Vert \mathbf{w}\Vert _2 \cdot \Vert \mathbf{q}\Vert _2} \text{ is the cosine similarity.}\]Here one new similarity function is introduced. Rather than obtaining the probability by simply comparing embedded vectors, it uses cosine similarity. Since the magnitude of the vectors does not matter when using this, it is suitable when the focus needs to be on the “difference in pattern” (here, the difference in direction). Also, in the prediction process the similarity computation is done after normalization, and this process enables a more stable similarity comparison. The computation simply replaces \(M \cdot q\) with the cosine similarity function.
Entropy Regularization

Entropy Regularization is one method that can be applied to the embedding process described above. For the probability \(p\), in the Entropy function defined as
\[\mathbb{H}(p) = -\sum_ip_i \log p_i\]the average of \(\mathbb{H}(p)\) over all queries in the Query Set is computed to regularize. Here, the goal becomes making the entropy small.