(self, inputs, state)
return output, new_state
Long short-term memory cell with attention (LSTMA).
Long short-term memory cell with attention (LSTMA).
Long short - term memory cell with attention ( LSTMA ) .
def call(self, inputs, state):
    """Long short-term memory cell with attention (LSTMA)."""
    if self._state_is_tuple:
      state, attns, attn_states = state
    else:
      states = state
      state = tf.slice(states, [0, 0], [-1, self._cell.state_size])
      attns = tf.slice(states, [0, self._cell.state_size],
                       [-1, self._attn_size])
      attn_states = tf.slice(
          states, [0, self._cell.state_size + self._attn_size],
          [-1, self._attn_size * self._attn_length])
    attn_states = tf.reshape(attn_states,
                             [-1, self._attn_length, self._attn_size])
    input_size = self._input_size
    if input_size is None:
      input_size = inputs.get_shape().as_list()[1]
    if self._linear1 is None:
      self._linear1 = _Linear([inputs, attns], input_size, True)
    inputs = self._linear1([inputs, attns])
    cell_output, new_state = self._cell(inputs, state)
    if self._state_is_tuple:
      new_state_cat = tf.concat(tf.nest.flatten(new_state), 1)
    else:
      new_state_cat = new_state
    new_attns, new_attn_states = self._attention(new_state_cat, attn_states)
    with tf.variable_scope("attn_output_projection"):
      if self._linear2 is None:
        self._linear2 = _Linear([cell_output, new_attns], self._attn_size, True)
      output = self._linear2([cell_output, new_attns])
    new_attn_states = tf.concat(
        [new_attn_states, tf.expand_dims(output, 1)], 1)
    new_attn_states = tf.reshape(
        new_attn_states, [-1, self._attn_length * self._attn_size])
    new_state = (new_state, new_attns, new_attn_states)
    if not self._state_is_tuple:
      new_state = tf.concat(list(new_state), 1)
    return output, new_state
def call ( self , inputs , state ) : if self . _state_is_tuple : state , attns , attn_states = state else : states = state state = tf . slice ( states , [ 0 , 0 ] , [ - 1 , self . _cell . state_size ] ) attns = tf . slice ( states , [ 0 , self . _cell . state_size ] , [ - 1 , self . _attn_size ] ) attn_states = tf . slice ( states , [ 0 , self . _cell . state_size + self . _attn_size ] , [ - 1 , self . _attn_size * self . _attn_length ] ) attn_states = tf . reshape ( attn_states , [ - 1 , self . _attn_length , self . _attn_size ] ) input_size = self . _input_size if input_size is None : input_size = inputs . get_shape ( ) . as_list ( ) [ 1 ] if self . _linear1 is None : self . _linear1 = _Linear ( [ inputs , attns ] , input_size , True ) inputs = self . _linear1 ( [ inputs , attns ] ) cell_output , new_state = self . _cell ( inputs , state ) if self . _state_is_tuple : new_state_cat = tf . concat ( tf . nest . flatten ( new_state ) , 1 ) else : new_state_cat = new_state new_attns , new_attn_states = self . _attention ( new_state_cat , attn_states ) with tf . variable_scope ( "attn_output_projection" ) : if self . _linear2 is None : self . _linear2 = _Linear ( [ cell_output , new_attns ] , self . _attn_size , True ) output = self . _linear2 ( [ cell_output , new_attns ] ) new_attn_states = tf . concat ( [ new_attn_states , tf . expand_dims ( output , 1 ) ] , 1 ) new_attn_states = tf . reshape ( new_attn_states , [ - 1 , self . _attn_length * self . _attn_size ] ) new_state = ( new_state , new_attns , new_attn_states ) if not self . _state_is_tuple : new_state = tf . concat ( list ( new_state ) , 1 ) return output , new_state