DecoderEvaluator
Decoder evaluation: grid search over (feature_factor, lr), cache, and compute best selection summary. Uses trainer for eval dataframe and model evaluation.
compute_metric_summaries
compute_metric_summaries(trainer, results, *, selector, metrics=None, extractor=default_extract_candidates, feature_factor_from_id=lambda cid: cid[0], lr_from_id=lambda cid: cid[1], empty_default_id='base', class_to_ff=None)
Build a per-metric (i.e., target classes) summary for a decoder grid evaluation.
This is a thin wrapper around the module-level compute_metric_summaries that
resolves default metrics from the trainer and passes through selector and
extraction behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Any
|
Trainer-like object used to resolve default metrics when
|
required |
results
|
Mapping[Any, Mapping[str, Any]]
|
Mapping from candidate id to evaluation result entries. The expected structure is the same as produced by decoder evaluation. |
required |
selector
|
SelectionPolicy
|
Policy used to choose the best candidate per metric (e.g., LMS thresholding (LMSThresholdPolicy), or metric*lms (LMSTimesMetricPolicy). |
required |
metrics
|
Optional[Sequence[str]]
|
Optional list of metric names to summarize. If None, uses the trainer's target feature classes. |
None
|
extractor
|
Callable[[Mapping[Any, Mapping[str, Any]]], Tuple[List[Candidate], BaseContext]]
|
Function that converts the raw |
default_extract_candidates
|
feature_factor_from_id
|
Callable[[CandidateId], float]
|
Function to extract feature_factor from a candidate id. |
lambda cid: cid[0]
|
lr_from_id
|
Callable[[CandidateId], float]
|
Function to extract learning_rate from a candidate id. |
lambda cid: cid[1]
|
empty_default_id
|
str
|
Fallback id used when no candidate is selected (for comparison with base). When the selector returns None, we first try the candidate with learning_rate != 0 and smallest absolute value; only if none exists do we use this default (representing the base model). |
'base'
|
Returns:
| Type | Description |
|---|---|
Dict[str, Dict[str, Any]]
|
A dict keyed by metric name with values containing selected metric |
Dict[str, Dict[str, Any]]
|
|
Source code in gradiend/evaluator/decoder.py
evaluate_decoder
evaluate_decoder(trainer, model_with_gradiend=None, feature_factors=None, lrs=None, part='decoder', output_path=None, selector=None, summary_extractor=default_extract_candidates, summary_feature_factor_from_id=lambda cid: cid['feature_factor'] if isinstance(cid, dict) else cid[0], summary_lr_from_id=lambda cid: cid['learning_rate'] if isinstance(cid, dict) else cid[1], summary_empty_default_id='base', use_cache=None, max_size_training_like=None, max_size_neutral=None, eval_batch_size=None, training_like_df=None, neutral_df=None, summary_metrics=None, target_class=None, increase_target_probabilities=True, plot=False, show=None)
Run decoder grid evaluation and return summary + grid for one direction (strengthen or weaken).
Only the dataset and feature-factor combinations required for the requested direction are computed. Use increase_target_probabilities=True (default) for strengthen, False for weaken.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Any
|
Trainer (protocol) with get_model, _model_for_decoder_eval, _get_decoder_eval_dataframe, and evaluate_base_model. |
required |
model_with_gradiend
|
Any
|
ModelWithGradiend instance or path. If None, uses trainer.get_model(). |
None
|
feature_factors
|
Optional[List[float]]
|
List of feature factors to test. If None, derived from direction and target classes. |
None
|
lrs
|
Optional[List[float]]
|
List of learning rates to test. |
None
|
part
|
str
|
which part of GRADIEND is used to derive GRADIEND-modified models (options: 'encoder-weight' |
'decoder-weight' | 'decoder-bias' | 'decoder-sum' | 'decoder'). All options besides |
'decoder'
|
output_path
|
Optional[str]
|
Optional explicit cache path. Overrides experiment_dir-based cache path. |
None
|
selector
|
Optional[SelectionPolicy]
|
SelectionPolicy, e.g. LMSThresholdPolicy(ratio=0.99) or LMSTimesMetricPolicy(). |
None
|
summary_extractor
|
Callable[[Mapping[Any, Mapping[str, Any]]], Tuple[List[Candidate], BaseContext]]
|
Candidate extractor for summary computation. Use a custom extractor to add derived metrics (e.g. bpi, fpi, mpi) to candidates; then pass summary_metrics so they are summarized. |
default_extract_candidates
|
summary_feature_factor_from_id
|
Callable[[CandidateId], float]
|
Function to extract feature_factor from candidate id. |
lambda cid: cid['feature_factor'] if isinstance(cid, dict) else cid[0]
|
summary_lr_from_id
|
Callable[[CandidateId], float]
|
Function to extract lr from candidate id. |
lambda cid: cid['learning_rate'] if isinstance(cid, dict) else cid[1]
|
summary_empty_default_id
|
str
|
Fallback id used when no candidate is selected (for comparison with base). When the selector returns None, we first try the candidate with learning_rate != 0 and smallest absolute value; only if none exists do we use this default (representing the base model). |
'base'
|
use_cache
|
Optional[bool]
|
If True, use cached results when available; if False, recompute. |
None
|
max_size_training_like
|
Optional[int]
|
Maximum size for generated training-like eval data. |
None
|
max_size_neutral
|
Optional[int]
|
Maximum size for generated neutral eval data (and LMS text cap). |
None
|
eval_batch_size
|
Optional[int]
|
Common eval batch size used for LMS. |
None
|
training_like_df
|
Optional[Any]
|
Optional explicit training-like DataFrame for probability scoring. |
None
|
neutral_df
|
Optional[Any]
|
Optional explicit neutral DataFrame for LMS scoring. |
None
|
summary_metrics
|
Optional[Sequence[str]]
|
Optional list of metric names to summarize. If None, uses direction and target classes (see increase_target_probabilities). |
None
|
target_class
|
Optional[Union[str, List[str]]]
|
If set, evaluate only for this target class (or list of classes). Restricts feature factors and datasets to those needed for the given class(es) for efficiency. When None, evaluates for all trainer target classes. |
None
|
increase_target_probabilities
|
bool
|
If True (default), compute strengthen summaries only (keys e.g. "3SG", "3PL"). If False, compute weaken summaries only (keys e.g. "3SG_weaken", "3PL_weaken"). Only the dataset–feature-factor combinations required for the chosen direction are evaluated. |
True
|
plot
|
bool
|
If True, after selection run any missing dataset evaluations needed for plotting, update cache incrementally, then call the trainer's plot_probability_shifts. |
False
|
show
|
Optional[bool]
|
If True, display the plot (e.g. plt.show()). If False, only save to file. When None and plot=True, defaults to True (same as evaluate_encoder: plot implies show). |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Flat dict with:
- For strengthen (increase_target_probabilities=True): one entry per target class (e.g. dec_result['3SG']).
- For weaken (increase_target_probabilities=False): one entry per target class with "_weaken" suffix
(e.g. dec_result['3SG_weaken']).
- Each summary entry contains selected metric |
Source code in gradiend/evaluator/decoder.py
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 | |