xai
get_activations(net, fun_control, batch_size, device='cpu')
¶
Get the activations of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
object
|
A neural network. |
required |
fun_control |
dict
|
A dictionary with the function control. |
required |
batch_size |
int
|
The batch size. |
required |
device |
str
|
The device to use. Defaults to “cpu”. |
'cpu'
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary with the activations of the neural network. |
Examples:
>>> from torch.utils.data import DataLoader
from spotpython.utils.init import fun_control_init
from spotpython.hyperparameters.values import set_control_key_value
from spotpython.data.diabetes import Diabetes
from spotpython.light.regression.netlightregression import NetLightRegression
from spotpython.hyperdict.light_hyper_dict import LightHyperDict
from spotpython.hyperparameters.values import add_core_model_to_fun_control
from spotpython.hyperparameters.values import (
get_default_hyperparameters_as_array, get_one_config_from_X)
from spotpython.hyperparameters.values import set_control_key_value
from spotpython.plot.xai import get_activations
fun_control = fun_control_init(
_L_in=10, # 10: diabetes
_L_out=1,
)
dataset = Diabetes()
set_control_key_value(control_dict=fun_control,
key="data_set",
value=dataset,
replace=True)
add_core_model_to_fun_control(fun_control=fun_control,
core_model=NetLightRegression,
hyper_dict=LightHyperDict)
X = get_default_hyperparameters_as_array(fun_control)
config = get_one_config_from_X(X, fun_control)
_L_in = fun_control["_L_in"]
_L_out = fun_control["_L_out"]
model = fun_control["core_model"](**config, _L_in=_L_in, _L_out=_L_out)
batch_size= config["batch_size"]
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False)
get_activations(model, fun_control=fun_control, batch_size=batch_size, device = "cpu")
{0: array([ 1.43207282e-01, 6.29711570e-03, 1.04200505e-01, -3.79187055e-03,
-1.74976081e-01, -7.97475874e-02, -2.00860098e-01, 2.48444706e-01, ...
Source code in spotpython/plot/xai.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 |
|
get_attributions(spot_tuner, fun_control, attr_method='IntegratedGradients', baseline=None, abs_attr=True, n_rel=5)
¶
Get the attributions of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spot_tuner |
object
|
The spot tuner object. |
required |
fun_control |
dict
|
A dictionary with the function control. |
required |
attr_method |
str
|
The attribution method. Defaults to “IntegratedGradients”. |
'IntegratedGradients'
|
baseline |
Tensor
|
The baseline for the attribution methods. Defaults to None. |
None
|
abs_attr |
bool
|
Whether the method should sort by the absolute attribution values. Defaults to True. |
True
|
n_rel |
int
|
The number of relevant features. Defaults to 5. |
5
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame (object): A DataFrame with the attributions. |
Source code in spotpython/plot/xai.py
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 |
|
get_gradients(net, fun_control, batch_size, device='cpu')
¶
Get the gradients of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
object
|
A neural network. |
required |
fun_control |
dict
|
A dictionary with the function control. |
required |
batch_size |
int
|
The batch size. |
required |
device |
str
|
The device to use. Defaults to “cpu”. |
'cpu'
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary with the gradients of the neural network. |
Examples:
>>> from torch.utils.data import DataLoader
from spotpython.utils.init import fun_control_init
from spotpython.hyperparameters.values import set_control_key_value
from spotpython.data.diabetes import Diabetes
from spotpython.light.regression.netlightregression import NetLightRegression
from spotpython.hyperdict.light_hyper_dict import LightHyperDict
from spotpython.hyperparameters.values import add_core_model_to_fun_control
from spotpython.hyperparameters.values import (
get_default_hyperparameters_as_array, get_one_config_from_X)
from spotpython.hyperparameters.values import set_control_key_value
from spotpython.plot.xai import get_activations
fun_control = fun_control_init(
_L_in=10, # 10: diabetes
_L_out=1,
)
dataset = Diabetes()
set_control_key_value(control_dict=fun_control,
key="data_set",
value=dataset,
replace=True)
add_core_model_to_fun_control(fun_control=fun_control,
core_model=NetLightRegression,
hyper_dict=LightHyperDict)
X = get_default_hyperparameters_as_array(fun_control)
config = get_one_config_from_X(X, fun_control)
_L_in = fun_control["_L_in"]
_L_out = fun_control["_L_out"]
model = fun_control["core_model"](**config, _L_in=_L_in, _L_out=_L_out)
batch_size= config["batch_size"]
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False)
get_gradients(model, fun_control=fun_control, batch_size=batch_size, device = "cpu")
{'layers.0.weight': array([ 0.10417588, -0.04161512, 0.10597267, 0.02180895, 0.12001498,
0.02890352, 0.0114617 , 0.08183316, 0.2495192 , 0.5108763 ,
0.14668094, -0.07902834, 0.00912531, 0.02640062, 0.14108546, ...
Source code in spotpython/plot/xai.py
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 |
|
get_layer_conductance(spot_tuner, fun_control, layer_idx)
¶
Compute the average layer conductance attributions for a specified layer in the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
spot_tuner |
Spot
|
The spot tuner object containing the trained model. |
required |
fun_control |
dict
|
The fun_control dictionary containing the hyperparameters used to train the model. |
required |
layer_idx |
int
|
Index of the layer for which to compute layer conductance attributions. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
numpy.ndarray: An array containing the average layer conductance attributions for the specified layer. The shape of the array corresponds to the shape of the attributions. |
Source code in spotpython/plot/xai.py
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 |
|
get_weights(net, return_index=False)
¶
Get the weights of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
object
|
A neural network. |
required |
return_index |
bool
|
Whether to return the index. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary with the weights of the neural network. |
index |
list
|
The layer index list. |
Examples:
>>> from torch.utils.data import DataLoader
from spotpython.utils.init import fun_control_init
from spotpython.hyperparameters.values import set_control_key_value
from spotpython.data.diabetes import Diabetes
from spotpython.light.regression.netlightregression import NetLightRegression
from spotpython.hyperdict.light_hyper_dict import LightHyperDict
from spotpython.hyperparameters.values import add_core_model_to_fun_control
from spotpython.hyperparameters.values import (
get_default_hyperparameters_as_array, get_one_config_from_X)
from spotpython.hyperparameters.values import set_control_key_value
from spotpython.plot.xai import get_activations
fun_control = fun_control_init(
_L_in=10, # 10: diabetes
_L_out=1,
)
dataset = Diabetes()
set_control_key_value(control_dict=fun_control,
key="data_set",
value=dataset,
replace=True)
add_core_model_to_fun_control(fun_control=fun_control,
core_model=NetLightRegression,
hyper_dict=LightHyperDict)
X = get_default_hyperparameters_as_array(fun_control)
config = get_one_config_from_X(X, fun_control)
_L_in = fun_control["_L_in"]
_L_out = fun_control["_L_out"]
model = fun_control["core_model"](**config, _L_in=_L_in, _L_out=_L_out)
batch_size= config["batch_size"]
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False)
get_weights(model)
{'Layer 0': array([-0.12895013, 0.01047492, -0.15705723, 0.11925378, -0.26944348,
0.23180881, -0.22984707, -0.25141433, -0.19982024, 0.1432175 ,
-0.11684369, 0.11833665, -0.2683918 , -0.19186287, -0.11611126,
-0.06214499, -0.2412386 , 0.20706299, -0.07457635, 0.10150522,
0.22361842, 0.05891514, 0.08647272, 0.3052416 , -0.1426217 ,
0.10016555, -0.14069483, 0.22599205, 0.25255737, -0.29155323,
0.2699465 , 0.1510033 , 0.13780165, 0.13018301, 0.26287982,
-0.04175457, -0.26743335, -0.09074122, -0.2227112 , 0.02090478,
-0.0590421 , -0.16961981, -0.02875188, 0.2995954 , -0.02494261,
0.01004025, -0.04931906, 0.04971322, 0.28176293, 0.19337103,
0.11224869, 0.06871963, 0.07456425, 0.12216929, -0.04086405,
-0.29390487, -0.19555901, 0.26992753, 0.01890203, -0.25616774,
0.04987782, 0.26129004, -0.29883513, -0.21289697, -0.12594265,
0.0126926 , -0.07375361, -0.03475064, -0.30828732, 0.14808285,
0.27756676, 0.19329056, -0.22393112, -0.25491226, 0.13131431,
0.00710201, 0.12963155, -0.3090024 , -0.01885444, 0.22301766],
dtype=float32),
Source code in spotpython/plot/xai.py
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 |
|
get_weights_conductance_last_layer(spot_tuner, fun_control)
¶
Get the weights and the conductance of the last layer.
Source code in spotpython/plot/xai.py
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 |
|
is_square(n)
¶
Check if a number is a square number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
The number to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the number is a square number, False otherwise. |
Examples:
>>> is_square(4)
True
>>> is_square(5)
False
Source code in spotpython/plot/xai.py
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 |
|
old_plot_nn_values_scatter(nn_values, nn_values_names='', absolute=True, cmap='gray', figsize=(6, 6), return_reshaped=False)
¶
Plot the values of a neural network. Can be used to plot the weights, gradients, or activations of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nn_values |
dict
|
A dictionary with the values of the neural network. For example, the weights, gradients, or activations. |
required |
nn_values_names |
str
|
The name of the values. Defaults to “”. |
''
|
absolute |
bool
|
Whether to use the absolute values. Defaults to True. |
True
|
cmap |
str
|
The colormap to use. Defaults to “gray”. |
'gray'
|
figsize |
tuple
|
The figure size. Defaults to (6, 6). |
(6, 6)
|
return_reshaped |
bool
|
Whether to return the reshaped values. Defaults to False. |
False
|
Source code in spotpython/plot/xai.py
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 |
|
plot_attributions(df, attr_method='IntegratedGradients')
¶
Plot the attributions of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
A DataFrame with the attributions. |
required |
attr_method |
str
|
The attribution method. Defaults to “IntegratedGradients”. |
'IntegratedGradients'
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in spotpython/plot/xai.py
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
|
plot_conductance_last_layer(weights_last, layer_conductance_last, show=True)
¶
Plot the conductance of the last layer.
Source code in spotpython/plot/xai.py
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 |
|
plot_nn_values_hist(nn_values, net, nn_values_names='', color='C0', columns=2)
¶
Plot the values of a neural network. Can be used to plot the weights, gradients, or activations of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nn_values |
dict
|
A dictionary with the values of the neural network. For example, the weights, gradients, or activations. |
required |
net |
object
|
A neural network. |
required |
color |
str
|
The color to use. Defaults to “C0”. |
'C0'
|
columns |
int
|
The number of columns. Defaults to 2. |
2
|
Source code in spotpython/plot/xai.py
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 |
|
plot_nn_values_scatter(nn_values, nn_values_names='', absolute=True, cmap='gray', figsize=(6, 6), return_reshaped=False, show=True)
¶
Plot the values of a neural network including a marker for padding values. For simplicity, this example will annotate ‘P’ directly on the plot for padding values using a unique marker value approach.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nn_values |
dict
|
A dictionary with the values of the neural network. For example, the weights, gradients, or activations. |
required |
nn_values_names |
str
|
The name of the values. Defaults to “”. |
''
|
absolute |
bool
|
Whether to use the absolute values. Defaults to True. |
True
|
cmap |
str
|
The colormap to use. Defaults to “gray”. |
'gray'
|
figsize |
tuple
|
The figure size. Defaults to (6, 6). |
(6, 6)
|
return_reshaped |
bool
|
Whether to return the reshaped values. Defaults to False. |
False
|
show |
bool
|
Whether to show the plot. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary with the reshaped values. |
Source code in spotpython/plot/xai.py
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 |
|
visualize_activations(net, fun_control, batch_size, device, absolute=True, cmap='gray', figsize=(6, 6))
¶
Scatter plots the activations of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
object
|
A neural network. |
required |
fun_control |
dict
|
A dictionary with the function control. |
required |
batch_size |
int
|
The batch size. |
required |
device |
str
|
The device to use. |
required |
absolute |
bool
|
Whether to use the absolute values. Defaults to True. |
True
|
cmap |
str
|
The colormap to use. Defaults to “gray”. |
'gray'
|
figsize |
tuple
|
The figure size. Defaults to (6, 6). |
(6, 6)
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in spotpython/plot/xai.py
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 |
|
visualize_activations_distributions(net, fun_control, batch_size, device='cpu', color='C0', columns=2)
¶
Plots a histogram of the activations of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
object
|
A neural network. |
required |
fun_control |
dict
|
A dictionary with the function control. |
required |
batch_size |
int
|
The batch size. |
required |
device |
str
|
The device to use. Defaults to “cpu”. |
'cpu'
|
color |
str
|
The color to use. Defaults to “C0”. |
'C0'
|
columns |
int
|
The number of columns. Defaults to 2. |
2
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in spotpython/plot/xai.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
|
visualize_gradient_distributions(net, fun_control, batch_size, device='cpu', color='C0', xlabel=None, stat='count', use_kde=True, columns=2)
¶
Plot the gradients distributions of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
object
|
A neural network. |
required |
fun_control |
dict
|
A dictionary with the function control. |
required |
batch_size |
int
|
The batch size. |
required |
device |
str
|
The device to use. Defaults to “cpu”. |
'cpu'
|
color |
str
|
The color to use. Defaults to “C0”. |
'C0'
|
xlabel |
str
|
The x label. Defaults to None. |
None
|
stat |
str
|
The stat. Defaults to “count”. |
'count'
|
use_kde |
bool
|
Whether to use kde. Defaults to True. |
True
|
columns |
int
|
The number of columns. Defaults to 2. |
2
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in spotpython/plot/xai.py
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 |
|
visualize_gradients(net, fun_control, batch_size, absolute=True, cmap='gray', figsize=(6, 6))
¶
Scatter plots the gradients of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
object
|
A neural network. |
required |
fun_control |
dict
|
A dictionary with the function control. |
required |
batch_size |
int
|
The batch size. |
required |
absolute |
bool
|
Whether to use the absolute values. Defaults to True. |
True
|
cmap |
str
|
The colormap to use. Defaults to “gray”. |
'gray'
|
figsize |
tuple
|
The figure size. Defaults to (6, 6). |
(6, 6)
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in spotpython/plot/xai.py
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 |
|
visualize_weights(net, absolute=True, cmap='gray', figsize=(6, 6))
¶
Scatter plots the weights of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
object
|
A neural network. |
required |
absolute |
bool
|
Whether to use the absolute values. Defaults to True. |
True
|
cmap |
str
|
The colormap to use. Defaults to “gray”. |
'gray'
|
figsize |
tuple
|
The figure size. Defaults to (6, 6). |
(6, 6)
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in spotpython/plot/xai.py
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
|
visualize_weights_distributions(net, color='C0', columns=2)
¶
Plot the weights distributions of a neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
object
|
A neural network. |
required |
color |
str
|
The color to use. Defaults to “C0”. |
'C0'
|
columns |
int
|
The number of columns. Defaults to 2. |
2
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in spotpython/plot/xai.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
|