mindspore.nn.BiDense

查看源文件
class mindspore.nn.BiDense(in1_channels, in2_channels, out_channels, weight_init=None, bias_init=None, has_bias=True, dtype=mstype.float32)[源代码]

双线性全连接层。

两个输入的密集连接层。公式如下:

y=x1TAx2+b,

其中 x1 是第一个输入Tensor,x2 是第二个输入Tensor,A 是一个权重矩阵,其数据类型与 x 相同, b 是一个偏置向量,其数据类型与 x 相同(仅当has_bias为 True 时)。

参数:
  • in1_channels (int) - BiDense层第一个输入Tensor的空间维度。

  • in2_channels (int) - BiDense层第二个输入Tensor的空间维度。

  • out_channels (int) - BiDense层输出Tensor的空间维度。

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) - 权重参数的初始化方法。str的值引用自函数 initializer。默认值: None

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) - 偏置参数的初始化方法。str的值引用自函数 initializer。默认值: None

  • has_bias (bool) - 是否使用偏置向量 bias 。默认值: True

  • dtype (mindspore.dtype) - Parameters的dtype。默认值: mstype.float32

形状:
  • input1 - (,Hin1),其中 Hin1=in1_channels 为任意维度. input1除最后一维外的维度需与其他输入保持一致。

  • input2 - (,Hin2),其中 Hin2=in2_channels 为任意维度. input2除最后一维外的维度需与其他输入保持一致。

  • output - (,Hout),其中 Hout=out_channels 为任意维度. output除最后一维外的维度需与所有输入保持一致。

数据类型:
  • input1 (Tensor) - 数据类型必须为float16或float32,且与input2一致。

  • input2 (Tensor) - 数据类型必须为float16或float32,且与input1一致。

  • output (Tensor) - 数据类型与输入保持一致。

权重:
  • weight (Parameter) - 权重参数,shape为 (out_channels,in1_channels,in2_channels)。 当 weight_init 设为 None 时,其初始化值服从 U(k,k),其中 k=1in1_channels

  • bias (Parameter) - 偏置参数,shape为 (out_channels)。 当 has_bias 设为 Truebias_init 设为 None 时,其初始化值服从 U(k,k),其中 k=1in1_channels

异常:
  • TypeError - in_channelsout_channels 不是整数。

  • TypeError - has_bias 不是bool值。

  • ValueError - weight_init 的shape长度不等于3,weight_init 的shape[0]不等于 out_channels,或者 weight_init 的shape[1]不等于 in1_channels,或者 weight_init 的shape[2]不等于 in2_channels

  • ValueError - bias_init 的shape长度不等于1或 bias_init 的shape[0]不等于 out_channels

支持平台:

Ascend GPU CPU

样例:

>>> 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)