(features,
                        seq_lens,
                        rnn_celltype="lstm",
                        rnn_nlayers=2,
                        rnn_nunits=128,
                        rnn_bidirectional=True,
                        dtype=tf.float32)
return x, state
Constructs an LSTM-based encoder.
Constructs an LSTM-based encoder.
Constructs an LSTM - based encoder .
def simple_lstm_encoder(features,
                        seq_lens,
                        rnn_celltype="lstm",
                        rnn_nlayers=2,
                        rnn_nunits=128,
                        rnn_bidirectional=True,
                        dtype=tf.float32):
  """Constructs an LSTM-based encoder."""
  x = features

  with tf.variable_scope("rnn_input"):
    x = tf.layers.dense(x, rnn_nunits)

  if rnn_celltype == "lstm":
    celltype = contrib_rnn.LSTMBlockCell
  else:
    raise NotImplementedError()

  cell = rnn.MultiRNNCell(
      [celltype(rnn_nunits) for _ in range(rnn_nlayers)])

  with tf.variable_scope("rnn"):
    if rnn_bidirectional:
      (x_fw, x_bw), (state_fw, state_bw) = tf.nn.bidirectional_dynamic_rnn(
          cell_fw=cell,
          cell_bw=cell,
          inputs=x,
          sequence_length=seq_lens,
          dtype=dtype)
      x = tf.concat([x_fw, x_bw], axis=2)

      state_fw, state_bw = state_fw[-1].h, state_bw[-1].h
      state = tf.concat([state_fw, state_bw], axis=1)
    else:
      # initial_state = cell.zero_state(batch_size, dtype)
      x, state = tf.nn.dynamic_rnn(
          cell=cell, inputs=x, sequence_length=seq_lens, dtype=dtype)
      state = state[-1].h

  return x, state
def simple_lstm_encoder ( features , seq_lens , rnn_celltype = "lstm" , rnn_nlayers = 2 , rnn_nunits = 128 , rnn_bidirectional = True , dtype = tf . float32 ) : x = features with tf . variable_scope ( "rnn_input" ) : x = tf . layers . dense ( x , rnn_nunits ) if rnn_celltype == "lstm" : celltype = contrib_rnn . LSTMBlockCell else : raise NotImplementedError ( ) cell = rnn . MultiRNNCell ( [ celltype ( rnn_nunits ) for _ in range ( rnn_nlayers ) ] ) with tf . variable_scope ( "rnn" ) : if rnn_bidirectional : ( x_fw , x_bw ) , ( state_fw , state_bw ) = tf . nn . bidirectional_dynamic_rnn ( cell_fw = cell , cell_bw = cell , inputs = x , sequence_length = seq_lens , dtype = dtype ) x = tf . concat ( [ x_fw , x_bw ] , axis = 2 ) state_fw , state_bw = state_fw [ - 1 ] . h , state_bw [ - 1 ] . h state = tf . concat ( [ state_fw , state_bw ] , axis = 1 ) else : # initial_state = cell.zero_state(batch_size, dtype) x , state = tf . nn . dynamic_rnn ( cell = cell , inputs = x , sequence_length = seq_lens , dtype = dtype ) state = state [ - 1 ] . h return x , state