paddlets.models.forecasting.dl.distributions.likelihood

class Likelihood(mode: str = 'distribution')[source]

Bases: ABC

Abstract class for a distributional regression model.

Parameters

mode (str) – The default value is “distribution” for probability distributional regression, for quantile regression, set “quantiles”.

abstract property num_params: int

Returns the number of parameters that define the probability distribution for one single target value.

Returns

The number of parameters.

Return type

int

abstract loss(model_output: Tensor, target: Tensor) Tensor[source]

Compute NLL loss by predicted distrbution parameters and ground truth target.

This is the basic way to compute the NLL loss. It can be overwritten by likelihood for which paddle proposes a numerically better NLL loss.

Parameters
  • model_output (paddle.Tensor) – The output of model.

  • target (paddle.Tensor) – The ground truth of the sample.

Returns

The loss computed by distribution parameters and ground truth.

Return type

paddle.Tensor

class GaussianLikelihood[source]

Bases: Likelihood

Univariate Gaussian distribution.

output_to_params(model_output: Tensor) Tensor[source]

Use softplus to rescale sigma parameter as it should be positive.

Parameters

model_output (paddle.Tensor) – The output of model.

Returns

The Gaussian distribution parameters(mu and sigma).

Return type

paddle.Tensor

params_to_distr(distr_params: Tensor) Distribution[source]

Construct Normal instance by parameters: mu and sigma.

Parameters

distr_params (paddle.Tensor) – Tensor of mu and sigma.

Returns

The Gaussian instance defined in paddle.

Return type

Distribution

get_mean(distr_params: Tensor) Tensor[source]

Return mean of the distribution.

Parameters

distr_params – Tensor of parameters mu and sigma.

Returns

Mean of Gaussian distribution.

Return type

paddle.Tensor

property num_params: int

For Gaussian, the number of parameters is 2.

Returns

The number of parameters of Gaussian distribution.

Return type

int

sample(model_output: Tensor, num_samples: int = 1) Tensor[source]

Samples a prediction from the model output:

1> output to distribution parameters;

2> distribution parameters to distribution;

3> sample by distribution

Parameters
  • model_output (paddle.Tensor) – The output of model.

  • num_samples (int) – The number of samples to be sampled.

Returns

The samples of distribution.

Return type

paddle.Tensor

loss(model_output: Tensor, target: Tensor) Tensor[source]

Compute NLL loss by predicted distrbution parameters and ground truth target.

This is the basic way to compute the NLL loss. It can be overwritten by likelihood for which paddle proposes a numerically better NLL loss.

Parameters
  • model_output (paddle.Tensor) – The output of model.

  • target (paddle.Tensor) – The ground truth of the sample.

Returns

The loss computed by model output and ground truth.

Return type

paddle.Tensor

class QuantileRegression(quantiles: Optional[List[float]] = [0.1, 0.5, 0.9])[source]

Bases: Likelihood

Quantile regression.

property num_params: int

For quantile regression, the number of parameters is the length of quantiles.

Returns

length of quantiles

Return type

int

loss(model_output: Tensor, target: Tensor) Tensor[source]

Compute quantile loss.

Parameters
  • model_output (paddle.Tensor) – The output of model, must be of shape (batch_size, n_timesteps, n_target_variables)

  • target – The ground truth of the sample, must be of shape (n_samples, n_timesteps, n_target_variables, n_quantiles)

Returns

The loss computed by model output and ground truth.

Return type

paddle.Tensor