Functional

import pyblaze.nn.functional as X

The functional module provides free functions that are missing from PyTorch.

General Functions

Gumbel

pyblaze.nn.functional.gumbel_softmax(logits: torch.Tensor, tau=1, hard=False, eps=1e-10, dim=- 1)[source]

Numerically stable version of PyTorch’s builtin Gumbel softmax.

Parameters
  • logits (torch.Tensor) – The values fed into the Gumbel softmax.

  • tau (float, default: 1) – Temperature parameter for the Gumbel distribution.

  • hard (bool, default: False) – Whether to obtain a one-hot output.

  • eps (float, default: 1e-10) – Constant for numerical stability.

  • dim (int, default: -1) – The dimension over which to apply the softmax.

Soft Round

pyblaze.nn.functional.softround(x)[source]

Rounds the input by using torch.round but does enable passing gradients. This is achieved in a similar fashion as for the Gumbel softmax.

Probability Distributions

Normal Distribution

pyblaze.nn.functional.log_prob_standard_normal(x)[source]

Computes the log-probability of observing the given data under a (multivariate) standard Normal distribution. Although this function is equivalent to the log_prob method of the torch.distributions.MultivariateNormal class, this implementation is much more efficient due to the restriction to standard Normal.

Parameters

x (torch.Tensor [N, D]) – The samples whose log-probability shall be computed (number of samples N, dimensionality D).

Returns

The log-probabilities for all samples.

Return type

torch.Tensor [N]

GMM

pyblaze.nn.functional.log_prob_standard_gmm(x, means)[source]

Computes the log-probability of observing the given data under a GMM consisting of (multivariate) standard normal distributions. Each component is assigned the same weight.

Parameters
  • x (torch.Tensor [N, D]) – The samples whose log-probability shall be computed (number of samples N, dimensionality D).

  • means (torch.Tensor [M, D]) – The means of the GMM.

Returns

The log-probabilities for all samples.

Return type

torch.Tensor [N]

Metrics

Accuracy

pyblaze.nn.functional.accuracy(y_pred, y_true)[source]

Computes the accuracy of the class predictions.

Parameters
  • y_pred (torch.LongTensor [N] or torch.FloatTensor [N, C]) – The class predictions made by the model. Can be either specific classes or predictions for each class.

  • y_true (torch.LongTensor [N] or torch.FloatTensor [N, C]) – The actual classes, either given as indices or one-hot vectors (more specifically, it may be any vector whose row-wise argmax values yield the class labels).

Returns

The accuracy.

Return type

torch.FloatTensor

Recall

pyblaze.nn.functional.recall(y_pred, y_true, c=1)[source]

Computes the recall score of the class predictions.

Parameters
  • y_pred (torch.LongTensor [N] or torch.FloatTensor [N, C]) – The class predictions made by the model. Can be either specific classes or predictions for each class.

  • y_true (torch.LongTensor [N]) – The actual classes.

  • c (int, default: 1) – The class to calculate the recall score for. Default assumes a binary classification setting.

Returns

The recall score.

Return type

torch.FloatTensor

Precision

pyblaze.nn.functional.precision(y_pred, y_true, c=1)[source]

Computes the precision score of the class predictions.

Parameters
  • y_pred (torch.LongTensor [N] or torch.FloatTensor [N, C]) – The class predictions made by the model. Can be either specific classes or predictions for each class.

  • y_true (torch.LongTensor [N]) – The actual classes.

  • c (int, default: 1) – The class to calculate the recall score for. Default assumes a binary classification setting.

Returns

The precision score.

Return type

torch.FloatTensor

F1 Score

pyblaze.nn.functional.f1_score(y_pred, y_true, c=1)[source]

Computes the F1-score of the class predictions.

Parameters
  • y_pred (torch.LongTensor [N] or torch.FloatTensor [N, C]) – The class predictions made by the model. Can be either specific classes or predictions for each class.

  • y_true (torch.LongTensor [N]) – The actual classes.

  • c (int, default: 1) – The class to calculate the recall score for. Default assumes a binary classification setting.

Returns

The F1-score.

Return type

torch.FloatTensor

ROC-AUC Score

pyblaze.nn.functional.roc_auc_score(y_pred, y_true)[source]

Computes the area under the ROC curve.

Parameters
  • y_pred (torch.FloatTensor [N]) – The (binary) predictions made by the model.

  • y_true (torch.LongTensor [N]) – The actual classes.

Returns

The ROC-AUC score.

Return type

torch.FloatTensor

Average Precision

pyblaze.nn.functional.average_precision(y_pred, y_true)[source]

Computes the average precision of the model predictions.

Parameters
  • y_pred (torch.FloatTensor [N]) – The (binary) predictions made by the model.

  • y_trye (torch.LongTensor [N]) – The actual classes.

Returns

The average precision.

Return type

torch.FloatTensor