mindspore.mint.nn.Linear

class mindspore.mint.nn.Linear(in_features, out_features, bias=True, weight_init=None, bias_init=None, dtype=None)[source]

The linear connected layer.

Applies linear connected layer for the input. This layer implements the operation as:

\[\text{outputs} = X * kernel + bias\]

where \(X\) is the input tensors, \(\text{kernel}\) is a weight matrix with the same data type as the \(X\) created by the layer, and \(\text{bias}\) is a bias vector with the same data type as the \(X\) created by the layer (only if has_bias is True).

Parameters
  • in_features (int) – The number of features in the input space.

  • out_features (int) – The number of features in the output space.

  • bias (bool) – Specifies whether the layer uses a bias vector \(\text{bias}\). Default: True.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable weight_init parameter. The dtype is same as x. The values of str refer to the function initializer. Default: None , weight will be initialized using HeUniform.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable bias_init parameter. The dtype is same as x. The values of str refer to the function initializer. Default: None , bias will be initialized using Uniform.

  • dtype (mindspore.dtype) – Data type of Parameter. Default: None .

Inputs:
  • x (Tensor) - Tensor of shape \((*, in\_features)\). The in_features in Args should be equal to \(in\_features\) in Inputs.

Outputs:

Tensor of shape \((*, out\_features)\).

Raises
  • TypeError – If in_features or out_features is not an int.

  • TypeError – If bias is not a bool.

  • ValueError – If length of shape of weight_init is not equal to 2 or shape[0] of weight_init is not equal to out_features or shape[1] of weight_init is not equal to in_features.

  • ValueError – If length of shape of bias_init is not equal to 1 or shape[0] of bias_init is not equal to out_features.

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> from mindspore import Tensor
>>> from mindspore import mint
>>> import numpy as np
>>> x = Tensor(np.array([[180, 234, 154], [244, 48, 247]]), mindspore.float32)
>>> net = mint.nn.Linear(3, 4)
>>> output = net(x)
>>> print(output.shape)
(2, 4)