【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics

·阅读摘要:
本文提出针对CV领域的多任务模型,设置一个可以学习损失权重的损失层,可以提高模型精度。
·参考文献:
[1] Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics

个人理解:我们使用传统的多任务时,损失函数一般都是各个任务的损失相加,最多会为每个任务的损失前添加权重系数。但是这样的超参数是很难去调参的,代价大,而且很难去调到一个最好的状态。最好的方式应该是交给深度学习。

论文最重要的部分在损失函数的设置与推导。 这对我们优化自己的多任务学习模型有指导意义。

[1] Homoscedastic uncertainty as task-dependent uncertainty (同方差不确定性)

作者的数学模型通过贝叶斯模型建立。作者首先提出贝叶斯建模中存在两类不确定性:

· 认知不确定性(Epistemic uncertainty):由于缺少训练数据而引起的不确定性
· 偶然不确定性(Aleatoric uncertainty):由于训练数据无法解释信息而引起的不确定性

而对于偶然不确定性,又分为如下两个子类:

· 数据依赖地(Data-dependant)或异方差(Heteroscedastic)不确定性
· 任务依赖地(Task-dependant)或同方差(Homoscedastic)不确定性

多任务中,任务不确定性捕获任务间相关置信度,反应回归或分类任务的内在不确定性。

【注】本篇论文的假设,是基于同方差不确定性的。关于同方差不确定性和异方差不确定性的通俗解释,可以参考知乎问题:https://www.zhihu.com/question/278182454/answer/398539763

[2] Multi-task likelihoods (多任务似然)

基于极大似然估计,假设 f W \mathbf{f}^{\mathbf{W}}f W为网络输出,W W W为该项输出的权重,则对于回归任务有:
p ( y ∣ f W ( x ) ) = N ( f W ( x ) , σ 2 ) p(y∣\mathbf{f}^{\mathbf{W}}(x))=N(\mathbf{f}^{\mathbf{W}}(x),σ^2 )p (y ∣f W (x ))=N (f W (x ),σ2 )
对于分类任务有:
p ( y ∣ f W ( x ) ) = S o f t m a x ( f W ( x ) ) p(y∣\mathbf{f}^{\mathbf{W}} (x))=Softmax(\mathbf{f}^{\mathbf{W}} (x))p (y ∣f W (x ))=S o f t m a x (f W (x ))
多任务的概率:
p ( y 1 , … , y K ​ ∣ f W ( x ) ) = p ( y 1 ​ ∣ f W ( x ) ) … p ( y K ​ ∣ f W ( x ) ) p(y_1,…,y_K​ ∣\mathbf{f}^{\mathbf{W}}(x))=p(y_1​ ∣\mathbf{f}^{\mathbf{W}} (x))…p(y_K​ ∣\mathbf{f}^{\mathbf{W}} (x))p (y 1 ​,…,y K ​​∣f W (x ))=p (y 1 ​​∣f W (x ))…p (y K ​​∣f W (x ))
例如对于回归任务来说,极大似然估计转化为最小化负对数:
l o g p ( y ∣ f W ( x ) ) ∝ − 1 2 σ 2 ​ ∥ ∥ ​ y − f W ( x ) ∥ ∥ ​ 2 − l o g σ log{p(y∣\mathbf{f}^{\mathbf{W}}(x))}∝− \frac{1}{2σ^2}​ {∥∥​ y−\mathbf{f}^{\mathbf{W}} (x) ∥∥​}^2 −log\sigma l o g p (y ∣f W (x ))∝−2 σ2 1 ​​∥∥​y −f W (x )∥∥​2 −l o g σ
其中 σ \sigma σ 表示测量噪声的方差。

双任务:

假设是两个回归任务,那么概率如下:

