Skip to content

Instantly share code, notes, and snippets.

@macleginn
Last active October 6, 2023 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macleginn/5b9eabc1106e88e3ec767e2d00553aaa to your computer and use it in GitHub Desktop.
Save macleginn/5b9eabc1106e88e3ec767e2d00553aaa to your computer and use it in GitHub Desktop.
import pickle
import numpy as np
import pandas as pd
import torch
import matplotlib.pyplot as plt
import seaborn as sns
from tqdm.auto import tqdm
with open('sts_attributions/shelf_approx_attr_l-9_N-100.pkl', 'rb') as inp:
shelf_approx = pickle.load(inp)
with open('sts_attributions/tuned_approx_attr_l-9_N-100.pkl', 'rb') as inp:
tuned_approx = pickle.load(inp)
with open('sts_attributions/exact_attr_l-9_N-100.pkl', 'rb') as inp:
exact = pickle.load(inp)
fig, axs = plt.subplots(1, 3, figsize=(15,5))
for ax_i, (attr_set, label) in enumerate(zip(
[shelf_approx, tuned_approx, exact],
['ShelfApprox', 'TunedApprox', 'Exact']
)):
xs = np.zeros(attr_set.shape[0])
ys = np.zeros(attr_set.shape[0])
for i in tqdm(range(attr_set.shape[0])):
A = torch.tensor(attr_set.iloc[i].attributions)
xs[i] = torch.relu(A).sum().item()
ys[i] = -torch.relu(-A).sum().item()
df = pd.DataFrame({'SumPositive': xs, 'SumNegative': ys})
sns.regplot(data=df, x='SumPositive', y='SumNegative', lowess=True,
scatter_kws={'s': 1, 'alpha': 0.3},
line_kws={'linewidth': 1}, ax=axs[ax_i])
axs[ax_i].set_title(label)
plt.savefig('sumpos_vs_sumneg.pdf')
# plt.show()
fig, axs = plt.subplots(1, 3, figsize=(15,5))
for ax_i, (attr_set, label) in enumerate(zip(
[shelf_approx, tuned_approx, exact],
['ShelfApprox', 'TunedApprox', 'Exact']
)):
xs = np.zeros(attr_set.shape[0])
for i in tqdm(range(attr_set.shape[0])):
A = torch.tensor(attr_set.iloc[i].attributions)
xs[i] = torch.relu(A).sum().item()
axs[ax_i].hist(xs, bins=50)
axs[ax_i].set_title(label)
axs[ax_i].set_xlim(0, 6)
plt.savefig('sumpos_hist.pdf')
# plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment