A multi-layer RNN composed of LSTMCells (or the provided lstm_cell_fn). Args: num_layers: Number of RNN layers to create. dim_input: Feature dimension of the input. dim_hidden: Dimension of the hidd
(
self,
num_layers: int,
dim_input: int,
dim_hidden: T.Union[T.Sequence[int], int],
bias: bool = True,
append_inputs: bool = False,
lstm_cell_fn: T.Callable = nn.LSTMCell,
dropout_prob: float = 0,
)
| 23 | """ |
| 24 | |
| 25 | def __init__( |
| 26 | self, |
| 27 | num_layers: int, |
| 28 | dim_input: int, |
| 29 | dim_hidden: T.Union[T.Sequence[int], int], |
| 30 | bias: bool = True, |
| 31 | append_inputs: bool = False, |
| 32 | lstm_cell_fn: T.Callable = nn.LSTMCell, |
| 33 | dropout_prob: float = 0, |
| 34 | ): |
| 35 | """ |
| 36 | A multi-layer RNN composed of LSTMCells (or the provided lstm_cell_fn). |
| 37 | |
| 38 | Args: |
| 39 | num_layers: |
| 40 | Number of RNN layers to create. |
| 41 | dim_input: |
| 42 | Feature dimension of the input. |
| 43 | dim_hidden: |
| 44 | Dimension of the hidden states. If an integer is provided, it will be used for all layers. |
| 45 | Otherwise, provide a list of integer, one for each layer. |
| 46 | bias: |
| 47 | Whether to learn bias at each layer. |
| 48 | append_inputs: |
| 49 | Whether to concatenate all previous layers' inputs to every layer's input. |
| 50 | Note that the input to i-th layer is the output hidden state of (i-1)-th layer (plus the |
| 51 | input to all previous layers if append_inputs is True.) |
| 52 | lstm_cell_fn: |
| 53 | RNNCell function to use. Can be one of |
| 54 | :py:func:`torch.nn.RNNCell`, |
| 55 | :py:func:`torch.nn.LSTMCell`, |
| 56 | :py:func:`torch.nn.GRUCell`. |
| 57 | dropout_prob: |
| 58 | Dropout probability on the hidden states. If non-zero, introduces a Dropout layer on the outputs of |
| 59 | each LSTM layer except the last layer. |
| 60 | |
| 61 | """ |
| 62 | super().__init__() |
| 63 | self.num_layers = num_layers |
| 64 | self.dim_input = dim_input |
| 65 | self.dim_hidden = dim_hidden |
| 66 | self.lstm_cell_fn = lstm_cell_fn |
| 67 | self.dropout_prob = dropout_prob |
| 68 | if isinstance(self.dim_hidden, int): |
| 69 | self.dim_hidden = [self.dim_hidden for _ in range(self.num_layers)] |
| 70 | elif len(self.dim_hidden) == 1: |
| 71 | self.dim_hidden = [self.dim_hidden[0] for _ in range(self.num_layers)] |
| 72 | self.append_inputs = append_inputs |
| 73 | self.cell_list = nn.ModuleList() |
| 74 | din = dim_input |
| 75 | for layer_idx in range(num_layers): |
| 76 | cell = self.lstm_cell_fn(din, self.dim_hidden[layer_idx], bias=bias) |
| 77 | self.cell_list.append(cell) |
| 78 | if self.append_inputs: |
| 79 | din = self.dim_hidden[layer_idx] + din |
| 80 | else: |
| 81 | din = self.dim_hidden[layer_idx] |
| 82 |