p ( y 1 , y 2 ​ ∣ f W ( x ) ) ​ = p ( y 1 ​ ∣ f W ( x ) ) ⋅ p ( y 2 ​ ∣ f W ( x ) ) = N ( y 1 ​ ; f W ( x ) , σ 1 2 ​ ) ⋅ N ( y 2 ​ ; f W ( x ) , σ 2 2 ​ ) p(y_1 ,y_2​ ∣\mathbf{f}^{\mathbf{W}} (x))​ =p(y_1​ ∣\mathbf{f}^{\mathbf{W}} (x))⋅p(y_2​ ∣\mathbf{f}^{\mathbf{W}} (x))=N(y_1​;\mathbf{f}^{\mathbf{W}} (x),σ_1^2​ )⋅N(y_2​ ;\mathbf{f}^{\mathbf{W}} (x),σ_2^2​ )p (y 1 ​,y 2 ​​∣f W (x ))​=p (y 1 ​​∣f W (x ))⋅p (y 2 ​​∣f W (x ))=N (y 1 ​​;f W (x ),σ1 2 ​​)⋅N (y 2 ​​;f W (x ),σ2 2 ​​)

为了优化我们的 Loss 函数 L ( W , σ 1 , σ 2 ) \mathcal{L}\left(\mathbf{W}, \sigma_{1}, \sigma_{2}\right)L (W ,σ1 ​,σ2 ​),取最小化负对数:

= − l o g p ( y 1 ​ , y 2 ​ ∣ f W ( x ) ) =−logp(y_1​,y_2​ ∣\mathbf{f}^{\mathbf{W}} (x))=−l o g p (y 1 ​​,y 2 ​​∣f W (x ))

∝ 1 2 σ 1 2 ​ ∥ y 1 ​ − f W ( x ) ∥ ​ 2 + 1 2 σ 2 2 ​ ∥ ​ y 2 ​ − f W ( x ) ∥ ​ 2 + l o g σ 1 ​ σ 2 ∝ {\frac{1}{2σ_1^2}​ }∥y_1​−\mathbf{f}^{\mathbf{W}} (x) ∥^​2 + \frac{1}{2σ_2^2}​ ∥​ y_2​ −\mathbf{f}^{\mathbf{W}} (x) ∥​^2 +logσ_1​ σ_2 ∝2 σ1 2 ​1 ​​∥y 1 ​​−f W (x )∥​2 +2 σ2 2 ​1 ​​∥​y 2 ​​−f W (x )∥​2 +l o g σ1 ​​σ2 ​

= 1 2 σ 1 2 ​ L 1 ​ ( W ) + 1 2 σ 2 2 ​ L 2 ​ ( W ) + l o g σ 1 ​ σ 2 ​ = \frac{1}{2σ_1^2}​ L_1​ (W)+ \frac{1}{2σ_2^2}​ L_2​ (W)+logσ_1​σ_2​=2 σ1 2 ​1 ​​L 1 ​​(W )+2 σ2 2 ​1 ​​L 2 ​​(W )+l o g σ1 ​​σ2 ​​

假设是一个回归任务和一个分类任务,仍然可以推导出上述结论:

= 1 2 σ 1 2 ​ L 1 ​ ( W ) + 1 2 σ 2 2 ​ L 2 ​ ( W ) + l o g σ 1 ​ σ 2 ​ = \frac{1}{2σ_1^2}​ L_1​ (W)+ \frac{1}{2σ_2^2}​ L_2​ (W)+logσ_1​σ_2​=2 σ1 2 ​1 ​​L 1 ​​(W )+2 σ2 2 ​1 ​​L 2 ​​(W )+l o g σ1 ​​σ2 ​​

所以,根据整个多任务问题的联合 Loss 形式,那么我们需要优化的参数不仅有 W W W 还有 σ 1 \sigma_{1}σ1 ​ 和 σ 2 \sigma_{2}σ2 ​ 。

各个公式的证明

【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics
【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics

; pytorch版代码实现

首先要明确,损失函数中的参数不仅有 W W W 还有 σ 1 \sigma_{1}σ1 ​ 和 σ 2 \sigma_{2}σ2 ​ 都是需要经过反向传播来学习的。

代码如下:


import math

import pylab
import numpy as np

import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader

def gen_data(N):
    X = np.random.randn(N, 1)
    w1 = 2.

    b1 = 8.

    sigma1 = 1e1
    Y1 = X.dot(w1) + b1 + sigma1 * np.random.randn(N, 1)
    w2 = 3
    b2 = 3.

    sigma2 = 1e0
    Y2 = X.dot(w2) + b2 + sigma2 * np.random.randn(N, 1)
    return X, Y1, Y2

class TrainData(Dataset):

    def __init__(self, feature_num, X, Y1, Y2):

        self.feature_num = feature_num

        self.X = torch.tensor(X, dtype=torch.float32)
        self.Y1 = torch.tensor(Y1, dtype=torch.float32)
        self.Y2 = torch.tensor(Y2, dtype=torch.float32)

    def __len__(self):
        return self.feature_num

    def __getitem__(self, idx):
        return self.X[idx,:], self.Y1[idx,:], self.Y2[idx,:]

class MultiTaskLossWrapper(nn.Module):
    def __init__(self, task_num, model):
        super(MultiTaskLossWrapper, self).__init__()
        self.model = model
        self.task_num = task_num
        self.log_vars = nn.Parameter(torch.zeros((task_num)))

    def forward(self, input, targets):

        outputs = self.model(input)

        precision1 = torch.exp(-self.log_vars[0])
        loss = torch.sum(precision1 * (targets[0] - outputs[0]) ** 2. + self.log_vars[0], -1)

        precision2 = torch.exp(-self.log_vars[1])
        loss += torch.sum(precision2 * (targets[1] - outputs[1]) ** 2. + self.log_vars[1], -1)

        loss = torch.mean(loss)

        return loss, self.log_vars.data.tolist()

class MTLModel(torch.nn.Module):
    def __init__(self, n_hidden, n_output):
        super(MTLModel, self).__init__()

        self.net1 = nn.Sequential(nn.Linear(1, n_hidden), nn.ReLU(), nn.Linear(n_hidden, n_output))
        self.net2 = nn.Sequential(nn.Linear(1, n_hidden), nn.ReLU(), nn.Linear(n_hidden, n_output))

    def forward(self, x):
        return [self.net1(x), self.net2(x)]
np.random.seed(0)

feature_num = 100
nb_epoch = 2000
batch_size = 20
hidden_dim = 1024

X, Y1, Y2 = gen_data(feature_num)
pylab.figure(figsize=(3, 1.5))
pylab.scatter(X[:, 0], Y1[:, 0])
pylab.scatter(X[:, 0], Y2[:, 0])
pylab.show()

train_data = TrainData(feature_num, X, Y1, Y2)
train_data_loader = DataLoader(train_data, shuffle=True, batch_size=batch_size)

model = MTLModel(hidden_dim, 1)

mtl = MultiTaskLossWrapper(2, model)
mtl

optimizer = torch.optim.Adam(mtl.parameters(), lr=0.001, eps=1e-07)

loss_list = []
for t in range(nb_epoch):
    cumulative_loss = 0

    for X, Y1, Y2 in train_data_loader:

        loss, log_vars = mtl(X, [Y1, Y2])

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        cumulative_loss += loss.item()

    loss_list.append(cumulative_loss/batch_size)
pylab.plot(loss_list)
pylab.show()

print(log_vars)
[4.2984442710876465, -0.2037072628736496]

print([math.exp(log_var) ** 0.5 for log_var in log_vars])
[8.578183137529612, 0.9031617364804738]

【注】假设 L o s s = a ∗ L 1 ( W ) + b ∗ L 2 ( W ) Loss = a * L_1(W) + b * L_2(W)L o s s =a ∗L 1 ​(W )+b ∗L 2 ​(W ),我们这样简单的学习参数a a a和b b b是不行的,这样Loss会越来越小,永无止境。我们需要设置L o s s Loss L o s s有正则项,使得它有最小值,这样学习才对。

Original: https://blog.csdn.net/qq_43592352/article/details/124715562
Author: 征途黯然.
Title: 【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/622969/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球