小样本学习介绍#

基本概念#

小样本学习并不是一个单独的学科领域,而是元学习 3Meta learning (computer science) 的一个特殊应用场景。它是在样本量比较少的情况下对数据进行预测。 如果仅有几张图片,拿来训练肯定是不行的,分分钟过拟合。因此,我们需要某种手段来防止这种过拟合的发生, 现在比较流行的一种思路就是先做预训练,让模型具有某种先验知识,或者说,让模型一定的自主学习能力,然后在此基础上进行微调。

注意这里所说的模型,它通常由两部分组成,一部分用来学习先验知识,另一部分用来微调。

回过头来看机器学习和深度学习。它们通常只有一个模型,而这个模型通常是针对某个特定问题提出的。 当有新问题出现时,模型的泛化能力就出现了问题。元学习致力于解决这个泛化能力,它可以针对一系列问题进行求解。

深度学习做训练,通常情况下是为了让模型具备识别某个事物的能力,也就是当给神经网络一张图片时,它能够看出来,这是一只狗,还是一只猫。 元学习,是为了学习比较异同的规则,也就是说,如果我们同时给网络两张图片,它能看出来,这两张图片是不是同一种东西。

连续读了几天论文,我发现,不管看哪个领域的论文,如果能够记住一些 约定俗称的符号 对我们读后面的论文都会有一定的帮助。而且,通常,在刚刚接触新领域时,肯定会碰到一些 新的名词或专业术语,能够及时地做好记录也是一个很好的习惯。

元学习一般将问题分为三类 4Timothy Hospedales, Antreas Antoniou, Paul Micaelli, and Amos Storkey. Meta-learning in neural networks: a survey. arXiv preprint arXiv:2004.05439, 2020.

  • 元知识的表征:学习元知识应该如何表示,比如,学习权重、学习超参数、学习损失函数等;

  • 元学习器:学习应该如何学习问题,比如,可以学习如何更快地梯度下降等;

  • 元目标:学习如何更好地在某个特定场景下取得良好效果,比如应用于小样本还是多样本。

../_images/overview-of-meta-learning-landscape.png

元学习一般包括两个学习器,learner 和 meta-learner,每个学习器都有一个超参数,但下图中没写。

  • learner 是用来解决手中数据的一个学习器,也叫 base-learner;

  • meta-learner 是预训练得到的一个学习器。

../_images/meta-learning-workflow.png

读一些相关文献,从论文中的伪代码上可以看出来,它一般包括两层循环,外层循环学习得到 meta-learner,内层循环学习得到 learner。 为了降低难度,我们看图来直观理解一下:

../_images/meta-learning-framework.png

为了能更进一步理解整体的工作过程,考虑一个例子:

../_images/meta-learning.png

假如我有一个 \(10\) 类的数据,每个类别 \(10\) 张图片,共 \(10 \times 10=100\) 张。 那元学习的过程就是,首先设置实验,比如 10-way 5-shot。 meta-train 的过程拿 imagenet 这种数据集 pre-train,meta-test 就是在我自己的数据( \(100\) 张图)上面 finetune, 最后拿到那套参数 \(\theta\),然后推理的时候每次就拿这个 \(\theta\) 算一下前向进行分类。

