Add a batch of predictions and references for the evaluation module's stack. Args: predictions (`list/array/tensor`, *optional*): Predictions. references (`list/array/tensor`, *optional*): References. Example: ```py
(self, *, predictions=None, references=None, **kwargs)
| 486 | return None |
| 487 | |
| 488 | def add_batch(self, *, predictions=None, references=None, **kwargs): |
| 489 | """Add a batch of predictions and references for the evaluation module's stack. |
| 490 | |
| 491 | Args: |
| 492 | predictions (`list/array/tensor`, *optional*): |
| 493 | Predictions. |
| 494 | references (`list/array/tensor`, *optional*): |
| 495 | References. |
| 496 | |
| 497 | Example: |
| 498 | |
| 499 | ```py |
| 500 | >>> import evaluate |
| 501 | >>> accuracy = evaluate.load("accuracy") |
| 502 | >>> for refs, preds in zip([[0,1],[0,1]], [[1,0],[0,1]]): |
| 503 | ... accuracy.add_batch(references=refs, predictions=preds) |
| 504 | ``` |
| 505 | """ |
| 506 | bad_inputs = [input_name for input_name in kwargs if input_name not in self._feature_names()] |
| 507 | if bad_inputs: |
| 508 | raise ValueError( |
| 509 | f"Bad inputs for evaluation module: {bad_inputs}. All required inputs are {list(self._feature_names())}" |
| 510 | ) |
| 511 | batch = {"predictions": predictions, "references": references, **kwargs} |
| 512 | batch = {input_name: batch[input_name] for input_name in self._feature_names()} |
| 513 | if self.writer is None: |
| 514 | self.selected_feature_format = self._infer_feature_from_batch(batch) |
| 515 | self._init_writer() |
| 516 | try: |
| 517 | for key, column in batch.items(): |
| 518 | if len(column) > 0: |
| 519 | self._enforce_nested_string_type(self.selected_feature_format[key], column[0]) |
| 520 | batch = self.selected_feature_format.encode_batch(batch) |
| 521 | self.writer.write_batch(batch) |
| 522 | except (pa.ArrowInvalid, TypeError): |
| 523 | if any(len(batch[c]) != len(next(iter(batch.values()))) for c in batch): |
| 524 | col0 = next(iter(batch)) |
| 525 | bad_col = [c for c in batch if len(batch[c]) != len(batch[col0])][0] |
| 526 | error_msg = ( |
| 527 | f"Mismatch in the number of {col0} ({len(batch[col0])}) and {bad_col} ({len(batch[bad_col])})" |
| 528 | ) |
| 529 | elif set(self.selected_feature_format) != {"references", "predictions"}: |
| 530 | error_msg = ( |
| 531 | f"Module inputs don't match the expected format.\n" |
| 532 | f"Expected format: {self.selected_feature_format },\n" |
| 533 | ) |
| 534 | error_msg_inputs = ",\n".join( |
| 535 | f"Input {input_name}: {summarize_if_long_list(batch[input_name])}" |
| 536 | for input_name in self.selected_feature_format |
| 537 | ) |
| 538 | error_msg += error_msg_inputs |
| 539 | else: |
| 540 | error_msg = ( |
| 541 | f"Predictions and/or references don't match the expected format.\n" |
| 542 | f"Expected format: {self.selected_feature_format },\n" |
| 543 | f"Input predictions: {summarize_if_long_list(predictions)},\n" |
| 544 | f"Input references: {summarize_if_long_list(references)}" |
| 545 | ) |