opacus/layers/dp_lstm.py
def forward(
        self, x: torch.Tensor, state_init: Tuple[torch.Tensor, torch.Tensor] = None
    ) -> Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
        r"""
        Implements the forward pass of the DPLSTM when a sequence is input.

        Args:
            x: Input sequence to the DPLSTM of shape ``[T, B, D]``
            state_init: Initial state of the LSTM as a tuple ``(h_init, c_init)``
                where ``h_init`` is the initial hidden state and ``c_init`` is the
                initial cell state of the DPLSTM (The default is ``None``, in which case both
                ``h_init`` and ``c_init`` default to zero tensors).

        Returns:
            ``output, (h_n, c_n)`` where ``output`` is of shape ``[T, B, H]`` and is a
            tensor containing the output features (``h_t``) from the last layer of the
            DPLSTM for each timestep ``t``. ``h_n`` is of shape ``[B,H]`` and is a
            tensor containing the hidden state for ``t = T``. ``c_n`` is of shape
            ``[B, H]`` tensor containing the cell state for ``t = T``.
        """

        x = self._rearrange_batch_dim(x)
        seq_length, batch_sz, _ = x.shape

        hc = [(None, None)] * seq_length
        x = torch.unbind(x, dim=0)

        if state_init:
            h_init, c_init = state_init
        else:
            h_init = torch.zeros(
                self.num_layers,
                batch_sz,
                self.hidden_size,
                dtype=x[0].dtype,
                device=x[0].device,
            )
            c_init = torch.zeros(
                self.num_layers,
                batch_sz,
                self.hidden_size,
                dtype=x[0].dtype,
                device=x[0].device,
            )

        layer = 0
        hc[0] = self._forward_cell(x[0], h_init[layer], c_init[layer])
        for t in range(1, seq_length):
            h_prev, c_prev = hc[t - 1]
            hc[t] = self._forward_cell(x[t], h_prev, c_prev)

        return (
            self._rearrange_batch_dim(torch.stack([hc_t[0] for hc_t in hc])),
            (hc[-1][0], hc[-1][1]),
        )
r"""
        Implements the forward pass of the DPLSTM when a sequence is input.

        Args:
            x: Input sequence to the DPLSTM of shape ``[T, B, D]``
            state_init: Initial state of the LSTM as a tuple ``(h_init, c_init)``
                where ``h_init`` is the initial hidden state and ``c_init`` is the
                initial cell state of the DPLSTM (The default is ``None``, in which case both
                ``h_init`` and ``c_init`` default to zero tensors).

        Returns:
            ``output, (h_n, c_n)`` where ``output`` is of shape ``[T, B, H]`` and is a
            tensor containing the output features (``h_t``) from the last layer of the
            DPLSTM for each timestep ``t``. ``h_n`` is of shape ``[B,H]`` and is a
            tensor containing the hidden state for ``t = T``. ``c_n`` is of shape
            ``[B, H]`` tensor containing the cell state for ``t = T``.
        
r"""
Implements the forward pass of the DPLSTM when a sequence is input.
r " " " Implements the forward pass of the DPLSTM when a sequence is input .
def forward(
        self, x: torch.Tensor, state_init: Tuple[torch.Tensor, torch.Tensor] = None
    ) -> Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
        x = self._rearrange_batch_dim(x)
        seq_length, batch_sz, _ = x.shape
        hc = [(None, None)] * seq_length
        x = torch.unbind(x, dim=0)
        if state_init:
            h_init, c_init = state_init
        else:
            h_init = torch.zeros(
                self.num_layers,
                batch_sz,
                self.hidden_size,
                dtype=x[0].dtype,
                device=x[0].device,
            )
            c_init = torch.zeros(
                self.num_layers,
                batch_sz,
                self.hidden_size,
                dtype=x[0].dtype,
                device=x[0].device,
            )
        layer = 0
        hc[0] = self._forward_cell(x[0], h_init[layer], c_init[layer])
        for t in range(1, seq_length):
            h_prev, c_prev = hc[t - 1]
            hc[t] = self._forward_cell(x[t], h_prev, c_prev)
        return (
            self._rearrange_batch_dim(torch.stack([hc_t[0] for hc_t in hc])),
            (hc[-1][0], hc[-1][1]),
        )
