Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -85,61 +85,77 @@ def sequence_to_kmer_vector(sequence: str, k: int = 4) -> np.ndarray:
|
|
| 85 |
# 3. SHAP-VALUE (ABLATION) CALCULATION
|
| 86 |
###############################################################################
|
| 87 |
|
| 88 |
-
def calculate_shap_values(model, x_tensor,
|
| 89 |
"""
|
| 90 |
-
Calculate feature attributions using Integrated Gradients.
|
| 91 |
|
| 92 |
Args:
|
| 93 |
model: A PyTorch model.
|
| 94 |
x_tensor: Input tensor of shape (1, num_features).
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
steps: Number of steps
|
|
|
|
| 98 |
|
| 99 |
Returns:
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
"""
|
| 104 |
model.eval()
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
|
| 145 |
|
|
|
|
| 85 |
# 3. SHAP-VALUE (ABLATION) CALCULATION
|
| 86 |
###############################################################################
|
| 87 |
|
| 88 |
+
def calculate_shap_values(model, x_tensor, baselines=None, steps=100, n_baselines=5):
|
| 89 |
"""
|
| 90 |
+
Calculate feature attributions using Integrated Gradients with multiple baselines.
|
| 91 |
|
| 92 |
Args:
|
| 93 |
model: A PyTorch model.
|
| 94 |
x_tensor: Input tensor of shape (1, num_features).
|
| 95 |
+
baselines: A list of baseline tensors, each of shape (1, num_features).
|
| 96 |
+
If None, defaults to n_baselines copies of the zero vector.
|
| 97 |
+
steps: Number of interpolation steps between the baseline and the input.
|
| 98 |
+
n_baselines: Number of baselines to use if baselines is None.
|
| 99 |
|
| 100 |
Returns:
|
| 101 |
+
avg_attributions: A numpy array of shape (num_features,) with averaged feature attributions.
|
| 102 |
+
avg_full_prob: The model's predicted probability for the target class ('human')
|
| 103 |
+
computed on the full input, averaged over baselines.
|
| 104 |
"""
|
| 105 |
model.eval()
|
| 106 |
+
|
| 107 |
+
# If no baselines are provided, generate a list of zero-vectors.
|
| 108 |
+
if baselines is None:
|
| 109 |
+
baselines = [torch.zeros_like(x_tensor) for _ in range(n_baselines)]
|
| 110 |
+
elif not isinstance(baselines, list):
|
| 111 |
+
baselines = [baselines]
|
| 112 |
+
|
| 113 |
+
all_attributions = []
|
| 114 |
+
full_probs = []
|
| 115 |
+
|
| 116 |
+
# For each baseline, compute integrated gradients.
|
| 117 |
+
for baseline in baselines:
|
| 118 |
+
# Compute the model's full prediction using the actual input.
|
| 119 |
+
with torch.no_grad():
|
| 120 |
+
full_output = model(x_tensor)
|
| 121 |
+
full_prob = torch.softmax(full_output, dim=1)[0, 1].item()
|
| 122 |
+
full_probs.append(full_prob)
|
| 123 |
+
|
| 124 |
+
# Create interpolated inputs from baseline to x_tensor.
|
| 125 |
+
scaled_inputs = [
|
| 126 |
+
baseline + (float(i) / steps) * (x_tensor - baseline)
|
| 127 |
+
for i in range(steps + 1)
|
| 128 |
+
]
|
| 129 |
+
scaled_inputs = torch.cat(scaled_inputs, dim=0) # Shape: (steps+1, num_features)
|
| 130 |
+
scaled_inputs.requires_grad = True
|
| 131 |
+
|
| 132 |
+
# Forward pass: compute outputs and target class probabilities for all interpolated inputs.
|
| 133 |
+
outputs = model(scaled_inputs)
|
| 134 |
+
probs = torch.softmax(outputs, dim=1)[:, 1] # Probabilities for 'human' class
|
| 135 |
+
|
| 136 |
+
# Backward pass: compute gradients of the probabilities with respect to inputs.
|
| 137 |
+
grads = torch.autograd.grad(
|
| 138 |
+
outputs=probs,
|
| 139 |
+
inputs=scaled_inputs,
|
| 140 |
+
grad_outputs=torch.ones_like(probs),
|
| 141 |
+
create_graph=False,
|
| 142 |
+
retain_graph=False
|
| 143 |
+
)[0] # Shape: (steps+1, num_features)
|
| 144 |
+
|
| 145 |
+
# Approximate the integral using the trapezoidal rule.
|
| 146 |
+
avg_grads = (grads[:-1] + grads[1:]) / 2.0 # Average gradients between successive steps.
|
| 147 |
+
integrated_grad = avg_grads.mean(dim=0, keepdim=True) # Mean over all steps.
|
| 148 |
+
|
| 149 |
+
# Multiply by the input difference to get attributions.
|
| 150 |
+
attributions = (x_tensor - baseline) * integrated_grad # Shape: (1, num_features)
|
| 151 |
+
all_attributions.append(attributions)
|
| 152 |
+
|
| 153 |
+
# Average attributions over all baselines.
|
| 154 |
+
avg_attributions = torch.stack(all_attributions, dim=0).mean(dim=0)
|
| 155 |
+
avg_full_prob = np.mean(full_probs)
|
| 156 |
+
|
| 157 |
+
return avg_attributions.squeeze().cpu().detach().numpy(), avg_full_prob
|
| 158 |
+
|
| 159 |
|
| 160 |
|
| 161 |
|