espnet2.uasr.loss.discriminator_loss.UASRDiscriminatorLoss
espnet2.uasr.loss.discriminator_loss.UASRDiscriminatorLoss
class espnet2.uasr.loss.discriminator_loss.UASRDiscriminatorLoss(weight: float = 1.0, smoothing: float = 0.0, smoothing_one_side: str2bool = False, reduction: str = 'sum')
Bases: AbsUASRLoss
A class that implements the discriminator loss for Unsupervised ASR (UASR).
This loss function is designed to optimize the performance of the discriminator in a UASR setup by applying binary cross-entropy loss on both the generated (fake) and real samples, with options for label smoothing and reduction strategies.
weight
The weight for the loss, where a value greater than 0 indicates that the loss will be computed.
- Type: float
smoothing
The amount of label smoothing to apply to the generated samples.
- Type: float
smoothing
If True, only applies smoothing to the generated samples.
- Type: bool
reduction
Specifies the reduction to apply to the output. Options are ‘none’, ‘mean’, or ‘sum’.
Type: str
Parameters:
- weight (float , optional) – Weight for the loss. Defaults to 1.0.
- smoothing (float , optional) – Label smoothing value. Defaults to 0.0.
- smoothing_one_side (str2bool , optional) – If True, apply smoothing only to generated samples. Defaults to False.
- reduction (str , optional) – Reduction method. Options are ‘none’, ‘mean’, or ‘sum’. Defaults to ‘sum’.
Returns: The computed loss for the dense and token outputs. If the weight is less than or equal to 0, returns 0.
Return type: Tuple[torch.Tensor, Optional[torch.Tensor]]
####### Examples
>>> loss_fn = UASRDiscriminatorLoss(weight=1.0, smoothing=0.1)
>>> dense_y = torch.tensor([[0.5], [0.2]], requires_grad=True)
>>> token_y = torch.tensor([[0.1], [0.9]], requires_grad=True)
>>> loss_dense, loss_token = loss_fn(dense_y, token_y, True)
NOTE
The loss is computed differently based on the is_discriminative_step argument, allowing for flexibility in training phases.
- Raises:
- ValueError – If the reduction method specified is not one of ‘none’,
- 'mean'****, or 'sum'. –
Initialize internal Module state, shared by both nn.Module and ScriptModule.
forward(dense_y: Tensor, token_y: Tensor, is_discriminative_step: str2bool)
Forward pass for the UASRDiscriminatorLoss.
This method computes the discriminator loss based on the predicted logits of generated and real samples. It applies binary cross-entropy loss with optional label smoothing.
- Parameters:
- dense_y (torch.Tensor) – Predicted logits of generated samples.
- token_y (torch.Tensor) – Predicted logits of real samples.
- is_discriminative_step (str2bool) – A flag indicating whether the current step is a discriminative step or not.
- Returns: A tuple containing the loss for dense predictions and the loss for token predictions. If the weight is less than or equal to zero, it returns 0.
- Return type: Tuple[torch.Tensor, Optional[torch.Tensor]]
NOTE
The loss is weighted by the weight attribute, and label smoothing is applied based on the smoothing and smoothing_one_sided attributes.
####### Examples
>>> loss_fn = UASRDiscriminatorLoss(weight=1.0, smoothing=0.1)
>>> dense_y = torch.tensor([[0.5], [0.2]], requires_grad=True)
>>> token_y = torch.tensor([[0.3], [0.7]], requires_grad=True)
>>> is_discriminative_step = True
>>> loss_dense, loss_token = loss_fn(dense_y, token_y,
... is_discriminative_step)
>>> print(loss_dense, loss_token)