mindspore.ops.DynamicGRUV2
- class mindspore.ops.DynamicGRUV2(direction='UNIDIRECTIONAL', cell_depth=1, keep_prob=1.0, cell_clip=- 1.0, num_proj=0, time_major=True, activation='tanh', gate_order='rzh', reset_after=True, is_training=True)[源代码]
为输入序列应用一个单层GRU(gated recurrent unit)。
其中
是在时刻t+1的隐藏状态, 是时刻t+1的输入, 为时刻t的隐藏状态或时刻0的初始隐藏状态。 、 、 分别为重置门、更新门和当前候选集。 , 为可学习权重和偏置。 是sigmoid激活函数, 为Hadamard乘积。- 参数:
direction (str,可选) - 指定GRU方向,str类型。默认值:
"UNIDIRECTIONAL"
。目前仅支持"UNIDIRECTIONAL"
。cell_depth (int,可选) - GRU单元深度。默认值:
1
。keep_prob (float,可选) - Dropout保留概率。默认值:
1.0
。cell_clip (float,可选) - 输出裁剪率。默认值:
-1.0
。num_proj (int,可选) - 投影维度。默认值:
0
。time_major (bool,可选) - 如为
True
,则指定输入的第一维度为序列长度 num_step ,如为False则第一维度为 batch_size 。默认值:True
。activation (str,可选) - 字符串,指定activation类型。默认值:
"tanh"
。目前仅支持取值"tanh"
。gate_order (str,可选) - 字符串,指定weight和bias中门的排列顺序,可选值为
"rzh"
或"zrh"
。默认值:"rzh"
。"rzh"
代表顺序为:重置门、更新门、隐藏门。"zrh"
代表顺序为:更新门、重置门、隐藏门。reset_after (bool,可选) - 是否在矩阵乘法后使用重置门。默认值:
True
。is_training (bool,可选) - 是否为训练模式。默认值:
True
。
- 输入:
x (Tensor) - 输入词序列。shape:
。数据类型支持float16。weight_input (Tensor) - 权重
。 shape: 。 数据类型支持float16。weight_hidden (Tensor) - 权重
。 shape: 。 数据类型支持float16。bias_input (Tensor) - 偏差
。shape: ,或 None 。与输入 init_h 的数据类型相同。bias_hidden (Tensor) - 偏差
。shape: ,或 None 。与输入 init_h 的数据类型相同。seq_length (Tensor) - 每个batch中序列的长度。shape:
。 目前仅支持 None 。init_h (Tensor) - 初始隐藏状态。shape:
。 数据类型支持float16和float32。
- 输出:
y (Tensor) - Tensor,与 bias_type 数据类型相同。shape如下:
,如果 num_proj 大于0, ,如果 num_proj 等于0。
output_h (Tensor) - Tensor,shape:
。与 bias_type 数据类型相同。update (Tensor) - Tensor,shape:
。与 bias_type 数据类型相同。reset (Tensor) - Tensor,shape:
。与 bias_type 数据类型相同。new (Tensor) - Tensor,shape:
。与 bias_type 数据类型相同。hidden_new (Tensor) - Tensor,shape:
。与 bias_type 数据类型相同。 关于 bias_type :如果 bias_input 和 bias_hidden 均为 None ,则 bias_type 为 init_h 的数据类型。
如果 bias_input 不为 None ,则 bias_type 为 bias_input 的数据类型。
如果 bias_input 为 None 而 bias_hidden 不为 None ,则 bias_type 为 bias_hidden 的数据类型。
- 异常:
TypeError - direction 、 activation 或 gate_order 不是str。
TypeError - cell_depth 或 num_proj 不是int类型。
TypeError - keep_prob 或 cell_clip 不是float类型。
TypeError - time_major 、 reset_after 或 is_training 不是bool类型。
TypeError - x 、 weight_input 、 weight_hidden 、 bias_input 、 bias_hidden 、 seq_length 或 ini_h 不是Tensor。
TypeError - x 、 weight_input 或 weight_hidden 的数据类型非float16。
TypeError - init_h 数据类型非float16或float32。
- 支持平台:
Ascend
样例:
>>> import numpy as np >>> from mindspore import Tensor, ops >>> x = Tensor(np.random.rand(2, 8, 64).astype(np.float16)) >>> weight_i = Tensor(np.random.rand(64, 48).astype(np.float16)) >>> weight_h = Tensor(np.random.rand(16, 48).astype(np.float16)) >>> bias_i = Tensor(np.random.rand(48).astype(np.float16)) >>> bias_h = Tensor(np.random.rand(48).astype(np.float16)) >>> init_h = Tensor(np.random.rand(8, 16).astype(np.float16)) >>> dynamic_gru_v2 = ops.DynamicGRUV2() >>> output = dynamic_gru_v2(x, weight_i, weight_h, bias_i, bias_h, None, init_h) >>> print(output[0].shape) (2, 8, 16)