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

Adds rule of thumb for determining embeddings size #2050

Merged
merged 1 commit into from
May 23, 2022
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
2 changes: 1 addition & 1 deletion ludwig/encoders/category_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CategoricalEmbedEncoder(Encoder):
def __init__(
self,
vocab: List[str],
embedding_size: int = 50,
embedding_size: Optional[int] = None,
embeddings_trainable: bool = True,
pretrained_embeddings: Optional[str] = None,
embeddings_on_cpu: bool = False,
Expand Down
18 changes: 12 additions & 6 deletions ludwig/modules/embedding_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

def embedding_matrix(
vocab: List[str],
embedding_size: int,
embedding_size: Optional[int] = None,
representation: str = "dense",
embeddings_trainable: bool = True,
pretrained_embeddings: Optional[str] = None,
Expand All @@ -43,7 +43,10 @@ def embedding_matrix(
if representation == "dense":
if pretrained_embeddings:
embeddings_matrix = load_pretrained_embeddings(pretrained_embeddings, vocab)
if embeddings_matrix.shape[-1] != embedding_size:
if embedding_size is None:
embedding_size = embeddings_matrix.shape[-1]
logger.info(f"Setting embedding size to be equal to {embeddings_matrix.shape[-1]}.")
elif embeddings_matrix.shape[-1] != embedding_size:
if not force_embedding_size:
embedding_size = embeddings_matrix.shape[-1]
logger.info(f"Setting embedding size to be equal to {embeddings_matrix.shape[-1]}.")
Expand All @@ -57,7 +60,10 @@ def embedding_matrix(
embedding_initializer_obj = torch.tensor(embeddings_matrix, dtype=torch.float32)

else:
if vocab_size < embedding_size and not force_embedding_size:
if embedding_size is None:
# use embedding size rule of thumb
embedding_size = min(512, int(round(1.6 * vocab_size**0.56)))
elif vocab_size < embedding_size and not force_embedding_size:
logger.info(
f" embedding_size ({embedding_size}) is greater than "
f"vocab_size ({vocab_size}). Setting embedding size to be "
Expand Down Expand Up @@ -86,7 +92,7 @@ def embedding_matrix(

def embedding_matrix_on_device(
vocab: List[str],
embedding_size: int,
embedding_size: Optional[int] = None,
representation: str = "dense",
embeddings_trainable: bool = True,
pretrained_embeddings: Optional[str] = None,
Expand All @@ -96,7 +102,7 @@ def embedding_matrix_on_device(
) -> Tuple[nn.Module, int]:
embeddings, embedding_size = embedding_matrix(
vocab,
embedding_size,
embedding_size=embedding_size,
representation=representation,
embeddings_trainable=embeddings_trainable,
pretrained_embeddings=pretrained_embeddings,
Expand All @@ -117,7 +123,7 @@ class Embed(LudwigModule):
def __init__(
self,
vocab: List[str],
embedding_size: int,
embedding_size: Optional[int] = None,
representation: str = "dense",
embeddings_trainable: bool = True,
pretrained_embeddings: Optional[str] = None,
Expand Down