(self,
                input: Tensor,
                c0: Optional[Tensor] = None,
                mask_pad: Optional[Tensor] = None)
return h, c
The forward method of the SRU layer.
The forward method of the SRU layer.
The forward method of the SRU layer .
def forward(self,
                input: Tensor,
                c0: Optional[Tensor] = None,
                mask_pad: Optional[Tensor] = None) -> Tuple[Tensor, Tensor]:
        """The forward method of the SRU layer.
        """

        if input.dim() != 2 and input.dim() != 3:
            raise ValueError("Input must be 2 or 3 dimensional")

        batch_size = input.size(-2)
        if c0 is None:
            c0 = torch.zeros(batch_size, self.output_size, dtype=input.dtype,
                             device=input.device)

        # apply layer norm before activation (i.e. before SRU computation)
        residual = input
        if self.layer_norm is not None and not self.normalize_after:
            input = self.layer_norm(input)

        # apply dropout for multiplication
        if self.training and (self.rnn_dropout > 0):
            mask = self.get_dropout_mask_((batch_size, input.size(-1)), self.rnn_dropout)
            input = input * mask.expand_as(input)

        # get the scaling constant; scale_x is a scalar
        scale_val: Optional[Tensor] = None
        scale_val = self.scale_x if self.rescale else None

        # get dropout mask
        mask_c: Optional[Tensor] = None
        if self.training and (self.dropout > 0):
            mask_c = self.get_dropout_mask_((batch_size, self.output_size),
                                            self.dropout)

        # compute U, V
        #   U is (length, batch_size, output_size * num_matrices)
        #   V is (output_size*2,) or (length, batch_size, output_size * 2) if provided
        U, V = self.compute_UV(input, c0, mask_pad)

        # apply elementwise recurrence to get hidden states h and c
        h, c = self.apply_recurrence(U, V, residual, c0, scale_val, mask_c, mask_pad)

        if self.layer_norm is not None and self.normalize_after:
            h = self.layer_norm(h)

        return h, c
def forward ( self , input : Tensor , c0 : Optional [ Tensor ] = None , mask_pad : Optional [ Tensor ] = None ) -> Tuple [ Tensor , Tensor ] : if input . dim ( ) != 2 and input . dim ( ) != 3 : raise ValueError ( "Input must be 2 or 3 dimensional" ) batch_size = input . size ( - 2 ) if c0 is None : c0 = torch . zeros ( batch_size , self . output_size , dtype = input . dtype , device = input . device ) # apply layer norm before activation (i.e. before SRU computation) residual = input if self . layer_norm is not None and not self . normalize_after : input = self . layer_norm ( input ) # apply dropout for multiplication if self . training and ( self . rnn_dropout > 0 ) : mask = self . get_dropout_mask_ ( ( batch_size , input . size ( - 1 ) ) , self . rnn_dropout ) input = input * mask . expand_as ( input ) # get the scaling constant; scale_x is a scalar scale_val : Optional [ Tensor ] = None scale_val = self . scale_x if self . rescale else None # get dropout mask mask_c : Optional [ Tensor ] = None if self . training and ( self . dropout > 0 ) : mask_c = self . get_dropout_mask_ ( ( batch_size , self . output_size ) , self . dropout ) # compute U, V #   U is (length, batch_size, output_size * num_matrices) #   V is (output_size*2,) or (length, batch_size, output_size * 2) if provided U , V = self . compute_UV ( input , c0 , mask_pad ) # apply elementwise recurrence to get hidden states h and c h , c = self . apply_recurrence ( U , V , residual , c0 , scale_val , mask_c , mask_pad ) if self . layer_norm is not None and self . normalize_after : h = self . layer_norm ( h ) return h , c