mindspore.nn.BiDense
- class mindspore.nn.BiDense(in1_channels, in2_channels, out_channels, weight_init=None, bias_init=None, has_bias=True)[source]
The bilinear dense connected layer.
Applies dense connected layer for two inputs. This layer implements the operation as:
\[y = x_1^T A x_2 + b,\]where \(x_{1}\) is the first input tensor, \(x_{2}\) is the second input tensor , \(A\) is a weight matrix with the same data type as the \(x_{*}\) created by the layer , and \(b\) is a bias vector with the same data type as the \(x_{*}\) created by the layer (only if has_bias is
True
).- Parameters
in1_channels (int) – The number of channels in the input1 space.
in2_channels (int) – The number of channels in the input2 space.
out_channels (int) – The number of channels in the output space.
weight_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable weight_init parameter. The values of str refer to the function initializer. Default:
None
.bias_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable bias_init parameter. The values of str refer to the function initializer. Default:
None
.has_bias (bool) – Specifies whether the layer uses \(\text{bias}\) vector. Default:
True
.
- Shape:
input1 - \((*, H_{in1})\) where \(H_{in1}=\text{in1_channels}\) and \(*\) means any number of additional dimensions including none. All but the last dimension of the inputs should be the same.
input2 - \((*, H_{in2})\) where \(H_{in2}=\text{in2_channels}\) and \(*\) means any number of additional dimensions including none. All but the last dimension of the inputs should be the same.
output - \((*, H_{out})\) where \(H_{out}=\text{out_channels}\) and \(*\) means any number of additional dimensions including none. All but the last dimension are the same shape as the inputs.
- Dtype:
input1 (Tensor) - The dtype must be float16 or float32 and be same as input2.
input1 (Tensor) - The dtype must be float16 or float32 and be same as input1.
output (Tensor) - With the same dtype as the inputs.
- Weights:
weight (Parameter) - The learnable weights with shape \((\text{out_channels}, \text{in1_channels}, \text{in2_channels})\). When weight_init is
None
, the values are initialized from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\), where \(k = \frac{1}{\text{in1_channels}}\).bias (Parameter) - The learnable bias of shape \((\text{out_channels})\). If has_bias is
True
and bias_init isNone
, the values are initialized from \(\mathcal{U}(-\sqrt{k}, \sqrt{k})\), where \(k = \frac{1}{\text{in1_channels}}\).
- Raises
TypeError – If in1_channels, in2_channels or out_channels is not an int.
TypeError – If has_bias is not a bool.
ValueError – If length of shape of weight_init is not equal to 3 or shape[0] of weight_init is not equal to out_channels or shape[1] of weight_init is not equal to in1_channels or shape[2] of weight_init is not equal to in2_channels.
ValueError – If length of shape of bias_init is not equal to 1 or shape[0] of bias_init is not equal to out_channels.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> from mindspore import Tensor, nn >>> import numpy as np >>> x1 = Tensor(np.random.randn(128, 20), mindspore.float32) >>> x2 = Tensor(np.random.randn(128, 30), mindspore.float32) >>> net = nn.BiDense(20, 30, 40) >>> output = net(x1, x2) >>> print(output.shape) (128, 40)