sciai.architecture.MLP

View Source On Gitee
class sciai.architecture.MLP(layers, weight_init='xavier_trunc_normal', bias_init='zeros', activation='tanh', last_activation=None)[source]

Multi-layer perceptron. The last layer is without activation function.

The first value in layers in Args should be equal to the size of last axis in_channels in input Tensor.

Parameters
  • layers (Union(tuple[int], list[int])) – List of numbers of neurons in each layer, e.g., [2, 10, 10, 1].

  • weight_init (Union[str, Initializer]) – The weight_init parameter for Dense. The dtype is the same as x. The values of str refer to the function initializer. Default: ‘xavier_trunc_normal’.

  • bias_init (Union[str, Initializer]) – The bias_init parameter for Dense. The dtype is same as x. The values of str refer to the function initializer. Default: ‘zeros’.

  • activation (Union[str, Cell, Primitive, FunctionType, None]) – Activation function applied to the output of each fully connected layer excluding the last layer. Both activation name, e.g. ‘relu’, and mindspore activation function, e.g. nn.ReLU(), are supported. Default: ‘tanh’.

  • last_activation (Union[str, Cell, Primitive, FunctionType, None]) – Activation function applied to the output of the last dense layer. The type rule is the same as activation.

Inputs:
  • x (Tensor) - Tensor of shape (∗, in_channels).

Outputs:

Union(Tensor, tuple[Tensor]), Output Tensor of the network.

Raises
  • TypeError – If layers is not one of list, tuple, or elements in layers are not ints.

  • TypeError – If activation is not one of str, Cell, Primitive, None.

  • TypeError – If last_activation is not one of str, Cell, Primitive, None.

  • ValueError – If weight_init is not one of str, Initializer.

  • ValueError – If bias_init is not one of str, Initializer.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from sciai.architecture import MLP
>>> x = ms.Tensor(np.array([[180, 234, 154], [244, 48, 247]]), ms.float32)
>>> net = MLP((3, 10, 4))
>>> output = net(x)
>>> print(output.shape)
(2, 4)
biases()[source]

Bias parameter list for all Dense layers.

Returns

list[Parameter], All bias Parameters.

weights()[source]

Weight parameter list for all Dense layers.

Returns

list[Parameter], All weight Parameters.