mindspore.ops.Dense

查看源文件
class mindspore.ops.Dense[源代码]

全连接融合算子。

适用于输入的密集连接算子。算子的实现如下:

output=x@wT+b,

其中 x 是输入Tensor, w 是一个权重矩阵,其数据类型与 x 相同, b 是一个偏置向量,其数据类型与 x 相同(仅当 b 不为 None 时)。

输入:
  • x (Tensor) - 输入Tensor。shape必须满足: len(x.shape)>0

  • w (Tensor) - 权重Tensor。shape必须满足: 若 len(x.shape)>1 ,则 len(w.shape)=2 。若 len(x.shape)=1 ,则 len(w.shape)=1w.shape[1]=x.shape[1]

  • b (Union[Tensor, None]) - 偏置Tensor。当 b 不为 None 时,shape必须满足: 若 len(x.shape)>1 ,则 len(b.shape)=0len(b.shape)=1 。若 len(b.shape)=1 , 则 b.shape[0]=w.shape[0] 。若 len(x.shape)=1 ,则 len(b.shape)=0

输出:

len(x.shape)>1 ,则输出shape为 (x.shape[:1],w.shape[0]) 的Tensor。若 len(x.shape)=1 ,则输出shape为 () 的Tensor。

支持平台:

Ascend GPU CPU

样例:

>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> x = Tensor(np.random.random((4, 5, 6, 7)).astype(np.float32))
>>> weight = Tensor(np.random.random((6, 7)).astype(np.float32))
>>> bias = Tensor(np.random.random((6,)).astype(np.float32))
>>> dense = ops.Dense()
>>> output = dense(x, weight, bias)
>>> print(output.shape)
(4, 5, 6, 6)