Loss.mtl(多任务学习) 模块¶
ppsci.loss.mtl
¶
AGDA
¶
Bases: LossAggregator
Adaptive Gradient Descent Algorithm
NOTE: This loss aggregator is only suitable for two-task learning and the first task loss must be PDE loss.
Attributes:
| Name | Type | Description |
|---|---|---|
should_persist |
bool
|
Whether to persist the loss aggregator when saving. Those loss aggregators with parameters and/or buffers should be persisted. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Layer
|
Training model. |
required |
M
|
int
|
Smoothing period. Defaults to 100. |
100
|
gamma
|
float
|
Smooth factor. Defaults to 0.999. |
0.999
|
Examples:
>>> import paddle
>>> from ppsci.loss import mtl
>>> model = paddle.nn.Linear(3, 4)
>>> loss_aggregator = mtl.AGDA(model)
>>> for i in range(5):
... x1 = paddle.randn([8, 3])
... x2 = paddle.randn([8, 3])
... y1 = model(x1)
... y2 = model(x2)
... pde_loss = paddle.sum(y1)
... bc_loss = paddle.sum((y2 - 2) ** 2)
... loss_aggregator({'pde_loss': pde_loss, 'bc_loss': bc_loss}).backward()
Source code in ppsci/loss/mtl/agda.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
GradNorm
¶
Bases: LossAggregator
GradNorm loss weighting algorithm.
reference: https://github.com/PredictiveIntelligenceLab/jaxpi/blob/main/jaxpi/models.py#L132-L146
Attributes:
| Name | Type | Description |
|---|---|---|
should_persist |
bool
|
Whether to persist the loss aggregator when saving. Those loss aggregators with parameters and/or buffers should be persisted. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Layer
|
Training model. |
required |
num_losses
|
int
|
Number of losses. Defaults to 1. |
1
|
update_freq
|
int
|
Weight updating frequency. Defaults to 1000. |
1000
|
momentum
|
float
|
Momentum \(m\) for moving weight. Defaults to 0.9. |
0.9
|
init_weights
|
List[float]
|
Initial weights list. Defaults to None. |
None
|
Examples:
>>> import paddle
>>> from ppsci.loss import mtl
>>> model = paddle.nn.Linear(3, 4)
>>> loss_aggregator = mtl.GradNorm(model, num_losses=2)
>>> for i in range(5):
... x1 = paddle.randn([8, 3])
... x2 = paddle.randn([8, 3])
... y1 = model(x1)
... y2 = model(x2)
... loss1 = paddle.sum(y1)
... loss2 = paddle.sum((y2 - 2) ** 2)
... loss_aggregator({'loss1': loss1, 'loss2': loss2}).backward()
Source code in ppsci/loss/mtl/grad_norm.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
LossAggregator
¶
Bases: Layer
Base class of loss aggregator mainly for multitask learning.
Attributes:
| Name | Type | Description |
|---|---|---|
should_persist |
bool
|
Whether to persist the loss aggregator when saving. Those loss aggregators with parameters and/or buffers should be persisted. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Layer
|
Training model. |
required |
Source code in ppsci/loss/mtl/base.py
PCGrad
¶
Bases: LossAggregator
Projecting Conflicting Gradients
Gradient Surgery for Multi-Task Learning
Code reference: https://github.com/tianheyu927/PCGrad/blob/master/PCGrad_tf.py
Attributes:
| Name | Type | Description |
|---|---|---|
should_persist |
bool
|
Whether to persist the loss aggregator when saving. Those loss aggregators with parameters and/or buffers should be persisted. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Layer
|
Training model. |
required |
Examples:
>>> import paddle
>>> from ppsci.loss import mtl
>>> model = paddle.nn.Linear(3, 4)
>>> loss_aggregator = mtl.PCGrad(model)
>>> for i in range(5):
... x1 = paddle.randn([8, 3])
... x2 = paddle.randn([8, 3])
... y1 = model(x1)
... y2 = model(x2)
... loss1 = paddle.sum(y1)
... loss2 = paddle.sum((y2 - 2) ** 2)
... loss_aggregator({'loss1': loss1, 'loss2': loss2}).backward()
Source code in ppsci/loss/mtl/pcgrad.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
Relobralo
¶
Bases: Layer
Relative Loss Balancing with Random Lookback
Multi-Objective Loss Balancing for Physics-Informed Deep Learning
Attributes:
| Name | Type | Description |
|---|---|---|
should_persist |
bool
|
Whether to persist the loss aggregator when saving. Those loss aggregators with parameters and/or buffers should be persisted. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_losses
|
int
|
Number of losses. |
required |
alpha
|
float
|
Ability for remembering past in paper. Defaults to 0.95. |
0.95
|
beta
|
float
|
Parameter for generating \(\rho\) from bernoulli distribution, and \(E[\rho](=\beta)\) should be close to 1. Defaults to 0.99. |
0.99
|
tau
|
float
|
Temperature factor. Equivalent to softmax when \(\tau\)=1.0, equivalent to argmax when \(\tau\)=0. Defaults to 1.0. |
1.0
|
eps
|
float
|
\(\epsilon\) to avoid divided by 0 in losses. Defaults to 1e-8. |
1e-08
|
Examples:
>>> import paddle
>>> from ppsci.loss import mtl
>>> model = paddle.nn.Linear(3, 4)
>>> loss_aggregator = mtl.Relobralo(num_losses=2)
>>> for i in range(5):
... x1 = paddle.randn([8, 3])
... x2 = paddle.randn([8, 3])
... y1 = model(x1)
... y2 = model(x2)
... loss1 = paddle.sum(y1)
... loss2 = paddle.sum((y2 - 2) ** 2)
... loss_aggregator({'loss1': loss1, 'loss2': loss2}).backward()
Source code in ppsci/loss/mtl/relobralo.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
Sum
¶
Bases: LossAggregator
Default loss aggregator which do simple summation for given losses as below.
Attributes:
| Name | Type | Description |
|---|---|---|
should_persist |
bool
|
Whether to persist the loss aggregator when saving. Those loss aggregators with parameters and/or buffers should be persisted. |