|
| 1 | +# The code of this file was partly adapted from |
| 2 | +# https://github.com/tum-pbs/ConFIG/tree/main/conflictfree. |
| 3 | +# It is therefore also subject to the following license. |
| 4 | +# |
| 5 | +# MIT License |
| 6 | +# |
| 7 | +# Copyright (c) 2024 TUM Physics-based Simulation |
| 8 | +# |
| 9 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +# of this software and associated documentation files (the "Software"), to deal |
| 11 | +# in the Software without restriction, including without limitation the rights |
| 12 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +# copies of the Software, and to permit persons to whom the Software is |
| 14 | +# furnished to do so, subject to the following conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be included in all |
| 17 | +# copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +# SOFTWARE. |
| 26 | + |
| 27 | + |
| 28 | +import torch |
| 29 | +from torch import Tensor |
| 30 | + |
| 31 | +from torchjd.aggregation._pref_vector_utils import ( |
| 32 | + _check_pref_vector, |
| 33 | + _pref_vector_to_str_suffix, |
| 34 | + _pref_vector_to_weighting, |
| 35 | +) |
| 36 | +from torchjd.aggregation.bases import Aggregator |
| 37 | +from torchjd.aggregation.sum import _SumWeighting |
| 38 | + |
| 39 | + |
| 40 | +class ConFIG(Aggregator): |
| 41 | + """ |
| 42 | + :class:`~torchjd.aggregation.bases.Aggregator` as defined in Equation 2 of `ConFIG: Towards |
| 43 | + Conflict-free Training of Physics Informed Neural Networks <https://arxiv.org/pdf/2408.11104>`_. |
| 44 | +
|
| 45 | + :param pref_vector: The preference vector used to weight the rows. If not provided, defaults to |
| 46 | + equal weights of 1. |
| 47 | +
|
| 48 | + .. admonition:: |
| 49 | + Example |
| 50 | +
|
| 51 | + Use ConFIG to aggregate a matrix. |
| 52 | +
|
| 53 | + >>> from torch import tensor |
| 54 | + >>> from torchjd.aggregation import ConFIG |
| 55 | + >>> |
| 56 | + >>> A = ConFIG() |
| 57 | + >>> J = tensor([[-4., 1., 1.], [6., 1., 1.]]) |
| 58 | + >>> |
| 59 | + >>> A(J) |
| 60 | + tensor([0.1588, 2.0706, 2.0706]) |
| 61 | +
|
| 62 | + .. note:: |
| 63 | + This implementation was adapted from the `official implementation |
| 64 | + <https://github.com/tum-pbs/ConFIG/tree/main/conflictfree>`_. |
| 65 | + """ |
| 66 | + |
| 67 | + def __init__(self, pref_vector: Tensor | None = None): |
| 68 | + super().__init__() |
| 69 | + _check_pref_vector(pref_vector) |
| 70 | + self.weighting = _pref_vector_to_weighting(pref_vector, default=_SumWeighting()) |
| 71 | + self._pref_vector = pref_vector |
| 72 | + |
| 73 | + def forward(self, matrix: Tensor) -> Tensor: |
| 74 | + weights = self.weighting(matrix) |
| 75 | + units = torch.nan_to_num((matrix / (matrix.norm(dim=1)).unsqueeze(1)), 0.0) |
| 76 | + best_direction = torch.linalg.pinv(units) @ weights |
| 77 | + |
| 78 | + if best_direction.norm() == 0: |
| 79 | + unit_target_vector = torch.zeros_like(best_direction) |
| 80 | + else: |
| 81 | + unit_target_vector = best_direction / best_direction.norm() |
| 82 | + |
| 83 | + length = torch.sum(torch.stack([torch.dot(grad, unit_target_vector) for grad in matrix])) |
| 84 | + |
| 85 | + return length * unit_target_vector |
| 86 | + |
| 87 | + def __repr__(self) -> str: |
| 88 | + return f"{self.__class__.__name__}(pref_vector={repr(self._pref_vector)})" |
| 89 | + |
| 90 | + def __str__(self) -> str: |
| 91 | + return f"ConFIG{_pref_vector_to_str_suffix(self._pref_vector)}" |
0 commit comments