class_docstr: Decodes a (random sample of a) dataset using parameters at given checkpoint and computes BLEU against references.
  
  :param model_folder: The model folder where checkpoint decoder outputs will be written to.
  :param inputs: Path(s) to file containing input sentences (and their factors).
  :param references: Path to file containing references (and their factors).
  :param source_vocabs: The source vocabularies.
  :param target_vocabs: The target vocabularies.
  :param device: The device to use for decoding.
  :param model: The translation model.
  :param max_input_len: Maximum input length.
  :param batch_size: Batch size.
  :param beam_size: Size of the beam.
  :param nbest_size: Size of nbest lists.
  :param length_penalty_alpha: Alpha factor for the length penalty.
  :param length_penalty_beta: Beta factor for the length penalty.
  :param max_output_length_num_stds: Number of standard deviations as safety margin for maximum output length.
  :param ensemble_mode: Ensemble mode: linear or log_linear combination.
  :param sample_size: Maximum number of sentences to sample and decode. If <=0, all sentences are used.
  :param random_seed: Random seed for sampling. Default: 42.
class_docstr_tok: ["Decodes","a","(","random","sample","of","a",")","dataset","using","parameters","at","given","checkpoint","and","computes","BLEU","against","references",".",":","param","model_folder",":","The","model","folder","where","checkpoint","decoder","outputs","will","be","written","to",".",":","param","inputs",":","Path","(","s",")","to","file","containing","input","sentences","(","and","their","factors",")",".",":","param","references",":","Path","to","file","containing","references","(","and","their","factors",")",".",":","param","source_vocabs",":","The","source","vocabularies",".",":","param","target_vocabs",":","The","target","vocabularies",".",":","param","device",":","The","device","to","use","for","decoding",".",":","param","model",":","The","translation","model",".",":","param","max_input_len",":","Maximum","input","length",".",":","param","batch_size",":","Batch","size",".",":","param","beam_size",":","Size","of","the","beam",".",":","param","nbest_size",":","Size","of","nbest","lists",".",":","param","length_penalty_alpha",":","Alpha","factor","for","the","length","penalty",".",":","param","length_penalty_beta",":","Beta","factor","for","the","length","penalty",".",":","param","max_output_length_num_stds",":","Number","of","standard","deviations","as","safety","margin","for","maximum","output","length",".",":","param","ensemble_mode",":","Ensemble","mode",":","linear","or","log_linear","combination",".",":","param","sample_size",":","Maximum","number","of","sentences","to","sample","and","decode",".","If","<","=0",",","all","sentences","are","used",".",":","param","random_seed",":","Random","seed","for","sampling",".","Default",":","42","."]
docstr: Decodes data set and evaluates given a checkpoint.
  
  :param output_name: Filename to write translations to. If None, will not write outputs.
  :return: Mapping of metric names to scores.
docstr_tok: ["Decodes","data","set","and","evaluates","given","a","checkpoint",".",":","param","output_name",":","Filename","to","write","translations","to",".","If","None",",","will","not","write","outputs",".",":","return",":","Mapping","of","metric","names","to","scores","."]
code: def decode_and_evaluate(
      self, output_name: Optional[str] = None
  ) -> Dict[str, float]:
      """
      Decodes data set and evaluates given a checkpoint.
  
      :param output_name: Filename to write translations to. If None, will not write outputs.
      :return: Mapping of metric names to scores.
      """
  
      # 1. Translate
  
      # Store original mode and set to eval mode in case the model is not yet
      # traced.
      original_mode = self.model.training
      self.model.eval()
  
      trans_wall_time = 0.0
      translations = []  # type: List[List[str]]
      with ExitStack() as exit_stack:
          outputs = [
              exit_stack.enter_context(
                  utils.smart_open(output_name.format(factor=idx), "w")
              )
              if output_name is not None
              else None
              for idx in range(self.model.num_target_factors)
          ]
  
          tic = time.time()
          trans_inputs = []  # type: List[inference.TranslatorInput]
          for i, inputs in enumerate(self.inputs_sentences):
              trans_inputs.append(
                  inference.make_input_from_multiple_strings(i, inputs)
              )
          trans_outputs = self.translator.translate(trans_inputs)
          trans_wall_time = time.time() - tic
          for trans_input, trans_output in zip(
              trans_inputs, trans_outputs
          ):
              output_strings = [trans_output.translation]
              if (
                  trans_output.factor_translations is not None
                  and len(outputs) > 1
              ):
                  output_strings += trans_output.factor_translations
              translations.append(output_strings)
              for output_string, output_file in zip(
                  output_strings, outputs
              ):
                  if output_file is not None:
                      print(output_string, file=output_file)
      avg_time = trans_wall_time / len(self.targets_sentences[0])
      translations = list(zip(*translations))  # type: ignore
  
      # Restore original model mode
      self.model.train(original_mode)
  
      # 2. Evaluate
  
      metrics = {
          C.BLEU: evaluate.raw_corpus_bleu(
              hypotheses=translations[0],
              references=self.targets_sentences[0],
              offset=evaluate.DEFAULT_OFFSET,
          ),
          C.CHRF: evaluate.raw_corpus_chrf(
              hypotheses=translations[0],
              references=self.targets_sentences[0],
          ),
          C.ROUGE1: evaluate.raw_corpus_rouge1(
              hypotheses=translations[0],
              references=self.targets_sentences[0],
          ),
          C.ROUGE2: evaluate.raw_corpus_rouge2(
              hypotheses=translations[0],
              references=self.targets_sentences[0],
          ),
          C.ROUGEL: evaluate.raw_corpus_rougel(
              hypotheses=translations[0],
              references=self.targets_sentences[0],
          ),
          C.LENRATIO: evaluate.raw_corpus_length_ratio(
              hypotheses=translations[0],
              references=self.targets_sentences[0],
          ),
          C.TER: evaluate.raw_corpus_ter(
              hypotheses=translations[0],
              references=self.targets_sentences[0],
          ),
          C.AVG_TIME: avg_time,
          C.DECODING_TIME: trans_wall_time,
      }
  
      if len(translations) > 1:  # metrics for other target factors
          for i, _ in enumerate(translations[1:], 1):
              # only BLEU
              metrics.update(
                  {
                      "f%d-%s"
                      % (i, C.BLEU): evaluate.raw_corpus_bleu(
                          hypotheses=translations[i],
                          references=self.targets_sentences[i],
                          offset=evaluate.DEFAULT_OFFSET,
                      )
                  }
              )
      return metrics
  
file_path: sockeye/sockeye/checkpoint_decoder.py
imports: import logging
  import os
  import random
  import time
  from contextlib import ExitStack
  from itertools import chain
  from typing import Any, Dict, Optional, List
  import torch
  from .None import constants
  from .None import evaluate
  from .None import inference
  from .None import model
  from .None import utils
  from .None import vocab
label_desc: Use image node_id 2 for calling the CheckpointDecoder obj's underlying member method code with example usage: obj.decode_and_evaluate(output_name) and returns: metrics