一直在等待,一直会等待 RNN系列--2

tf.nn.rnn_cell.MultiRNNCell

        单层RNN的能力有限,有时需要多层的RNN。将x输入第一层RNN的后得到隐层状态h,这个隐层状态就相当于第二层RNN的输入,第二层RNN的隐层状态又相当于第三层RNN的输入,以此类推。

num_units = [128, 64]
cells = [BasicLSTMCell(num_units=n) for n in num_units]
stacked_rnn_cell = MultiRNNCell(cells)

初始化参数

__init__(
    cells,
    state_is_tuple=True
)

cells: 以RNNCells构成的list
state_is_tuple: If True, accepted and returned states are n-tuples, where n = len(cells). If False, the states are all concatenated along the column axis. This latter behavior will soon be deprecated.

tf.nn.rnn_cell.GRUCell

        Gated Recurrent Unit cell
        源代码解析

@tf_export("nn.rnn_cell.GRUCell")
class GRUCell(LayerRNNCell):


  def call(self, inputs, state):
    """Gated recurrent unit (GRU) with nunits cells."""

    gate_inputs = math_ops.matmul(
        array_ops.concat([inputs, state], 1), self._gate_kernel)
    gate_inputs = nn_ops.bias_add(gate_inputs, self._gate_bias)

    value = math_ops.sigmoid(gate_inputs)
    r, u = array_ops.split(value=value, num_or_size_splits=2, axis=1)

    r_state = r * state

    candidate = math_ops.matmul(
        array_ops.concat([inputs, r_state], 1), self._candidate_kernel)
    candidate = nn_ops.bias_add(candidate, self._candidate_bias)

    c = self._activation(candidate)
    new_h = u * state + (1 - u) * c
    return new_h, new_h

tf.nn.rnn_cell.BasicLSTMCell

# TODO(scottzhu): Stop exporting this class in TF 2.0.
# TF 2.0 时将会被移除

@tf_export("nn.rnn_cell.BasicLSTMCell")
class BasicLSTMCell(LayerRNNCell):
  def call(self, inputs, state):
    """Long short-term memory cell (LSTM).

    Args:
      inputs: `2-D` tensor with shape `[batch_size, input_size]`.
      state: An `LSTMStateTuple` of state tensors, each shaped
        `[batch_size, num_units]`, if `state_is_tuple` has been set to
        `True`.  Otherwise, a `Tensor` shaped
        `[batch_size, 2 * num_units]`.

    Returns:
      A pair containing the new hidden state, and the new state (either a
        `LSTMStateTuple` or a concatenated state, depending on
        `state_is_tuple`).
    """
    sigmoid = math_ops.sigmoid
    one = constant_op.constant(1, dtype=dtypes.int32)
    # Parameters of gates are concatenated into one multiply for efficiency.
    if self._state_is_tuple:
      c, h = state
    else:
      c, h = array_ops.split(value=state, num_or_size_splits=2, axis=one)

    gate_inputs = math_ops.matmul(
        array_ops.concat([inputs, h], 1), self._kernel)
    gate_inputs = nn_ops.bias_add(gate_inputs, self._bias)

    # i = input_gate, j = new_input, f = forget_gate, o = output_gate
    i, j, f, o = array_ops.split(
        value=gate_inputs, num_or_size_splits=4, axis=one)

    forget_bias_tensor = constant_op.constant(self._forget_bias, dtype=f.dtype)
    # Note that using `add` and `multiply` instead of `+` and `*` gives a
    # performance improvement. So using those at the cost of readability.
    add = math_ops.add
    multiply = math_ops.multiply
    new_c = add(multiply(c, sigmoid(add(f, forget_bias_tensor))),
                multiply(sigmoid(i), self._activation(j)))
    new_h = multiply(self._activation(new_c), sigmoid(o))

    if self._state_is_tuple:
      new_state = LSTMStateTuple(new_c, new_h)
    else:
      new_state = array_ops.concat([new_c, new_h], 1)
    return new_h, new_state

tf.nn.rnn_cell.LSTMCell

