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

Make resizable layer work with textcat and transformers #820

Merged
merged 4 commits into from
Feb 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions thinc/layers/resizable.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def resize_linear_weighted(
return layer
elif new_nO == layer.get_dim("nO"):
return layer
elif layer.has_dim("nI") is None:
layer.set_dim("nO", new_nO, force=True)
return layer
Comment on lines +62 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find, Paul. Seems correct to me!


dims = {name: layer.maybe_get_dim(name) for name in layer.dim_names}
dims["nO"] = new_nO
Expand Down
32 changes: 32 additions & 0 deletions thinc/tests/layers/test_resizable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest
from functools import partial
from thinc.api import resizable, Linear
from thinc.layers.resizable import resize_model, resize_linear_weighted


@pytest.fixture
def model():
output_layer = Linear(nO=None, nI=None)
fill_defaults = {"b": 0, "W": 0}
model = resizable(
output_layer,
resize_layer=partial(resize_linear_weighted, fill_defaults=fill_defaults),
)
return model


def test_resizable_linear_default_name(model):
assert model.name == "resizable(linear)"


def test_resize_model(model):
"""Test that resizing the model doesn't cause an exception."""
resize_model(model, new_nO=10)
resize_model(model, new_nO=11)

model.set_dim("nO", 0, force=True)
resize_model(model, new_nO=10)

model.set_dim("nI", 10, force=True)
model.set_dim("nO", 0, force=True)
resize_model(model, new_nO=10)