Attribution Methods¶
Attribution methods quantitatively measure the contribution of each of a function's individual inputs to its output. Gradient-based attribution methods compute the gradient of a model with respect to its inputs to describe how important each input is towards the output prediction. These methods can be applied to assist in explaining deep networks.
TruLens provides implementations of several such techniques, found in this package.
AttributionMethod
¶
Bases: AbstractBaseClass
Interface used by all attribution methods.
An attribution method takes a neural network model and provides the ability to assign values to the variables of the network that specify the importance of each variable towards particular predictions.
Source code in trulens_explain/trulens/nn/attribution.py
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 |
|
model: ModelWrapper
property
¶
Model for which attributions are calculated.
__init__(model, rebatch_size=None, *args, **kwargs)
abstractmethod
¶
Abstract constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
ModelWrapper
|
ModelWrapper Model for which attributions are calculated. |
required |
rebatch_size |
int
|
int (optional) Will rebatch instances to this size if given. This may be required for GPU usage if using a DoI which produces multiple instances per user-provided instance. Many valued DoIs will expand the tensors sent to each layer to original_batch_size * doi_size. The rebatch size will break up original_batch_size * doi_size into rebatch_size chunks to send to model. |
None
|
Source code in trulens_explain/trulens/nn/attribution.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
attributions(*model_args, **model_kwargs)
¶
Returns attributions for the given input. Attributions are in the same shape as the layer that attributions are being generated for.
The numeric scale of the attributions will depend on the specific implementations of the Distribution of Interest and Quantity of Interest. However it is generally related to the scale of gradients on the Quantity of Interest.
For example, Integrated Gradients uses the linear interpolation Distribution of Interest which subsumes the completeness axiom which ensures the sum of all attributions of a record equals the output determined by the Quantity of Interest on the same record.
The Point Distribution of Interest will be determined by the gradient at a single point, thus being a good measure of model sensitivity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_args |
ArgsLike
|
ArgsLike, model_kwargs: KwargsLike
The args and kwargs given to the call method of a model. This
should represent the records to obtain attributions for, assumed
to be a batched input. if |
()
|
Returns - np.ndarray when single attribution_cut input, single qoi output - or ArgsLike[np.ndarray] when single input, multiple output (or vice versa) - or ArgsLike[ArgsLike[np.ndarray]] when multiple output (outer), multiple input (inner)
An array of attributions, matching the shape and type of `from_cut`
of the slice. Each entry in the returned array represents the degree
to which the corresponding feature affected the model's outcome on
the corresponding point.
If attributing to a component with multiple inputs, a list for each
will be returned.
If the quantity of interest features multiple outputs, a list for
each will be returned.
Source code in trulens_explain/trulens/nn/attribution.py
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 |
|
AttributionResult
dataclass
¶
_attribution method output container.
Source code in trulens_explain/trulens/nn/attribution.py
63 64 65 66 67 68 69 70 71 |
|
InputAttribution
¶
Bases: InternalInfluence
Attributions of input features on either internal or output quantities. This is essentially an alias for
InternalInfluence(
model,
(trulens.nn.slices.InputCut(), cut),
qoi,
doi,
multiply_activation)
Source code in trulens_explain/trulens/nn/attribution.py
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 |
|
__init__(model, qoi_cut=None, qoi='max', doi_cut=None, doi='point', multiply_activation=True, *args, **kwargs)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model for which attributions are calculated. |
required | |
qoi_cut |
The cut determining the layer from which the QoI is derived.
Expects a If an If a
|
None
|
|
qoi |
quantities.QoI | int | tuple | str
Quantity of interest to attribute. Expects a If an If a tuple or list of two integers is given, then the quantity of interest is taken to be the comparative quantity for the class given by the first integer against the class given by the second integer, i.e., ```python quantities.ComparativeQoI(*qoi)
If the string, |
'max'
|
|
doi_cut |
For models which have non-differentiable pre-processing at the start of the model, specify the cut of the initial differentiable input form. For NLP models, for example, this could point to the embedding layer. If not provided, InputCut is assumed. |
None
|
|
doi |
distributions.DoI | str
Distribution of interest over inputs. Expects a If the string, If the string, |
'point'
|
|
multiply_activation |
bool, optional Whether to multiply the gradient result by its corresponding activation, thus converting from "influence space" to "attribution space." |
True
|
Source code in trulens_explain/trulens/nn/attribution.py
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 |
|
IntegratedGradients
¶
Bases: InputAttribution
Implementation for the Integrated Gradients method from the following paper:
Axiomatic Attribution for Deep Networks
This should be cited using:
@INPROCEEDINGS{
sundararajan17axiomatic,
author={Mukund Sundararajan and Ankur Taly, and Qiqi Yan},
title={Axiomatic Attribution for Deep Networks},
booktitle={International Conference on Machine Learning (ICML)},
year={2017},
}
This is essentially an alias for
InternalInfluence(
model,
(trulens.nn.slices.InputCut(), trulens.nn.slices.OutputCut()),
'max',
trulens.nn.distributions.LinearDoi(baseline, resolution),
multiply_activation=True)
Source code in trulens_explain/trulens/nn/attribution.py
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 |
|
__init__(model, baseline=None, resolution=50, doi_cut=None, qoi='max', qoi_cut=None, *args, **kwargs)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
ModelWrapper
|
Model for which attributions are calculated. |
required |
baseline |
The baseline to interpolate from. Must be same shape as the
input. If |
None
|
|
resolution |
int
|
Number of points to use in the approximation. A higher resolution is more computationally expensive, but gives a better approximation of the mathematical formula this attribution method represents. |
50
|
Source code in trulens_explain/trulens/nn/attribution.py
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 |
|
InternalInfluence
¶
Bases: AttributionMethod
Internal attributions parameterized by a slice, quantity of interest, and distribution of interest.
The slice specifies the layers at which the internals of the model are to be exposed; it is represented by two cuts, which specify the layer the attributions are assigned to and the layer from which the quantity of interest is derived. The Quantity of Interest (QoI) is a function of the output specified by the slice that determines the network output behavior that the attributions are to describe. The Distribution of Interest (DoI) specifies the records over which the attributions are aggregated.
More information can be found in the following paper:
Influence-Directed Explanations for Deep Convolutional Networks
This should be cited using:
@INPROCEEDINGS{
leino18influence,
author={
Klas Leino and
Shayak Sen and
Anupam Datta and
Matt Fredrikson and
Linyi Li},
title={
Influence-Directed Explanations
for Deep Convolutional Networks},
booktitle={IEEE International Test Conference (ITC)},
year={2018},
}
Source code in trulens_explain/trulens/nn/attribution.py
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 |
|
__init__(model, cuts, qoi, doi, multiply_activation=True, return_grads=False, return_doi=False, *args, **kwargs)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
ModelWrapper
|
Model for which attributions are calculated. |
required |
cuts |
SliceLike
|
The slice to use when computing the attributions. The slice
keeps track of the layer whose output attributions are
calculated and the layer for which the quantity of interest is
computed. Expects a If a single A cut (or the cuts within the tuple) can also be represented as
an |
required |
qoi |
QoiLike
|
Quantity of interest to attribute. Expects a If an
If a tuple or list of two integers is given, then the quantity of interest is taken to be the comparative quantity for the class given by the first integer against the class given by the second integer, i.e.,
If a callable is given, it is interpreted as a function representing the QoI, i.e.,
If the string,
|
required |
doi |
DoiLike
|
Distribution of interest over inputs. Expects a If the string,
If the string,
|
required |
multiply_activation |
bool
|
Whether to multiply the gradient result by its corresponding activation, thus converting from "influence space" to "attribution space." |
True
|
Source code in trulens_explain/trulens/nn/attribution.py
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 |
|