Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -88,59 +88,59 @@ def sequence_to_kmer_vector(sequence: str, k: int = 4) -> np.ndarray:
|
|
88 |
def calculate_shap_values(model, x_tensor, baseline=None, steps=50):
|
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 |
baseline: Tensor of the same shape as x_tensor to use as the reference.
|
96 |
If None, defaults to a tensor of zeros.
|
97 |
steps: Number of steps in the Riemann approximation of the integral.
|
98 |
-
|
99 |
Returns:
|
100 |
attributions: A numpy array of shape (num_features,) with feature attributions.
|
101 |
-
|
102 |
-
|
103 |
"""
|
104 |
model.eval()
|
105 |
if baseline is None:
|
106 |
baseline = torch.zeros_like(x_tensor)
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
# Generate interpolated inputs between the baseline and the actual input.
|
109 |
scaled_inputs = [
|
110 |
baseline + (float(i) / steps) * (x_tensor - baseline)
|
111 |
for i in range(steps + 1)
|
112 |
]
|
113 |
-
scaled_inputs = torch.cat(scaled_inputs, dim=0) #
|
114 |
scaled_inputs.requires_grad = True
|
115 |
|
116 |
# Forward pass: compute model outputs for all interpolated inputs.
|
117 |
-
outputs = model(scaled_inputs) #
|
118 |
-
probs = torch.softmax(outputs, dim=1)[:, 1] #
|
119 |
|
120 |
-
# Backward pass: compute gradients of the probability with respect to
|
121 |
grads = torch.autograd.grad(
|
122 |
outputs=probs,
|
123 |
inputs=scaled_inputs,
|
124 |
grad_outputs=torch.ones_like(probs),
|
125 |
create_graph=False,
|
126 |
retain_graph=False
|
127 |
-
)[0] #
|
128 |
|
129 |
# Approximate the integral using the trapezoidal rule.
|
130 |
-
|
131 |
-
|
132 |
-
# Average the gradients over all steps.
|
133 |
-
integrated_grad = avg_grads.mean(dim=0, keepdim=True) # shape: (1, num_features)
|
134 |
|
135 |
-
# Scale the integrated gradients by the difference between the input and
|
136 |
-
attributions = (x_tensor - baseline) * integrated_grad
|
137 |
|
138 |
-
|
139 |
-
with torch.no_grad():
|
140 |
-
baseline_output = model(baseline)
|
141 |
-
baseline_prob = torch.softmax(baseline_output, dim=1)[0, 1].item()
|
142 |
|
143 |
-
return attributions.squeeze().cpu().numpy(), baseline_prob
|
144 |
|
145 |
|
146 |
###############################################################################
|
|
|
88 |
def calculate_shap_values(model, x_tensor, baseline=None, steps=50):
|
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 |
baseline: Tensor of the same shape as x_tensor to use as the reference.
|
96 |
If None, defaults to a tensor of zeros.
|
97 |
steps: Number of steps in the Riemann approximation of the integral.
|
98 |
+
|
99 |
Returns:
|
100 |
attributions: A numpy array of shape (num_features,) with feature attributions.
|
101 |
+
full_prob: The model's predicted probability for the target class (human)
|
102 |
+
when using the actual input.
|
103 |
"""
|
104 |
model.eval()
|
105 |
if baseline is None:
|
106 |
baseline = torch.zeros_like(x_tensor)
|
107 |
+
|
108 |
+
# Compute the model's prediction for the full input.
|
109 |
+
with torch.no_grad():
|
110 |
+
full_output = model(x_tensor)
|
111 |
+
full_probs = torch.softmax(full_output, dim=1)
|
112 |
+
full_prob = full_probs[0, 1].item() # Probability for 'human'
|
113 |
+
|
114 |
# Generate interpolated inputs between the baseline and the actual input.
|
115 |
scaled_inputs = [
|
116 |
baseline + (float(i) / steps) * (x_tensor - baseline)
|
117 |
for i in range(steps + 1)
|
118 |
]
|
119 |
+
scaled_inputs = torch.cat(scaled_inputs, dim=0) # Shape: (steps+1, num_features)
|
120 |
scaled_inputs.requires_grad = True
|
121 |
|
122 |
# Forward pass: compute model outputs for all interpolated inputs.
|
123 |
+
outputs = model(scaled_inputs) # Shape: (steps+1, num_classes)
|
124 |
+
probs = torch.softmax(outputs, dim=1)[:, 1] # Probability for 'human'
|
125 |
|
126 |
+
# Backward pass: compute gradients of the probability with respect to inputs.
|
127 |
grads = torch.autograd.grad(
|
128 |
outputs=probs,
|
129 |
inputs=scaled_inputs,
|
130 |
grad_outputs=torch.ones_like(probs),
|
131 |
create_graph=False,
|
132 |
retain_graph=False
|
133 |
+
)[0] # Shape: (steps+1, num_features)
|
134 |
|
135 |
# Approximate the integral using the trapezoidal rule.
|
136 |
+
avg_grads = (grads[:-1] + grads[1:]) / 2.0 # Average gradient between steps.
|
137 |
+
integrated_grad = avg_grads.mean(dim=0, keepdim=True) # Mean over all steps.
|
|
|
|
|
138 |
|
139 |
+
# Scale the integrated gradients by the difference between the input and baseline.
|
140 |
+
attributions = (x_tensor - baseline) * integrated_grad
|
141 |
|
142 |
+
return attributions.squeeze().cpu().numpy(), full_prob
|
|
|
|
|
|
|
143 |
|
|
|
144 |
|
145 |
|
146 |
###############################################################################
|