美文网首页
tensorflow 循环神经网络RNN

tensorflow 循环神经网络RNN

作者: AfterlifeYeah | 来源:发表于2017-04-02 17:11 被阅读0次
    • 在 tensorflow 中实现 LSTM 结构的循环神经网络的前向传播过程,即使用 BasicLSTMCell
    # 定义一个 LSTM 结构,LSTM 中使用的变量会在该函数中自动被声明
    lstm = tf.contrib.rnn.BasicLSTMCell(lstm_hidden_size)
    
    # 将 LSTM 中的状态初始化为全 0  数组,batch_size 给出一个 batch 的大小
    state = lstm.zero_state(batch_size, tf.float32)
    
    # 定义损失函数
    loss = 0.0
    
    # num_steps 表示最大的序列长度
    for i in range(num_steps):
      # 在第一个时刻声明 LSTM 结构中使用的变量,在之后的时刻都需要服用之前定义好的变量
      if i>0:
        tf.get_variable_scope().reuse_variables()
      # 每一步处理时间序列中的一个时刻。将当前输入(current_input)和前一时刻状态(state)传入定义的 LSTM 结构就可以得到当前 LSTM 结构的输出 lstm_output 和更新后的状态 state
      lstm_output, state = lstm(current_input, state)
    
      # 将当前时刻 LSTM 结构的输出传入一个全连接层得到最后的输出
      final_output = fully_connected(lstm_output)
    
      # 计算当前时刻输出的损失
      loss += calc_loss(final_output, expected_output)
    
    • 在 tensorflow中实现双向RNN(BiRNN),使用 MultiRNNCell
    lstm = tf.contrib.rnn.BasicLSTMCell(lstm_hidden_size)
    # 使用 MultiRNNCell 类实现深层循环网络中每一个时刻的前向传播过程,number_of_layers 表示有多少层
    stacked_lstm = tf.contrib.rnn.MultiRNNCell([lstm] * number_of_layers)
    
    state = stacked_lstm.zero_state(batch_size, tf.float32)
    
    for i in range(len(num_steps)):
      if i>0:
        tf.get_variable_scope().reuse_variables()
      stacked_lstm_output, state = stacked_lstm(current_input, state)
      final_output = fully_connected(stacked_lstm_output)
      loss += calc_loss(final_output, expected_output)
    
    • 循环神经网络 RNN 中的 dropout, 使用 DropoutWrapper
    # 定义 LSTM 结构
    lstm = tf.contrib.rnn.BasicLSTMCell(lstm_hidden_size)
    
    # 使用 DropoutWrapper 类来实现 dropout 功能,input_keep_prob 控制输出的 dropout 概率
    dropout_lstm = tf.contrib.rnn.DropoutWrapper(lstm, input_keep_prob=0.5)
    
    stacked_lstm = tf.contrib.rnn.MultiRNNCell([dropout_lstm] * number_of_layers)
    

    相关文章

      网友评论

          本文标题:tensorflow 循环神经网络RNN

          本文链接:https://www.haomeiwen.com/subject/myugottx.html