|
| 1 | +Monitoring aggregations |
| 2 | +======================= |
| 3 | + |
| 4 | +The :doc:`Aggregator <../docs/aggregation/bases>` class is a subclass of :class:`torch.nn.Module`. |
| 5 | +This allows registering hooks, which can be used to monitor some information about aggregations. |
| 6 | +The following code example demonstrates registering a hook to compute and print the cosine |
| 7 | +similarity between the aggregation performed by :doc:`UPGrad <../docs/aggregation/upgrad>` and the |
| 8 | +average of the gradients, and another hook to compute and print the weights of the weighting of |
| 9 | +:doc:`UPGrad <../docs/aggregation/upgrad>`. |
| 10 | + |
| 11 | +Updating the parameters of the model with the average gradient is equivalent to using gradient |
| 12 | +descent on the average of the losses. Observing a cosine similarity smaller than 1 means that |
| 13 | +Jacobian descent is doing something different than gradient descent. With |
| 14 | +:doc:`UPGrad <../docs/aggregation/upgrad>`, this happens when the original gradients conflict (i.e. |
| 15 | +they have a negative inner product). |
| 16 | + |
| 17 | +.. code-block:: python |
| 18 | + :emphasize-lines: 9-11, 13-18, 33-34 |
| 19 | +
|
| 20 | + import torch |
| 21 | + from torch.nn import Linear, MSELoss, ReLU, Sequential |
| 22 | + from torch.optim import SGD |
| 23 | + from torch.nn.functional import cosine_similarity |
| 24 | +
|
| 25 | + from torchjd import mtl_backward |
| 26 | + from torchjd.aggregation import UPGrad |
| 27 | +
|
| 28 | + def print_weights(_, __, weights: torch.Tensor) -> None: |
| 29 | + """Prints the extracted weights.""" |
| 30 | + print(f"Weights: {weights}") |
| 31 | +
|
| 32 | + def print_similarity_with_gd(_, inputs: torch.Tensor, aggregation: torch.Tensor) -> None: |
| 33 | + """Prints the cosine similarity between the aggregation and the average gradient.""" |
| 34 | + matrix = inputs[0] |
| 35 | + gd_output = matrix.mean(dim=0) |
| 36 | + similarity = cosine_similarity(aggregation, gd_output, dim=0) |
| 37 | + print(f"Cosine similarity: {similarity.item():.4f}") |
| 38 | +
|
| 39 | + shared_module = Sequential(Linear(10, 5), ReLU(), Linear(5, 3), ReLU()) |
| 40 | + task1_module = Linear(3, 1) |
| 41 | + task2_module = Linear(3, 1) |
| 42 | + params = [ |
| 43 | + *shared_module.parameters(), |
| 44 | + *task1_module.parameters(), |
| 45 | + *task2_module.parameters(), |
| 46 | + ] |
| 47 | +
|
| 48 | + loss_fn = MSELoss() |
| 49 | + optimizer = SGD(params, lr=0.1) |
| 50 | + aggregator = UPGrad() |
| 51 | +
|
| 52 | + aggregator.weighting.register_forward_hook(print_weights) |
| 53 | + aggregator.register_forward_hook(print_similarity_with_gd) |
| 54 | +
|
| 55 | + inputs = torch.randn(8, 16, 10) # 8 batches of 16 random input vectors of length 10 |
| 56 | + task1_targets = torch.randn(8, 16, 1) # 8 batches of 16 targets for the first task |
| 57 | + task2_targets = torch.randn(8, 16, 1) # 8 batches of 16 targets for the second task |
| 58 | +
|
| 59 | + for input, target1, target2 in zip(inputs, task1_targets, task2_targets): |
| 60 | + features = shared_module(input) |
| 61 | + output1 = task1_module(features) |
| 62 | + output2 = task2_module(features) |
| 63 | + loss1 = loss_fn(output1, target1) |
| 64 | + loss2 = loss_fn(output2, target2) |
| 65 | +
|
| 66 | + optimizer.zero_grad() |
| 67 | + mtl_backward(losses=[loss1, loss2], features=features, aggregator=aggregator) |
| 68 | + optimizer.step() |
0 commit comments