artist.optim.loss ================= .. py:module:: artist.optim.loss Classes ------- .. autoapisummary:: artist.optim.loss.Loss artist.optim.loss.VectorLoss artist.optim.loss.FocalSpotLoss artist.optim.loss.PixelLoss artist.optim.loss.KLDivergenceLoss artist.optim.loss.AngleLoss Functions --------- .. autoapisummary:: artist.optim.loss.mean_loss_per_heliostat Module Contents --------------- .. py:class:: Loss(loss_function: torch.nn.Module) Initialize the base loss. Parameters ---------- loss_function : torch.nn.Module A torch module implementing a loss. .. py:attribute:: loss_function .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor :abstractmethod: Compute the loss. Parameters ---------- prediction : torch.Tensor The predicted values. Shape is variable. ground_truth : torch.Tensor The ground truth. Shape is variable. \*\*kwargs : Any Keyword arguments. Raises ------ NotImplementedError This abstract method must be overridden. .. py:class:: VectorLoss Bases: :py:obj:`Loss` Initialize the vector loss. .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor Compute the vector loss. Parameters ---------- prediction : torch.Tensor The predicted values. Shape is ``[number_of_samples, ...]``. ground_truth : torch.Tensor The ground truth. Shape is ``[number_of_samples, ...]``. \*\*kwargs : Any Keyword arguments. ``reduction_dimensions`` is an expected keyword argument for the vector loss. Raises ------ ValueError If expected keyword arguments are not passed. Returns ------- torch.Tensor The summed MSE vector loss reduced along the specified dimensions. Shape is ``[number_of_samples]``. .. py:class:: FocalSpotLoss(scenario: artist.scenario.scenario.Scenario) Bases: :py:obj:`Loss` Initialize the focal spot loss. Parameters ---------- scenario : Scenario The scenario. .. py:attribute:: scenario .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor Compute the focal spot loss. First the focal spots of the prediction are computed, then the loss is computed and reduced along the specified dimensions. Parameters ---------- prediction : torch.Tensor The predicted values. Shape is ``[number_of_samples, bitmap_resolution_e, bitmap_resolution_u]``. ground_truth : torch.Tensor The ground truth. Shape is ``[number_of_samples, 4]``. \*\*kwargs : Any Keyword arguments. ``target_area_indices`` and ``device`` are expected keyword arguments for the focal spot loss. Raises ------ ValueError If expected keyword arguments are not passed. Returns ------- torch.Tensor The focal spot loss. Shape is ``[number_of_samples]``. .. py:class:: PixelLoss(scenario: artist.scenario.scenario.Scenario) Bases: :py:obj:`Loss` Initialize the pixel loss. Parameters ---------- scenario : Scenario The scenario. .. py:attribute:: scenario .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor Compute the pixel loss. First the predicted bitmaps and the ground truth are normalized, then the loss is computed and reduced along the specified dimensions. Parameters ---------- prediction : torch.Tensor The predicted values. Shape is ``[number_of_samples, bitmap_resolution_e, bitmap_resolution_u]``. ground_truth : torch.Tensor The ground truth. Shape is ``[number_of_samples, bitmap_resolution_e, bitmap_resolution_u]``. \*\*kwargs : Any Keyword arguments. ``reduction_dimensions``, ``target_area_indices``, and ``device`` are expected keyword arguments for the pixel loss. Raises ------ ValueError If expected keyword arguments are not passed. Returns ------- torch.Tensor The summed MSE pixel loss reduced along the specified dimensions. Shape is ``[number_of_samples]``. .. py:class:: KLDivergenceLoss Bases: :py:obj:`Loss` Initialize the Kullback-Leibler divergence loss. .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor Compute the Kullback-Leibler divergence loss :math:`D_{\mathrm{KL}}(P \parallel Q)`. The elements in the prediction and ground truth are normalized and shifted, to be greater or equal to zero. The KL-divergence is defined by: .. math:: D_{\mathrm{KL}}(P \parallel Q) = \sum_{x} P(x) \log \frac{P(x)}{Q(x)}, where :math:`P` is the ground truth distribution and :math:`Q` is the approximation or prediction of :math:`Q`. The KL-divergence is an asymmetric function. Switching :math:`P` and :math:`Q` has the following effect: :math:`P \parallel Q` Penalizes extra mass in the prediction where the ground truth has none. :math:`Q \parallel P` Penalizes missing mass in the prediction where the ground truth has mass. Parameters ---------- prediction : torch.Tensor The predicted values. Shape is ``[number_of_samples, bitmap_resolution_e, bitmap_resolution_u]``. ground_truth : torch.Tensor The ground truth. Shape is ``[number_of_samples, bitmap_resolution_e, bitmap_resolution_u]``. \*\*kwargs : Any Keyword arguments. ``reduction_dimensions`` is an expected keyword argument for the KL-divergence loss. Raises ------ ValueError If expected keyword arguments are not passed. Returns ------- torch.Tensor The summed KL-divergence loss reduced along the specified dimensions. Shape is ``[number_of_samples]``. .. py:class:: AngleLoss Bases: :py:obj:`Loss` Initialize the angle loss. .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor Compute the cosine similarity between the prediction and ground truth. Parameters ---------- prediction : torch.Tensor The predicted values. Shape is ``[number_of_samples, 4]``. ground_truth : torch.Tensor The ground truth. Shape is ``[number_of_samples, 4]``. \*\*kwargs : Any Keyword arguments. Returns ------- torch.Tensor The summed loss reduced along the specified dimensions. Shape is ``[number_of_samples]``. .. py:function:: mean_loss_per_heliostat(loss_per_sample: torch.Tensor, number_of_samples_per_heliostat: int) -> torch.Tensor Calculate the mean loss per heliostat from a loss per sample. Parameters ---------- loss_per_sample : torch.Tensor Loss per sample. Shape is ``[number_of_samples]``. number_of_samples_per_heliostat : int Number of samples per heliostat. Returns ------- torch.Tensor Loss per heliostat. Shape is ``[number_of_heliostats]``.