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'
|
class_to_ff
|
Optional[Mapping[str, float]]
|
Optional mapping from class id to the feature factor
that strengthens it. Used for strengthen and |
None
|
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, split='test', max_size=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, plot_kwargs=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
|
split
|
Optional[Any]
|
Dataset split used for training-like decoder evaluation rows.
Defaults to |
'test'
|
max_size
|
Optional[int]
|
Shared evaluation-size alias. If set and explicit decoder caps are omitted, caps both training-like decoder rows and neutral/LMS rows. |
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
|
plot_kwargs
|
Optional[Dict[str, Any]]
|
Optional dict of options forwarded to plot_probability_shifts when plot=True.
E.g. plot_kwargs=dict(figsize=(5, 3), show=False). The evaluate_decoder |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Flat dict with:
(e.g. dec_result['3SG_weaken']).
a
|
Source code in gradiend/evaluator/decoder.py
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 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 | |