# Effect of Probabilistic Similarity Measure on Metric-Based Few-Shot Classification
## 4 Proposed Method
### Overall Process  
Figure 2 shows the overall structure of the proposed few-shot classification model using probabilistic similarity. The overall process follows the conventional metric-based few-shot classifier illustrated in Figure 1, but there is an additional module for obtaining probabilistic similarity.  
Under the \(N\)-way K-shot classification scenario, a subset for an episode contains examples from \(N\) different classes, each of which is decomposed as the support set \(S_{k}\) with \(K\) samples and the query set \(Q_{k}\) with the remaining samples (\(k=1,\ \dots,\ N\)). Using the support set \(S_{k}\), the prototype vector for each class is calculated and the set of intra-class difference vectors is also composed. The samples in the query set \(Q_{k}\) are given to a few-shot classifier for conducting classification and evaluating loss value. The loss \(L\) for the training episode is defined by using softmax over distances between queries and prototypes, which can be written as:  
\[L=\sum_{k=1}^{N}\bigg{[}\sum_{\mathbf{\alpha}\in Q_{k}}\frac{1}{N|Q_{k}|}\Big{\{} dist\Big{(}f_{\phi}(\mathbf{x}),\mathbf{c_{k}},\sum_{t}\Big{)}+\log\sum_{\mathbf{\alpha}\in Q _{k}}exp\Big{(}-dist\Big{(}f_{\phi}(\mathbf{x}),\mathbf{c_{k}},\sum_{t}\Big{)}\Big{)} \Big{\}}\bigg{]}. \tag{13}\]  
The proposed episodic learning process is summarized in Algorithm 1.  
```
Input: Training set \(\mathcal{D}=\{(\mathbf{x}_{1},\ y_{1}),\ \dots,(\mathbf{x}_{T},\ y_{T})\}\), where \(y_{i}\in\{1,\ \dots,\ N_{cls}\}\). \(\mathcal{D}_{k}\) denotes the subset of \(\mathcal{D}\) composed of all samples \((\mathbf{x}_{i},\ y_{i})\) with class label \(y_{i}=k\). Output: Trained embedding networks \(f_{\phi}\) for each episode \(t\)do  Compose a subset \(V\) by randomly selecting \(N\) values from \(\{1,\ \dots,\ N_{cls}\}\)  Set \(\Omega_{t}\leftarrow\varnothing\) for each class label \(k\) in \(V\)do  Compose a support set \(S_{k}\) by randomly selecting \(K\) examples from \(\mathcal{D}_{k}\)  Compose a query set \(Q_{k}\) by subtracting \(S_{k}\) from \(\mathcal{D}_{k}\) ( \(Q_{k}\leftarrow\mathcal{D}_{k}-S_{k}\))  Calculate class prototype \(\mathbf{c_{k}}\leftarrow\big{(}\sum_{x\in S_{k}}f_{\phi}(\mathbf{x})\big{)}/|S_{k}|\)  Compose set of difference vectors \(\ \Omega_{t}\leftarrow\Omega_{t}\cup\{\delta|\mathbf{\delta}=\mathbf{x}-\mathbf{x}^{\prime},\ \mathbf{x}\in S_{k},\ \mathbf{x}^{\prime}\in S_{k}\}\) endfor\(k\)  Calculate covariance matrix \(\sum_{\Omega_{t}}\) using \(\Omega_{t}\)  Update covariance matrix \(\sum_{t}\leftarrow(1-\alpha)\sum_{\Omega_{t}}+\alpha\sum_{t-1}\)  Set loss \(L\gets 0\) for each class \(k\) in \(V\)do  for each \(\mathbf{x}\) in \(Q_{k}\)do \(L\gets L+\frac{1}{N|Q_{k}|}\bigg{[}dist\big{(}f_{\phi}(\mathbf{x}),\mathbf{c_{k}}, \sum_{t}\big{)}+\log\sum\limits_{\mathbf{x}^{\prime}\in Q_{k}}exp\big{(}-dist \big{(}f_{\phi}(\mathbf{x}^{\prime}),\mathbf{c_{k}},\sum_{t}\big{)}\big{)}\bigg{]}\) endfor\(\mathbf{x}\) endfor\(k\)  Update network parameters \(\phi\) using a gradient descent optimizer with loss \(L\) endfor\(t\)
```  
**Algorithm 1**A training algorithm for embedding network in \(N\)-way \(K\)-shot problem. \(N\) is the number of classes per episode, \(K\) is the number of support samples per class, \(N_{cls}\) is the total number of classes in the training set.  
In the test phase, samples from new classes that are not seen during learning are given. Each test class is also decomposed as support data and query data. After calculating the prototype vector and similarity function by using the optimized embedding function through the learning phase, query samples are assigned to the class of the closest prototype.  
Figure 2: The overall structure of the proposed few-shot classification model.  
In this case, the covariance matrix used for distance calculation is obtained by using Equation (12).