pulsatrix 1.1.0
Loading...
Searching...
No Matches
tcav.hpp
Go to the documentation of this file.
1
25#pragma once
26
27#include <cstdint>
28#include <vector>
29
31#include "pulsatrix/tensor.hpp"
32
33namespace pulsatrix {
34
44
45struct CavOptions {
48 double l2 = 0.01;
49 int64_t iterations = 500;
52 int64_t holdout_every = 3;
53};
54
56struct Cav {
58 std::vector<double> direction;
62 double accuracy = 0;
63};
64
71[[nodiscard]] Cav TrainCav(const Tensor& concept_activations, const Tensor& random_activations, const CavOptions& options = {});
72
80[[nodiscard]] std::vector<double> ConceptualSensitivities(const Tensor& gradients, const Cav& cav);
81
83[[nodiscard]] double TcavScore(const std::vector<double>& sensitivities);
84
87 double t = 0;
90 double p_value = 1;
91};
92
100[[nodiscard]] TTestResult StudentTTest(const std::vector<double>& a, const std::vector<double>& b);
101
103[[nodiscard]] Tensor LayerActivations(ExplainerContext& ctx, const Tensor& inputs, NodeId layer);
104
107[[nodiscard]] Tensor LayerGradients(ExplainerContext& ctx, const Tensor& inputs, NodeId layer, int64_t target, DeviceBackend* backend);
108
113 double alpha = 0.05;
114};
115
119 std::vector<double> scores;
122 std::vector<double> random_scores;
124 std::vector<double> cav_accuracies;
125 double mean_score = 0;
129 bool significant = false;
130};
131
144[[nodiscard]] TcavResult RunTcav(ExplainerContext& ctx, NodeId layer, int64_t target, const Tensor& class_inputs, const Tensor& concept_inputs,
145 const std::vector<Tensor>& random_inputs, DeviceBackend* backend, const TcavOptions& options = {});
146
147} // namespace pulsatrix
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
Wraps an ordered chain of Modules, running them via Module::forward_traced to build a real Computatio...
Definition explainer_context.hpp:67
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
Stable interface every explainer gets, regardless of type (charter Part 2 SS2).
Definition acquisition_functions.hpp:16
Tensor LayerActivations(ExplainerContext &ctx, const Tensor &inputs, NodeId layer)
The layer's activations for inputs, from one forward pass: shape (n, width).
Tensor LayerGradients(ExplainerContext &ctx, const Tensor &inputs, NodeId layer, int64_t target, DeviceBackend *backend)
The gradient of logit target with respect to the layer, for each of inputs: shape (n,...
size_t NodeId
Stable identifier for a Node within its owning ComputationGraph.
Definition node.hpp:18
TTestResult StudentTTest(const std::vector< double > &a, const std::vector< double > &b)
Student's two-sample t-test with pooled variance, two-sided, as scipy.stats.ttest_ind computes by def...
std::vector< double > ConceptualSensitivities(const Tensor &gradients, const Cav &cav)
Each input's directional derivative along the CAV: the row of gradients dotted with cav....
Cav TrainCav(const Tensor &concept_activations, const Tensor &random_activations, const CavOptions &options={})
Trains a CAV from a layer's activations on concept examples and on random examples.
TcavResult RunTcav(ExplainerContext &ctx, NodeId layer, int64_t target, const Tensor &class_inputs, const Tensor &concept_inputs, const std::vector< Tensor > &random_inputs, DeviceBackend *backend, const TcavOptions &options={})
TCAV with Kim et al.'s significance test, for one concept, layer and class.
CavMethod
How TrainCav() finds the concept direction.
Definition tcav.hpp:36
@ LogisticRegression
The weights of an L2-regularized logistic regression, as Kim et al. train them. The features are stan...
@ DifferenceOfMeans
The difference of the two sets' mean activations. It needs no fitting, and doesn't rotate towards dim...
double TcavScore(const std::vector< double > &sensitivities)
The fraction of sensitivities above zero.
Definition tcav.hpp:45
int64_t iterations
Definition tcav.hpp:49
double l2
The L2 penalty on the standardized weights, per example.
Definition tcav.hpp:48
CavMethod method
Definition tcav.hpp:46
int64_t holdout_every
Every this-many-th example of each set is held out to measure accuracy. 0 holds none out,...
Definition tcav.hpp:52
A concept activation vector.
Definition tcav.hpp:56
double accuracy
How well the direction's linear classifier separates concept from random examples that it wasn't fitt...
Definition tcav.hpp:62
std::vector< double > direction
A unit vector in the layer's activation space, pointing towards the concept.
Definition tcav.hpp:58
A two-sample t-test's result.
Definition tcav.hpp:86
double degrees_of_freedom
Definition tcav.hpp:88
double p_value
Two-sided.
Definition tcav.hpp:90
double t
Definition tcav.hpp:87
Definition tcav.hpp:109
CavOptions cav
Definition tcav.hpp:110
double alpha
The significance level for RunTcav()'s t-test. With several concepts, layers or classes,...
Definition tcav.hpp:113
RunTcav()'s result.
Definition tcav.hpp:117
double mean_score
Definition tcav.hpp:125
std::vector< double > random_scores
Each random set's TCAV score, as a concept, against the next random set: what chance looks like.
Definition tcav.hpp:122
std::vector< double > cav_accuracies
The CAVs' accuracies, concept against each random set.
Definition tcav.hpp:124
bool significant
Whether test.p_value is below the alpha asked for.
Definition tcav.hpp:129
double mean_random_score
Definition tcav.hpp:126
std::vector< double > scores
The concept's TCAV score against each random set.
Definition tcav.hpp:119
TTestResult test
Definition tcav.hpp:127
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).