Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiP: Globalization from local explainer #24

Merged
merged 19 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/metrics/localization/identical_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ def __init__(
super().__init__(model, train_dataset, device, *args, **kwargs)
self.scores = []

def update(
self,
test_labels: torch.Tensor,
explanations: torch.Tensor
):
def update(self, test_labels: torch.Tensor, explanations: torch.Tensor):
"""
Used to implement metric-specific logic.
"""
Expand Down
4 changes: 1 addition & 3 deletions src/metrics/randomization/model_randomization.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ def update(
corrs = self.correlation_measure(explanations, rand_explanations)
self.results["rank_correlations"].append(corrs)

def compute(
self,
):
def compute(self):
return torch.cat(self.results["rank_correlations"]).mean()

def reset(self):
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/unnamed/top_k_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def compute(self, *args, **kwargs):
return len(torch.unique(self.all_top_k_examples))

def reset(self, *args, **kwargs):
self.all_top_k_examples = []
self.all_top_k_examples = torch.empty(0, self.top_k)

def load_state_dict(self, state_dict: dict, *args, **kwargs):
self.all_top_k_examples = state_dict["all_top_k_examples"]
Expand Down
24 changes: 24 additions & 0 deletions src/utils/aggregators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from abc import ABC
gumityolcu marked this conversation as resolved.
Show resolved Hide resolved

import torch


class ExplanationsAggregator(ABC):
def __init__(self, training_size: int, *args, **kwargs):
self.scores = torch.zeros(training_size)

def update(self, explanations: torch.Tensor):
raise NotImplementedError

def get_global_ranking(self) -> torch.Tensor:
gumityolcu marked this conversation as resolved.
Show resolved Hide resolved
return self.scores.argsort()


class SumAggregator(ExplanationsAggregator):
def update(self, explanations: torch.Tensor) -> torch.Tensor:
self.scores += explanations.sum(dim=0)


class AbsSumAggregator(ExplanationsAggregator):
def update(self, explanations: torch.Tensor) -> torch.Tensor:
self.scores += explanations.abs().sum(dim=0)
21 changes: 20 additions & 1 deletion src/utils/common.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import functools
from functools import reduce
from typing import Any, Callable, Mapping
from typing import Any, Callable, Mapping, Optional

import torch
import torch.utils
import torch.utils.data

from utils.explain_wrapper import SelfInfluenceFunction


def _get_module_from_name(model: torch.nn.Module, layer_name: str) -> Any:
Expand All @@ -22,3 +26,18 @@ def make_func(func: Callable, func_kwargs: Mapping[str, ...] | None, **kwargs) -
func_kwargs = kwargs

return functools.partial(func, **func_kwargs)


def get_self_influence_ranking(
gumityolcu marked this conversation as resolved.
Show resolved Hide resolved
model: torch.nn.Module,
model_id: str,
cache_dir: Optional[str],
training_data: torch.utils.data.Dataset,
self_influence_fn: SelfInfluenceFunction,
self_influence_fn_kwargs: Optional[dict] = None,
) -> torch.Tensor:
size = len(training_data)
self_inf = torch.zeros((size,))
for i, (x, y) in enumerate(training_data):
self_inf[i] = self_influence_fn(model, model_id, cache_dir, training_data, i, **self_influence_fn_kwargs)
gumityolcu marked this conversation as resolved.
Show resolved Hide resolved
return self_inf.argsort()
14 changes: 13 additions & 1 deletion src/utils/explain_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ def __call__(
test_tensor: torch.Tensor,
method: str,
) -> torch.Tensor:
...
pass


class SelfInfluenceFunction(Protocol):
gumityolcu marked this conversation as resolved.
Show resolved Hide resolved
def __call__(
self,
model: torch.nn.Module,
model_id: str,
cache_dir: Optional[str],
train_dataset: torch.utils.data.Dataset,
id: int,
) -> torch.Tensor:
pass


def explain(
gumityolcu marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading