Reader Module¶
pipelines.pipelines.nodes.reader.ernie_dureader ¶
ErnieReader ¶
Transformer based model for extractive Question Answering based on ERNIE3.0.
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 | |
__init__ ¶
__init__(model_name_or_path: str, model_version: Optional[str] = None, context_window_size: int = 150, batch_size: int = 50, use_gpu: bool = True, no_ans_boost: float = 0.0, return_no_answer: bool = False, top_k: int = 10, top_k_per_candidate: int = 3, top_k_per_sample: int = 1, num_processes: Optional[int] = None, max_seq_len: int = 256, doc_stride: int = 128, progress_bar: bool = True, duplicate_filtering: int = 0, use_confidence_scores: bool = True, proxies: Optional[Dict[str, str]] = None, local_files_only=False, force_download=False, use_auth_token: Optional[Union[str, bool]] = None, n_best_per_sample: int = 1, use_confidence_scores_for_ranking: bool = False, n_best: int = 5, **kwargs)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name_or_path |
str
|
Directory of a saved model or the name of a public model e.g. 'ernie-gram-zh-finetuned-dureader-robust'. |
required |
context_window_size |
int
|
The size, in characters, of the window around the answer span that is used when displaying the context around the answer. |
150
|
batch_size |
int
|
Number of samples the model receives in one batch for inference. Memory consumption is much lower in inference mode. Recommendation: Increase the batch size to a value so only a single batch is used. |
50
|
use_gpu |
bool
|
Whether to use GPU (if available) |
True
|
no_ans_boost |
float
|
How much the no_answer logit is boosted/increased. If set to 0 (default), the no_answer logit is not changed. If a negative number, there is a lower chance of "no_answer" being predicted. If a positive number, there is an increased chance of "no_answer" |
0.0
|
return_no_answer |
bool
|
Whether to include no_answer predictions in the results. |
False
|
top_k |
int
|
The maximum number of answers to return |
10
|
top_k_per_candidate |
int
|
How many answers to extract for each candidate doc that is coming from the retriever (might be a long text). Note that this is not the number of "final answers" you will receive (see |
3
|
top_k_per_sample |
int
|
How many answers to extract from each small text passage that the model can process at once (one "candidate doc" is usually split into many smaller "passages"). You usually want a very small value here, as it slows down inference and you don't gain much of quality by having multiple answers from one passage. Note that this is not the number of "final answers" you will receive (see |
1
|
num_processes |
Optional[int]
|
The number of processes for |
None
|
max_seq_len |
int
|
Max sequence length of one input text for the model |
256
|
doc_stride |
int
|
Length of striding window for splitting long texts (used if |
128
|
progress_bar |
bool
|
Whether to show a tqdm progress bar or not. Can be helpful to disable in production deployments to keep the logs clean. |
True
|
duplicate_filtering |
int
|
Answers are filtered based on their position. Both start and end position of the answers are considered. The higher the value, answers that are more apart are filtered out. 0 corresponds to exact duplicates. -1 turns off duplicate removal. |
0
|
use_confidence_scores |
bool
|
Sets the type of score that is returned with every predicted answer. |
True
|
proxies |
Optional[Dict[str, str]]
|
Dict of proxy servers to use for downloading external models. Example: {'http': 'some.proxy:1234', 'http://hostname': 'my.proxy:3111'} |
None
|
local_files_only |
Whether to force checking for local files only (and forbid downloads) |
False
|
|
force_download |
Whether fo force a (re-)download even if the model exists locally in the cache. |
False
|
|
n_best |
int
|
The number of positive answer spans for each document. |
5
|
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
aggregate_preds ¶
Aggregate passage level predictions to create document level predictions. This method assumes that all passages of each document are contained in preds i.e. that there are no incomplete documents. The output of this step are prediction spans. No answer is represented by a (-1, -1) span on the document level
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
calibrate_confidence_scores ¶
calibrate_confidence_scores(document_store: BaseDocumentStore, device: Optional[str] = None, label_index: str = 'label', doc_index: str = 'eval_document', label_origin: str = 'gold_label')
Calibrates confidence scores on evaluation documents in the DocumentStore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document_store |
BaseDocumentStore
|
DocumentStore containing the evaluation documents |
required |
device |
Optional[str]
|
The device on which the tensors should be processed. Choose from "cpu" and "cuda" or use the Reader's device by default. |
None
|
label_index |
str
|
Index/Table name where labeled questions are stored |
'label'
|
doc_index |
str
|
Index/Table name where documents that are used for evaluation are stored |
'eval_document'
|
label_origin |
str
|
Field name where the gold labels are stored |
'gold_label'
|
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
formatted_preds ¶
formatted_preds(preds: List[QACandidate], baskets: List[SampleBasket], logits: Optional[paddle.Tensor] = None, **kwargs)
Takes a list of passage level predictions, each corresponding to one sample, and converts them into document level predictions. Leverages information in the SampleBaskets. Assumes that we are being passed predictions from ALL samples in the one SampleBasket i.e. all passages of a document. Logits should be None, because we have already converted the logits to predictions before calling formatted_preds. (see Inferencer._get_predictions_and_aggregate()).
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
formatted_preds_wrapper ¶
Format predictions for inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits |
Tensor
|
Model logits. |
required |
Returns:
| Type | Description |
|---|---|
|
Predictions in the right format. |
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
get_top_candidates ¶
Returns top candidate answers as a list of Span objects. Operates on a matrix of summed start and end logits. This matrix corresponds to a single sample (includes special tokens, question tokens, passage tokens). This method always returns a list of len n_best_per_sample + 1 (it is comprised of the n_best_per_sample positive answers along with the one no_answer)
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
logits_to_preds ¶
logits_to_preds(logits: paddle.Tensor, span_mask: paddle.Tensor, start_of_word: paddle.Tensor, seq_2_start_t: paddle.Tensor, max_answer_length: int = 1000, **kwargs)
Get the predicted index of start and end token of the answer. Note that the output is at token level and not word level. Note also that these logits correspond to the tokens of a sample (i.e. special tokens, question tokens, passage_tokens)
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | |
pred_to_doc_idxs
staticmethod
¶
Converts the passage level predictions to document level predictions. Note that on the doc level we don't have special tokens or question tokens. This means that a no answer cannot be prepresented by a (0,0) qa_answer but will instead be represented by (-1, -1)
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
predict ¶
Use loaded QA model to find answers for a query in the supplied list of Document.
Returns dictionaries containing answers sorted by (desc.) score. Example:
|{
| 'query': 'Who is the father of Arya Stark?',
| 'answers':[Answer(
| 'answer': 'Eddard,',
| 'context': "She travels with her father, Eddard, to King's Landing when he is",
| 'score': 0.9787139466668613,
| 'offsets_in_context': [Span(start=29, end=35],
| 'offsets_in_context': [Span(start=347, end=353],
| 'document_id': '88d1ed769d003939d3a0d28034464ab2'
| ),...
| ]
|}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query |
str
|
Query string |
required |
documents |
List[Document]
|
List of Document in which to search for the answer |
required |
top_k |
Optional[int]
|
The maximum number of answers to return |
None
|
Returns:
| Type | Description |
|---|---|
|
Dict containing query and answers |
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
predict_on_texts ¶
Use loaded QA model to find answers for a question in the supplied list of Document. Returns dictionaries containing answers sorted by (desc.) score. Example:
|{
| 'question': 'Who is the father of Arya Stark?',
| 'answers':[
| {'answer': 'Eddard,',
| 'context': " She travels with her father, Eddard, to King's Landing when he is ",
| 'offset_answer_start': 147,
| 'offset_answer_end': 154,
| 'score': 0.9787139466668613,
| 'document_id': '1337'
| },...
| ]
|}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question |
str
|
Question string |
required |
documents |
List of documents as string type |
required | |
top_k |
Optional[int]
|
The maximum number of answers to return |
None
|
Returns:
| Type | Description |
|---|---|
|
Dict containing question and answers |
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
reduce_preds ¶
This function contains the logic for choosing the best answers from each passage. In the end, it returns the n_best predictions on the document level.
Source code in pipelines/pipelines/nodes/reader/ernie_dureader.py
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 | |
to_qa_preds ¶
Groups Span objects together in a QAPred object