mindspore.ops.DynamicRNN

class mindspore.ops.DynamicRNN(cell_type='LSTM', direction='UNIDIRECTIONAL', cell_depth=1, use_peephole=False, keep_prob=1.0, cell_clip=- 1.0, num_proj=0, time_major=True, activation='tanh', forget_bias=0.0, is_training=True)[源代码]

将循环神经网络应用到输入上。当前仅支持LSTM。

it+1=σ(Wixxt+1+bix+Wihh(t)+bih)ft+1=σ(Wfxxt+1+bfx+Wfhh(t)+bfh)c~t+1=tanh(Wcxxt+1+bcx+Wchh(t)+bch)ot+1=σ(Woxxt+1+box+Wohh(t)+boh)ct+1=ft+1c(t)+itc~t+1ht+1=ot+1tanh(ct+1)

其中, ht+1 是在 t+1 时刻的隐藏状态。 xt+1 是在 t+1 时刻的输入。 ht 是在 t 时刻的隐藏状态或在 0 时刻的初始隐藏状态。 σ 是sigmoid函数, Hadamard 积。 W,b 是公式中输出和输入之间的可学习权重。

例如, Wixbix 是把 x 转换为 i 的权重和偏置。

参数:
  • cell_type (str) - 指定Cell类型。当前仅支持LSTM。默认值:LSTM。

  • direction (str) - 指定单向或双向。默认值:UNIDIRECTIONAL。当前仅支持UNIDIRECTIONAL。

  • cell_depth (int) - 指定cell的层数。默认值:1。

  • use_peephole (bool) - 是否使用”peephole connections”。默认值:False。

  • keep_prob (float) - 指定保留率,即每个元素被保留的概率。1.0表示所有元素全部保留。默认值:1.0。

  • cell_clip (float) - 将Cell裁剪到指定的值,负值表示禁用。默认值:-1.0。

  • num_proj (int) - 投影矩阵的输出维数。默认值:0。

  • time_major (bool) - 指定输入 x 的数据排列格式。如果为True,格式为 (num_step,batch_size,input_size),如果为False,格式为:(batch_size,num_step,input_size) 。默认值:True。当前仅支持True。

  • activation (str) - 指定激活函数。默认值:tanh。当前仅支持tanh。

  • forget_bias (float) - 指定遗忘门的偏置。默认值:0.0。

  • is_training (bool) - 指定是否开启训练。默认值:True。

输入:
  • x (Tensor) - 输入的词汇。shape为 (num_step,batch_size,input_size) 的Tensor。数据类型必须为float16。

  • w (Tensor) - 输入的权重。shape为 (input_size+hidden_size,4hidden_size) 的Tensor。数据类型必须为float16。

  • b (Tensor) - 输入的偏置。shape为 (4hidden_size) 的Tensor。数据类型必须为float16或float32。

  • seq_length (Tensor) - 每个批次中句子的真实长度。shape为 (batch_size,) 的Tensor。当前仅支持None。

  • init_h (Tensor) - 在初始时刻的隐藏状态。shape为 (1,batch_size,hidden_size) 的Tensor。数据类型必须为float16。

  • init_c (Tensor) - 在初始时刻的Cell状态。shape为 (1,batch_size,hidden_size) 的Tensor。数据类型必须为float16。

输出:
  • y (Tensor) - 所有时刻输出层的输出向量,shape为 (num_step,batch_size,hidden_size) 的Tensor。数据类型与输入 b 相同。

  • output_h (Tensor) - 所有时刻输出层的输出向量,shape为 (num_step,batch_size,hidden_size) 的Tensor。数据类型为float16。

  • output_c (Tensor) - 所有时刻的Cell状态的输出向量,shape为 (num_step,batch_size,hidden_size) 的Tensor。数据类型与输入 b 相同。

  • i (Tensor) - 更新输入门的权重,shape为 (num_step,batch_size,hidden_size) 的Tensor。数据类型与输入 b 相同。

  • j (Tensor) - 更新新门的权重,shape为 (num_step,batch_size,hidden_size) 的Tensor。数据类型与输入 b 相同。

  • f (Tensor) - 更新遗忘门的权重,shape为 (num_step,batch_size,hidden_size) 的Tensor。数据类型输入 b 相同。

  • o (Tensor) - 更新输出门的权重,shape为 (num_step,batch_size,hidden_size) 的Tensor。数据类型与输入 b 相同。

  • tanhct (Tensor) - 更新tanh的权重,shape为 (num_step,batch_size,hidden_size) 的Tensor。数据类型与输入 b 相同。

异常:
  • TypeError - cell_typedirectionactivation 不是str。

  • TypeError - cell_Deepnum_proj 不是int。

  • TypeError - keep_probcell_clipforget_bias 不是float。

  • TypeError - use_peehpoltime_majoris_training 不是bool。

  • TypeError - xwbseq_lengthinit_hinit_c 不是Tensor。

  • TypeError - xwinit_hnit_c 的数据类型不是float16。

  • TypeError - b 的数据类型既不是float16也不是float32。

支持平台:

Ascend

样例:

>>> x = Tensor(np.random.rand(2, 16, 64).astype(np.float16))
>>> w = Tensor(np.random.rand(96, 128).astype(np.float16))
>>> b = Tensor(np.random.rand(128).astype(np.float16))
>>> init_h = Tensor(np.random.rand(1, 16, 32).astype(np.float16))
>>> init_c = Tensor(np.random.rand(1, 16, 32).astype(np.float16))
>>> dynamic_rnn = ops.DynamicRNN()
>>> output = dynamic_rnn(x, w, b, None, init_h, init_c)
>>> print(output[0].shape)
(2, 16, 32)