# 2.7 迁移学习（Transfer learning）

将已经训练好的模型的一部分知识（网络结构）直接应用到另一个类似模型中去。比如已经训练好一个猫类识别的神经网络模型，直接把该模型中的一部分网络结构应用到使用X光片预测疾病的模型中去，这种学习方法被称为**迁移学习（Transfer Learning）**

如果已经有一个训练好的神经网络用来做图像识别。想要构建另一个X光片进行诊断的模型。迁移学习的做法是无需重新构建新的模型，而是利用之前的神经网络模型，只改变样本输入、输出以及输出层的权重系数$$W^{\[L]},\ b^{\[L]}$$，即对新的样本(X,Y)，重新训练输出层权重系数$$W^{\[L]},\ b^{\[L]}$$，其它层所有的权重系数$$W^{\[L]},\ b^{\[L]}$$保持不变

![](https://2314428465-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhI0S0DK8pwlrmD%2F-Le0cKOp1vaxoORIi4ak%2F-Le0cbtJXp5YhEF9F6p1%2F9.bmp?generation=1556953084394495\&alt=media)

* 如果需要构建新模型的样本数量较少，可以只训练输出层的权重系数$$W^{\[L]},\ b^{\[L]}$$，保持其它层所有的权重系数$$W^{\[l]},\ b^{\[l]}$$不变
* 如果样本数量足够多，可以只保留网络结构，重新训练所有层的权重系数。这种做法使得模型更加精确，因为样本对模型的影响最大
* 择哪种方法通常由数据量决定

如果重新训练所有权重系数，初始$$W^{\[l]},\ b^{\[l]}$$由之前的模型训练得到，这一过程称为pre-training。之后，不断调试、优化$$W^{\[l]},\ b^{\[l]}$$的过程称为fine-tuning。pre-training和fine-tuning分别对应上图中的黑色箭头和红色箭头

迁移学习能这么做的原因是神经网络浅层部分能够检测出许多图片固有特征，例如图像边缘、曲线等。使用之前训练好的神经网络部分结果有助于更快更准确地提取X光片特征。二者处理的都是图片，而图片处理是有相同的地方，第一个训练好的神经网络已经实现如何提取图片有用特征。即便是即将训练的第二个神经网络样本数目少，仍然可以根据第一个神经网络结构和权重系数得到健壮性好的模型

迁移学习可以保留原神经网络的一部分，再添加新的网络层，可以去掉输出层后再增加额外一些神经层

![](https://2314428465-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhI0S0DK8pwlrmD%2F-Le0cKOp1vaxoORIi4ak%2F-Le0cbtTUgbwhjx-ABQB%2F10.bmp?generation=1556953083900051\&alt=media)

迁移学习的应用场合主要包括三点：

* **Task A and B have the same input x.**
* **You have a lot more data for Task A than Task B.**
* **Low level features from A could be helpful for learning B.**

![](https://2314428465-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhI0S0DK8pwlrmD%2F-Le0cKOp1vaxoORIi4ak%2F-Le0cbtVh8kBzikOCTXU%2F382import.png?generation=1556953084321678\&alt=media)![](https://2314428465-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhI0S0DK8pwlrmD%2F-Le0cKOp1vaxoORIi4ak%2F-Le0cbtXTWWiKji0Ho8Z%2F383import.png?generation=1556953082644862\&alt=media)![](https://2314428465-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhI0S0DK8pwlrmD%2F-Le0cKOp1vaxoORIi4ak%2F-Le0cbtZaWu4xNjwazDr%2F385import.png?generation=1556953072366300\&alt=media)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://baozoulin.gitbook.io/neural-networks-and-deep-learning/di-san-men-ke-jie-gou-hua-ji-qi-xue-xi-xiang-mu-structuring-machine-learning-projects/di-san-men-ke-structuring-machine-learning-projects/ml-strategy/27-qian-yi-xue-xi-ff08-transfer-learning.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
