Compute the evaluation module. Usage of positional arguments is not allowed to prevent mistakes. Args: predictions (`list/array/tensor`, *optional*): Predictions. references (`list/array/tensor`, *optional*): References.
(self, *, predictions=None, references=None, **kwargs)
| 413 | self.filelocks = filelocks |
| 414 | |
| 415 | def compute(self, *, predictions=None, references=None, **kwargs) -> Optional[dict]: |
| 416 | """Compute the evaluation module. |
| 417 | |
| 418 | Usage of positional arguments is not allowed to prevent mistakes. |
| 419 | |
| 420 | Args: |
| 421 | predictions (`list/array/tensor`, *optional*): |
| 422 | Predictions. |
| 423 | references (`list/array/tensor`, *optional*): |
| 424 | References. |
| 425 | **kwargs (optional): |
| 426 | Keyword arguments that will be forwarded to the evaluation module [`~evaluate.EvaluationModule.compute`] |
| 427 | method (see details in the docstring). |
| 428 | |
| 429 | Return: |
| 430 | `dict` or `None` |
| 431 | |
| 432 | - Dictionary with the results if this evaluation module is run on the main process (`process_id == 0`). |
| 433 | - `None` if the evaluation module is not run on the main process (`process_id != 0`). |
| 434 | |
| 435 | ```py |
| 436 | >>> import evaluate |
| 437 | >>> accuracy = evaluate.load("accuracy") |
| 438 | >>> accuracy.compute(predictions=[0, 1, 1, 0], references=[0, 1, 0, 1]) |
| 439 | ``` |
| 440 | """ |
| 441 | all_kwargs = {"predictions": predictions, "references": references, **kwargs} |
| 442 | if predictions is None and references is None: |
| 443 | missing_kwargs = {k: None for k in self._feature_names() if k not in all_kwargs} |
| 444 | all_kwargs.update(missing_kwargs) |
| 445 | else: |
| 446 | missing_inputs = [k for k in self._feature_names() if k not in all_kwargs] |
| 447 | if missing_inputs: |
| 448 | raise ValueError( |
| 449 | f"Evaluation module inputs are missing: {missing_inputs}. All required inputs are {list(self._feature_names())}" |
| 450 | ) |
| 451 | inputs = {input_name: all_kwargs[input_name] for input_name in self._feature_names()} |
| 452 | compute_kwargs = {k: kwargs[k] for k in kwargs if k not in self._feature_names()} |
| 453 | |
| 454 | if any(v is not None for v in inputs.values()): |
| 455 | self.add_batch(**inputs) |
| 456 | self._finalize() |
| 457 | |
| 458 | self.cache_file_name = None |
| 459 | self.filelock = None |
| 460 | self.selected_feature_format = None |
| 461 | |
| 462 | if self.process_id == 0: |
| 463 | self.data.set_format(type=self.info.format) |
| 464 | |
| 465 | inputs = {input_name: self.data[input_name][:] for input_name in self._feature_names()} |
| 466 | with temp_seed(self.seed): |
| 467 | output = self._compute(**inputs, **compute_kwargs) |
| 468 | |
| 469 | if self.buf_writer is not None: |
| 470 | self.buf_writer = None |
| 471 | del self.data |
| 472 | self.data = None |