Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add document for loss weight learning #441

Merged
merged 20 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@

version: 2

build:
os: ubuntu-22.04
tools:
python: "3.7"

sphinx:
configuration: docs/source/conf.py

python:
version: "3.7"
install:
- requirements: requirements/runtime.txt
- requirements: requirements/docs.txt
Expand Down
50 changes: 29 additions & 21 deletions docs/source/train.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,31 +236,39 @@ EasyRec支持两种损失函数配置方式:1)使用单个损失函数;2
多目标学习任务中,人工指定多个损失函数的静态权重通常不能获得最好的效果。EasyRec支持损失函数权重自适应学习,示例如下:

```protobuf
losses {
loss_type: CLASSIFICATION
learn_loss_weight: true
}
losses {
loss_type: BINARY_FOCAL_LOSS
learn_loss_weight: true
binary_focal_loss {
gamma: 2.0
alpha: 0.85
}
}
losses {
loss_type: PAIRWISE_FOCAL_LOSS
learn_loss_weight: true
pairwise_focal_loss {
session_name: "client_str"
hinge_margin: 1.0
}
}
loss_weight_strategy: Uncertainty
losses {
loss_type: CLASSIFICATION
learn_loss_weight: true
}
losses {
loss_type: BINARY_FOCAL_LOSS
learn_loss_weight: true
binary_focal_loss {
gamma: 2.0
alpha: 0.85
}
}
losses {
loss_type: PAIRWISE_FOCAL_LOSS
learn_loss_weight: true
pairwise_focal_loss {
session_name: "client_str"
hinge_margin: 1.0
}
}
```

通过`learn_loss_weight`参数配置是否需要开启权重自适应学习,默认不开启。开启之后,`weight`参数不再生效。

参考论文:《Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics》
- loss_weight_strategy: Uncertainty
- 表示通过不确定性来度量损失函数的权重;目前在`learn_loss_weight: true`时必须要设置该值
- loss_weight_strategy: Random
- 表示损失函数的权重设定为归一化的随机数

参考论文:
- 《 Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics 》
- 《 [Reasonable Effectiveness of Random Weighting: A Litmus Test for Multi-Task Learning](https://arxiv.org/abs/2111.10603) 》

## 训练命令

Expand Down
6 changes: 4 additions & 2 deletions easy_rec/python/layers/common_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,17 @@ def call(self, config, training):
ln_name = self._group_name + 'f_%d' % i
fea = layer_norm(fea, name=ln_name, reuse=self._reuse)
if do_dropout and output_feature_list:
fea = self.dropout.apply(fea, training=training)
fea = self.dropout.call(fea, training=training)
if do_feature_dropout:
fea = tf.div(fea, keep_prob) * mask[i]
feature_list[i] = fea
if do_feature_dropout:
features = tf.concat(feature_list, axis=-1)

if do_dropout and not do_feature_dropout:
features = self.dropout.apply(features, training=training)
features = self.dropout.call(features, training=training)
if features.shape.ndims == 3 and int(features.shape[0]) == 1:
features = tf.squeeze(features, axis=0)

if config.only_output_feature_list:
return feature_list
Expand Down
4 changes: 3 additions & 1 deletion easy_rec/python/model/multi_task_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from easy_rec.python.model.rank_model import RankModel
from easy_rec.python.protos import tower_pb2
from easy_rec.python.protos.loss_pb2 import LossType
from easy_rec.python.protos.easy_rec_model_pb2 import EasyRecModel

if tf.__version__ >= '2.0':
tf = tf.compat.v1
Expand Down Expand Up @@ -188,7 +189,8 @@ def get_learnt_loss(self, loss_type, name, value):
else:
return tf.exp(-uncertainty) * value + 0.5 * uncertainty
else:
raise ValueError('Unsupported loss weight strategy: ' + strategy.Name)
strategy_name = EasyRecModel.LossWeightStrategy.Name(strategy)
raise ValueError('Unsupported loss weight strategy: ' + strategy_name)

def build_loss_graph(self):
"""Build loss graph for multi task model."""
Expand Down
6 changes: 5 additions & 1 deletion easy_rec/python/tools/feature_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ def _feature_dim_dropout_ratio(self):
group_name = feature_group.group_name

logit_p_name = 'logit_p' if group_name == 'all' else 'logit_p_%s' % group_name
logit_p = reader.get_tensor(logit_p_name)
try:
logit_p = reader.get_tensor(logit_p_name)
except Exception:
print('get `logit_p` failed, try to get `backbone/logit_p`')
logit_p = reader.get_tensor('backbone/' + logit_p_name)
feature_dims_importance = tf.sigmoid(logit_p)
with tf.Session() as sess:
feature_dims_importance = feature_dims_importance.eval(session=sess)
Expand Down
Loading