# 2.8 多任务学习（Multi-task learning）

在迁移学习中，步骤是串行的，从任务$$A$$里学习然后只是迁移到任务$$B$$。在多任务学习中是同时开始学习的，试图让单个神经网络同时做几件事情，希望每个任务都能帮到其他所有任务

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/a4e496893ed0fb928300f59f26f89cf1.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/a4e496893ed0fb928300f59f26f89cf1.png)

假设无人驾驶需要同时检测行人、车辆、停车标志，还有交通灯各种其他东西

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/f82865ea25c1f62b7e1981df0609a3e2.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/f82865ea25c1f62b7e1981df0609a3e2.png)

如果输入图像$$x^{(i)}$$，那么 $$y^{(i)}$$不再是一个标签，而是有4个标签。在这个例子中，没有行人，有一辆车，有一个停车标志，没有交通灯。所以 $$y^{(i)}$$是个4×1向量。将训练集的标签水平堆叠起来，从$$y^{(1)}$$一直到$$y^{(m)}$$：

$$
\begin{bmatrix}
\vdots  & \vdots & \vdots & \vdots & \vdots\\
y^{(1)}  & y^{(2)} & y^{(3)} & \cdots & y^{(m)}\\
\vdots & \vdots & \vdots & \vdots & \vdots
\end{bmatrix}
$$

矩阵$$Y$$变成$$4\times m$$矩阵

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/c818eb171eff10f2762ede47c5a28a6f.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/c818eb171eff10f2762ede47c5a28a6f.png)

输出四个节点，第一个节点是预测图中有没有行人，第二个预测有没有车，第三预测有没有停车标志，第四预测有没有交通灯，所以$$\hat y$$是四维

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/f25a0a781024508b02c8ff42011474e0.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/f25a0a781024508b02c8ff42011474e0.png)

整个训练集的平均损失：

$$
\frac{1}{m}\sum\_{i = 1}^{m}{\sum\_{j = 1}^{4}{L(\hat y\_{j}^{(i)},y\_{j}^{(i)})}}
$$

$$\sum\_{j = 1}^{4}{L(\hat y\_{j}^{(i)},y\_{j}^{(i)})}$$是单个预测的损失，所以这是对四个分量的求和，行人、车、停车标志、交通灯，标志$$L$$指的是**logistic损失**：

$$
L(\hat y\_{j}^{(i)},y\_{j}^{(i)}) = - y\_{j}^{(i)}\log\hat y\_{j}^{(i)} - (1 - y\_{j}^{(i)})log(1 - \hat y\_{j}^{(i)})
$$

Multi-task learning与Softmax regression的区别在于：

* Multi-task learning是multiple labels的，即输出向量y可以有多个元素为1
* Softmax regression是single label的，即输出向量y只有一个元素为1

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/91f56940e94af25b0d7a46fa8dde9075.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/91f56940e94af25b0d7a46fa8dde9075.png)

神经网络一些早期特征，在识别不同物体时都会用到，训练一个神经网络做四件事情会比训练四个完全独立的神经网络分别做四件事性能要更好

多任务学习也可以处理图像只有部分物体被标记的情况。比如没有标记是否有停车标志，或者是否有交通灯。也许有些样本都有标记，有些样本只标记了有没有车，然后还有一些是问号

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/dbca02c8a624c00bdf088c56c8122609.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/dbca02c8a624c00bdf088c56c8122609.png)

即使是这样的数据集，也可以在上面训练算法，同时做四个任务，即使一些图像只有一小部分标签，其他是问号。训练算法的方式是对$$j$$从1到4只对带0和1标签的$$j$$值求和，当有问号就在求和时忽略那个项

多任务学习当三件事为真时有意义的：

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/a4f0d10a340481a4189328870259d0ed.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/a4f0d10a340481a4189328870259d0ed.png)

* 训练的一组任务，可以共用低层次特征。对于无人驾驶的例子，同时识别交通灯、汽车和行人是有道理的，这些物体有相似的特征
* 如果每个任务的数据量很接近，这个准则没那么绝对，不一定对
* 想要从多任务学习得到很大性能提升，其他任务加起来必须要有比单个任务大得多的数据量

多任务学习会降低性能的唯一情况是神经网络还不够大。但如果可以训练一个足够大的神经网络，多任务学习肯定不会或者很少会降低性能

在实践中，多任务学习的使用频率要低于迁移学习。因为很难找到那么多相似且数据量对等的任务可以用单一神经网络训练。不过在计算机视觉领域，物体检测这个例子是最显著的例外情况

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

![](https://2314428465-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhI0S0DK8pwlrmD%2F-Le0cKOp1vaxoORIi4ak%2F-Le0cbcSxZgjn41A34uu%2F387import.png?generation=1556953084820597\&alt=media)![](https://2314428465-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Le0cHhI0S0DK8pwlrmD%2F-Le0cKOp1vaxoORIi4ak%2F-Le0cbcUt_SdXzmJOgY9%2F388import.png?generation=1556953118372130\&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/28-duo-ren-wu-xue-xi-ff08-multi-task-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.
