On Episodes, Prototypical Networks, and Few-Shot Learning

1 Introduction . The problem of few-shot learning ( FSL ) – classifying examples from previously unseen classes given only a handful of training data – has considerably grown in popularity within the machine learning community in the last few years . The reason is likely twofold . First , being able to perform well on FSL problems is important for several applications , from learning new symbols [ 23 ] to drug discovery [ 2 ] . Second , since the aim of researchers interested in meta-learning is to design systems that can quickly learn novel concepts by generalising from previously encountered learning tasks , FSL benchmarks are often adopted as a practical way to empirically validate meta-learning algorithms . To the best of our knowledge , there is not a widely recognised definition of meta-learning . In a recent survey , Hospedales et al . [ 22 ] informally describe it as “ the process of improving a learning algorithm over multiple learning episodes ” . In practical terms , following the compelling rationale that “ test and train conditions should match ” [ 48 , 13 ] , several seminal meta-learning papers ( e.g . [ 48 , 32 , 14 ] ) have emphasised the importance of organising training into episodes , i.e . learning problems with a limited amount of “ training ” ( the support set ) and “ test ” examples ( the query set ) to mimic the test-time scenario presented by FSL benchmarks . However , several recent works ( e.g . [ 9 , 49 , 11 , 44 ] ) showed that simple baselines can outperform established FSL meta-learning methods by using embeddings pre-trained with the standard crossentropy loss , thus casting a doubt on the importance of episodes in FSL . Inspired by these results , we aim at understanding the practical usefulness of episodic learning in popular FSL methods relying on metric-based nonparametric classifiers such as Matching and Prototypical Networks [ 48 , 40 ] . We chose this family of methods because they do not perform any adaptation at test time . This allows us ∗Work done while research intern at FiveAI 35th Conference on Neural Information Processing Systems ( NeurIPS 2021 ) . to test the efficacy of episodic training without having to significantly change the baseline algorithms , which could potentially introduce confounding factors . In this work we perform a case study focussed on Matching Networks [ 48 ] and Prototypical Networks [ 40 ] , and we show that within this family of methods episodic learning a ) is detrimental for performance , b ) is analogous to randomly discarding examples from a batch and c ) introduces a set of superfluous hyperparameters that require careful tuning . Without episodic learning , these methods are closely related to the classic Neighbourhood Component Analysis ( NCA ) [ 19 , 35 ] on deep embeddings and achieve , without bells and whistles , an accuracy that is competitive with recent methods on multiple FSL benchmarks : miniImageNet , CIFAR-FS and tieredImageNet . PyTorch code is available at https : //github.com/fiveai/on-episodes-fsl . 2 Background and method . This section is divided as follows : Sec . 2.1 introduces episodic learning and illustrates a data efficiency issue encountered with nonparametric few-shot learners based on episodes ; Sec . 2.2 introduces the losses from Snell et al . [ 40 ] , Vinyals et al . [ 48 ] and Goldberger et al . [ 19 ] which we use throughout our experiments ; and Sec . 2.3 explains the three options we explored to perform FSL classification with previously-trained feature embeddings . 2.1 Episodic learning . A common strategy to train FSL methods is to consider a distribution Ê over possible subsets of labels that is as close as possible to the one encountered during evaluation E 2 [ 48 ] . Each episodic batch BE = { S , Q } is obtained by first sampling a subset of labels L from Ê , and then sampling images constituting both support set S and query set Q from the set of images with labels in L , where S = { ( s1 , y1 ) , . . . , ( sn , yn ) } , Q = { ( q1 , y1 ) , . . . , ( qm , ym ) } , and Sk and Qk denote the sets of images with label y = k in the support set and query set respectively . For most methods , this corresponds to training on a series of mini-batches in which each image belongs to either the support or the query set . Support and query sets are constructed such that they both contain all the classes of L , and a fixed number of images per class . Therefore , episodes are defined by three variables : the number of classes w = |L| ( the “ ways ” ) , the number of examples per class in the support set n = |Sk| ( the “ shots ” ) , and the number of examples per class in the query set m = |Qk| . During evaluation , the set { w , n , m } defines the problem setup . Instead , at training time { w , n , m } can be seen as a set of hyperparameters controlling the batch creation , and that ( as we will see in Sec . 3.2 ) requires careful tuning . 2Note that , in FSL , the sets of classes encountered during training and evaluation are disjoint . In a Maximum Likelihood Estimation framework , training on these episodes can be written as arg max θ E L∼Ê E S∼L Q∼L ∑ ( qi , yi ) ∈Q logPθ ( yi|qi , S , ρ ) . ( 1 ) For the sake of brevity , with a slight abuse of notation we omit the function fθ ( e.g . a deep neural network ) which is used to obtain a representation for the images in S and Q , and whose parameters θ are optimised during the training process . Note that the optimisation of Eq . 1 depends on an optional set of parameters ρ . This is obtained by an “ inner ” optimisation procedure , whose scope is limited to the current episode [ 22 ] . The idea is that the “ outer ” optimisation loop , by attending to a distribution of episodes , will appropriately shift the inductive bias of the algorithm located in the inner loop , thus learning how to learn [ 47 ] . In recent years , many interesting proposals have been made about what form ρ should have , and how it should be computed . For instance , in MAML [ 14 ] ρ takes the form of an update of the global parameters θ , while Ravi and Larochelle [ 32 ] learn to optimise by considering ρ as set of the hyper-parameters of the optimiser ’ s update rule . Other methods , such as Matching and Prototypical Networks [ 48 , 40 ] , avoid learning a separate set of parameters ρ altogether , and utilise a nonparametric learner ( such as nearest neighbour classifiers ) at the inner level . We chose to focus our case study on these methods not only because they have been seminal for the community , but also for ease of experimental design . Having ρ = ∅ considerably reduces the design complexity of the algorithm , thus allowing precise ablations to understand the efficacy of episodic learning without considerably changing the nature of the original algorithms . Considerations on data efficiency . The constraints imposed by episodic learning on the role each image has in a training batch has subtle but important implications , illustrated in Fig . 1 by highlighting the number of distances contributing to the loss . By dividing batches between support and query set ( S and Q ) during training , episodes have the side effect of disregarding many of the distances between labelled examples that would constitute useful training signal for nonparametric FSL methods . More specifically , for metric-based nonparametric methods , the number of training distances that are omitted in a batch because of the episodic strategy grows quadratically as O ( w2 ( m2 + n2 ) ) ( derivation shown in Appendix A ) . Table 1 breaks down this difference in terms of gradients from positives and negatives distance pairs ( which we simply refer to as positives and negatives throughout the rest of the paper ) . In a typical training batch with w = 20 , m = 15 and n = 5 [ 40 ] , ignoring the episodic constraints increases the number of both positives and negatives by more than 150 % . In the remainder of this paper , we conduct a case study to illustrate how this issue affects two of the most popular FSL algorithms relying on nonparametric approaches at the inner level : Prototypical Networks [ 40 ] and Matching Networks [ 48 ] . 2.2 Loss functions . Prototypical Networks ( PNs ) [ 40 ] are one of the most popular and effective approaches in the few-shot learning literature . They are at the core of several recently proposed FSL methods ( e.g . [ 27 , 18 , 1 , 50 , 7 ] ) , and they are used in a number of applied machine learning works ( e.g . EEG scan analysis for autism [ 36 ] and glaucoma grading [ 17 ] ) . During training , episodes consisting of a support set S and a query set Q are sampled as described in Sec . 2.1 . Then , a prototype for each class k is computed as the mean embedding of the samples from the support set belonging to that class : ck = ( 1/|Sk| ) · ∑ ( si , yk ) ∈Sk fθ ( si ) , where fθ is a deep neural network with parameters θ learned via Eq . 1 . Let C = { ( c1 , y1 ) , . . . , ( ck , yk ) } be the set of prototypes and corresponding labels . The loss can be written as follows : LPNs = −1 |Q| ∑ ( qi , yi ) ∈Q log ( exp−‖fθ ( qi ) − cyi‖2∑ k′ exp−‖fθ ( qi ) − ck′‖2 ) , where k′ is an index that goes over all classes . Matching Networks ( MNs ) [ 48 ] are closely related to PNs in the multi-shot case and equivalent in the 1-shot case . Rather than aggregating the embeddings of the same class into prototypes , this loss directly computes a softmax over individual embeddings of the support set , as : LMNs = −1 |Q| ∑ ( qi , y ) ∈Q log ∑ sj ∈Sy exp−‖fθ ( qi ) − fθ ( sj ) ‖2 ∑ sk ∈S exp−‖fθ ( qi ) − fθ ( sk ) ‖2 . In their work , Vinyals et al . [ 48 ] use the cosine rather than the Euclidean distance . However , ( as [ 40 ] ) we observed that the Euclidean distance is a better choice for FSL problems , and thus we use it in all the losses of our experiments . Note that Vinyals et al . [ 48 ] also suggest a variant to LMNs ( MNs with “ Full Context Embeddings ” ) , where an LSTM ( with an extra set of parameters ) is used to condition the way the inputs are embedded in the current support set . In our experiments , we did not consider this variant as it falls in the category of adaptive episodic learning approaches ( ρ 6= ∅ , see Sec . 2.1 ) . Neighbourhood Component Analysis ( NCA ) . LMNs and LPNs sum over the likelihoods that a query image belongs to the same class of a certain sample ( or prototype ) from the support set by computing the softmax over the distances between the query and the support samples ( or prototypes ) . This is closely related to the Neighbourhood Component Analysis approach by Goldberger et al . [ 19 ] ( and expanded to the non-linear case by Salakhutdinov et al . [ 35 ] and Frosst et al . [ 15 ] ) , except for a few important differences which we discuss at the end of this section . Let i ∈ [ 1 , b ] be the indices of the images within a batch B . The NCA loss can be written as : LNCA = −1 |B| ∑ i∈1 , ... , b log ∑ j∈1 , ... , b j 6=i yi=yj exp−‖zi − zj‖2 ∑ k∈1 , ... , b k 6=i exp−‖zi − zk‖2 , where zi = fθ ( xi ) is an image embedding and yi its corresponding label . By minimising this loss , distances between embeddings from the same class will be minimised , while distances between embeddings from different classes will be maximised . Importantly , note how the concepts of support set and query set here do not exist . More simply , the images ( and respective labels ) constituting the batch B = { ( x1 , y1 ) , . . . , ( xb , yb ) } are sampled uniformly . Given the similarity between these three losses , and considering that PNs and MNs do not perform episode-specific parameter adaptation , { w , m , n } can be simply interpreted as the set of hyperparameters controlling the sampling of mini-batches during training . More specifically , PNs , MNs and NCA differ in three aspects : I . First and foremost , due to the nature of episodic learning , PNs and MNs only consider pairwise distances between the query and the support set ( Fig . 1 left ) ; NCA instead uses all the distances within a batch and treats each example in the same way ( Fig . 1 right ) . II . Only PNs rely on the creation of prototypes . III . Because of how L , S and Q are sampled in episodic learning ( Eq . 1 ) , for PNs and MNs some images might be sampled more frequently than others ( sampling “ with replacement ” ) . NCA instead visits every image of the dataset once for each epoch ( sampling “ without replacement ” ) . To investigate the effects of these three differences , in Sec . 3 we conduct a wide range of experiments .

Summary: This paper presents a new perspective on the episodic learning of nonparametric few-shot learning methods. The main claim is that current popular nonparametric methods, such as Prototypical Networks (PNs) and Matching Networks (MNs), are not data-efficient because less gradient signal is propagated during training due to the artificial division of the data points to support and query sets. The authors instead propose to use the standard learning protocol with batches and an equivalent loss function based on the Neighbourhood Component Analysis (NCA). This loss function exploits all connections between data points in the batch. The authors then propose three techniques to perform a few-shot classification during evaluation based on k-NN, nearest centroid, and soft assignment. Through extensive experiments, the authors justify their perspective and show comparable results to other recent FSL methods (not necessarily nonparametric ones).