元学习一般有三种基本解决方法:

  • 基于度量的方法(学习事物背后的关联)

    • 目标是学习不同样本之间的度量或距离函数;

    • 5Gregory Koch, Richard Zemel, Ruslan Salakhutdinov, and others. Siamese neural networks for one-shot image recognition. In ICML deep learning workshop, volume 2. Lille, 2015.6Oriol Vinyals, Charles Blundell, Timothy Lillicrap, Daan Wierstra, and others. Matching networks for one shot learning. Advances in neural information processing systems, 29:3630–3638, 2016.7Flood Sung, Yongxin Yang, Li Zhang, Tao Xiang, Philip HS Torr, and Timothy M Hospedales. Learning to compare: relation network for few-shot learning. In Proceedings of the IEEE conference on computer vision and pattern recognition, 1199–1208. 2018.8Jake Snell, Kevin Swersky, and Richard S Zemel. Prototypical networks for few-shot learning. arXiv preprint arXiv:1703.05175, 2017.
    • 比如 Siamese Network5Gregory Koch, Richard Zemel, Ruslan Salakhutdinov, and others. Siamese neural networks for one-shot image recognition. In ICML deep learning workshop, volume 2. Lille, 2015.,Matching Network6Oriol Vinyals, Charles Blundell, Timothy Lillicrap, Daan Wierstra, and others. Matching networks for one shot learning. Advances in neural information processing systems, 29:3630–3638, 2016.,Relation Network7Flood Sung, Yongxin Yang, Li Zhang, Tao Xiang, Philip HS Torr, and Timothy M Hospedales. Learning to compare: relation network for few-shot learning. In Proceedings of the IEEE conference on computer vision and pattern recognition, 1199–1208. 2018.,Prototypical Network8Jake Snell, Kevin Swersky, and Richard S Zemel. Prototypical networks for few-shot learning. arXiv preprint arXiv:1703.05175, 2017.

  • 基于模型的方法(学习如何建模)

    • 目标是让元学习器(Meta-Learner)学习一个后验概率 \(P_\theta(y|\mathbf{x})\)

    • 9Adam Santoro, Sergey Bartunov, Matthew Botvinick, Daan Wierstra, and Timothy Lillicrap. Meta-learning with memory-augmented neural networks. In International conference on machine learning, 1842–1850. PMLR, 2016.10Tsendsuren Munkhdalai and Hong Yu. Meta networks. In International Conference on Machine Learning, 2554–2563. PMLR, 2017.11Nikhil Mishra, Mostafa Rohaninejad, Xi Chen, and Pieter Abbeel. Meta-learning with temporal convolutions. 2017.
    • 比如 MANN9Adam Santoro, Sergey Bartunov, Matthew Botvinick, Daan Wierstra, and Timothy Lillicrap. Meta-learning with memory-augmented neural networks. In International conference on machine learning, 1842–1850. PMLR, 2016.,Meta Network10Tsendsuren Munkhdalai and Hong Yu. Meta networks. In International Conference on Machine Learning, 2554–2563. PMLR, 2017.,TCML11Nikhil Mishra, Mostafa Rohaninejad, Xi Chen, and Pieter Abbeel. Meta-learning with temporal convolutions. 2017.

  • 基于优化的方法(学习如何学习)

    • 目标是加快模型的求解速度;

    • 12Sachin Ravi and Hugo Larochelle. Optimization as a model for few-shot learning. In ICLR 2017 : International Conference on Learning Representations 2017. 2017.13Marcin Andrychowicz, Misha Denil, Sergio Gomez, Matthew W Hoffman, David Pfau, Tom Schaul, Brendan Shillingford, and Nando De Freitas. Learning to learn by gradient descent by gradient descent. In Advances in neural information processing systems, 3981–3989. 2016.14Chelsea Finn, Pieter Abbeel, and Sergey Levine. Model-agnostic meta-learning for fast adaptation of deep networks. In International Conference on Machine Learning, 1126–1135. PMLR, 2017.15Barret Zoph and Quoc V Le. Neural architecture search with reinforcement learning. arXiv preprint arXiv:1611.01578, 2016.16Jane X Wang, Zeb Kurth-Nelson, Dhruva Tirumala, Hubert Soyer, Joel Z Leibo, Remi Munos, Charles Blundell, Dharshan Kumaran, and Matt Botvinick. Learning to reinforcement learn. arXiv preprint arXiv:1611.05763, 2016.17Yan Duan, John Schulman, Xi Chen, Peter L. Bartlett, Ilya Sutskever, and Pieter Abbeel. Rl^2: fast reinforcement learning via slow reinforcement learning. arXiv preprint arXiv:1611.02779, 2016.
    • 比如 LSTM-based12Sachin Ravi and Hugo Larochelle. Optimization as a model for few-shot learning. In ICLR 2017 : International Conference on Learning Representations 2017. 2017.13Marcin Andrychowicz, Misha Denil, Sergio Gomez, Matthew W Hoffman, David Pfau, Tom Schaul, Brendan Shillingford, and Nando De Freitas. Learning to learn by gradient descent by gradient descent. In Advances in neural information processing systems, 3981–3989. 2016.,MAML14Chelsea Finn, Pieter Abbeel, and Sergey Levine. Model-agnostic meta-learning for fast adaptation of deep networks. In International Conference on Machine Learning, 1126–1135. PMLR, 2017.,Repitile,RL-based15Barret Zoph and Quoc V Le. Neural architecture search with reinforcement learning. arXiv preprint arXiv:1611.01578, 2016.16Jane X Wang, Zeb Kurth-Nelson, Dhruva Tirumala, Hubert Soyer, Joel Z Leibo, Remi Munos, Charles Blundell, Dharshan Kumaran, and Matt Botvinick. Learning to reinforcement learn. arXiv preprint arXiv:1611.05763, 2016.17Yan Duan, John Schulman, Xi Chen, Peter L. Bartlett, Ilya Sutskever, and Pieter Abbeel. Rl^2: fast reinforcement learning via slow reinforcement learning. arXiv preprint arXiv:1611.02779, 2016.

