//  [python]
def encoder(features, mode, vocab, hps):
  """Model function.

  Atttention seq2seq model, augmented with an encoder
  over the targets of the nearest neighbors.

  Args:
    features: Dictionary of input Tensors.
    mode: train or eval. Keys from tf.estimator.ModeKeys.
    vocab: A list of strings of words in the vocabulary.
    hps: Hyperparams.

  Returns:
    Encoder outputs.
  """

  # [batch_size, src_len]
  src_inputs = features["src_inputs"]
  src_len = features["src_len"]

  with tf.variable_scope("embeddings"):
    embeddings = tf.get_variable(
        "embeddings",
        [vocab.size(), hps.emb_dim],
        dtype=tf.float32,
        initializer=tf.initializers.variance_scaling())

  # [batch_size, src_len, emb_dim]
  src_encoder_input_emb = tf.nn.embedding_lookup(embeddings, src_inputs)
  if mode == tf_estimator.ModeKeys.TRAIN and hps.emb_drop > 0.:
    src_encoder_input_emb = tf.nn.dropout(
        src_encoder_input_emb, keep_prob=1.0-hps.emb_drop)

  src_att_context, neighbor_att_context = None, None
  src_copy_context, neighbor_copy_context = None, None
  with tf.variable_scope("src_encoder"):

    # 2 * [batch_size, src_len, encoder_dim]
    src_encoder_outputs, src_encoder_states = tf.nn.bidirectional_dynamic_rnn(
        cell_fw=get_rnn_cell(
            mode=mode, hps=hps,
            input_dim=hps.emb_dim,
            num_units=hps.encoder_dim,
            num_layers=hps.num_encoder_layers,
            dropout=hps.encoder_drop,
            cell_type=hps.rnn_cell),
        cell_bw=get_rnn_cell(
            mode=mode, hps=hps,
            input_dim=hps.emb_dim,
            num_units=hps.encoder_dim,
            num_layers=hps.num_encoder_layers,
            dropout=hps.encoder_drop,
            cell_type=hps.rnn_cell),
        inputs=src_encoder_input_emb,
        dtype=tf.float32,
        sequence_length=src_len)

    # [batch_size, src_len, 2*encoder_dim]
    src_encoder_outputs = tf.concat(src_encoder_outputs, 2)
    with tf.variable_scope("src_att_context"):
      src_att_context = _build_context(
          hps=hps,
          encoder_outputs=src_encoder_outputs)
    if hps.use_copy:
      with tf.variable_scope("src_copy_context"):
        src_copy_context = _build_context(
            hps=hps,
            encoder_outputs=src_encoder_outputs)

  if hps.model == "nn2seq":

    # [batch_size, neighbor_len]
    neighbor_inputs = features["neighbor_inputs"]
    neighbor_len = features["neighbor_len"]

    # [batch_size, neighbor_len, emb_dim]
    neighbor_input_emb = tf.nn.embedding_lookup(
        embeddings, neighbor_inputs)
    if mode == tf_estimator.ModeKeys.TRAIN and hps.emb_drop > 0.:
      neighbor_input_emb = tf.nn.dropout(
          neighbor_input_emb, keep_prob=1.0-hps.emb_drop)
    with tf.variable_scope("neighbor_encoder"):
      # 2 * [batch_size, neighbor_len, encoder_dim]
      neighbor_encoder_outputs, _ = \
          tf.nn.bidirectional_dynamic_rnn(
              cell_fw=get_rnn_cell(
                  mode=mode, hps=hps,
                  input_dim=hps.emb_dim,
                  num_units=hps.encoder_dim,
                  num_layers=1,
                  dropout=hps.encoder_drop,
                  cell_type=hps.rnn_cell),
              cell_bw=get_rnn_cell(
                  mode=mode, hps=hps,
                  input_dim=hps.emb_dim,
                  num_units=hps.encoder_dim,
                  num_layers=1,
                  dropout=hps.encoder_drop,
                  cell_type=hps.rnn_cell),
              inputs=neighbor_input_emb,
              dtype=tf.float32,
              sequence_length=neighbor_len)

      neighbor_encoder_outputs = tf.concat(neighbor_encoder_outputs, 2)
      with tf.variable_scope("neighbor_att_context"):
        neighbor_att_context = _build_context(
            hps=hps,
            encoder_outputs=neighbor_encoder_outputs)
      if hps.use_copy:
        with tf.variable_scope("neighbor_copy_context"):
          neighbor_copy_context = _build_context(
              hps=hps,
              encoder_outputs=neighbor_encoder_outputs)
  att_context, copy_context = None, None
  if hps.model == "nn2seq":
    att_context = tf.concat([src_att_context, neighbor_att_context], 1)
    if hps.use_copy:
      copy_context = tf.concat(
          [src_copy_context, neighbor_copy_context], 1)
  elif hps.model == "seq2seq":
    att_context = src_att_context
    if hps.use_copy:
      copy_context = src_copy_context
  else:
    assert False, "baseline `model` should be [`nn2seq`, `seq2seq`]."
  if hps.use_bridge:
    with tf.variable_scope("bridge"):
      out_dim = hps.num_decoder_layers * hps.decoder_dim
      if hps.rnn_cell == "lstm":
        fw_states, bw_states = src_encoder_states
        hs = tf.concat([fw_states[-1].h, bw_states[-1].h], axis=1)
        cs = tf.concat([fw_states[-1].c, bw_states[-1].c], axis=1)

        h_state = tf.layers.dense(
            hs, units=out_dim,
            activation=tf.nn.tanh,
            use_bias=True,
            kernel_initializer=tf.contrib.layers.xavier_initializer(),
            name="h_layer")
        c_state = tf.layers.dense(
            cs, units=out_dim,
            activation=tf.nn.tanh,
            use_bias=True,
            kernel_initializer=tf.contrib.layers.xavier_initializer(),
            name="c_layer")
      elif hps.rnn_cell == "gru":
        fw_states, bw_states = src_encoder_states
        hs = tf.concat([fw_states[-1], bw_states[-1]], axis=1)
        h_state = tf.layers.dense(
            hs, units=out_dim,
            activation=tf.nn.tanh,
            use_bias=True,
            kernel_initializer=tf.contrib.layers.xavier_initializer(),
            name="h_layer")
        c_state = None
  else:
    h_state, c_state = None, None

  return EncoderOutputs(
      embeddings=embeddings,
      att_context=att_context,
      copy_context=copy_context,
      states=(h_state, c_state)
  )