tensorflow RNN layer的搭建(GRU,LSTM等)

以GRU为例:

import tensorflow as tf

def get_encoder_layer(inputs, rnn_size, num_layers, layer_name):
    
    with tf.variable_scope(layer_name):
            
        def get_gru_cell(rnn_size):
            gru_cell = tf.contrib.rnn.GRUCell(rnn_size, kernel_initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=2),bias_initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=2))
            return gru_cell

        cell = tf.contrib.rnn.MultiRNNCell([get_gru_cell(rnn_size) for _ in range(num_layers)])

        encoder_output, encoder_state = tf.nn.dynamic_rnn(cell, inputs, dtype=tf.float32)

    return encoder_output, encoder_state

其中,inputs的维度为[None, step_dim, item_dim] 分别为: 样本量*RNN时间步数*每一步输入向量长度
rnn_size 即为item_dim
num_layers 为RNN层数
encoder_output, encoder_state为RNN的两个输出,分别为所有状态的列表 [ h 1 , h 2 , ⋯   , h N ] [h_1, h_2, \cdots, h_N] [h1,h2,,hN]和最后一个状态 h N h_N hN
在使用中可根据需要任选其一。
对于LSTM,只需将其中的gru_cell替换为lstm_cell即可:

import tensorflow as tf

def get_lstm_layer(inputs, rnn_size, num_layers, layer_name):

    with tf.variable_scope(layer_name):

        def get_lstm_cell(rnn_size):
            lstm_cell = tf.contrib.rnn.LSTMCell(rnn_size, initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=2))
            return lstm_cell

        cell = tf.contrib.rnn.MultiRNNCell([get_lstm_cell(rnn_size) for _ in range(num_layers)])

        encoder_output, encoder_state = tf.nn.dynamic_rnn(cell, inputs, dtype=tf.float32)

    return encoder_output, encoder_state

你可能感兴趣的:(技术总结)