MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / __init__

Method __init__

imperative/python/megengine/module/rnn.py:202–249  ·  view source on GitHub ↗
(
        self,
        input_size: int,
        hidden_size: int,
        num_layers: int = 1,
        bias: bool = True,
        batch_first: bool = False,
        dropout: float = 0,
        bidirectional: bool = False,
        proj_size: int = 0,
    )

Source from the content-addressed store, hash-verified

200
201class RNNBase(Module):
202 def __init__(
203 self,
204 input_size: int,
205 hidden_size: int,
206 num_layers: int = 1,
207 bias: bool = True,
208 batch_first: bool = False,
209 dropout: float = 0,
210 bidirectional: bool = False,
211 proj_size: int = 0,
212 ) -> None:
213 super(RNNBase, self).__init__()
214 self.input_size = input_size
215 self.hidden_size = hidden_size
216 self.num_layers = num_layers
217 self.bias = bias
218 self.batch_first = batch_first
219 self.dropout = float(dropout)
220 self.bidirectional = bidirectional
221 self.num_directions = 2 if self.bidirectional else 1
222 self.proj_size = proj_size
223
224 # check validity of dropout
225 if (
226 not isinstance(dropout, numbers.Number)
227 or not 0 <= dropout <= 1
228 or isinstance(dropout, bool)
229 ):
230 raise ValueError(
231 "Dropout should be a float in [0, 1], which indicates the probability "
232 "of an element to be zero"
233 )
234
235 if proj_size < 0:
236 raise ValueError(
237 "proj_size should be a positive integer or zero to disable projections"
238 )
239 elif proj_size >= hidden_size:
240 raise ValueError("proj_size has to be smaller than hidden_size")
241
242 self.cells = []
243 for layer in range(self.num_layers):
244 self.cells.append([])
245 for _ in range(self.num_directions):
246 self.cells[layer].append(self.create_cell(layer))
247 # parameters have been initialized during the creation of the cells
248 # if flatten, then delete cells
249 self._flatten_parameters(self.cells)
250
251 def _flatten_parameters(self, cells):
252 gate_hidden_size = cells[0][0].gate_hidden_size

Callers

nothing calls this directly

Calls 4

create_cellMethod · 0.95
_flatten_parametersMethod · 0.95
__init__Method · 0.45
appendMethod · 0.45

Tested by

no test coverage detected