class_docstr: a `Predictor` is a thin wrapper around an AllenNLP model that handles JSON -> JSON predictions
  that can be used for serving models through the web API or making predictions in bulk.
docstr: Context manager that captures the internal-module outputs of
  this predictor's model. The idea is that you could use it as follows:
  
  ```
      with predictor.capture_model_internals() as internals:
          outputs = predictor.predict_json(inputs)
  
      return {**outputs, "model_internals": internals}
  ```
docstr_tok: ["Context","manager","that","captures","the","internal-module","outputs","of","this","predictor","'s","model",".","The","idea","is","that","you","could","use","it","as","follows",":","``","`","with","predictor.capture_model_internals","(",")","as","internals",":","outputs","=","predictor.predict_json","(","inputs",")","return","{","*","*","outputs",",","``","model_internals","''",":","internals","}","``","`"]
code: def capture_model_internals(
      self, module_regex: str = ".*"
  ) -> Iterator[dict]:
      """
      Context manager that captures the internal-module outputs of
      this predictor's model. The idea is that you could use it as follows:
  
      ```
          with predictor.capture_model_internals() as internals:
              outputs = predictor.predict_json(inputs)
  
          return {**outputs, "model_internals": internals}
      ```
      """
      results = {}
      hooks = []
  
      # First we'll register hooks to add the outputs of each module to the results dict.
      def add_output(idx: int):
          def _add_output(mod, _, outputs):
              results[idx] = {
                  "name": str(mod),
                  "output": sanitize(outputs),
              }
  
          return _add_output
  
      regex = re.compile(module_regex)
      for idx, (name, module) in enumerate(self._model.named_modules()):
          if regex.fullmatch(name) and module != self._model:
              hook = module.register_forward_hook(add_output(idx))
              hooks.append(hook)
  
      # If you capture the return value of the context manager, you get the results dict.
      yield results
  
      # And then when you exit the context we remove all the hooks.
      for hook in hooks:
          hook.remove()
  
imports: from typing import List, Iterator, Dict, Tuple, Any, Type, Union, Optional
  import logging
  from os import PathLike
  import json
  import re
  from contextlib import contextmanager
  import numpy
  import torch
  from torch.utils.hooks import RemovableHandle
  from torch import Tensor
  from torch import backends
  from allennlp.common import Registrable, plugins
  from allennlp.common.util import JsonDict, sanitize
  from allennlp.data import DatasetReader, Instance
  from allennlp.data.batch import Batch
  from allennlp.models import Model
  from allennlp.models.archival import Archive, load_archive
  from allennlp.nn import util
label_desc: Use image node_id 10 for calling the Predictor obj's underlying member method code with example usage: obj.capture_model_internals(module_regex) and returns: _add_output