基于度量的方法很好理解,它主要的构件就是嵌入模块和度量模块 1赵凯琳,靳小龙,王元卓.小样本学习研究综述.软件学报,2021,32(2):349-369

  • 嵌入模块 \(f\) 将数据样本映射为特征向量;

  • 度量模块 \(g\) 比较待测样本于其他样本的特征向量的相似度。

../_images/metric-based-meta-learning.png

注:图片中的颜色,每种颜色都会有一个特征向量。把待测样本归类为相似度最高的样本所属的类别。分类器一般选择 Softmax。

基于模型的方法,是学习如何建模。乍一看这个名字,还以为是让算法学习搭建网络结构呢,其实不然。 网络结构还是人为定义好的,他要学习的是一种建立模型的能力,而不是具体的模型,也就是如何从少量数据中求解 \(P_\theta(y|\mathbf{x})\)

神经图灵机的框架 18Alex Graves, Greg Wayne, and Ivo Danihelka. Neural turing machines. CoRR, 2014. URL: http://arxiv.org/abs/1410.5401, arXiv:1410.5401. 提供了一个通用的模型(见下图),在此基础上衍生出了 MANN 和 Meta Network。

../_images/neural-tuning-machine.png

具体的模型是由记忆产生的,在不同的记忆下,对应不同的函数(Read heads 读取记忆,Write heads 产生记忆)。 对于新任务,模型把数据集载入记忆,Read heads 根据权重合理地读取记忆,就形成了对这个任务的建模。

基于优化的方法,是我们通常讲的,如何学会学习,也就是说,如何学到一个合适的初始化,让模型更快地收敛。比如 MAML [14] (见下图)。

../_images/model-agnostic-meta-learning.png

注:图中的粗实线是元学习的过程,灰色线是每个任务(task)。元学习器为每个任务学习优化参数 \(\theta_i^*\),这些优化参数的矢量和为 \(\theta\)。 当面对新场景时,用平均后的参数 \(\theta\) 来初始化新场景,可以更快地收敛。

以上是我于 2021 年 10 月 28 日做的一次 PPT 组会分享,详见 Slide

然后,更多更加详细的论文方法汇总在下面了2小样本学习与元学习资料调研:白小鱼

  • Data Augmentation / Hallucination Based / Sample Synthesis (learn to augment data)

    • GAN: Covariance-Preserving Adversarial Augmentation Networks - 2018 NeurIPS

    • Low-Shot Learning from Imaginary Data - 2018 CVPR

    • \(\Delta\)-encoder: Sample Synthesis - 2018 NeurIPS

    • Semantic Feature Augmentation - 2018 arXiv

  • Metric-Learning Based (learn to compare)

    • Siamese Network - 2015 ICML

    • Matching Network - 2016 NIPS

    • Prototype Network - 2017 NIPS

    • Relation Network - 2018 CVPR

    • Covariance Metric Network - 2019 AAAI

    • Deep Nearest Neighbor Neural Network - 2019 CVPR

    • Large Margin Meta-Learning - 2018 arXiv

    • RepMet: Few-Shot Detection - 2019 CVPR

  • Meta-Learning Based (learn to learn)

    • Recurrent meta-learners

    • Optimizers

      • MAML: Model-Agnostic Meta-Learning - 2017 ICML

      • Meta-SGD - 2017 arXiv

      • LEO: Meta-Learning with Latent Embedding Optimization - 2019 ICLR

      • Reptile - 2018 arXiv

      • Meta-Learner LSTM - 2017 ICLR

      • Dynamic FSL - 2018 CVPR

      • MTL: Meta-Transfer Learning - 2019 CVPR

      • Meta Network - 2017 ICML

  • Finetune Based

    • Baseline for Few-Shot Image Classification - 2019 arXiv

例: 孪生网络#

构造正负样本(Train Data)

