-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path_random.py
More file actions
47 lines (36 loc) · 1.7 KB
/
Copy path_random.py
File metadata and controls
47 lines (36 loc) · 1.7 KB
1
2
3
4
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
38
39
40
41
42
43
44
45
46
47
import torch
from torch import Tensor
from torch.nn import functional as F
from torchjd._linalg import Matrix
from ._aggregator_bases import WeightedAggregator
from ._mixins import Stochastic
from ._weighting_bases import Weighting
class RandomWeighting(Weighting[Matrix], Stochastic):
"""
:class:`~torchjd.aggregation._weighting_bases.Weighting` that generates positive random weights
at each call.
:param seed: Seed for the internal random number generator. If ``None``, a seed is drawn from
the global PyTorch RNG to fork an independent stream.
"""
def __init__(self, seed: int | None = None) -> None:
Weighting.__init__(self)
Stochastic.__init__(self, seed=seed)
def forward(self, matrix: Tensor, /) -> Tensor:
random_vector = torch.randn(
matrix.shape[0], device=matrix.device, dtype=matrix.dtype, generator=self.generator
)
weights = F.softmax(random_vector, dim=-1)
return weights
class Random(WeightedAggregator, Stochastic):
"""
:class:`~torchjd.aggregation._aggregator_bases.Aggregator` that computes a random combination of
the rows of the provided matrices, as defined in algorithm 2 of `Reasonable Effectiveness of
Random Weighting: A Litmus Test for Multi-Task Learning
<https://arxiv.org/pdf/2111.10603.pdf>`_.
:param seed: Seed for the internal random number generator. If ``None``, a seed is drawn from
the global PyTorch RNG to fork an independent stream.
"""
def __init__(self, seed: int | None = None) -> None:
weighting = RandomWeighting(seed=seed)
WeightedAggregator.__init__(self, weighting)
Stochastic.__init__(self, generator=weighting.generator)