MTL¶
minnormsolver
¶
This script includes code adapted from the 'impartial-vaes' repository with minor modifications. The original code can be found at: https://github.com/adrianjav/impartial-vaes
Credit to the original authors: Adrian Javaloy, Maryam Meghdadi, and Isabel Valera for their valuable work.
MinNormLinearSolver
¶
Bases: Module
Solves the min norm problem in case of 2 vectors (lies on a line).
Source code in vambn/modelling/mtl/minnormsolver.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
forward(v1v1, v1v2, v2v2)
¶
Solver execution on scalar products of 2 vectors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v1v1 |
float
|
Scalar product |
required |
v1v2 |
float
|
Scalar product |
required |
v2v2 |
float
|
Scalar product |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
A tuple containing: - gamma (float): Min-norm solution c = (gamma, 1. - gamma). - cost (float): The norm of min-norm point. |
Source code in vambn/modelling/mtl/minnormsolver.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
MinNormPlanarSolver
¶
Bases: Module
Solves the min norm problem in case the vectors lie on the same plane.
Source code in vambn/modelling/mtl/minnormsolver.py
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 |
|
__init__(n_tasks)
¶
Initializes the MinNormPlanarSolver.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_tasks |
int
|
Number of tasks/vectors. |
required |
Source code in vambn/modelling/mtl/minnormsolver.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
forward(grammian)
¶
Planar case solver, when Vi lies on the same plane.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grammian |
Tensor
|
Grammian matrix G[i, j] = [ |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Coefficients c = [c1, ... cn] that solves the min-norm problem. |
Source code in vambn/modelling/mtl/minnormsolver.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
line_solver_vectorized(v1v1, v1v2, v2v2)
¶
Linear case solver, but for collection of vector pairs (Vi, Vj).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v1v1 |
Tensor
|
Vector of scalar products |
required |
v1v2 |
Tensor
|
Vector of scalar products |
required |
v2v2 |
Tensor
|
Vector of scalar products |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
A tuple containing: - gamma (Tensor): Vector of min-norm solution c = (gamma, 1. - gamma). - cost (Tensor): Vector of the norm of min-norm point. |
Source code in vambn/modelling/mtl/minnormsolver.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
MinNormSolver
¶
Bases: Module
Solves the min norm problem in the general case.
Source code in vambn/modelling/mtl/minnormsolver.py
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
__init__(n_tasks, max_iter=250, stop_crit=1e-06)
¶
Initializes the MinNormSolver.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_tasks |
int
|
Number of tasks/vectors. |
required |
max_iter |
int
|
Maximum number of iterations. Defaults to 250. |
250
|
stop_crit |
float
|
Stopping criterion. Defaults to 1e-6. |
1e-06
|
Source code in vambn/modelling/mtl/minnormsolver.py
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 |
|
forward(vecs)
¶
General case solver using simplex projection algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vecs |
Tensor
|
2D tensor V, where each row is a vector Vi. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Coefficients c = [c1, ... cn] that solves the min-norm problem. |
Source code in vambn/modelling/mtl/minnormsolver.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
next_point(cur_val, grad)
¶
Computes the next point in the optimization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cur_val |
Tensor
|
Current value. |
required |
grad |
Tensor
|
Gradient. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
The next point. |
Source code in vambn/modelling/mtl/minnormsolver.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
projection_to_simplex(gamma)
¶
Projects gamma to the simplex.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gamma |
Tensor
|
The input tensor to project. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
The projected tensor. |
Source code in vambn/modelling/mtl/minnormsolver.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
moo
¶
This script includes code adapted from the 'impartial-vaes' repository with minor modifications. The original code can be found at: https://github.com/adrianjav/impartial-vaes
Credit to the original authors: Adrian Javaloy, Maryam Meghdadi, and Isabel Valera for their valuable work.
MOOForLoop
¶
Bases: Module
A PyTorch Module for Multiple Objective Optimization (MOO) within a loop.
Source code in vambn/modelling/mtl/moo.py
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 162 163 164 165 166 167 168 169 170 171 172 |
|
moo_method
property
¶
Get the MOO method.
__init__(num_heads, moo_method=None)
¶
Initialize the MOOForLoop module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_heads |
int
|
Number of heads for extending the input. |
required |
moo_method |
Module
|
The MOO method to be used. Default is None. |
None
|
Source code in vambn/modelling/mtl/moo.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
forward(z)
¶
Forward pass. Extend the input to the number of heads and store it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
z |
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Extended input tensor. |
Source code in vambn/modelling/mtl/moo.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
MooMulti
¶
Bases: Module
A PyTorch Module for Multiple Objective Optimization (MOO) within a loop.
Source code in vambn/modelling/mtl/moo.py
23 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 |
|
moo_method
property
¶
Get the MOO method.
__init__(num_modules, moo_method=None)
¶
Initialize the MooMulti module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_modules |
int
|
Number of heads for extending the input. |
required |
moo_method |
Module
|
The MOO method to be used. Default is None. |
None
|
Source code in vambn/modelling/mtl/moo.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
forward(z)
¶
Forward pass. Extend the input to the number of heads and store it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
z |
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Extended input tensor. |
Source code in vambn/modelling/mtl/moo.py
87 88 89 90 91 92 93 94 95 96 97 |
|
MultiMOOForLoop
¶
Bases: Module
A PyTorch Module for applying multiple MOOForLoop modules in parallel.
Source code in vambn/modelling/mtl/moo.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
__init__(num_heads, moo_methods)
¶
Initialize the MultiMOOForLoop module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_heads |
int
|
Number of heads for each MOOForLoop. |
required |
moo_methods |
Sequence[Module]
|
List of MOO methods to be used. |
required |
Source code in vambn/modelling/mtl/moo.py
178 179 180 181 182 183 184 185 186 187 188 189 |
|
forward(*args)
¶
Forward pass. Applies each MOOForLoop to its corresponding input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
Tensor
|
Variable number of input tensors. |
()
|
Returns:
Name | Type | Description |
---|---|---|
Generator |
Generator[Tensor, None, None]
|
A generator of extended input tensors after applying MOOForLoop. |
Source code in vambn/modelling/mtl/moo.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
setup_moo(hparams, num_tasks)
¶
Setup the multi-task learning module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hparams |
List[MtlMethodParams]
|
MTL method parameters. |
required |
num_tasks |
int
|
Number of tasks to perform. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If invalid method name is provided. |
Returns:
Type | Description |
---|---|
Module
|
nn.Module: Module for MTL objective. |
Source code in vambn/modelling/mtl/moo.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
mtl
¶
This script includes code adapted from the 'impartial-vaes' repository with minor modifications. The original code can be found at: https://github.com/adrianjav/impartial-vaes
Credit to the original authors: Adrian Javaloy, Maryam Meghdadi, and Isabel Valera for their valuable work.
CAGrad
¶
Bases: MOOMethod
CAGrad method for multiple objective optimization.
Source code in vambn/modelling/mtl/mtl.py
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 |
|
__init__(alpha)
¶
Initialize CAGrad method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
float
|
Alpha parameter for CAGrad. |
required |
Source code in vambn/modelling/mtl/mtl.py
840 841 842 843 844 845 846 847 848 |
|
forward(grads, inputs, outputs)
¶
Compute new gradients using CAGrad method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Gradients tensor. |
required |
inputs |
Tensor
|
Input tensor. |
required |
outputs |
Tensor
|
Output tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
New gradients tensor. |
Source code in vambn/modelling/mtl/mtl.py
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 |
|
Compose
¶
Bases: MOOMethod
Compose multiple MOO methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
modules |
MOOMethod
|
List of MOO methods to compose. |
()
|
Attributes:
Name | Type | Description |
---|---|---|
methods |
ModuleList
|
List of MOO methods. |
requires_input |
bool
|
Flag indicating if input is required. |
Source code in vambn/modelling/mtl/mtl.py
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 |
|
forward(grads, inputs, outputs)
¶
Apply composed MOO methods sequentially.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Gradients tensor. |
required |
inputs |
Tensor
|
Input tensor. |
required |
outputs |
Tensor
|
Output tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Modified gradients. |
Source code in vambn/modelling/mtl/mtl.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
GradDrop
¶
Bases: MOOMethod
Gradient Dropout (GradDrop) method for MOO.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
leakage |
List[float]
|
List of leakage rates for each task. |
required |
Attributes:
Name | Type | Description |
---|---|---|
leakage |
List[float]
|
List of leakage rates for each task. |
Source code in vambn/modelling/mtl/mtl.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
|
__init__(leakage)
¶
Initialize GradDrop method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
leakage |
List[float]
|
List of leakage rates for each task. |
required |
Raises:
Type | Description |
---|---|
AssertionError
|
If any leakage rate is not in the range [0, 1]. |
Source code in vambn/modelling/mtl/mtl.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
forward(grads, inputs, outputs)
¶
Compute new gradients using GradDrop method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Gradients tensor. |
required |
inputs |
Tensor
|
Input tensor. |
required |
outputs |
Tensor
|
Output tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: New gradients tensor. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the number of leakage parameters does not match the number of task gradients. |
Source code in vambn/modelling/mtl/mtl.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
|
GradNorm
¶
Bases: GradNormBase
Gradient Normalization (GradNorm) method for MOO.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
GradNormBase |
class
|
Base class for GradNorm. |
required |
Attributes:
Name | Type | Description |
---|---|---|
requires_input |
bool
|
Flag indicating whether input is required. |
Methods:
Name | Description |
---|---|
forward |
Compute new gradients using GradNorm method. |
Source code in vambn/modelling/mtl/mtl.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
|
forward(grads, inputs, outputs)
¶
Compute new gradients using GradNorm method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Gradients tensor. |
required |
inputs |
Tensor
|
Input tensor. |
required |
outputs |
Tensor
|
Output tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: New gradients tensor. |
Source code in vambn/modelling/mtl/mtl.py
445 446 447 448 449 450 451 452 453 454 455 456 457 |
|
GradNormBase
¶
Bases: MOOMethod
Base class for Gradient Normalization (GradNorm) method.
Source code in vambn/modelling/mtl/mtl.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
|
weight: torch.Tensor
property
¶
Compute normalized weights.
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Normalized weights. |
__init__(num_tasks, alpha, update_at=20)
¶
Initialize GradNormBase method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_tasks |
int
|
Number of tasks. |
required |
alpha |
float
|
Alpha parameter for GradNorm. |
required |
update_at |
int
|
Update interval. |
20
|
Source code in vambn/modelling/mtl/mtl.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
|
GradNormModified
¶
Bases: GradNormBase
Modified Gradient Normalization (GradNorm) method for MOO.
Uses task-gradient convergence instead of task loss convergence.
Attributes:
Name | Type | Description |
---|---|---|
requires_input |
bool
|
Indicates whether the method requires input tensor. |
Methods:
Name | Description |
---|---|
forward |
Compute new gradients using modified GradNorm method. |
Source code in vambn/modelling/mtl/mtl.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
|
forward(grads, inputs, outputs)
¶
Compute new gradients using modified GradNorm method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Gradients tensor. |
required |
inputs |
Tensor
|
Input tensor. |
required |
outputs |
Tensor
|
Output tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: New gradients tensor. |
Source code in vambn/modelling/mtl/mtl.py
476 477 478 479 480 481 482 483 484 485 486 487 488 |
|
GradVac
¶
Bases: MOOMethod
Gradient Vaccination (GradVac) method for MOO.
Source code in vambn/modelling/mtl/mtl.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 |
|
__init__(decay)
¶
Initialize GradVac method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
decay |
float
|
Decay rate for EMA. |
required |
Source code in vambn/modelling/mtl/mtl.py
536 537 538 539 540 541 542 543 544 |
|
forward(grads, inputs, outputs)
¶
Compute new gradients using GradVac method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Gradients tensor. |
required |
inputs |
Tensor
|
Input tensor. |
required |
outputs |
Tensor
|
Output tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
New gradients tensor. |
Source code in vambn/modelling/mtl/mtl.py
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 |
|
IMTLG
¶
Bases: MOOMethod
IMTLG method for multiple objective optimization.
Source code in vambn/modelling/mtl/mtl.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
forward(grads, inputs, outputs)
¶
Compute new gradients using IMTLG method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Gradients tensor. |
required |
inputs |
Tensor
|
Input tensor. |
required |
outputs |
Tensor
|
Output tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: New gradients tensor. |
Source code in vambn/modelling/mtl/mtl.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
Identity
¶
Bases: MOOMethod
Identity MOO method that returns the input gradients unchanged.
Source code in vambn/modelling/mtl/mtl.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
forward(grads, inputs, outputs)
¶
Return the input gradients unchanged.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Input gradients. |
required |
inputs |
Tensor
|
Input tensor. |
required |
outputs |
Tensor
|
Output tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Unchanged input gradients. |
Source code in vambn/modelling/mtl/mtl.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
MGDAUB
¶
Bases: MOOMethod
MGDA-UB method for multiple objective optimization.
Source code in vambn/modelling/mtl/mtl.py
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 |
|
forward(grads, inputs, outputs)
¶
Compute new gradients using MGDA-UB method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Gradients tensor. |
required |
inputs |
Tensor
|
Input tensor. |
required |
outputs |
Tensor
|
Output tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: New gradients tensor. |
Source code in vambn/modelling/mtl/mtl.py
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 |
|
MOOMethod
¶
Bases: Module
Base class for multiple objective optimization (MOO) methods.
Source code in vambn/modelling/mtl/mtl.py
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 |
|
forward(grads, inputs, outputs)
abstractmethod
¶
Computes the new task gradients based on the original ones.
Given K gradients of size D, returns a new set of K gradients of size D based on some criterion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Tensor of size K x D with the different gradients. |
required |
inputs |
Tensor
|
Tensor with the input of the forward pass (if requires_input is set to True). |
required |
outputs |
Tensor
|
Tensor with the K outputs of the module (not used currently). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: A tensor of the same size as |
Source code in vambn/modelling/mtl/mtl.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
MinNormSolver
¶
Solver for finding the minimum norm solution in the convex hull of vectors.
Source code in vambn/modelling/mtl/mtl.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
|
find_min_norm_element(vecs)
staticmethod
¶
Find the minimum norm element in the convex hull of vectors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vecs |
List
|
List of vectors. |
required |
Returns:
Type | Description |
---|---|
Tuple | None
|
Minimum norm element and its cost. |
Source code in vambn/modelling/mtl/mtl.py
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
|
MtlMethods
¶
Bases: Enum
Enumeration of available multi-task learning methods.
Source code in vambn/modelling/mtl/mtl.py
902 903 904 905 906 907 908 909 910 911 912 |
|
NSGD
¶
Bases: MOOMethod
Normalized Stochastic Gradient Descent (NSGD) method for MOO.
Source code in vambn/modelling/mtl/mtl.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
__init__(num_tasks, update_at=20)
¶
Initialize NSGD method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_tasks |
int
|
Number of tasks. |
required |
update_at |
int
|
Update interval. |
20
|
Source code in vambn/modelling/mtl/mtl.py
226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
forward(grads, inputs, outputs)
¶
Compute new gradients using NSGD method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Gradients tensor. |
required |
inputs |
Tensor
|
Input tensor. |
required |
outputs |
Tensor
|
Output tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: New gradients tensor. |
Source code in vambn/modelling/mtl/mtl.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
PCGrad
¶
Bases: MOOMethod
Projected Conflicting Gradient (PCGrad) method for MOO.
Attributes:
Name | Type | Description |
---|---|---|
requires_input |
bool
|
Indicates whether the method requires input tensor. |
Source code in vambn/modelling/mtl/mtl.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
|
forward(grads, inputs, outputs)
¶
Compute new gradients using PCGrad method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
Tensor
|
Gradients tensor. |
required |
inputs |
Tensor
|
Input tensor. |
required |
outputs |
Tensor
|
Output tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: New gradients tensor. |
Source code in vambn/modelling/mtl/mtl.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
|
divide(numer, denom)
¶
Numerically stable division.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
numer |
Tensor
|
Numerator tensor. |
required |
denom |
Tensor
|
Denominator tensor. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor: Result of numerically stable division. |
Source code in vambn/modelling/mtl/mtl.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
gradient_normalizers(grads, losses, normalization_type)
¶
Compute gradient normalizers based on the specified normalization type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grads |
dict
|
A dictionary of gradients. |
required |
losses |
dict
|
A dictionary of losses. |
required |
normalization_type |
str
|
The type of normalization ('l2', 'loss', 'loss+', 'none'). |
required |
Returns:
Type | Description |
---|---|
dict
|
A dictionary of gradient normalizers. |
Source code in vambn/modelling/mtl/mtl.py
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 |
|
norm(tensor)
¶
Compute the L2 norm of a tensor along the last dimension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor |
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor: L2 norm of the input tensor. |
Source code in vambn/modelling/mtl/mtl.py
24 25 26 27 28 29 30 31 32 33 34 |
|
projection(u, v)
¶
Project vector u onto vector v.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
u |
Tensor
|
Vector to be projected. |
required |
v |
Tensor
|
Vector onto which u is projected. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor: Projection of u onto v. |
Source code in vambn/modelling/mtl/mtl.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
unitary(tensor)
¶
Normalize the tensor to unit norm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor |
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor: Unitary (normalized) tensor. |
Source code in vambn/modelling/mtl/mtl.py
58 59 60 61 62 63 64 65 66 67 68 |
|
parameters
¶
MtlMethodParams
dataclass
¶
Params and method description for multi-task learning.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Name of the MTL method. |
update_at |
Optional[int]
|
Update interval, specific to certain methods. |
alpha |
Optional[float]
|
Alpha parameter, specific to certain methods. |
Source code in vambn/modelling/mtl/parameters.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
__post_init__()
¶
Post-initialization to set default values for specific methods.
Source code in vambn/modelling/mtl/parameters.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
utils
¶
This script includes code adapted from the 'impartial-vaes' repository with minor modifications. The original code can be found at: https://github.com/adrianjav/impartial-vaes
Credit to the original authors: Adrian Javaloy, Maryam Meghdadi, and Isabel Valera for their valuable work.
batch_product(batch, weight)
¶
Multiplies each slice of the first dimension of batch by the corresponding scalar in the weight vector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch |
Tensor
|
Tensor of size [B, ...]. |
required |
weight |
Tensor
|
Tensor of size [B]. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor: A tensor such that |
Source code in vambn/modelling/mtl/utils.py
14 15 16 17 18 19 20 21 22 23 24 25 26 |
|