Linear

class Linear(in_features, out_features, bias=True, compute_mode='default', **kwargs)[源代码]

对输入进行线性变换。例如,若有输入x,则输出y为:

\[y = xW^T + b\]

其中 \(y_i= \sum_j W_{ij} x_j + b_i\)

参数
  • in_features (int) – 各输入样本的大小。

  • out_features (int) – 各输出样本的大小。

  • bias (bool) – if it’s False, the layer will not learn an additional bias. Default: True.

Shape:
  • x: \((*, H_{in})\), where * means any number of dimensions including none where \(H_{in}\) = in_features.

  • y: \((*, H_{out})\), where all but the last dimension are the same shape as the input where :math:`H_{out} = out_features.

实际案例

>>> import numpy as np
>>> m = M.Linear(in_features=3, out_features=1)
>>> inp = mge.tensor(np.arange(0, 6).astype("float32").reshape(2, 3))
>>> oup = m(inp)
>>> oup.numpy().shape
(2, 1)