mindspore.ops.GLU
- class mindspore.ops.GLU(axis=- 1)[源代码]
计算输入Tensor的门线性单元激活函数(Gated Linear Unit activation function)值。
其中,
表示输入Tensor x 拆分后的前一半元素, 表示 x 拆分后的另一半元素。 这里 为sigmoid函数, 是Hadamard乘积。 请参考 Language Modeling with Gated Convluational Networks 。- 参数:
axis (int,可选) - 指定分割输入 x 的轴,取值范围 [-r, r) ,其中 r 为 x 的维度数。默认值:
-1
,输入 x 的最后一维。
- 输入:
x (Tensor) - 待计算的Tensor,数据类型为浮点型,shape为
,其中 * 为任意额外维度,且要求 为偶数。 为 x 在被 axis 选中的维度上的大小。
- 输出:
Tensor,数据类型与输入 x 相同,shape为
,其中 。- 异常:
TypeError - x 不是Tensor或 axis 不是整型。
IndexError - axis 的值超出了 [-r, r) 的范围,其中 r 为 x 的维度数。
RuntimeError - x 数据类型不被支持。
RuntimeError - x 在被 axis 选中的维度上的长度不为偶数。
- 支持平台:
Ascend
CPU
样例:
>>> from mindspore import ops, Tensor >>> from mindspore import dtype as mstype >>> import numpy as np >>> axis = 0 >>> x = Tensor(np.array([0.3220, 0.9545, 0.7879, 0.0975, 0.3698, ... 0.5135, 0.5740, 0.3435, 0.1895, 0.8764, ... 0.4980, 0.9673, 0.9879, 0.6988, 0.9022, ... 0.9304, 0.1558, 0.0153, 0.1559, 0.9852]).reshape([2, 2, 5]), mstype.float32) >>> glu = ops.GLU(axis=axis) >>> y = glu(x) >>> print(y) [[[0.20028052 0.6916126 0.57412136 0.06512236 0.26307625] [0.3682598 0.3093122 0.17306386 0.10212085 0.63814086]]]