|
| 1 | +""" |
| 2 | +RefinementInterface class for handling the refinement of points in a neural |
| 3 | +network training process. |
| 4 | +""" |
| 5 | + |
| 6 | +import torch |
| 7 | +from abc import ABCMeta |
| 8 | +from lightning.pytorch import Callback |
| 9 | +from torch_geometric.data.feature_store import abstractmethod |
| 10 | +from torch_geometric.nn.conv import point_transformer_conv |
| 11 | +from ...condition.domain_equation_condition import DomainEquationCondition |
| 12 | + |
| 13 | + |
| 14 | +class RefinementInterface(Callback): |
| 15 | + """ |
| 16 | + Interface class of Refinement |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, sample_every): |
| 20 | + """ |
| 21 | + Initializes the RefinementInterface. |
| 22 | +
|
| 23 | + :param int sample_every: The number of epochs between each refinement. |
| 24 | + """ |
| 25 | + self.sample_every = sample_every |
| 26 | + self.conditions = None |
| 27 | + self.dataset = None |
| 28 | + self.solver = None |
| 29 | + |
| 30 | + def on_train_start(self, trainer, _): |
| 31 | + """ |
| 32 | + Called when the training begins. It initializes the conditions and |
| 33 | + dataset. |
| 34 | +
|
| 35 | + :param lightning.pytorch.Trainer trainer: The trainer object. |
| 36 | + :param _: Unused argument. |
| 37 | + """ |
| 38 | + self.problem = trainer.solver.problem |
| 39 | + self.solver = trainer.solver |
| 40 | + self.conditions = {} |
| 41 | + for name, cond in self.problem.conditions.items(): |
| 42 | + if isinstance(cond, DomainEquationCondition): |
| 43 | + self.conditions[name] = cond |
| 44 | + self.dataset = trainer.datamodule.train_dataset |
| 45 | + |
| 46 | + @property |
| 47 | + def points(self): |
| 48 | + """ |
| 49 | + Returns the points of the dataset. |
| 50 | + """ |
| 51 | + return self.dataset.conditions_dict |
| 52 | + |
| 53 | + def on_train_epoch_end(self, trainer, _): |
| 54 | + """ |
| 55 | + Performs the refinement at the end of each training epoch (if needed). |
| 56 | +
|
| 57 | + :param lightning.pytorch.Trainer trainer: The trainer object. |
| 58 | + :param _: Unused argument. |
| 59 | + """ |
| 60 | + if trainer.current_epoch % self.sample_every == 0: |
| 61 | + self.update() |
| 62 | + |
| 63 | + def update(self): |
| 64 | + """ |
| 65 | + Performs the refinement of the points. |
| 66 | + """ |
| 67 | + new_points = {} |
| 68 | + for name, condition in self.conditions.items(): |
| 69 | + new_points[name] = {"input": self.sample(name, condition)} |
| 70 | + self.dataset.update_data(new_points) |
| 71 | + |
| 72 | + def per_point_residual(self, conditions_name=None): |
| 73 | + """ |
| 74 | + Computes the residuals for a PINN object. |
| 75 | +
|
| 76 | + :return: the total loss, and pointwise loss. |
| 77 | + :rtype: tuple |
| 78 | + """ |
| 79 | + # compute residual |
| 80 | + res_loss = {} |
| 81 | + tot_loss = [] |
| 82 | + points = self.points |
| 83 | + if conditions_name is None: |
| 84 | + conditions_name = list(self.conditions.keys()) |
| 85 | + for name in conditions_name: |
| 86 | + cond = self.conditions[name] |
| 87 | + cond_points = points[name]["input"] |
| 88 | + target = self._compute_residual(cond_points, cond.equation) |
| 89 | + res_loss[name] = torch.abs(target).as_subclass(torch.Tensor) |
| 90 | + tot_loss.append(torch.abs(target)) |
| 91 | + return torch.vstack(tot_loss).tensor.mean(), res_loss |
| 92 | + |
| 93 | + def _compute_residual(self, pts, equation): |
| 94 | + pts.requires_grad_(True) |
| 95 | + pts.retain_grad() |
| 96 | + return equation.residual(pts, self.solver.forward(pts)) |
| 97 | + |
| 98 | + @abstractmethod |
| 99 | + def sample(self, condition): |
| 100 | + """ |
| 101 | + Samples new points based on the condition. |
| 102 | + """ |
| 103 | + pass |
0 commit comments