63 const std::vector<Tensor>& background, int64_t feature_index,
64 int64_t target_index,
float grid_min,
float grid_max, int64_t grid_size,
66 if (background.empty()) {
67 throw std::invalid_argument(
"PDP::explain: background must not be empty");
70 PULSATRIX_ASSERT(feature_index >= 0 && feature_index < background[0].numel());
72 std::vector<std::vector<float>> background_values;
73 background_values.reserve(background.size());
74 for (
const Tensor& instance : background) {
75 background_values.push_back(instance.to_host_vector());
77 std::vector<float> curve(
static_cast<size_t>(grid_size));
79 for (int64_t k = 0; k < grid_size; ++k) {
80 float v = (grid_size == 1) ? grid_min : grid_min +
static_cast<float>(k) * (grid_max - grid_min) /
81 static_cast<float>(grid_size - 1);
84 for (
size_t b = 0; b < background.size(); ++b) {
85 const Tensor& instance = background[b];
86 std::vector<float> perturbed_values = background_values[b];
87 perturbed_values[
static_cast<size_t>(feature_index)] = v;
89 Tensor output = predict(perturbed);
92 curve[
static_cast<size_t>(k)] = sum /
static_cast<float>(background.size());
96 background[0].device());
98 {{
"feature_index", std::to_string(feature_index)},
99 {
"target_index", std::to_string(target_index)},
100 {
"grid_size", std::to_string(grid_size)}}};
PULSATRIX_ASSERT – debug-only invariant check for programmer errors, distinct from throw (used for ca...
#define PULSATRIX_ASSERT(cond)
Aborts with a diagnostic message if cond is false. Debug-only – use for conditions that indicate a bu...
Definition assert.hpp:22
First-class explanation result type – values, method, and metadata together.
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
PDP_j(v) = (1/|B|) * sum_{b in B} f(x_j=v, x_{-j}=b_{-j}) – for each grid value v,...
Definition pdp.hpp:38
Attribution explain(const std::function< Tensor(const Tensor &)> &predict, const std::vector< Tensor > &background, int64_t feature_index, int64_t target_index, float grid_min, float grid_max, int64_t grid_size, DeviceBackend *backend) const
Computes the PDP curve for one feature.
Definition pdp.hpp:62
static const ExplainerCapabilities & capabilities()
What it answers and needs: its ExplainerCatalog() entry.
Definition pdp.hpp:41
An N-dimensional shape. A plain aggregate of dimensions with no invariant beyond "non-negative dimens...
Definition shape.hpp:24
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
DeviceType device() const
Which device this tensor's buffer conceptually resides on.
Definition tensor.hpp:122
DeviceBackend * backend() const
The backend that owns this tensor's buffer. Not owned by the Tensor.
Definition tensor.hpp:130
const Shape & shape() const
This tensor's shape.
Definition tensor.hpp:113
float read_element(int64_t flat_index) const
Reads one element to the host through the owning backend, on any device.
Abstract interface isolating vendor-specific memory/compute operations from Tensor/ComputationGraph.
What each explainer answers: the nine question categories (Liao, Gruen and Miller,...
DeviceBackend * backend_beside(const Tensor &like, DeviceBackend *backend)
The backend to allocate a tensor through that must live beside like.
Definition attribution.hpp:49
Definition acquisition_functions.hpp:16
const ExplainerCapabilities & ExplainerCapabilitiesOf(std::string_view name)
The catalog entry named name.
An explanation result: the raw attribution values, the method that produced them, and any relevant me...
Definition attribution.hpp:23
One explainer's declaration: what it answers and what it needs.
Definition explainer_capabilities.hpp:99
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).