r"""Applies a linear transformation to the input. For instance, if input is x, then output y is: .. math:: y = xW^T + b where :math:`y_i= \sum_j W_{ij} x_j + b_i` Args: in_features(:class:`int`): size of each input sample. out_features(:class:`int`): s
| 7 | |
| 8 | |
| 9 | class Linear(Module): |
| 10 | r"""Applies a linear transformation to the input. For instance, if input |
| 11 | is x, then output y is: |
| 12 | |
| 13 | .. math:: |
| 14 | |
| 15 | y = xW^T + b |
| 16 | |
| 17 | where :math:`y_i= \sum_j W_{ij} x_j + b_i` |
| 18 | |
| 19 | Args: |
| 20 | in_features(:class:`int`): size of each input sample. |
| 21 | out_features(:class:`int`): size of each output sample. |
| 22 | bias(:class:`bool`): if it's ``False``, the layer will not learn an additional ``bias``. |
| 23 | Default: ``True``. |
| 24 | |
| 25 | Shape: |
| 26 | - x: :math:`(*, H_{in})`, where * means any number of dimensions including none where :math:`H_{in}` = in_features. |
| 27 | - y: :math:`(*, H_{out})`, where all but the last dimension are the same shape as the input where :math:`H_{out} = out_features. |
| 28 | |
| 29 | Examples: |
| 30 | >>> import numpy as np |
| 31 | >>> m = M.Linear(in_features=3, out_features=1) |
| 32 | >>> inp = mge.tensor(np.arange(0, 6).astype("float32").reshape(2, 3)) |
| 33 | >>> oup = m(inp) |
| 34 | >>> oup.numpy().shape |
| 35 | (2, 1) |
| 36 | """ |
| 37 | |
| 38 | def __init__( |
| 39 | self, |
| 40 | in_features: int, |
| 41 | out_features: int, |
| 42 | bias: bool = True, |
| 43 | compute_mode: str = "default", |
| 44 | **kwargs |
| 45 | ): |
| 46 | super().__init__(**kwargs) |
| 47 | self.out_features = out_features |
| 48 | self.in_features = in_features |
| 49 | w_shape = (out_features, in_features) |
| 50 | self.weight = Parameter(np.zeros(w_shape, dtype=np.float32)) |
| 51 | self.bias = None |
| 52 | if bias: |
| 53 | b_shape = (out_features,) |
| 54 | self.bias = Parameter(np.zeros(b_shape, dtype=np.float32)) |
| 55 | self.compute_mode = compute_mode |
| 56 | self.reset_parameters() |
| 57 | |
| 58 | def _get_fanin(self): |
| 59 | return self.in_features |
| 60 | |
| 61 | def reset_parameters(self) -> None: |
| 62 | fanin = self._get_fanin() |
| 63 | std = np.sqrt(1 / fanin) |
| 64 | init.normal_(self.weight, 0.0, std) |
| 65 | if self.bias is not None: |
| 66 | init.zeros_(self.bias) |
no outgoing calls