Skip to content

Commit

Permalink
Add UCI tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
pomonam committed Mar 20, 2024
1 parent da1646c commit 30240bd
Show file tree
Hide file tree
Showing 14 changed files with 988 additions and 1,080 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
<a href="#"><img width="380" img src=".assets/kronfluence.svg" alt="Kronfluence"/></a>
</p>


<p align="center">
<a href="https://pypi.org/project/kronfluence">
<img alt="License" src="https://img.shields.io/pypi/v/kronfluence.svg?style=flat-square">
</a>
<a href="https://github.com/pomonam/kronfluence/blob/main/LICENSE">
<img alt="License" src="https://img.shields.io/badge/License-Apache_2.0-blue.svg">
</a>
Expand Down
32 changes: 32 additions & 0 deletions examples/cifar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# CIFAR-10 & ResNet-9 Example

This directory contains scripts designed for training ResNet-9 on CIFAR-10.

## Training

To train a regression model on the Concrete dataset, run the following command:
```bash
python train.py --dataset_name concrete \
--dataset_dir ./data \
--output_dir ./checkpoints \
--train_batch_size 32 \
--eval_batch_size 1024 \
--learning_rate 0.03 \
--weight_decay 1e-5 \
--num_train_epochs 20 \
--seed 1004
```

# Computing Pairwise Influence Scores

To obtain a pairwise influence scores using EKFAC, run the following command:
```bash
python analyze.py --dataset_name concrete \
--dataset_dir ./data \
--checkpoint_dir ./checkpoints \
--factor_strategy ekfac
```

# Counterfactual Evaluation

You can check the notebook `tutorial.ipynb` for running the counterfactual evaluation.
Empty file added examples/cifar/analyze.py
Empty file.
Empty file.
3 changes: 2 additions & 1 deletion examples/uci/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
scikit-learn
jupyter
jupyter
tueplots
2,007 changes: 939 additions & 1,068 deletions examples/uci/tutorial.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/factors/test_covariances.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

from kronfluence.analyzer import Analyzer, prepare_model
from kronfluence.arguments import FactorArguments
from kronfluence.task import Task
from kronfluence.utils.constants import (
ACTIVATION_COVARIANCE_MATRIX_NAME,
COVARIANCE_FACTOR_NAMES,
GRADIENT_COVARIANCE_MATRIX_NAME,
NUM_COVARIANCE_PROCESSED,
)
from kronfluence.task import Task
from kronfluence.utils.dataset import DataLoaderKwargs
from tests.utils import ATOL, RTOL, check_tensor_dict_equivalence, prepare_test

Expand Down
2 changes: 1 addition & 1 deletion tests/factors/test_eigens.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from kronfluence.analyzer import Analyzer, prepare_model
from kronfluence.arguments import FactorArguments
from kronfluence.task import Task
from kronfluence.utils.constants import (
ACTIVATION_EIGENVECTORS_NAME,
EIGENDECOMPOSITION_FACTOR_NAMES,
Expand All @@ -16,7 +17,6 @@
LAMBDA_MATRIX_NAME,
NUM_LAMBDA_PROCESSED,
)
from kronfluence.task import Task
from kronfluence.utils.dataset import DataLoaderKwargs
from tests.utils import ATOL, RTOL, check_tensor_dict_equivalence, prepare_test

Expand Down
2 changes: 1 addition & 1 deletion tests/scores/test_pairwise_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from kronfluence.analyzer import Analyzer, prepare_model
from kronfluence.arguments import FactorArguments, ScoreArguments
from kronfluence.utils.constants import ALL_MODULE_NAME
from kronfluence.task import Task
from kronfluence.utils.constants import ALL_MODULE_NAME
from kronfluence.utils.dataset import DataLoaderKwargs
from tests.utils import ATOL, RTOL, check_tensor_dict_equivalence, prepare_test

Expand Down
2 changes: 1 addition & 1 deletion tests/scores/test_self_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from kronfluence.analyzer import Analyzer, prepare_model
from kronfluence.arguments import FactorArguments, ScoreArguments
from kronfluence.utils.constants import ALL_MODULE_NAME
from kronfluence.task import Task
from kronfluence.utils.constants import ALL_MODULE_NAME
from kronfluence.utils.dataset import DataLoaderKwargs
from tests.utils import ATOL, RTOL, check_tensor_dict_equivalence, prepare_test

Expand Down
5 changes: 1 addition & 4 deletions tests/test_per_sample_gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@

from kronfluence.analyzer import Analyzer, prepare_model
from kronfluence.arguments import FactorArguments
from kronfluence.utils.constants import (
LAMBDA_MATRIX_NAME,
PRECONDITIONED_GRADIENT_NAME,
)
from kronfluence.module.tracked_module import ModuleMode, TrackedModule
from kronfluence.module.utils import set_mode, update_factor_args
from kronfluence.task import Task
from kronfluence.utils.constants import LAMBDA_MATRIX_NAME, PRECONDITIONED_GRADIENT_NAME
from kronfluence.utils.dataset import DataLoaderKwargs
from tests.utils import (
ATOL,
Expand Down
3 changes: 2 additions & 1 deletion tests/testable_tasks/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import torch.nn.functional as F
import torchvision
from accelerate.utils import set_seed
from kronfluence.task import Task
from torch import nn
from torch.utils import data

from kronfluence.task import Task

BATCH_TYPE = Tuple[torch.Tensor, torch.Tensor]


Expand Down
3 changes: 2 additions & 1 deletion tests/testable_tasks/language_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import torch
import torch.nn.functional as F
from datasets import load_dataset
from kronfluence.task import Task
from torch import nn
from torch.utils import data
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, Conv1D

from kronfluence.task import Task

BATCH_TYPE = Dict[str, torch.Tensor]


Expand Down
3 changes: 2 additions & 1 deletion tests/testable_tasks/text_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import torch
import torch.nn.functional as F
from datasets import load_dataset
from kronfluence.task import Task
from torch import nn
from torch.utils import data
from transformers import AutoConfig, AutoModelForSequenceClassification, AutoTokenizer

from kronfluence.task import Task

BATCH_TYPE = Dict[str, torch.Tensor]


Expand Down

0 comments on commit 30240bd

Please sign in to comment.