speechbrain.utils.Accuracy module

Calculate accuracy.

Authors * Jianyuan Zhong 2020

Summary

Classes:

AccuracyStats

Module for calculate the overall one-step-forward prediction accuracy.

Functions:

Accuracy

Calculates the accuracy for predicted log probabilities and targets in a batch.

Reference

speechbrain.utils.Accuracy.Accuracy(log_probabilities, targets, length=None)[source]

Calculates the accuracy for predicted log probabilities and targets in a batch.

Parameters:
  • log_probabilities (tensor) – Predicted log probabilities (batch_size, time, feature).

  • targets (tensor) – Target (batch_size, time).

  • length (tensor) – Length of target (batch_size,).

Example

>>> probs = torch.tensor([[0.9, 0.1], [0.1, 0.9], [0.8, 0.2]]).unsqueeze(0)
>>> acc = Accuracy(torch.log(probs), torch.tensor([1, 1, 0]).unsqueeze(0), torch.tensor([2/3]))
>>> print(acc)
(1.0, 2.0)
class speechbrain.utils.Accuracy.AccuracyStats[source]

Bases: object

Module for calculate the overall one-step-forward prediction accuracy.

Example

>>> probs = torch.tensor([[0.9, 0.1], [0.1, 0.9], [0.8, 0.2]]).unsqueeze(0)
>>> stats = AccuracyStats()
>>> stats.append(torch.log(probs), torch.tensor([1, 1, 0]).unsqueeze(0), torch.tensor([2/3]))
>>> acc = stats.summarize()
>>> print(acc)
0.5
append(log_probabilities, targets, length=None)[source]

This function is for updating the stats according to the prediction and target in the current batch.

Parameters:
  • log_probabilities (tensor) – Predicted log probabilities (batch_size, time, feature).

  • targets (tensor) – Target (batch_size, time).

  • length (tensor) – Length of target (batch_size,).

summarize()[source]

Computes the accuract metric.