Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. from egexplainer import AttributionPriorExplainer
  2.  
  3. ## in addition to whatever you normally do,
  4. ## you also need to make explainer object
  5. background_dataset = ExVivoDrugData(X_train,y_train) # another dataset class containing your training data
  6. APExp = AttributionPriorExplainer(background_dataset, 64,k=2)
  7.  
  8. ## using explainer in example training loop
  9. for i, (features, labels) in tqdm(enumerate(train_loader)):
  10.     features, labels = features.cuda().float(), labels.cuda().float()
  11.     optimizer.zero_grad()
  12.  
  13.     outputs = model(features).view(-1)
  14.    
  15.     # get the expected gradients attributions for your batch    
  16.     eg = APExp.shap_values(model,features)
  17.     ma_eg = eg.abs().mean(0) # do some arbitrary function of your attributions
  18.                              # here i was doing a graph penalty on the average magnitude explanations
  19.     graph_term = ma_eg.unsqueeze(1)[4862:,:].t().matmul(dense_adj.matmul(ma_eg.unsqueeze(1)[4862:,:]))
  20.     loss = mse_term(outputs, labels) + 10.*graph_term # add to your loss term
  21.  
  22.  
  23.     loss.backward(retain_graph=True)
  24.     optimizer.step()
  25.     train_losses.append(loss.item())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement