首页 > 其他分享 >【多任务学习】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

时间:2023-01-16 18:01:24浏览次数:59  
标签:__ loss Multi Task nn Geometry self torch num


·阅读摘要:
  本文提出针对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)不确定性

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


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

【多任务学习】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_多任务_02为该项输出的权重,则对于回归任务有:
【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_多任务_03
  对于分类任务有:
【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_多任务_04
  多任务的概率:
【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_深度学习_05
  例如对于回归任务来说,极大似然估计转化为最小化负对数:
【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_深度学习_06
【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_机器学习_07

  双任务:

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

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

【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_机器学习_09,取最小化负对数:

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

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

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

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

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

【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_多任务_02 还有 【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_学习_15【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_多任务_16

各个公式的证明

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


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

pytorch版代码实现

【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_多任务_02 还有 【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_学习_15【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_多任务_16

  代码如下:

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 # ground truth
Y1 = X.dot(w1) + b1 + sigma1 * np.random.randn(N, 1)
w2 = 3
b2 = 3.
sigma2 = 1e0 # ground truth
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

# https://github.com/keras-team/keras/blob/master/keras/optimizers.py
# k.epsilon() = keras.backend.epsilon()
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]
# Found standard deviations (ground truth is 10 and 1):
print([math.exp(log_var) ** 0.5 for log_var in log_vars])
[8.578183137529612, 0.9031617364804738]

【注】假设 【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_方差_22,我们这样简单的学习参数【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_方差_23【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_多任务_24是不行的,这样Loss会越来越小,永无止境。我们需要设置【多任务学习】Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics_多任务_25有正则项,使得它有最小值,这样学习才对。


标签:__,loss,Multi,Task,nn,Geometry,self,torch,num
From: https://blog.51cto.com/u_15942590/6010681

相关文章

  • 【多标签文本分类】Balancing Methods for Multi-label Text Classification with Lon
    ·阅读摘要:  本文更像是对多标签文本分类的损失函数的综述,文中提到的几个损失函数(包括为了解决长尾问题的损失函数)都是前人已经提出的。·参考文献:  [1]BalancingM......
  • ORBSLAM-Atlas: a robust and accurate multi-map system
    摘要-我们提出ORBSLAM-Atlas,该系统能够处理无限制的非连接的子图的数量,其包括一个鲁棒的地图合并算法能够检测子图之间的公共区域并无缝融合他们。ORBSLAM算法杰出的鲁棒性......
  • Embracing Domain Differences in Fake News- Cross-domain Fake News Detection usin
    一、摘要随着社交媒体的快速发展,假新闻已经成为一个重大的社会问题,它无法通过人工调查及时解决。这激发了大量关于自动假新闻检测的研究。大多数研究探索了基于新闻记录......
  • 动手学数据分析 -- Task01_3
    复习:在前面我们已经学习了Pandas基础,知道利用Pandas读取csv数据的增删查改,今天我们要学习的就是探索性数据分析,主要介绍如何利用Pandas进行排序、算术计算以及计算描述函数......
  • 动手学数据分析 -- Task02
    复习:数据分析的第一步,加载数据我们已经学习完毕了。当数据展现在我们面前的时候,我们所要做的第一步就是认识他,今天我们要学习的就是了解字段含义以及初步观察数据。1第一......
  • WPF StreamGeometry
    使用路径标记语法StreamGeometry来设计最大化最小化恢复和关闭按钮,以下为恢复按钮的路径,和拿邮件系统按钮做了对比,完全吻合前一张图片设置了不透明度为40后一张为邮......
  • 动手学数据分析 -- Task01
    复习:这门课程得主要目的是通过真实的数据,以实战的方式了解数据分析的流程和熟悉数据分析python的基本操作。知道了课程的目的之后,我们接下来我们要正式的开始数据分析的实......
  • Execution failed for task ':app:checkDebugDuplicateClasses'解决办法
    Afailureoccurredwhileexecutingcom.android.build.gradle.internal.tasks.CheckDuplicatesRunnable >Duplicateclassandroid.support.v4.app.INotificationSi......
  • ORA-06502: PL/SQL: 'Numeric or Value Error' When CLOB Convert to VARCHAR2 on a M
    OnadatabasewithmultibytecharactersetlikeAL32UTF8specifiedforNLS_CHARACTERSETthefollowingerrorisreceivedwhenaCLOBwhichcontainsmorethan819......
  • postgis:通过命令创建几何图形-geometry
    postgis:通过命令创建几何图形-geometry已被阅读1516次 | 文章分类:geoserver | 2019-12-2322:32可以利用数据库的查询工具创建空间数据表一:执行查看结......