def forward ( self , x : torch . Tensor , state_init : Tuple [ torch . Tensor , torch . Tensor ] = None ) -> Tuple [ torch . Tensor , Tuple [ torch . Tensor , torch . Tensor ] ] : x = self . _rearrange_batch_dim ( x ) seq_length , batch_sz , _ = x . shape hc = [ ( None , None ) ] * seq_length x = torch . unbind ( x , dim = 0 ) if state_init : h_init , c_init = state_init else : h_init = torch . zeros ( self . num_layers , batch_sz , self . hidden_size , dtype = x [ 0 ] . dtype , device = x [ 0 ] . device , ) c_init = torch . zeros ( self . num_layers , batch_sz , self . hidden_size , dtype = x [ 0 ] . dtype , device = x [ 0 ] . device , ) layer = 0 hc [ 0 ] = self . _forward_cell ( x [ 0 ] , h_init [ layer ] , c_init [ layer ] ) for t in range ( 1 , seq_length ) : h_prev , c_prev = hc [ t - 1 ] hc [ t ] = self . _forward_cell ( x [ t ] , h_prev , c_prev ) return ( self . _rearrange_batch_dim ( torch . stack ( [ hc_t [ 0 ] for hc_t in hc ] ) ) , ( hc [ - 1 ] [ 0 ] , hc [ - 1 ] [ 1 ] ) , )
r"""
Implements the forward pass of the DPLSTM when a sequence is input.
r " " " Implements the forward pass of the DPLSTM when a sequence is input .
r"""
        Implements the forward pass of the DPLSTM when a sequence is input.

        Args:
            x: Input sequence to the DPLSTM of shape ``[T, B, D]``
            state_init: Initial state of the LSTM as a tuple ``(h_init, c_init)``
                where ``h_init`` is the initial hidden state and ``c_init`` is the
                initial cell state of the DPLSTM (The default is ``None``, in which case both
                ``h_init`` and ``c_init`` default to zero tensors).

        Returns:
            ``output, (h_n, c_n)`` where ``output`` is of shape ``[T, B, H]`` and is a
            tensor containing the output features (``h_t``) from the last layer of the
            DPLSTM for each timestep ``t``. ``h_n`` is of shape ``[B,H]`` and is a
            tensor containing the hidden state for ``t = T``. ``c_n`` is of shape
            ``[B, H]`` tensor containing the cell state for ``t = T``.
        """
``output, (h_n, c_n)`` where ``output`` is of shape ``[T, B, H]`` and is a
tensor containing the output features (``h_t``) from the last layer of the
DPLSTM for each timestep ``t``. ``h_n`` is of shape ``[B,H]`` and is a
tensor containing the hidden state for ``t = T``. ``c_n`` is of shape
``[B, H]`` tensor containing the cell state for ``t = T``.
` ` output ( h_n c_n ) ` ` where ` ` output ` ` is of shape ` ` [ T B H ] ` ` and is a tensor containing the output features ( ` ` h_t ` ` ) from the last layer of the DPLSTM for each timestep ` ` t ` ` . ` ` h_n ` ` is of shape ` ` [ B H ] ` ` and is a tensor containing the hidden state for ` ` t = T ` ` . ` ` c_n ` ` is of shape ` ` [ B H ] ` ` tensor containing the cell state for ` ` t = T ` ` .
self
x
torch.Tensor
Input sequence to the DPLSTM of shape ``[T, B, D]``
Input sequence to the DPLSTM of shape ` ` [ T B D ] ` `
state_init
Tuple[torch.Tensor, torch.Tensor]
Initial state of the LSTM as a tuple ``(h_init, c_init)``
where ``h_init`` is the initial hidden state and ``c_init`` is the
initial cell state of the DPLSTM (The default is ``None``, in which case both
``h_init`` and ``c_init`` default to zero tensors).
Initial state of the LSTM as a tuple ` ` ( h_init c_init ) ` ` where ` ` h_init ` ` is the initial hidden state and ` ` c_init ` ` is the initial cell state of the DPLSTM ( The default is ` ` None ` ` in which case both ` ` h_init ` ` and ` ` c_init ` ` default to zero tensors ) .