class_docstr: Control
      
docstr: Builds the model.
          
code: def _build_model(self, inputs, vocab, gamma, lambda_g):
      """Builds the model."""
      embedder = WordEmbedder(
          vocab_size=vocab.size, hparams=self._hparams.embedder
      )
      encoder = UnidirectionalRNNEncoder(hparams=self._hparams.encoder)
  
      # text_ids for encoder, with BOS token removed
      enc_text_ids = inputs["text_ids"][:, 1:]
      enc_outputs, final_state = encoder(
          embedder(enc_text_ids), sequence_length=inputs["length"] - 1
      )
      z = final_state[:, self._hparams.dim_c :]
  
      # Encodes label
      label_connector = MLPTransformConnector(self._hparams.dim_c)
  
      # Gets the sentence representation: h = (c, z)
      labels = tf.cast(
          tf.reshape(inputs["labels"], [-1, 1]), tf.float32
      )
      c = label_connector(labels)
      c_ = label_connector(1 - labels)
      h = tf.concat([c, z], 1)
      h_ = tf.concat([c_, z], 1)
  
      # Teacher-force decoding and the auto-encoding loss for G
      decoder = AttentionRNNDecoder(
          memory=enc_outputs,
          memory_sequence_length=inputs["length"] - 1,
          cell_input_fn=lambda inputs, attention: inputs,
          vocab_size=vocab.size,
          hparams=self._hparams.decoder,
      )
  
      connector = MLPTransformConnector(decoder.state_size)
  
      g_outputs, _, _ = decoder(
          initial_state=connector(h),
          inputs=inputs["text_ids"],
          embedding=embedder,
          sequence_length=inputs["length"] - 1,
      )
  
      loss_g_ae = tx.losses.sequence_sparse_softmax_cross_entropy(
          labels=inputs["text_ids"][:, 1:],
          logits=g_outputs.logits,
          sequence_length=inputs["length"] - 1,
          average_across_timesteps=True,
          sum_over_timesteps=False,
      )
  
      # Gumbel-softmax decoding, used in training
      start_tokens = tf.ones_like(inputs["labels"]) * vocab.bos_token_id
      end_token = vocab.eos_token_id
      gumbel_helper = GumbelSoftmaxEmbeddingHelper(
          embedder.embedding, start_tokens, end_token, gamma
      )
  
      (
          soft_outputs_,
          _,
          soft_length_,
      ) = decoder(helper=gumbel_helper, initial_state=connector(h_))
  
      # Greedy decoding, used in eval
      outputs_, _, length_ = decoder(
          decoding_strategy="infer_greedy",
          initial_state=connector(h_),
          embedding=embedder,
          start_tokens=start_tokens,
          end_token=end_token,
      )
  
      # Creates classifier
      classifier = Conv1DClassifier(hparams=self._hparams.classifier)
      clas_embedder = WordEmbedder(
          vocab_size=vocab.size, hparams=self._hparams.embedder
      )
  
      # Classification loss for the classifier
      clas_logits, clas_preds = classifier(
          inputs=clas_embedder(ids=inputs["text_ids"][:, 1:]),
          sequence_length=inputs["length"] - 1,
      )
      loss_d_clas = tf.nn.sigmoid_cross_entropy_with_logits(
          labels=tf.cast(inputs["labels"], tf.float32),
          logits=clas_logits,
      )
      loss_d_clas = tf.reduce_mean(loss_d_clas)
      accu_d = tx.evals.accuracy(
          labels=inputs["labels"], preds=clas_preds
      )
  
      # Classification loss for the generator, based on soft samples
      soft_logits, soft_preds = classifier(
          inputs=clas_embedder(soft_ids=soft_outputs_.sample_id),
          sequence_length=soft_length_,
      )
      loss_g_clas = tf.nn.sigmoid_cross_entropy_with_logits(
          labels=tf.cast(1 - inputs["labels"], tf.float32),
          logits=soft_logits,
      )
      loss_g_clas = tf.reduce_mean(loss_g_clas)
  
      # Accuracy on soft samples, for training progress monitoring
      accu_g = tx.evals.accuracy(
          labels=1 - inputs["labels"], preds=soft_preds
      )
  
      # Accuracy on greedy-decoded samples, for training progress monitoring
      _, gdy_preds = classifier(
          inputs=clas_embedder(ids=outputs_.sample_id),
          sequence_length=length_,
      )
      accu_g_gdy = tx.evals.accuracy(
          labels=1 - inputs["labels"], preds=gdy_preds
      )
  
      # Aggregates losses
      loss_g = loss_g_ae + lambda_g * loss_g_clas
      loss_d = loss_d_clas
  
      # Creates optimizers
      g_vars = collect_trainable_variables(
          [embedder, encoder, label_connector, connector, decoder]
      )
      d_vars = collect_trainable_variables([clas_embedder, classifier])
  
      train_op_g = get_train_op(
          loss_g, g_vars, hparams=self._hparams.opt
      )
      train_op_g_ae = get_train_op(
          loss_g_ae, g_vars, hparams=self._hparams.opt
      )
      train_op_d = get_train_op(
          loss_d, d_vars, hparams=self._hparams.opt
      )
  
      # Interface tensors
      self.losses = {
          "loss_g": loss_g,
          "loss_g_ae": loss_g_ae,
          "loss_g_clas": loss_g_clas,
          "loss_d": loss_d_clas,
      }
      self.metrics = {
          "accu_d": accu_d,
          "accu_g": accu_g,
          "accu_g_gdy": accu_g_gdy,
      }
      self.train_ops = {
          "train_op_g": train_op_g,
          "train_op_g_ae": train_op_g_ae,
          "train_op_d": train_op_d,
      }
      self.samples = {
          "original": inputs["text_ids"][:, 1:],
          "transferred": outputs_.sample_id,
      }
  
      self.fetches_train_g = {
          "loss_g": self.train_ops["train_op_g"],
          "loss_g_ae": self.losses["loss_g_ae"],
          "loss_g_clas": self.losses["loss_g_clas"],
          "accu_g": self.metrics["accu_g"],
          "accu_g_gdy": self.metrics["accu_g_gdy"],
      }
      self.fetches_train_d = {
          "loss_d": self.train_ops["train_op_d"],
          "accu_d": self.metrics["accu_d"],
      }
      fetches_eval = {"batch_size": get_batch_size(inputs["text_ids"])}
      fetches_eval.update(self.losses)
      fetches_eval.update(self.metrics)
      fetches_eval.update(self.samples)
      self.fetches_eval = fetches_eval
  
file_path: texar/examples/text_style_transfer/ctrl_gen_model.py
imports: import tensorflow
  import texar.tf
  from texar.tf.modules import WordEmbedder, UnidirectionalRNNEncoder, MLPTransformConnector, AttentionRNNDecoder, GumbelSoftmaxEmbeddingHelper, Conv1DClassifier
  from texar.tf.core import get_train_op
  from texar.tf.utils import collect_trainable_variables, get_batch_size
label_desc: Use image node_id 2 for calling the CtrlGenModel obj's underlying member method code with example usage: obj._build_model(inputs, vocab, gamma, lambda_g) without return types