正样本是从某个类别中选出两张图片,组成一个元组, \((class1, class1, 1)\)。 负样本是从不同的类别中各选出一张图片,也组成一个三元组, \((class1, class2, 0)\)

构造模型

对图片提取特征,生成一个特征向量。 将两张图片同时输入到网络中,可以得到两个特征向量,对这两个特征向量做差,可以得到它们之间的差异。 然后,对做差后的特征向量应用全连接神经网络,映射为一个标量,通过 Sigmoid 函数后得到它们之间的相似度。

更新参数

Ground Truth 为 One hot 向量,这是 \(y\)。 对模型得到的预测值,\(\hat{y}\) 与标准值 \(y\) 做 Cross Entropy,记作 Loss。 为了使 Loss 最小,应用反向传播更新参数。

Triplet Loss

首先从某个类中选出一个锚点(anchor),然后再从这个类中选出一个正样本(positive sample),最后从另一个类中选出一个负样本(negative sample),构成三元组 \((pos, anchor, neg)\)。 将这三张图片都输入到网络中,可以得到三个特征向量 \(f(x^+), f(x^a), f(x^-)\)。 三个向量,两两之间分别计算欧氏距离。 目标是让正样本的特征向量和锚点的特征向量之间距离越小越好,负样本的特征向量与锚点的特征向量之间的距离越大越好。 \(Loss(x^a, x^+, x^-)=max\left\{0, d^+ + \alpha - d^-\right\}\) 其中 \(\alpha\) 是一个超参。更新网络参数,最小化 Loss。

备注

近些年,准确率较高的都是 Embedding,把图片映射成特征向量,想法和 Siamese Network 相似。

预训练和微调#

这是一个很简单的思路,在大数据集 Train Set 上做 Pretraining,在小数据集 Support Set 上做 Fine Tuning。 这种方式虽然简单,但是准确率相对较高。代码实现参考《迁移学习简明手册19王晋东等. 迁移学习简明手册. 2018. URL: https://kdocs.cn/l/ch4VStxYGjpp.》上手实践部分。

  • Step1: Pretraining

    • Pretrain a CNN on large-scale training data.

    • Use the CNN for feature extraction.

  • Step2: Fine Tuning

    • Training a classifier on the support set.

    • Tricks:

      • Using \(\mathbf{M}\) to initialize \(\mathbf{W}\).

      • Entropy regularization.

      • Cosine similarity + Softmax classifier.

  • Step3: Few Shot Prediction

    • Map images in the support set to feature vectors.

    • Obtain the mean feature vector of each class, \(\mu_1, \mu_2, \dots, \mu_k\)

    • Compare the feature of query with \(\mu_1, \mu_2, \dots, \mu_k\)

Cosine similarity

衡量两个向量之间的相似度。两个向量的内积等于 \(cos\theta=\mathbf{x}^T\mathbf{w}\)

Softmax Function

可以把一个向量映射成一个概率分布,通常用于输出层。首先对向量的每一个元素取指数,然后做归一化。每个概率值表示对每个类别的 Confidence。 Softmax 会让最大值变大,让最小值变小,突出差异性。

Fine Tuning

再 Support Set 上学习 \(\mathbf{W}\)\(\mathbf{b}\) 就是做 Fine Tuning。 之前没有学习 \(\mathbf{W}\)\(\mathbf{b}\) 直接让 \(\mathbf{b} = 0\)\(\mathbf{W} = \mathbf{M}\)。 其中 \(\mathbf{M}\) 是每一个类别的均值向量组成的矩阵。

应用场景#

资料调研#

科普视频#

科普博文#

领域综述#

教学视频#

  • CS 330: Deep Multi-Task and Meta Learning 主页哔哩哔哩 17.75 小时

  • Chelsea Finn: Meta-Learning: from Few-Shot Learning to Rapid Reinforcement Learning 主页哔哩哔哩

  • Chelsea Finn: Building Unsupervised Versatile Agents with Meta-Learning YouTube 1 小时

  • 李宏毅:Meta Learning YouTube哔哩哔哩

特邀演讲#

  • Generalizing from Few Examples with Meta-Learning by Hugo Larochelle VideoSlides

  • Workshop on Meta-Learning (MetaLearn 2021) Video

  • Deep Learning: Bridging Theory and Practice Video

  • Challenges in Multi-Task Learning and Meta-Learning VideoSlides

  • The Big Problem with Meta-Learning and How Bayesians Can Fix It VideoSlides

算法实现#

数据集#

领域学者#