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

rt+1=σ(Wirxt+1+bir+Whrh(t)+bhr)zt+1=σ(Wizxt+1+biz+Whzh(t)+bhz)nt+1=tanh(Winxt+1+bin+rt+1(Whnh(t)+bhn))ht+1=(1zt+1)nt+1+zt+1h(t)

其中 ht+1 是在时刻t+1的隐藏状态, xt+1 是时刻t+1的输入, ht 为时刻t的隐藏状态或时刻0的初始隐藏状态。 rt+1zt+1nt+1 分别为重置门、更新门和当前候选集。 Wb 为可学习权重和偏置。 σ 是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: (num_step,batch_size,input_size) 。数据类型支持float16。

  • weight_input (Tensor) - 权重 W{ir,iz,in} 。 shape: (input_size,3×hidden_size) 。 数据类型支持float16。

  • weight_hidden (Tensor) - 权重 W{hr,hz,hn} 。 shape: (hidden_size,3×hidden_size) 。 数据类型支持float16。

  • bias_input (Tensor) - 偏差 b{ir,iz,in} 。shape: (3×hidden_size) ,或 None 。与输入 init_h 的数据类型相同。

  • bias_hidden (Tensor) - 偏差 b{hr,hz,hn} 。shape: (3×hidden_size) ,或 None 。与输入 init_h 的数据类型相同。

  • seq_length (Tensor) - 每个batch中序列的长度。shape: (batch_size) 。 目前仅支持 None

  • init_h (Tensor) - 初始隐藏状态。shape: (batch_size,hidden_size) 。 数据类型支持float16和float32。

输出:
  • y (Tensor) - Tensor,与 bias_type 数据类型相同。shape如下:

    • (num_step,batch_size,min(hidden_size,num_proj)) ,如果 num_proj 大于0,

    • (num_step,batch_size,hidden_size) ,如果 num_proj 等于0。

  • output_h (Tensor) - Tensor,shape: (num_step,batch_size,hidden_size) 。与 bias_type 数据类型相同。

  • update (Tensor) - Tensor,shape: (num_step,batch_size,hidden_size) 。与 bias_type 数据类型相同。

  • reset (Tensor) - Tensor,shape: (num_step,batch_size,hidden_size) 。与 bias_type 数据类型相同。

  • new (Tensor) - Tensor,shape: (num_step,batch_size,hidden_size) 。与 bias_type 数据类型相同。

  • hidden_new (Tensor) - Tensor,shape: (num_step,batch_size,hidden_size) 。与 bias_type 数据类型相同。 关于 bias_type :

    • 如果 bias_inputbias_hidden 均为 None ,则 bias_typeinit_h 的数据类型。

    • 如果 bias_input 不为 None ,则 bias_typebias_input 的数据类型。

    • 如果 bias_inputNonebias_hidden 不为 None ,则 bias_typebias_hidden 的数据类型。

异常:
  • TypeError - directionactivationgate_order 不是str。

  • TypeError - cell_depthnum_proj 不是int类型。

  • TypeError - keep_probcell_clip 不是float类型。

  • TypeError - time_majorreset_afteris_training 不是bool类型。

  • TypeError - xweight_inputweight_hiddenbias_inputbias_hiddenseq_lengthini_h 不是Tensor。

  • TypeError - xweight_inputweight_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)