Skip to content

feat: float pca dims #163

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

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 6 additions & 3 deletions model2vec/distill/distillation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
logger = logging.getLogger(__name__)


PCADimType = Union[int, None, Literal["auto"]]
PCADimType = Union[int, None, float, Literal["auto"]]


def distill_from_model(
Expand Down Expand Up @@ -258,10 +258,13 @@ def _post_process_embeddings(embeddings: np.ndarray, pca_dims: PCADimType, apply
f"PCA dimension ({pca_dims}) is larger than the number of tokens in the vocabulary ({embeddings.shape[0]}). Not applying PCA."
)
elif pca_dims <= embeddings.shape[1]:
logger.info(f"Applying PCA with n_components {pca_dims}")
if isinstance(pca_dims, float):
logger.info(f"Applying PCA with {pca_dims} explained variance.")
else:
logger.info(f"Applying PCA with n_components {pca_dims}")

orig_dims = embeddings.shape[1]
p = PCA(n_components=pca_dims, whiten=False)
p = PCA(n_components=pca_dims, svd_solver="full")
embeddings = p.fit_transform(embeddings)

if embeddings.shape[1] < orig_dims:
Expand Down
1 change: 1 addition & 0 deletions tests/test_distillation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
(None, True, "auto", False), # Subword, PCA set to 'auto'
(None, True, 1024, False), # Subword, PCA set to high number.
(None, True, None, True), # No PCA applied
(None, True, 0.9, True), # PCA as float applied
(["wordA", "wordB"], False, 4, True), # Custom vocab without subwords PCA and Zipf applied
(None, False, 256, True), # use_subword = False without passing a vocabulary should raise an error
],
Expand Down
Loading