-
Notifications
You must be signed in to change notification settings - Fork 52
finetuning wrapper #152
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
Draft
anuragg1209
wants to merge
25
commits into
main
Choose a base branch
from
finetuning_wrapper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
finetuning wrapper #152
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
c499a4d
finetuning wrapper
anuragg1209 ee4a990
Apply suggestion from @gemini-code-assist[bot]
daeb82c
Some fixes
anuragg1209 fb69741
fix the eval config and ensure consistency with context size used du…
anuragg1209 742781d
Refactor finetune_classifier.py to improve evaluation consistency and…
anuragg1209 552e127
replace print with logging
anuragg1209 e7c3838
ruff fix
anuragg1209 21b2826
Change meta_dataset_size to n_inference_context_samples in examples file
anuragg1209 b8a3f4c
ensure query labels are a subset of context labels
anuragg1209 877e65b
-Add gradient clipping and mixed precision training to FinetunedTabPF…
anuragg1209 c8c0141
Fix non-diversity of training samples during finetuning
anuragg1209 a5dd225
finetuning wrapper
anuragg1209 c8a380d
Apply suggestion from @gemini-code-assist[bot]
98cf50d
Some fixes
anuragg1209 2b7b61e
fix the eval config and ensure consistency with context size used du…
anuragg1209 ac8da19
Refactor finetune_classifier.py to improve evaluation consistency and…
anuragg1209 f8c6351
replace print with logging
anuragg1209 8f64da2
ruff fix
anuragg1209 1714364
Change meta_dataset_size to n_inference_context_samples in examples file
anuragg1209 45310d1
ensure query labels are a subset of context labels
anuragg1209 6d1957b
-Add gradient clipping and mixed precision training to FinetunedTabPF…
anuragg1209 871ce0e
Fix non-diversity of training samples during finetuning
anuragg1209 8a35b95
Ruff fix
anuragg1209 89eed89
Merge remote-tracking branch 'origin/finetuning_wrapper' into finetun…
anuragg1209 e1bdbc7
ruff fix
anuragg1209 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -146,3 +146,6 @@ sftp_freiburg.json | |
| *.py.new | ||
| commit_messages.txt | ||
| CLAUDE.md | ||
|
|
||
| # VSCode debugging | ||
| vscode_remote_debugging/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import numpy as np | ||
| import torch | ||
| from sklearn.datasets import fetch_covtype | ||
| from sklearn.metrics import log_loss, roc_auc_score | ||
| from sklearn.model_selection import train_test_split | ||
|
|
||
| from tabpfn import TabPFNClassifier | ||
| from tabpfn_extensions.finetune.finetune_classifier import FinetunedTabPFNClassifier | ||
|
|
||
| # 1. Load and prepare the data | ||
| # We use a small subset for a quick demonstration. | ||
| print("--- 1. Loading Data ---") | ||
| X_all, y_all = fetch_covtype(return_X_y=True, shuffle=True) | ||
| X, y = X_all[:50_000], y_all[:50_000] | ||
|
|
||
| # Create a final hold-out test set. This is NOT used during fine-tuning. | ||
| X_train, X_test, y_train, y_test = train_test_split( | ||
| X, | ||
| y, | ||
| test_size=0.2, | ||
| random_state=42, | ||
| stratify=y, | ||
| ) | ||
|
|
||
|
|
||
| # Calculate ROC AUC | ||
| def calculate_roc_auc(y_true: np.ndarray, y_pred_proba: np.ndarray) -> float: | ||
| if len(np.unique(y_true)) == 2: | ||
| return roc_auc_score(y_true, y_pred_proba[:, 1]) # pyright: ignore[reportReturnType] | ||
| return roc_auc_score(y_true, y_pred_proba, multi_class="ovr", average="weighted") # pyright: ignore[reportReturnType] | ||
|
|
||
|
|
||
| # 2. Initial model evaluation on test set | ||
|
|
||
| base_clf = TabPFNClassifier( | ||
| device="cuda" if torch.cuda.is_available() else "cpu", | ||
| n_estimators=8, | ||
| ignore_pretraining_limits=True, | ||
| ) | ||
| base_clf.fit(X_train, y_train) | ||
|
|
||
| base_pred_proba = base_clf.predict_proba(X_test) | ||
| roc_auc = calculate_roc_auc(y_test, base_pred_proba) # pyright: ignore[reportReturnType, reportArgumentType] | ||
| log_loss_score = log_loss(y_test, base_pred_proba) | ||
|
|
||
| print(f"📊 Initial Test ROC: {roc_auc:.4f}") | ||
| print(f"📊 Initial Test Log Loss: {log_loss_score:.4f}\n") | ||
|
|
||
| # 3. Initialize and run fine-tuning | ||
| print("--- 2. Initializing and Fitting Model ---\n") | ||
|
|
||
| # Instantiate the wrapper with your desired hyperparameters | ||
| finetuned_clf = FinetunedTabPFNClassifier( | ||
| device="cuda" if torch.cuda.is_available() else "cpu", | ||
| epochs=10, | ||
| learning_rate=1e-6, | ||
| n_inference_context_samples=10_000, | ||
| finetune_split_ratio=0.3, | ||
| random_state=42, | ||
| n_estimators=2, | ||
| patience=3, | ||
| ignore_pretraining_limits=True, | ||
| grad_clip_value=1.0, | ||
| ) | ||
|
|
||
| # 4. Call .fit() to start the fine-tuning process on the training data | ||
| finetuned_clf.fit(X_train, y_train) # pyright: ignore[reportArgumentType] | ||
| print("\n") | ||
|
|
||
| # 5. Evaluate the fine-tuned model | ||
| print("--- 3. Evaluating Model on Held-out Test Set ---\n") | ||
noahho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| y_pred_proba = finetuned_clf.predict_proba(X_test) # pyright: ignore[reportArgumentType] | ||
|
|
||
| roc_auc = calculate_roc_auc(y_test, y_pred_proba) # pyright: ignore[reportArgumentType] | ||
| loss = log_loss(y_test, y_pred_proba) | ||
|
|
||
| print(f"📊 Final Test ROC: {roc_auc:.4f}") | ||
| print(f"📊 Final Test Log Loss: {loss:.4f}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """Fine-tuning utilities for TabPFN models.""" | ||
|
|
||
| from .finetune_classifier import FinetunedTabPFNClassifier | ||
|
|
||
| __all__ = ["FinetunedTabPFNClassifier"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.