This post assumes some familiarity with the idea of concept erasure and our LEACE concept erasure method. We encourage the reader to consult our arXiv paper for background.
For a PyTorch implementation of this method, see the OracleFitter class in our GitHub repository.
WARNING: Because this erasure transformation depends on the ground truth concept label, it can increase the nonlinearly-extractable information about the target concept inside a representation, even though it eliminates the linearly available information. For this reason, optimizing deep neural networks on top of O-LEACE’d representations is not recommended; for those use cases we recommend vanilla LEACE.
In our paper LEACE: Perfect linear concept erasure in closed form, we derived a concept erasure method that does not require access to concept labels at inference time. That is, we can fit an erasure function on a labeled training dataset, then apply the function to unlabeled datapoints. It turns out, however, that we can achieve an even more surgical edit if we have oracle access to the label z for each x. In Theorem 1 below, we derive Oracle LEACE (“O-LEACE”), a closed-form formula for the nearest X′ to any X such that no linear classifier can do better than chance at predicting Z from X′, or equivalently Cov(X′,Z)=0.
The resulting XLEACE′ is “nearest” to X with respect to all positive semidefinite (p.s.d.) inner products aTMb defined on Rd simultaneously. This is because, by expressing X in a basis that diagonalizes M, we can decompose the problem into d independent subproblems, one for each component Xi of X. Each subproblem can then be viewed as an orthogonal projection, not in Rd, but in the vector space of scalar, real-valued random variables. For geometric intuition, see the figure below.
Orthogonal projection of ith component of X, itself a vector in the random variable Hilbert space H, onto the span of the components of Z. The residual Xi−projZXi is the closest vector to Xi orthogonal to, and hence uncorrelated with, Z=span({Z1,Z2})
Derivation
Prior work has noted that computing an orthogonal projection in a random variable Hilbert space is equivalent to solving an ordinary least squares regression problem. Our theorem is a natural extension of this work: we find that XLEACE′ is equal to the OLS residual from regressing X on Z, plus a constant shift needed to ensure that erasing Z does not change the mean of X. Note that the regression problem here is reversed from the usual one: we are predicting X from Z, rather than the other way around.
Theorem 1.
Let H be the Hilbert space of square-integrable real-valued random variables equipped with the inner product ⟨ξ,ζ⟩H:=E[ξζ]. Let (X,Z) be random vectors in Hd and Hk respectively. Then for every p.s.d. inner product ⟨a,b⟩M=aTMb on Rd, the objective
argminX′∈HdEX′−XM2s.t.Cov(X′,Z)=0
is minimized by the (appropriately shifted) ordinary least squares residuals from regressing X on Z:
XLEACE′=X+ΣXZΣZZ+(E[Z]−Z).
Proof.
Assume w.l.o.g. that X and X′ are represented in a basis diagonalizing M, so we may write
EX′−XM2=i=1∑dmiE[(Xi′−Xi)2],(1)
where m1,…,md≥0 are eigenvalues of M. Crucially, each term in this sum is independent from the others, allowing us to decompose the primal problem into d separate subproblems of the form ∥Xi′−Xi∥H2, one for each component i of (X,X′). We may also discard the mi terms since they are non-negative constants, and hence do not affect the optimal Xi′ for any i.
Factoring out constants. Now consider the subspace C=span(1)⊂H consisting of all constant (i.e. zero variance) random variables. Orthogonally decomposing Xi along C yields Xi=X~i+μi, where μi=E[Xi]∈C and X~i=X−E[X]i∈C⊥, and likewise for Xi′. Our objective is now
Since μi′ and μi are orthogonal to X~i′ and X~i, and the constraint Cov(X′,Z)=0 is invariant to constant shifts, we can optimize the two terms in Eq. 2 independently. The first term is trivial: it is minimized when μi′=μi, and hence Xi′=X~i′+E[Xi].
Orthogonal projection. We can now rewrite the zero covariance condition as an orthogonality constraint on X~i′. Specifically, for every i∈1…d we have
where Z~=Z−E[Z]. In other words, we seek the nearest X~i′ to X~i orthogonal to Z=span({Z~1,…,Z~k}), which is simply the orthogonal projection of X~i onto Z⊥. This in turn is equal to the ordinary least squares residual from regressing X~ on Z~:
Putting it all together. Plugging Eq. 4 into Xi′=X~i′+E[Xi] and combining all components into vector form yields
XLEACE′=X−ΣXZΣZZ+(Z−E[Z]),(5)
which completes the proof.
Diff-in-means for binary concepts
In our last blog post, we showed that the difference-in-means direction δ=E[X∣Z=1]−E[X∣Z=0] is worst-case optimal for performing additive edits to binary concepts in neural network representations. We now show that a similar result holds for concept erasure: Equation 5 simplifies to an expression involving δ when Z is binary.
Equivalence of cross-covariance and diff-in-means
We first show that the cross-covariance matrix in this case has a very close relationship with the difference-in-means direction vectors δj=E[X∣Zj=1]−E[X∣Zj=0] for each class j. This result is general, and holds for the multiclass case as well as the binary one.
Lemma 1.
Let X and Z be random vectors of finite first moment taking values in Rd and {z∈{0,1}k:zTz=1} respectively, where ∀j:Var(Zj)>0. Then each column j of ΣXZ is precisely Var(Zj)δj.
Proof.
Let P(Zj) be the probability that the jth entry of Z is 1. Then for column j of ΣXZ we have
where the penultimate step is valid since Zj∈{0,1} and hence (Zj)2=Zj. Therefore we have
Cov(X,Zj)=Var(Zj)δj.(9)
Diff-in-means oracle eraser
We now have the tools to simplify Equation 5 in the binary case.
Theorem 2. If Z is binary, then the least-squares oracle eraser is given by the difference-in-means additive edit
XLEACE′=X+(P[Z]−Z)δ.(10)
If the classes are balanced, i.e. P(Z=0)=P(Z=1)=21, this simplifies to
XLEACE′={X+21δX−21δif Z=0if Z=1(11)
Proof. By Lemma 1, we can rewrite ΣXZ=Var(Z)δ. Since ΣZZ is a 1 x 1 matrix whose only element is Var(Z), we have ΣZZ+=Var(Z)−1. Plugging these into Eq. 5 yields
In many cases in interpretability research, we do have access to concept labels for all inputs we are interested in, so O-LEACE may be applicable in those contexts. Care must be taken, however, to ensure that the method does not “backfire” and make the target concept more easily extractable. We have observed in preliminary experiments that when we train a deep neural network from scratch on O-LEACE’d data, the network often performs better at extracting the concept than when trained on the original data. This backfiring phenomenon is due to the fact that the O-LEACE transformation is dependent on Z, and therefore transmits Shannon information about Z, even though it perfectly removes linearly extractable information about Z.
We suspect O-LEACE is most useful in cases where we would like to remove information about Z from the activations of a frozen, pre-trained model, since the model does not have the opportunity to adapt to the subtle artifacts that O-LEACE leaves in the representation. LEACE, by contrast, cannot possibly increase the mutual information between X and Z due to the data processing inequality, and therefore should be safe to use in any context.
Thank you to Alex Mallen and Brennan Dury for helpful feedback on this post.