Attribution and Training-Data Transparency › Key Parameters
Language AI HandbookAll chapters The key parameters for this linear influence example are:
- L2: Strength of the L2 penalty on word weights. Logistic loss is convex. Positive L2 gives curvature to the word weights, and logistic loss gives the unregularized bias positive curvature at finite logits. Adding positive damping increases each Hessian eigenvalue.
- max_iter: Maximum LBFGS iterations. Enough iterations are needed to reach a converged optimum, which the influence approximation assumes.
- damping: Regularization added to the Hessian diagonal. A positive shift improves conditioning for the positive-definite Hessian used here and can change the magnitude or ordering of the signed scores.
- vocabulary: The vocabulary size plus one intercept coordinate sets the size of the parameter vector and Hessian. Each review uses a binary bag-of-words representation.
L2 shrinks the word weights. A smaller penalty can leave little curvature in low-variance directions, while a larger penalty can suppress useful coefficients. max_iter limits the optimizer’s work. Check the reported gradient norm because the derivation assumes a stationary fit. Damping changes the linear system and its conditioning, and the vocabulary sets the feature dimension. For an attribution report, name the corresponding choices and give the diagnostics you used to check them.
PythonIn [4]Expand
L2 = 0.1
def total_loss(theta):
logits = X @ theta
data_loss = F.binary_cross_entropy_with_logits(logits, y, reduction="mean")
reg = 0.5 * L2 * torch.sum(theta[:-1] ** 2)
return data_loss + reg
def per_example_loss(theta, x, target):
logit = torch.dot(theta, x)
return F.binary_cross_entropy_with_logits(logit, target, reduction="sum")
theta = torch.zeros(X.shape[1], dtype=torch.float64, requires_grad=True)
optimizer = torch.optim.LBFGS(
[theta],
lr=1.0,
max_iter=300,
line_search_fn="strong_wolfe",
)
def closure():
optimizer.zero_grad()
loss = total_loss(theta)
loss.backward()
return loss
optimizer.step(closure)ConsoleOut [5]Expand
Final training loss: 0.4874 Training gradient norm: 2.10e-06
The model has now learned a linear decision boundary. The L2 penalty contributes a diagonal matrix with on each word-weight coordinate and zero on the intercept coordinate. The damping shift adds 0.001 to every diagonal entry, including the intercept. The undamped objective is convex and the damped Hessian is positive definite.
The loss reports the fitted objective. The gradient norm checks how close the fit is to a stationary point, which is the condition used in the derivation.
Computing influence on the test sentence
We now compute the gradient of the test loss, the Hessian of the training objective, and the gradient of each training example's loss. The influence of upweighting example on the test loss is the negative dot product of the training gradient with times the test gradient. After damping the Hessian, we estimate the leave-one-out effect by dividing by the number of training examples and flipping the sign.
The code below performs three computations in sequence. First, it computes the gradient of the test loss with respect to the parameters; this is the direction in which the test loss changes as the parameters move. Second, it computes the Hessian of the full regularized training objective and adds a small damping term. Third, it solves the damped system once, which means each training example's influence is a single dot product rather than a separate matrix inversion. This is the same trick that makes Hessian-vector-product methods feasible in larger models: solve for a vector once, then reuse it.
PythonIn [6]Expand
x_test = vectorize("this movie is great")
y_test = torch.tensor(1.0, dtype=torch.float64)
grad_test = torch.autograd.grad(per_example_loss(theta, x_test, y_test), theta)[
0
]
H = torch.autograd.functional.hessian(total_loss, theta)
damping = 1e-3
H_damped = H + damping * torch.eye(H.shape[0], dtype=torch.float64)
# Solve the damped system once and reuse its solution for each example.
v = torch.linalg.solve(H_damped, grad_test)
grad_train = torch.stack(
[
torch.autograd.grad(per_example_loss(theta, X[i], y[i]), theta)[0]
for i in range(len(raw_docs))
]
)
# Effect of upweighting a training example on the test loss.
influence_up = -(grad_train @ v)
# Approximate change in test loss if the example is removed.
loo_effect = -influence_up / len(raw_docs)PythonIn [7]Expand
influence_df = pd.DataFrame(
{
"document": [text for text, _ in raw_docs],
"label": [
"positive" if label == 1 else "negative" for _, label in raw_docs
],
"influence_up": influence_up.detach().numpy(),
"loo_effect": loo_effect.detach().numpy(),
}
).sort_values("loo_effect", ascending=False)ConsoleOut [8]Expand
Most helpful for this test sentence (removal increases loss):
document label loo_effect
this movie is good positive 0.165365
wonderful story and great acting positive 0.072263
i love this movie positive 0.056143
great film love it positive 0.055221
Most harmful for this test sentence (removal decreases loss):
document label loo_effect
i really love the acting positive -0.009122
i hate this movie negative -0.064995
this movie is boring negative -0.165365Read the table by sign. Removing a review with positive estimated effect raises the positive test sentence's loss. Removing a review with negative estimated effect lowers it.
“This movie is good” and “this movie is boring” share three of their four tokens with the test sentence and have opposite estimated effects. The labels differ while the overlap score is identical.
Use the next figure to compare lexical overlap with estimated influence. It plots token-set Jaccard overlap against the same leave-one-out effects used above. It includes all ten training reviews, including ties.
VisualizationOut [10]Expand

Read 0.02 as an estimated change in loss under the local approximation, in the units of the loss function. In the comparison below, inspect ranking agreement and agreement in effect size separately.
The next comparison refits the classifier ten times, once for each removed review, and compares the numerical test-loss change with the damped influence estimate. Each direct refit keeps the original divisor of ten, vocabulary, L2 penalty, zero initialization, and optimizer settings. The refit values are subject to numerical convergence tolerance.
VisualizationOut [12]Expand

In this example the two methods agree on the sign for all ten reviews. Their Spearman rank correlation is about 0.988. The “this movie is good” review shows why rank agreement does not establish magnitude agreement. Its estimated removal effect is about 0.165 in test-loss units, compared with 0.373 after refitting.
Visualizing the ranking
The following figure ranks the training documents by their estimated leave-one-out effect. Positive bars are helpful for the test sentence; negative bars are harmful. The figure uses the model's precomputed influence values. It does not retrain the model.
Plotting the ranking rather than reading it from a table makes the target-specificity visible at a glance. The bars fan out from zero, and the ordering is a claim about which documents the model would miss if they were gone. Because the figure is generated from the same precomputed scores as the table, it does not add any new evidence by itself. Its value is in communication: a reader who will not parse a column of numbers will often read a bar chart correctly.
VisualizationOut [13]Expand

The chart ranks the reviews for the positive test sentence. The estimate for “i really hate the acting” is about +0.00027 loss units, which is barely visible at this scale. For a target-sensitivity check, use the negative review “this movie is boring,” one of the training rows. Recompute its target loss and gradient and reuse the fitted model and training Hessian. In this fixture, the two score rankings have a Spearman correlation of about −0.903.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!