o
    %ݫi)                     @   s6   d Z ddlZddlmZ ddlZG dd dejZdS )zImplementation of a Recurrent Language Model.

Authors
 * Mirco Ravanelli 2020
 * Peter Plantinga 2020
 * Ju-Chieh Chou 2020
 * Titouan Parcollet 2020
 * Abdel 2020
    N)nnc                
       sJ   e Zd ZdZdejjdejj	j
ddddddf
 fd	d
	ZdddZ  ZS )RNNLMa  This model is a combination of embedding layer, RNN, DNN.
    It can be used for RNNLM.

    Arguments
    ---------
    output_neurons : int
        Number of entries in embedding table, also the number of neurons in
        output layer.
    embedding_dim : int
        Size of embedding vectors (default 128).
    activation : torch class
        A class used for constructing the activation layers for DNN.
    dropout : float
        Neuron dropout rate applied to embedding, RNN, and DNN.
    rnn_class : torch class
        The type of RNN to use in RNNLM network (LiGRU, LSTM, GRU, RNN)
    rnn_layers : int
        The number of recurrent layers to include.
    rnn_neurons : int
        Number of neurons in each layer of the RNN.
    rnn_re_init : bool
        Whether to initialize rnn with orthogonal initialization.
    return_hidden : bool
        Whether to return hidden states (default True).
    dnn_blocks : int
        The number of linear neural blocks to include.
    dnn_neurons : int
        The number of neurons in the linear layers.

    Example
    -------
    >>> model = RNNLM(output_neurons=5)
    >>> inputs = torch.Tensor([[1, 2, 3]])
    >>> outputs = model(inputs)
    >>> outputs.shape
    torch.Size([1, 3, 5])
       g333333?   i   F   i   c                    s   t    tjjj||d| _tj|d| _||||||d| _	|	| _
d| _tjjjd d |gd| _t|
D ]0}| jjtjjj|ddd | jjtjjjd	d
 | jj| dd
 | jjtjj|ddd
 q6tjjj||d| _d S )N)num_embeddingsembedding_dim)p)
input_sizehidden_size
num_layersdropoutre_initF)input_shapeTlinear)	n_neuronsbias
layer_namenorm)r   actr   )r
   r   )super__init__sbnnet	embedding	Embeddingr   Dropoutr   rnnreturn_hiddenreshape
containers
Sequentialdnnrangeappendr   Linearnormalization	LayerNormtorchout)selfoutput_neuronsr   
activationr   	rnn_class
rnn_layersrnn_neuronsrnn_re_initr   
dnn_blocksdnn_neuronsblock_index	__class__ R/home/ubuntu/.local/lib/python3.10/site-packages/speechbrain/lobes/models/RNNLM.pyr   8   s<   
zRNNLM.__init__Nc                 C   s|   |  |}| |}t|jdkr|jdd}d| _| ||\}}| |}| |}| jr5|j	dd}| j
r<||fS |S )z:Processes the input tensor x and returns an output tensor.r   r   )dimT)r   r   lenshape	unsqueezer   r   r"   r)   squeezer   )r*   xhxhiddenr)   r6   r6   r7   forwardg   s   



zRNNLM.forward)N)__name__
__module____qualname____doc__r(   r   	LeakyReLUr   r   RNNLSTMr   r@   __classcell__r6   r6   r4   r7   r      s    )/r   )rD   r(   r   speechbrainr   Moduler   r6   r6   r6   r7   <module>   s
    
