predictor
allennlp.predictors.predictor
Predictor#
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.
load_line#
class Predictor(Registrable):
| ...
| 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.
dump_line#
class Predictor(Registrable):
| ...
| 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.
predict_json#
class Predictor(Registrable):
| ...
| def predict_json(self, inputs: JsonDict) -> JsonDict
json_to_labeled_instances#
class Predictor(Registrable):
| ...
| 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
Instance
s given by the model's output.
Returns
List[instance]
A list ofInstance
's.
get_gradients#
class Predictor(Registrable):
| ...
| 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.
Parameters
- instances :
List[Instance]
Returns
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.
Notes
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.
get_interpretable_layer#
class Predictor(Registrable):
| ...
| def get_interpretable_layer(self) -> torch.nn.Module
Returns the input/embedding layer of the model. If the predictor wraps around a non-AllenNLP model, this function should be overridden to specify the correct input/embedding layer. For the cases where the input layer is an embedding layer, this should be the layer 0 of the embedder.
get_interpretable_text_field_embedder#
class Predictor(Registrable):
| ...
| def get_interpretable_text_field_embedder(self) -> torch.nn.Module
Returns the first TextFieldEmbedder
of the model.
If the predictor wraps around a non-AllenNLP model,
this function should be overridden to specify the correct embedder.
capture_model_internals#
class Predictor(Registrable):
| ...
| @contextmanager
| 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}
predict_instance#
class Predictor(Registrable):
| ...
| def predict_instance(self, instance: Instance) -> JsonDict
predictions_to_labeled_instances#
class Predictor(Registrable):
| ...
| 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.
predict_batch_json#
class Predictor(Registrable):
| ...
| def predict_batch_json(self, inputs: List[JsonDict]) -> List[JsonDict]
predict_batch_instance#
class Predictor(Registrable):
| ...
| def predict_batch_instance(
| self,
| instances: List[Instance]
| ) -> List[JsonDict]
from_path#
class Predictor(Registrable):
| ...
| @classmethod
| def from_path(
| cls,
| archive_path: Union[str, Path],
| predictor_name: str = None,
| cuda_device: int = -1,
| dataset_reader_to_load: str = "validation",
| frozen: bool = True,
| import_plugins: bool = True,
| overrides: Union[str, Dict[str, Any]] = ""
| ) -> "Predictor"
Instantiate a Predictor
from an archive path.
If you need more detailed configuration options, such as overrides,
please use from_archive
.
Parameters
- archive_path :
Union[str, Path]
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
)
Ifcuda_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 callmodel.eval()
when building the predictor. - import_plugins :
bool
, optional (default =True
)
IfTrue
, 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 byallennlp.common.plugins.import_plugins()
. - overrides :
Union[str, Dict[str, Any]]
, optional (default =""
)
JSON overrides to apply to the unarchivedParams
object.
Returns
Predictor
A Predictor instance.
from_archive#
class Predictor(Registrable):
| ...
| @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.