{"id":49557582,"url":"https://github.com/ashmod/learning-not-to-learn","last_synced_at":"2026-05-03T05:53:23.231Z","repository":{"id":293875346,"uuid":"984944394","full_name":"ashmod/learning-not-to-learn","owner":"ashmod","description":"PyTorch implementation of the paper \"Learning Not to Learn,\" an adversarial approach to mitigate dataset bias in deep neural networks.","archived":false,"fork":false,"pushed_at":"2025-05-19T14:18:36.000Z","size":973,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-03T05:53:00.177Z","etag":null,"topics":["algorithms","analysis","data-science","deep-learning","machine-learning","research"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ashmod.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-16T19:27:10.000Z","updated_at":"2025-05-19T14:18:40.000Z","dependencies_parsed_at":"2025-06-23T11:38:21.048Z","dependency_job_id":"88518eb7-a1d0-49d9-b171-adb4f7a7b2e7","html_url":"https://github.com/ashmod/learning-not-to-learn","commit_stats":null,"previous_names":["dizzydroid/learning-not-to-learn","ashmod/learning-not-to-learn"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ashmod/learning-not-to-learn","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashmod%2Flearning-not-to-learn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashmod%2Flearning-not-to-learn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashmod%2Flearning-not-to-learn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashmod%2Flearning-not-to-learn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ashmod","download_url":"https://codeload.github.com/ashmod/learning-not-to-learn/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashmod%2Flearning-not-to-learn/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32559716,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-03T03:21:47.309Z","status":"ssl_error","status_checked_at":"2026-05-03T03:21:43.884Z","response_time":103,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithms","analysis","data-science","deep-learning","machine-learning","research"],"created_at":"2026-05-03T05:53:22.778Z","updated_at":"2026-05-03T05:53:23.226Z","avatar_url":"https://github.com/ashmod.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Learning Not To Learn: A User-Friendly Implementation\n\nThis repository provides a user-friendly PyTorch implementation of the paper \"[Learning Not to Learn: Training Deep Neural Networks with Biased Data](https://arxiv.org/abs/1812.10352)\" by Kim et al. (CVPR 2019). The goal of this project is to replicate the paper's adversarial approach to mitigate dataset bias in a way that is more accessible, configurable, and easier to understand and extend.\n\nThe core idea is to train a feature extractor that \"unlearns\" bias information by making it difficult for a separate bias prediction network to identify the bias from the extracted features, while still allowing a main task classifier to perform well.\n\n## Features\n\n* **Modular Codebase:** Clear separation of concerns for data loading, model definitions, training logic, and utilities.\n* **Configuration-Driven:** Experiments are managed via YAML configuration files, making it easy to define and track different setups.\n* **Automated Workflow:** A `Makefile` is provided for common tasks like setup, training, evaluation, and cleaning.\n* **User-Friendly Setup:** Includes scripts for dataset download and clear instructions.\n* **Phased Training:** Supports optional pre-training phases for components.\n* **Comprehensive Logging:** Console logging and TensorBoard integration for tracking metrics.\n* **Robust Checkpointing:** Saves and loads model checkpoints, including optimizer states, for resuming training and evaluation. Includes \"best model\" saving.\n* **Gradient Reversal Layer (GRL):** Implements GRL for the adversarial training component.\n* **Jupyter Notebooks:** For data exploration, model sanity checks, and (template for) results analysis.\n\n## Project Structure\n\n```\n\nlearning-not-to-learn/\n├── .gitignore               # Git ignore file\n├── Makefile                 # Automation for common tasks\n├── README.md                # This file\n├── requirements.txt         # Python package dependencies\n├── environment.yml          # Optional: For Conda environments\n│\n├── configs/                 # Configuration files for experiments\n│   └── colored_mnist_default.yaml # Example config\n│\n├── data/                    # Default location for datasets\n│   └── colored_mnist/       # Data for Colored MNIST (after download script)\n│   └── README.md            # Brief note about dataset storage\n│\n├── notebooks/               # Jupyter notebooks\n│   ├── 01_data_exploration_and_visualization.ipynb\n│   ├── 02_model_sanity_checks.ipynb\n│   └── 03_results_analysis.ipynb (template)\n│\n├── results/                 # Output directory (logs, checkpoints, plots) - in .gitignore\n│\n├── scripts/                 # Utility scripts\n│   └── download_dataset.sh  # Script to download and extract Colored MNIST\n│\n└── src/                     # Main source code\n├── __init__.py\n├── data_loader.py       # Dataset and DataLoader classes\n├── main.py              # Main script for training and evaluation\n├── models.py            # Neural network architecture definitions (f, g, h, GRL)\n├── trainer.py           # Trainer class with training/evaluation logic\n└── utils.py             # Utility functions (config loading, seeding, logging)\n\n````\n\n## Getting Started\n\n### 1. Clone the Repository\n\n```bash\ngit clone https://github.com/dizzydroid/learning-not-to-learn\ncd learning-not-to-learn\n````\n\n### 2\\. Create a Virtual Environment (Recommended)\n\n**Using `venv`:**\n\n```bash\npython -m venv venv\nsource venv/bin/activate  # On Windows: venv\\Scripts\\activate\n```\n\n**Using Conda:**\n\n```bash\nconda create -n lntl_env python=3.9  # Or your preferred Python version\nconda activate lntl_env\n```\n\n### 3\\. Setup Project using Makefile\n\nThe easiest way to install dependencies and download the dataset is by using the `Makefile`:\n\n```bash\nmake setup\n```\n\nThis command will:\n\n1.  Install Python dependencies from `requirements.txt` (including `gdown` for dataset download).\n2.  Download and extract the Colored MNIST dataset to `./data/colored_mnist/` using `scripts/download_dataset.sh`.\n\nAlternatively, you can run these steps manually:\n\n  * Install dependencies: `make install` or `pip install -r requirements.txt \u0026\u0026 pip install gdown`\n  * Download data: `make download_data` or `bash scripts/download_dataset.sh`\n\n## Configuration\n\nExperiments are controlled by YAML configuration files located in the `configs/` directory. `configs/colored_mnist_default.yaml` is provided as a template. Modify this file or create new ones to define your experimental setup (dataset paths, model parameters, training hyperparameters, etc.).\n\nYou can list available configurations using:\n\n```bash\nmake list_configs\n```\n\n## Running Experiments with Makefile\n\nThe `Makefile` provides convenient shortcuts for common operations. Run `make help` to see all available targets.\n\n### Training\n\n  * **Run a full training session** (uses `DEFAULT_TRAIN_EPOCHS` from Makefile, e.g., 50 epochs, and `configs/colored_mnist_default.yaml`):\n    ```bash\n    make train\n    ```\n  * **Run a quick 1-epoch test training:**\n    ```bash\n    make quick_train\n    ```\n  * **Customize training:**\n    ```bash\n    make train EPOCHS=100 CONFIG=configs/your_custom_config.yaml DEVICE=cuda:0\n    ```\n\nOutputs (logs, TensorBoard files, checkpoints) will be saved to the directory specified in the `logging.output_dir` and `project.experiment_name` fields of your config (e.g., `results/colored_mnist_baseline/`).\n\n### Resuming Training\n\nTo resume training from a checkpoint, you'll need to use the `python src/main.py` command directly, specifying the checkpoint path:\n\n```bash\npython src/main.py --config configs/your_config.yaml --mode train --checkpoint_path path/to/your/checkpoint.pth\n```\n\n### Evaluation\n\n  * **Evaluate the best model** from the default experiment run:\n    ```bash\n    make eval\n    ```\n  * **Evaluate a specific checkpoint:**\n    ```bash\n    make eval CHECKPOINT=./results/colored_mnist_baseline/checkpoints/checkpoint_epoch_XX.pth\n    ```\n\n### Monitoring with TensorBoard\n\n  * Launch TensorBoard for the default experiment:\n    ```bash\n    make tensorboard\n    ```\n    Then open `http://localhost:6006` in your browser.\n\n## Jupyter Notebooks\n\nThe `notebooks/` directory contains:\n\n  * **`01_data_exploration_and_visualization.ipynb`**: Load and visualize the Colored MNIST dataset to understand its properties and biases.\n  * **`02_model_sanity_checks.ipynb`**: Instantiate and test individual model components ($f$, g, $h$) with dummy data to verify shapes and forward passes.\n  * **`03_results_analysis.ipynb`**: A template for loading metrics from TensorBoard, plotting learning curves, and performing other analyses on completed training runs.\n\nStart Jupyter Lab/Notebook using:\n\n```bash\nmake notebook\n```\n\n## Core Logic (\"Learning Not To Learn\")\n\n1.  **Feature Extractor ($f$)**: A network (e.g., CNN) that learns a representation of the input data.\n2.  **Task Classifier ($g$)**: A network (e.g., MLP) that takes features from $f$ and predicts the main task label.\n3.  **Bias Predictor ($h$)**: A network that takes features from $f$ and tries to predict a known bias in the data.\n4.  **Adversarial Training**:\n      * $f$ and $g$ are trained to minimize the main task classification loss.\n      * $h$ is trained to minimize its bias prediction loss.\n      * $f$ is *also* trained to *maximize* $h$'s bias prediction loss (typically via a Gradient Reversal Layer - GRL).\n      * The `training.adversarial_lambda` in the config controls the strength of this adversarial component.\n\nThe goal is for $f$ to learn features that are discriminative for the main task but contain minimal information about the unwanted bias.\n\n## Citation\n\n```bibtex\n@inproceedings{kim2019learning,\n  title={Learning not to learn: Training deep neural networks with biased data},\n  author={Kim, Byungju and Kim, Hyunwoo and Kim, Kyungsu and Kim, Sungjin and Kim, Junmo},\n  booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},\n  pages={9012--9020},\n  year={2019}\n}\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [`LICENSE`](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashmod%2Flearning-not-to-learn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fashmod%2Flearning-not-to-learn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashmod%2Flearning-not-to-learn/lists"}