Skip to content

Interpretability, attribution, and explanation audits · Interpretability, explanations, and model-mechanism analysis · EXP007 · Diagnostic

TCAV: testing with concept activation vectors

Summarize how a class score changes in a learned activation-space direction associated with a user-supplied concept.

Runs in CodeSplash (Experimental) Complete calculation CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Availability
Runs in CodeSplash (Experimental)
Code example
Complete calculation
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through explain.tcav. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Summarize how a class score changes in a learned activation-space direction associated with a user-supplied concept.

Question / hypotheses

Across the selected class examples, how often does the score increase along the fitted concept direction? Statistical comparisons require repeated concept/control constructions and an explicit inference design.

Architecture / computational structure

Examples of a concept and controls are embedded at a chosen layer. A linear separator defines a concept activation vector. Directional derivatives of the target score are calculated for evaluation examples and aggregated, commonly as the fraction with positive derivative.

Assumptions

Concept examples and controls must represent the intended semantic contrast. Linear separability in the chosen layer is only an operational definition. Sampling units, correlated examples, repeated control sets and multiple tested concepts affect uncertainty.

Formula / computational definition

SC,k,l(x)=∇hlfk(x)⊤vCl,TCAVC,k,l=1∣Xk∣∑x∈Xk1{SC,k,l(x)>0}.S_{C,k,l}(x)=\nabla_{h_l}f_k(x)^\top v_C^l,\qquad \mathrm{TCAV}_{C,k,l}=\frac1{|X_k|}\sum_{x\in X_k}\mathbf1\{S_{C,k,l}(x)>0\}.

Parameters and notation

Fitted explanation parameter: concept separator direction v. Model weights remain fixed. Choices include layer l, concept/control datasets, separator regularization, class k, sign convention, repetitions, and multiplicity procedure. A CAV’s scale should be normalized or otherwise recorded. Here h_l is the layer-l representation, f_k the class-k scalar score, v_C^l the oriented concept-separator normal, X_k the evaluated class-k examples, and the indicator counts positive directional derivatives.

Fitting, fine-tuning, and adaptation

Refit concept directions after changing the encoder; an old vector is not generally meaningful in a newly rotated representation. Test concept-prediction quality independently and evaluate whether adaptation changes concept sensitivity or merely representation coordinates.

How to use and calibrate

Use several control sets, report the distribution of scores and concept-separator performance, and compare against random concept baselines. Distinguish a descriptive positive-direction fraction from a formally calibrated significance claim.

Example application

Investigate whether a model’s texture concept sensitivity persists on images with unfamiliar backgrounds.

Interpretation / pitfalls

Watch out

A concept classifier can learn collection artifacts. Directional sensitivity is not evidence that the real concept causes the class or that the network represents only that concept. Testing many layers/concepts without correction inflates discoveries.

Python and R code examples

Scope: Compute directional sensitivities and a sign-fraction score for supplied gradients and a supplied normalized CAV; concept fitting and inferential repetitions are not implemented. Python: NumPy; R: base R.

Validation: Python executed successfully on the supplied fixture; R reviewed but unexecuted. Full implementations and supported operation details are provided in the linked primary/official sources.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Complete calculation — Compute directional sensitivities and a sign-fraction score for supplied gradients and a supplied normalized CAV; concept fitting and inferential repetitions are not implemented. Python: NumPy; R: base R. · needs numpy

import numpy as np
grad=np.array([[1.,2.],[-1.,.2],[.4,-.1]]);v=np.array([1.,1.]);v/=np.linalg.norm(v)
s=grad@v;score=np.mean(s>0);print(s,score)
assert np.isclose(score,2/3)
R

Complete calculation — Compute directional sensitivities and a sign-fraction score for supplied gradients and a supplied normalized CAV; concept fitting and inferential repetitions are not implemented. Python: NumPy; R: base R.

grad<-rbind(c(1,2),c(-1,.2),c(.4,-.1));v<-c(1,1)/sqrt(2)
s<-drop(grad%*%v);score<-mean(s>0);print(s);print(score)
stopifnot(abs(score-2/3)<1e-12)

Sources

From the CodeSplash reference, entry EXP007.