@tf_export("nn.rnn_cell.LSTMCell")
class LSTMCell(LayerRNNCell):
  def call(self, inputs, state):
    """Run one step of LSTM.

    Args:
      inputs: input Tensor, 2D, `[batch, num_units].
      state: if `state_is_tuple` is False, this must be a state Tensor,
        `2-D, [batch, state_size]`.  If `state_is_tuple` is True, this must be a
        tuple of state Tensors, both `2-D`, with column sizes `c_state` and
        `m_state`.

    Returns:
      A tuple containing:

      - A `2-D, [batch, output_dim]`, Tensor representing the output of the
        LSTM after reading `inputs` when previous state was `state`.
        Here output_dim is:
           num_proj if num_proj was set,
           num_units otherwise.
      - Tensor(s) representing the new state of LSTM after reading `inputs` when
        the previous state was `state`.  Same type and shape(s) as `state`.

    Raises:
      ValueError: If input size cannot be inferred from inputs via
        static shape inference.
    """
    num_proj = self._num_units if self._num_proj is None else self._num_proj
    sigmoid = math_ops.sigmoid

    if self._state_is_tuple:
      (c_prev, m_prev) = state
    else:
      c_prev = array_ops.slice(state, [0, 0], [-1, self._num_units])
      m_prev = array_ops.slice(state, [0, self._num_units], [-1, num_proj])

    input_size = inputs.get_shape().with_rank(2)[1]
    if input_size.value is None:
      raise ValueError("Could not infer input size from inputs.get_shape()[-1]")

    # i = input_gate, j = new_input, f = forget_gate, o = output_gate
    lstm_matrix = math_ops.matmul(
        array_ops.concat([inputs, m_prev], 1), self._kernel)
    lstm_matrix = nn_ops.bias_add(lstm_matrix, self._bias)

    i, j, f, o = array_ops.split(
        value=lstm_matrix, num_or_size_splits=4, axis=1)
    # Diagonal connections
    if self._use_peepholes:
      c = (sigmoid(f + self._forget_bias + self._w_f_diag * c_prev) * c_prev +
           sigmoid(i + self._w_i_diag * c_prev) * self._activation(j))
    else:
      c = (sigmoid(f + self._forget_bias) * c_prev + sigmoid(i) *
           self._activation(j))

    if self._cell_clip is not None:
      # pylint: disable=invalid-unary-operand-type
      c = clip_ops.clip_by_value(c, -self._cell_clip, self._cell_clip)
      # pylint: enable=invalid-unary-operand-type
    if self._use_peepholes:
      m = sigmoid(o + self._w_o_diag * c) * self._activation(c)
    else:
      m = sigmoid(o) * self._activation(c)

    if self._num_proj is not None:
      m = math_ops.matmul(m, self._proj_kernel)

      if self._proj_clip is not None:
        # pylint: disable=invalid-unary-operand-type
        m = clip_ops.clip_by_value(m, -self._proj_clip, self._proj_clip)
        # pylint: enable=invalid-unary-operand-type

    new_state = (LSTMStateTuple(c, m) if self._state_is_tuple else
                 array_ops.concat([c, m], 1))
    return m, new_state
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CNN-RNN-CTC是一种用于语音识别的深度学习模型。这个模型结合了卷积神经网络(CNN),循环神经网络(RNN)和连续标签分类(CTC)的算法。 首先,卷积神经网络(CNN)被用来从原始语音信号中提取特征。CNN通过一系列卷积和池化操作,可以有效地捕捉到语音信号中的时频特征。这些特征在后续的处理中起到了很重要的作用。 其次,循环神经网络(RNN)在特征提取后的序列数据上进行处理。RNN具有记忆功能,可以处理变长的序列数据。这使得RNN能够更好地建模语音信号的时序关系,从而提高语音识别的性能。 最后,连续标签分类(CTC)是一种解决无对齐标签序列训练问题的方法。在语音识别中,输入序列和输出序列之间的对齐是未知的,这使得传统的监督学习方法难以应用。CTC通过引入一个空白标签和重复标签,可以将输入序列的输出序列映射到最有可能的标签序列。通过优化CTC损失函数,我们可以训练模型来进行语音识别,并且不需要进行手工的对齐。 总而言之,CNN-RNN-CTC模型将卷积神经网络的特征提取能力,循环神经网络的序列建模能力和连续标签分类的对齐能力相结合,能够有效地解决语音识别中的训练问题,提高语音识别的性能。 ### 回答2: CNN-RNN-CTC是一种常用的深度学习模型,适用于序列标注任务,如语音识别或文本识别。该模型结合了卷积神经网络(CNN)、循环神经网络(RNN)和连续条件随机场(CTC)的优势。 首先,CNN经常被用于图像处理任务,能够有效提取图像特征。在CNN-RNN-CTC模型中,CNN用来对输入的声学特征或图像进行特征提取,将其转化为更适合序列任务的形式。 其次,RNN是一种能够处理序列数据的神经网络,能够捕捉到数据的时间依赖关系。在CNN-RNN-CTC模型中,RNN用来对CNN提取的特征进行进一步处理,从而得到更加准确的序列标注结果。 最后,CTC是一种解决序列对齐问题的方法。在CNN-RNN-CTC模型中,CTC用来实现无对齐标签的序列学习,可以自动进行对齐和标注的训练。它中的条件随机场层可以根据输入序列和标签序列之间的对应关系,计算出最可能的标签序列。 综上所述,CNN-RNN-CTC模型能够利用CNN提取输入的特征,RNN处理序列数据,CTC解决标签对齐问题,从而有效地解决序列标注任务。在语音识别或文本识别等方面有较好的应用效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值