# A Closer Look at Few-Shot Classification with Many Novel Classes
## 4 Simple Hierarchy-Aware Pipeline
### Fine-Tuning with Class Hierarchy Capturing
#### 4.2.3 Structured Representation Learning with Class Hierarchy  
To fully leverage the class hierarchy, we explore two approaches to structure the learning process. One approach employs a metric-level constraint to ensure that Euclidean distances between prototypes closely approximate the theoretical distances defined by the class hierarchy. Another approach uses hierarchy triplet loss, which imposes sample-level constraints to maintain distance between similar samples from the same parent classes.  
**CPCC Regularization.** Firstly, we define the distance between prototype \(i\) and prototype \(j\) on the class hierarchy \(d_{T}(i,j)\) as follows:  
\[d_{T}(i,j)=H-\sum_{k=1}^{H}\left(\mathbb{I}\Big{(}r_{i}^{(k)}=r_{j}^{(k)} \Big{)}\right), \tag{5}\]  
where \(r_{i}\) represents the hierarchy cluster result of the prototype \(i\), with \(k\)th element \(r_{i}^{(k)}\) indicating the cluster ID to which the prototype belongs at level \(k\). \(H\) represents the overall height of the class hierarchy, which is greater than one. \(\mathbb{I}\) is the indicator function, and the range of \(d_{T}(i,j)\) spans the interval \([1,H]\).  
To quantify the alignment between the class hierarchy and feature transformations, we incorporate Cophenetic Correlation Coefficient (CPCC) regularization. The CPCC measures the correlation between the class hierarchy distances and the cosine distances between class prototypes, computed as follows:  
\[\text{CPCC}(d_{T},\rho_{Z})=\frac{\text{cov}(d_{T},\rho_{Z})}{\sqrt{\text{var }(d_{T})\cdot\text{var}(\rho_{Z})}} \tag{6}\]  
where \(d_{T}\) and \(\rho_{Z}\) represent the sets of pairwise class hierarchy and cosine distances, respectively. \(\text{cov}(\cdot)\) and \(\text{var}(\cdot)\) denote the covariance and variance, respectively.  
The total training loss is a combination of cross-entropy loss (\(\mathcal{L}_{\text{CE}}\)) and the CPCC regularization term (CPCC), leading to the following formulation:  
\[\mathcal{L}_{train}=\sum_{(x,y)\in\mathcal{S}}\ell_{\text{CE}}(y,g(f_{\theta }(x)))-\lambda\cdot\text{CPCC}(d_{T},\rho_{Z}), \tag{7}\]  
where \(\lambda\) is the weighting factor for the CPCC regularization term and \(\mathcal{S}\) represents the data-augmented support set for the few-shot task.  
**Hierarchy triplet loss.** In the following, we also offer another way to learn structured representation with class hierarchy based on triplet loss with adaptive margin.  
We re-scale the \(d_{T}(i,j)\) to the interval \([0,1)\) to obtain the adaptive margin \(M(i,j)\) between anchor prototype \(i\) and negative prototype \(j\):  
\[M(i,j)=\frac{d_{T}(i,j)(1-d_{mean})}{H}+d_{mean}-d_{i}+d_{j}, \tag{8}\]where \(d_{i}\) and \(d_{j}\) are the average cosine distance of the samples from class \(i\) and class \(j\), respectively. \(d_{mean}\) is given by \(d_{mean}=\frac{1}{\mathcal{L}}\sum_{i=1}^{N}d_{i}\).  
After obtaining the adaptive margin \(M(i,j)\), for the sake of computation overhead, we randomly sample triplets \(\mathcal{T}_{z}=\left(\mathbf{x}_{a},\mathbf{x}_{p},\mathbf{x}_{n}\right)\) from the data-augmented support set \(\mathcal{S}\) and obtain the corresponding adaptive margin \(M(i,j)\) for sample \(x_{a}\) and negative sample \(x_{n}\) which comes from the \(i\)th class and the \(j\)th class, respectively. The hierarchy triplet loss is given by  
\[\mathcal{L}_{htl}=\frac{1}{\left|\mathcal{T}^{\mathcal{M}}\right|}\sum_{ \mathcal{T}^{s}\in\mathcal{T}^{\mathcal{M}}}\left[\left\|\mathbf{x}_{a}- \mathbf{x}_{p}\right\|^{2}-\left\|\mathbf{x}_{a}-\mathbf{x}_{n}\right\|^{2}+M (i,j)\right]_{+} \tag{9}\]