code: def __init__(
      self,
      translate_params,
  ):
      self.models = translate_params["ensemble_models"]
      decoding_params = translate_params["decoding_params"]
      self.beam_size = decoding_params["beam_size"]
  
      assert len(self.models) > 0
      source_vocab = self.models[0]["source_vocab"]
      target_vocab = self.models[0]["target_vocab"]
      for model in self.models:
          assert model["source_vocab"] == source_vocab
          assert model["target_vocab"] == target_vocab
  
      self.source_vocab_size = len(source_vocab)
      self.target_vocab_size = len(target_vocab)
  
      self.decoder_scope_names = [
          "model{}".format(i) for i in range(len(self.models))
      ]
  
      self.model = Seq2SeqModelHelper(init_params=True)
  
      self.encoder_inputs = self.model.net.AddExternalInput(
          "encoder_inputs"
      )
      self.encoder_lengths = self.model.net.AddExternalInput(
          "encoder_lengths"
      )
      self.max_output_seq_len = self.model.net.AddExternalInput(
          "max_output_seq_len"
      )
  
      fake_seq_lengths = self.model.param_init_net.ConstantFill(
          [],
          "fake_seq_lengths",
          shape=[self.beam_size],
          value=100000,
          dtype=core.DataType.INT32,
      )
  
      beam_decoder = BeamSearchForwardOnly(
          beam_size=self.beam_size,
          model=self.model,
          go_token_id=seq2seq_util.GO_ID,
          eos_token_id=seq2seq_util.EOS_ID,
      )
      step_model = beam_decoder.get_step_model()
  
      state_configs = []
      output_log_probs = []
      attention_weights = []
      for model, scope_name in zip(
          self.models,
          self.decoder_scope_names,
      ):
          (
              state_configs_per_decoder,
              output_log_probs_per_decoder,
              attention_weights_per_decoder,
          ) = self._build_decoder(
              model=self.model,
              step_model=step_model,
              model_params=model["model_params"],
              scope=scope_name,
              previous_tokens=beam_decoder.get_previous_tokens(),
              timestep=beam_decoder.get_timestep(),
              fake_seq_lengths=fake_seq_lengths,
          )
          state_configs.extend(state_configs_per_decoder)
          output_log_probs.append(output_log_probs_per_decoder)
          if attention_weights_per_decoder is not None:
              attention_weights.append(attention_weights_per_decoder)
  
      assert len(attention_weights) > 0
      num_decoders_with_attention_blob = (
          self.model.param_init_net.ConstantFill(
              [],
              "num_decoders_with_attention_blob",
              value=1 / float(len(attention_weights)),
              shape=[1],
          )
      )
      # [beam_size, encoder_length, 1]
      attention_weights_average = _weighted_sum(
          model=step_model,
          values=attention_weights,
          weight=num_decoders_with_attention_blob,
          output_name="attention_weights_average",
      )
  
      num_decoders_blob = self.model.param_init_net.ConstantFill(
          [],
          "num_decoders_blob",
          value=1 / float(len(output_log_probs)),
          shape=[1],
      )
      # [beam_size, target_vocab_size]
      output_log_probs_average = _weighted_sum(
          model=step_model,
          values=output_log_probs,
          weight=num_decoders_blob,
          output_name="output_log_probs_average",
      )
      word_rewards = self.model.param_init_net.ConstantFill(
          [],
          "word_rewards",
          shape=[self.target_vocab_size],
          value=0.0,
          dtype=core.DataType.FLOAT,
      )
      (
          self.output_token_beam_list,
          self.output_prev_index_beam_list,
          self.output_score_beam_list,
          self.output_attention_weights_beam_list,
      ) = beam_decoder.apply(
          inputs=self.encoder_inputs,
          length=self.max_output_seq_len,
          log_probs=output_log_probs_average,
          attentions=attention_weights_average,
          state_configs=state_configs,
          data_dependencies=[],
          word_rewards=word_rewards,
      )
  
      workspace.RunNetOnce(self.model.param_init_net)
      workspace.FeedBlob(
          "word_rewards",
          self.build_word_rewards(
              vocab_size=self.target_vocab_size,
              word_reward=translate_params["decoding_params"][
                  "word_reward"
              ],
              unk_reward=translate_params["decoding_params"][
                  "unk_reward"
              ],
          ),
      )
  
      workspace.CreateNet(
          self.model.net,
          input_blobs=[
              str(self.encoder_inputs),
              str(self.encoder_lengths),
              str(self.max_output_seq_len),
          ],
      )
  
      logger.info("Params created: ")
      for param in self.model.params:
          logger.info(param)
  
imports: from abc import ABCMeta, abstractmethod
  import argparse
  import logging
  import numpy
  import sys
  from caffe2.python import core, rnn_cell, workspace
  from caffe2.python.models.seq2seq.beam_search import BeamSearchForwardOnly
  from caffe2.python.models.seq2seq.seq2seq_model_helper import Seq2SeqModelHelper
  import caffe2.python.models.seq2seq.seq2seq_util
label_desc: Use image node_id 5 to create a new Seq2SeqModelCaffe2EnsembleDecoder object from inherited base classes: Seq2SeqModelCaffe2EnsembleDecoderBase with example: obj = Seq2SeqModelCaffe2EnsembleDecoder(translate_params)