Experiment
Experiment
Bases: Observable
A class representing an experiment for model interpretability.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The machine learning model to be interpreted. |
required |
data |
DataSource
|
The data used for the experiment. |
required |
modality |
Modality
|
The type of modality (image, tabular, text, time series) the model is designed for. |
required |
explainers |
Sequence[Explainer]
|
Explainer objects or their arguments for interpreting the model. |
required |
postprocessors |
Optional[Sequence[Callable]]
|
Postprocessing functions to apply to explanations. |
required |
metrics |
Optional[Sequence[Metric]]
|
Evaluation metrics used to assess model interpretability. |
required |
input_extractor |
Optional[Callable[[Any], Any]]
|
Function to extract inputs from data. |
None
|
label_extractor |
Optional[Callable[[Any], Any]]
|
Function to extract labels from data. |
None
|
target_extractor |
Optional[Callable[[Any], Any]]
|
Function to extract targets from data. |
None
|
cache_device |
Optional[Union[device, str]]
|
Device to cache data and results. |
None
|
target_labels |
bool
|
True if the target is a label, False otherwise. |
False
|
Attributes:
Name | Type | Description |
---|---|---|
modality |
Modality
|
Object defining the modality-specific control flow of the experiment. |
manager |
ExperimentManager
|
Manager object for the experiment. |
all_explainers |
Sequence[Explainer]
|
All explainer objects used in the experiment. |
all_metrics |
Sequence[Metric]
|
All evaluation metrics used in the experiment. |
errors |
Sequence[Error]
|
|
is_image_task |
bool
|
True if the modality is an image-related modality, False otherwise. |
has_explanations |
bool
|
True if the experiment has explanations, False otherwise. |
Source code in pnpxai/core/experiment/experiment.py
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 |
|
evaluate_batch(data_ids, explainer_id, postprocessor_id, metric_id)
Evaluates selected batch of data within experiment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Sequence[int]
|
A sequence of data IDs to specify the subset of data to postprocess. |
required |
explainer_id |
int
|
An explainer ID to specify the explainer to use. |
required |
postprocessor_id |
int
|
A postprocessor ID to specify the postprocessor to use. |
required |
metric_id |
int
|
A metric ID to evaluate the model explanations. |
required |
Returns:
Type | Description |
---|---|
Batched model evaluations corresponding to data ids. |
This method orchestrates the experiment by configuring the manager, obtaining explainer instance, processing data, generating explanations, and evaluating results. It then caches the results in the manager, and returns back to the user.
Source code in pnpxai/core/experiment/experiment.py
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 |
|
explain_batch(data_ids, explainer_id)
Explains selected batch of data within experiment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Sequence[int]
|
A sequence of data IDs to specify the subset of data to process. |
required |
Returns:
Type | Description |
---|---|
Batched model explanations corresponding to data ids. |
This method orchestrates the experiment by configuring the manager, obtaining explainer instance, processing data, and generating explanations. It then caches the results in the manager, and returns back to the user.
Source code in pnpxai/core/experiment/experiment.py
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 |
|
get_all_inputs_flattened()
Retrieve and flatten all input data.
Returns:
Type | Description |
---|---|
Sequence[Tensor]
|
Flattened input data from all available data. |
This method retrieves input data from all available data points using the input extractor and flattens it.
Source code in pnpxai/core/experiment/experiment.py
399 400 401 402 403 404 405 406 407 408 409 410 |
|
get_evaluations_flattened(data_ids=None)
Retrieve and flatten evaluations for all explainers and metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Optional[Sequence[int]]
|
A sequence of data IDs to specify the subset of data to process. |
None
|
Returns:
Type | Description |
---|---|
Sequence[Sequence[Sequence[Tensor]]]
|
Flattened evaluations for all explainers and metrics. |
This method retrieves flattened evaluations for each explainer and metric using the manager's get_flat_evaluations method.
Note: The input parameters allow for flexibility in specifying subsets of data to process. If not provided, the method processes all available data.
Source code in pnpxai/core/experiment/experiment.py
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 |
|
get_explainers_ranks()
Calculate and return rankings for explainers based on evaluations.
Returns:
Type | Description |
---|---|
Optional[Sequence[Sequence[int]]]
|
Rankings of explainers. Returns None if rankings cannot be calculated. |
This method calculates rankings for explainers based on evaluations and metric scores. It considers metric priorities and sorting preferences to produce rankings.
Source code in pnpxai/core/experiment/experiment.py
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 |
|
get_explanations_flattened(data_ids=None)
Retrieve and flatten explanations from all explainers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Optional[Sequence[int]]
|
A sequence of data IDs to specify the subset of data to process. |
None
|
Returns:
Type | Description |
---|---|
Sequence[Sequence[Tensor]]
|
Flattened explanations from all explainers. |
This method retrieves flattened explanations for each explainer using the manager's get_flat_explanations
method.
Note: The input parameters allow for flexibility in specifying subsets of data to process. If not provided, the method processes all available data.
Source code in pnpxai/core/experiment/experiment.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
|
get_inputs_flattened(data_ids=None)
Retrieve and flatten last run input data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Optional[Sequence[int]]
|
A sequence of data IDs to specify the subset of data to process. |
None
|
Returns:
Type | Description |
---|---|
Sequence[Tensor]
|
Flattened input data. |
This method retrieves input data using the input extractor and flattens it for further processing.
Note: The input parameters allow for flexibility in specifying subsets of data to process. If not provided, the method processes all available data.
Source code in pnpxai/core/experiment/experiment.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
|
get_labels_flattened(data_ids=None)
Retrieve and flatten labels data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Optional[Sequence[int]]
|
A sequence of data IDs to specify the subset of data to process. |
None
|
Returns:
Type | Description |
---|---|
Sequence[Tensor]
|
Flattened labels data. |
This method retrieves label data using the label extractor and flattens it for further processing.
Source code in pnpxai/core/experiment/experiment.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
|
get_outputs_flattened(data_ids=None)
Retrieve and flatten model outputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Optional[Sequence[int]]
|
A sequence of data IDs to specify the subset of data to process. |
None
|
Returns:
Type | Description |
---|---|
Sequence[Tensor]
|
Flattened model outputs. |
This method retrieves flattened model outputs using the manager's get_flat_outputs method.
Note: The input parameters allow for flexibility in specifying subsets of data to process. If not provided, the method processes all available data.
Source code in pnpxai/core/experiment/experiment.py
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
|
get_targets_flattened(data_ids=None)
Retrieve and flatten target data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Optional[Sequence[int]]
|
A sequence of data IDs to specify the subset of data to process. |
None
|
Returns:
Type | Description |
---|---|
Sequence[Tensor]
|
Flattened target data. |
This method retrieves target data using the target extractor and flattens it for further processing.
Note: The input parameters allow for flexibility in specifying subsets of data to process. If not provided, the method processes all available data.
Source code in pnpxai/core/experiment/experiment.py
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
|
optimize(data_ids, explainer_id, metric_id, direction='maximize', sampler='tpe', n_trials=None, timeout=None, **kwargs)
Optimize experiment hyperparameters by processing data, generating explanations, evaluating with metrics, caching and retrieving the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Union[int, Sequence[int]]
|
A single data ID or sequence of data IDs to specify the subset of data to process. |
required |
explainer_id |
int
|
An explainer ID to specify the explainer to use. |
required |
metric_id |
int
|
A metric ID to evaluate optimizer decisions. |
required |
direction |
Literal['minimize', 'maximize']
|
A string to specify the direction of optimization. |
'maximize'
|
sampler |
Literal['grid', 'random', 'tpe']
|
A string to specify the sampler to use for optimization. |
'tpe'
|
n_trials |
Optional[int]
|
An integer to specify the number of trials for optimization. If none passed, the number of trials is inferred from |
None
|
timeout |
Optional[float]
|
A float to specify the timeout for optimization. Ignored, if |
None
|
Returns:
Type | Description |
---|---|
The Experiment instance with updated results and state. |
This method orchestrates the experiment by configuring the manager, obtaining explainer and metric instances, processing data, generating explanations, and evaluating metrics. It then saves the results in the manager.
Note: The input parameters allow for flexibility in specifying subsets of data, explainers, and metrics to process. If not provided, the method processes all available data, explainers, postprocessors, and metrics.
Source code in pnpxai/core/experiment/experiment.py
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 |
|
postprocess_batch(data_ids, explainer_id, postprocessor_id)
Postprocesses selected batch of data within experiment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Sequence[int]
|
A sequence of data IDs to specify the subset of data to postprocess. |
required |
explainer_id |
int
|
An explainer ID to specify the explainer to use. |
required |
postprocessor_id |
int
|
A postprocessor ID to specify the postprocessor to use. |
required |
Returns:
Type | Description |
---|---|
Batched postprocessed model explanations corresponding to data ids. |
This method orchestrates the experiment by configuring the manager, obtaining explainer instance, processing data, and generating explanations. It then caches the results in the manager, and returns back to the user.
Source code in pnpxai/core/experiment/experiment.py
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 |
|
predict_batch(data_ids)
Predicts results of the experiment for selected batch of data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Sequence[int]
|
A sequence of data IDs to specify the subset of data to process. |
required |
Returns:
Type | Description |
---|---|
Batched model outputs corresponding to data ids. |
This method orchestrates the experiment by configuring the manager and processing data. It then caches the results in the manager, and returns back to the user.
Source code in pnpxai/core/experiment/experiment.py
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 |
|
run_batch(explainer_id, postprocessor_id, metric_id, data_ids=None)
Runs the experiment for selected batch of data, explainer, postprocessor and metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_ids |
Sequence[int]
|
A sequence of data IDs to specify the subset of data to process. |
None
|
explainer_id |
int
|
ID of explainer to use for the run. |
required |
postprocessor_id |
int
|
ID of postprocessor to use for the run. |
required |
metrics_id |
int
|
ID of metric to use for the run. |
required |
Returns:
Type | Description |
---|---|
dict
|
The dictionary of inputs, labels, outputs, targets, explainer, explanation, postprocessor, postprocessed, metric, and evaluation. |
This method orchestrates the experiment by configuring the manager, obtaining explainer and metric instances, processing data, generating explanations, and evaluating metrics. It then caches the results in the manager, and returns back to the user.
Note: The input parameters allow for flexibility in specifying subset of data, explainer, postprocessor and metric to process.
Source code in pnpxai/core/experiment/experiment.py
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 |
|