code: def random_search(
      scores_info_export_path, num_trials, report_oracle_bleu=False
  ):
      with open(scores_info_export_path, "rb") as f:
          scores_info = pickle.load(f)
  
      dummy_task = DummyTask()
  
      if report_oracle_bleu:
          oracle_scorer = bleu.Scorer(
              bleu.BleuConfig(
                  pad=vocab_constants.PAD_ID,
                  eos=vocab_constants.EOS_ID,
                  unk=vocab_constants.UNK_ID,
              )
          )
  
          for example in scores_info:
              smoothed_bleu = []
              for hypo in example["hypos"]:
                  eval_score = smoothed_sentence_bleu(
                      dummy_task,
                      torch.IntTensor(example["target_tokens"]),
                      torch.IntTensor(hypo),
                  )
                  smoothed_bleu.append(eval_score)
              best_hypo_ind = np.argmax(smoothed_bleu)
              example["best_hypo_ind"] = best_hypo_ind
  
              oracle_scorer.add(
                  torch.IntTensor(example["target_tokens"]),
                  torch.IntTensor(example["hypos"][best_hypo_ind]),
              )
  
          print("oracle BLEU: ", oracle_scorer.score())
  
      num_features = scores_info[0]["scores"].shape[1]
      assert all(
          example["scores"].shape[1] == num_features
          for example in scores_info
      ), "All examples must have the same number of scores!"
      feature_weights = np.zeros(num_features)
      feature_weights[0] = 1
      score = evaluate_weights(
          scores_info, feature_weights, length_penalty=1
      )
      print("base BLEU: ", score)
      best_score = score
      best_weights = feature_weights
      best_length_penalty = 0
  
      nonzero_features = identify_nonzero_features(scores_info)
  
      for i in range(num_trials):
          feature_weights = np.zeros(num_features)
          random_weights = np.random.dirichlet(
              np.ones(nonzero_features.size)
          )
          feature_weights[nonzero_features] = random_weights
          length_penalty = 1.5 * np.random.random()
  
          score = evaluate_weights(
              scores_info, feature_weights, length_penalty
          )
          if score > best_score:
              best_score = score
              best_weights = feature_weights
              best_length_penalty = length_penalty
  
          print(f"\r[{i}]  best: {best_score}", end="", flush=True)
  
      print()
      print("best weights: ", best_weights)
      print("best length penalty: ", length_penalty)
  
      return best_weights, best_length_penalty, best_score
  
file_path: translate/pytorch_translate/rescoring/weights_search.py
imports: import argparse
  import pickle
  import numpy
  import torch
  from fairseq.scoring import bleu
  from pytorch_translate import vocab_constants
  from pytorch_translate.data.dictionary import Dictionary
  from pytorch_translate.generate import smoothed_sentence_bleu
label_desc: Use image node_id 4 for calling a global function with example usage: random_search(scores_info_export_path, num_trials, report_oracle_bleu) and returns: best_weights,  best_length_penalty,  best_score