hiyata commited on
Commit
7394998
·
verified ·
1 Parent(s): 345980e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -89,41 +89,41 @@ def calculate_shap_values(model, x_tensor):
89
  model.eval()
90
  device = next(model.parameters()).device
91
 
92
- # Create background dataset (baseline)
93
  background = np.zeros((300, x_tensor.shape[1]))
94
 
95
  try:
96
  # Try using DeepExplainer (efficient for neural networks)
97
  explainer = shap.DeepExplainer(model, background)
98
-
99
- # Calculate SHAP values
100
  shap_values_all = explainer.shap_values(x_tensor)
101
-
102
  # Get SHAP values for human class (index 1)
103
  shap_values = shap_values_all[1][0]
104
-
105
  except Exception as e:
106
  print(f"DeepExplainer failed, falling back to KernelExplainer: {str(e)}")
107
 
108
- # Create model wrapper function
109
  def model_predict(x):
 
 
 
 
 
110
  with torch.no_grad():
111
- tensor_x = torch.FloatTensor(x).to(device)
112
  output = model(tensor_x)
113
  probs = torch.softmax(output, dim=1)[:, 1] # Human probability
114
  return probs.cpu().numpy()
115
 
116
- # Create baseline distribution
117
- background = np.zeros((1, x_tensor.shape[1]))
118
 
119
  # Use KernelExplainer as fallback
120
  explainer = shap.KernelExplainer(model_predict, background)
121
-
122
- # Calculate SHAP values
123
  x_numpy = x_tensor.cpu().numpy()
124
  shap_values = explainer.shap_values(x_numpy, nsamples=100)
125
 
126
- # Get human probability
127
  with torch.no_grad():
128
  output = model(x_tensor)
129
  probs = torch.softmax(output, dim=1)
 
89
  model.eval()
90
  device = next(model.parameters()).device
91
 
92
+ # Create a background dataset (baseline) with a sufficient number of samples
93
  background = np.zeros((300, x_tensor.shape[1]))
94
 
95
  try:
96
  # Try using DeepExplainer (efficient for neural networks)
97
  explainer = shap.DeepExplainer(model, background)
98
+ # Calculate SHAP values using DeepExplainer
 
99
  shap_values_all = explainer.shap_values(x_tensor)
 
100
  # Get SHAP values for human class (index 1)
101
  shap_values = shap_values_all[1][0]
 
102
  except Exception as e:
103
  print(f"DeepExplainer failed, falling back to KernelExplainer: {str(e)}")
104
 
105
+ # Define a wrapper function to ensure proper input shape
106
  def model_predict(x):
107
+ # Ensure x is a numpy array and has at least 2 dimensions
108
+ if not isinstance(x, np.ndarray):
109
+ x = np.array(x)
110
+ if x.ndim == 1:
111
+ x = np.expand_dims(x, axis=0)
112
  with torch.no_grad():
113
+ tensor_x = torch.tensor(x, dtype=torch.float, device=device)
114
  output = model(tensor_x)
115
  probs = torch.softmax(output, dim=1)[:, 1] # Human probability
116
  return probs.cpu().numpy()
117
 
118
+ # Re-create a larger background for KernelExplainer if needed
119
+ background = np.zeros((300, x_tensor.shape[1]))
120
 
121
  # Use KernelExplainer as fallback
122
  explainer = shap.KernelExplainer(model_predict, background)
 
 
123
  x_numpy = x_tensor.cpu().numpy()
124
  shap_values = explainer.shap_values(x_numpy, nsamples=100)
125
 
126
+ # Get human probability from model prediction
127
  with torch.no_grad():
128
  output = model(x_tensor)
129
  probs = torch.softmax(output, dim=1)