-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tailor): rename convert function to_embedding_model (#103)
* refactor(tailor): rename convert function to_embedding_model * refactor(tailor): rename convert function to_embedding_model * fix(helper): fix get_framework function
- Loading branch information
Showing
14 changed files
with
132 additions
and
111 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Empty file.
This file contains 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,55 @@ | ||
import paddle | ||
import pytest | ||
import tensorflow as tf | ||
import torch | ||
|
||
from finetuner.helper import get_framework | ||
from finetuner.tailor import to_embedding_model | ||
|
||
|
||
class LastCellPT(torch.nn.Module): | ||
def forward(self, x): | ||
out, _ = x | ||
return out[:, -1, :] | ||
|
||
|
||
class LastCellPD(paddle.nn.Layer): | ||
def forward(self, x): | ||
out, _ = x | ||
return out[:, -1, :] | ||
|
||
|
||
embed_models = { | ||
'keras': lambda: tf.keras.Sequential( | ||
[ | ||
tf.keras.layers.Embedding(input_dim=5000, output_dim=64), | ||
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)), | ||
tf.keras.layers.Dense(32), | ||
] | ||
), | ||
'torch': lambda: torch.nn.Sequential( | ||
torch.nn.Embedding(num_embeddings=5000, embedding_dim=64), | ||
torch.nn.LSTM(64, 64, bidirectional=True, batch_first=True), | ||
LastCellPT(), | ||
torch.nn.Linear(in_features=2 * 64, out_features=32), | ||
), | ||
'paddle': lambda: paddle.nn.Sequential( | ||
paddle.nn.Embedding(num_embeddings=5000, embedding_dim=64), | ||
paddle.nn.LSTM(64, 64, direction='bidirectional'), | ||
LastCellPD(), | ||
paddle.nn.Linear(in_features=2 * 64, out_features=32), | ||
), | ||
} | ||
|
||
|
||
@pytest.mark.parametrize('framework', ['keras', 'paddle', 'torch']) | ||
@pytest.mark.parametrize('freeze', [True, False]) | ||
@pytest.mark.parametrize('output_dim', [None, 2]) | ||
def test_to_embedding_fn(framework, output_dim, freeze): | ||
m = embed_models[framework]() | ||
assert get_framework(m) == framework | ||
m1 = to_embedding_model( | ||
m, input_size=(5000,), input_dtype='int64', freeze=freeze, output_dim=output_dim | ||
) | ||
assert m1 | ||
assert get_framework(m1) == framework |
Oops, something went wrong.