dgmr

Module for various loss functions used with DGMR GAN.

class mfai.pytorch.losses.dgmr.GridCellLoss(precip_weight_cap)[source]

Bases: Module

Grid Cell Regularizer loss from Skillful Nowcasting, see equation (3) of the original paper (https://arxiv.org/pdf/2104.00954.pdf).

Parameters:

precip_weight_cap (float)

forward(generated_images, targets)[source]

Forward function.

Calculates the grid cell regularizer value, assumes generated images are the mean predictions from 6 calls to the generator (Monte Carlo estimation of the expectations for the latent variable).

\[\begin{split}L_R(\Theta) = \frac{1}{HWN} \\| (\mathbb{E}_Z [G_|theta(Z; X_{1:M})] - X_{M+1:M+T}) \circ w(X_{M+1:M+T}) \\|_1\end{split}\]

where H, W and T represent height, width and leadtimes.

Note

Instead of apply the formula of the weights describe in the original article (\(w(y) = max(y+1, precip\_weight\_cap)\)), we implement a formula closer to the pseudocode released by Google Deepmind. So our formula is : \(w(y) = clip(y, 1, precip\_weight\_cap)\), which mean that weights are between 1 and precip_weight_cap.

Parameters:
  • generated_images (Tensor) – generated images from the generator. Tensor of shape (N B T C H W), where N is the number of generated images.

  • targets (Tensor) – Ground truth future frames. Tensor of shape (B T C H W).

Return type:

Tensor

Returns:

Grid Cell Regularizer term

mfai.pytorch.losses.dgmr.loss_hinge_disc(score_generated, score_real)[source]

Discriminator Hinge loss.

Return type:

Tensor

Parameters:
mfai.pytorch.losses.dgmr.loss_hinge_gen(score_generated)[source]

Generator Hinge loss.

Return type:

Tensor

Parameters:

score_generated (Tensor)