Skip to content


[ allennlp.predictors.predictor ]

Predictor Objects#

class Predictor(Registrable):
 | def __init__(
 |     self,
 |     model: Model,
 |     dataset_reader: DatasetReader,
 |     frozen: bool = True
 | ) -> None

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.


 | def load_line(self, line: str) -> JsonDict

If your inputs are not in JSON-lines format (e.g. you have a CSV) you can override this function to parse them correctly.


 | def dump_line(self, outputs: JsonDict) -> str

If you don't want your outputs in JSON-lines format you can override this function to output them differently.


 | def predict_json(self, inputs: JsonDict) -> JsonDict


 | def json_to_labeled_instances(
 |     self,
 |     inputs: JsonDict
 | ) -> List[Instance]

Converts incoming json to a Instance, runs the model on the newly created instance, and adds labels to the Instances given by the model's output.


  • List[instance]
    A list of Instance's.


 | def get_gradients(
 |     self,
 |     instances: List[Instance]
 | ) -> Tuple[Dict[str, Any], Dict[str, Any]]

Gets the gradients of the loss with respect to the model inputs.


  • instances : List[Instance]


  • Tuple[Dict[str, Any], Dict[str, Any]]
    The first item is a Dict of gradient entries for each input. The keys have the form {grad_input_1: ..., grad_input_2: ... } up to the number of inputs given. The second item is the model's output.


Takes a JsonDict representing the inputs of the model and converts them to Instances), sends these through the model forward function after registering hooks on the embedding layer of the model. Calls backward on the loss and then removes the hooks.


 | @contextmanager
 | def capture_model_internals(self) -> 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}


 | def predict_instance(self, instance: Instance) -> JsonDict


 | def predictions_to_labeled_instances(
 |     self,
 |     instance: Instance,
 |     outputs: Dict[str, numpy.ndarray]
 | ) -> List[Instance]

This function takes a model's outputs for an Instance, and it labels that instance according to the output. For example, in classification this function labels the instance according to the class with the highest probability. This function is used to to compute gradients of what the model predicted. The return type is a list because in some tasks there are multiple predictions in the output (e.g., in NER a model predicts multiple spans). In this case, each instance in the returned list of Instances contains an individual entity prediction as the label.


 | def predict_batch_json(self, inputs: List[JsonDict]) -> List[JsonDict]


 | def predict_batch_instance(
 |     self,
 |     instances: List[Instance]
 | ) -> List[JsonDict]


 | @classmethod
 | def from_path(
 |     cls,
 |     archive_path: str,
 |     predictor_name: str = None,
 |     cuda_device: int = -1,
 |     dataset_reader_to_load: str = "validation",
 |     frozen: bool = True,
 |     import_plugins: bool = True
 | ) -> "Predictor"

Instantiate a Predictor from an archive path.

If you need more detailed configuration options, such as overrides, please use from_archive.


  • archive_path : str
    The path to the archive.
  • predictor_name : str, optional (default = None)
    Name that the predictor is registered as, or None to use the predictor associated with the model.
  • cuda_device : int, optional (default = -1)
    If cuda_device is >= 0, the model will be loaded onto the corresponding GPU. Otherwise it will be loaded onto the CPU.
  • dataset_reader_to_load : str, optional (default = "validation")
    Which dataset reader to load from the archive, either "train" or "validation".
  • frozen : bool, optional (default = True)
    If we should call model.eval() when building the predictor.
  • import_plugins : bool, optional (default = True)
    If True, we attempt to import plugins before loading the predictor. This comes with additional overhead, but means you don't need to explicitly import the modules that your predictor depends on as long as those modules can be found by allennlp.common.plugins.import_plugins().


  • Predictor
    A Predictor instance.


 | @classmethod
 | def from_archive(
 |     cls,
 |     archive: Archive,
 |     predictor_name: str = None,
 |     dataset_reader_to_load: str = "validation",
 |     frozen: bool = True
 | ) -> "Predictor"

Instantiate a Predictor from an Archive; that is, from the result of training a model. Optionally specify which Predictor subclass; otherwise, we try to find a corresponding predictor in DEFAULT_PREDICTORS, or if one is not found, the base class (i.e. Predictor) will be used. Optionally specify which DatasetReader should be loaded; otherwise, the validation one will be used if it exists followed by the training dataset reader. Optionally specify if the loaded model should be frozen, meaning model.eval() will be called.