o
    ei	                     @   s2   d Z ddlmZ ddlmZ G dd dejZdS )zM
Assorted reusable neural network modules.

Authors
 * Artem Ploujnikov 2023
    )nn)length_to_maskc                       s*   e Zd ZdZ fddZdddZ  ZS )DoneDetectora  A wrapper for the done detector using a model (e.g. a CRDNN) and
    an output layer.

    The goal of using a wrapper is to apply masking before the output layer
    (e.g. Softmax) so that the model can't "cheat" by outputting probabilities
    in the masked area

    Arguments
    ---------
    model: torch.nn.Module
        the model used to make the prediction
    out: torch.nn.Module
        the output function

    Example
    -------
    >>> import torch
    >>> from torch import nn
    >>> from speechbrain.nnet.activations import Softmax
    >>> from speechbrain.nnet.containers import Sequential
    >>> from speechbrain.nnet.linear import Linear
    >>> from speechbrain.lobes.models.CRDNN import CRDNN
    >>> crdnn = CRDNN(
    ...     input_size=80,
    ...     cnn_blocks=1,
    ...     cnn_kernelsize=3,
    ...     rnn_layers=1,
    ...     rnn_neurons=16,
    ...     dnn_blocks=1,
    ...     dnn_neurons=16
    ... )
    >>> model_out = Linear(n_neurons=1, input_size=16)
    >>> model_act = nn.Sigmoid()
    >>> model = Sequential(
    ...     crdnn,
    ...     model_out,
    ...     model_act
    ... )
    >>> out = Softmax(
    ...     apply_log=False,
    ... )
    >>> done_detector = DoneDetector(
    ...     model=model,
    ...     out=out,
    ... )
    >>> preds = torch.randn(4, 10, 80) # Batch x Length x Feats
    >>> length = torch.tensor([1., .8, .5, 1.])
    >>> preds_len = done_detector(preds, length)
    >>> preds_len.shape
    torch.Size([4, 10, 1])
    c                    s   t    || _|| _d S N)super__init__modelout)selfr   r	   	__class__ T/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/speechbrain/nnet/utils.pyr   B   s   

zDoneDetector.__init__Nc                 C   sH   |  |}|dur|d}t|| |d}||d }| |}|S )a>  Computes the forward pass

        Arguments
        ---------
        feats: torch.Tensor
            the features used for the model (e.g. spectrograms)
        length: torch.Tensor
            a tensor of relative lengths

        Returns
        -------
        preds: torch.Tensor
            predictions
        N   )lengthmax_len)r   sizer   	unsqueezer	   )r
   featsr   r	   r   maskr   r   r   forwardG   s   


zDoneDetector.forwardr   )__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r      s    4r   N)r   torchr   speechbrain.dataio.dataior   Moduler   r   r   r   r   <module>   s    