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

Add test for retrieval model with transformer block #833

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6eea6ab
Add test for transformer with RetrievalModelV2
oliverholworthy Oct 26, 2022
4f25bce
Update test for transformer retrieval model
oliverholworthy Oct 27, 2022
dcf594f
Remove test_retrieval from test_block
oliverholworthy Oct 27, 2022
3c87d81
Allow index param to be optional to `Encoder.encode`
oliverholworthy Oct 28, 2022
d22d3ba
Correct target extraction in `SequencePredictNext`
oliverholworthy Oct 28, 2022
fada3a8
Replace ragged coercion with axis aware tf.squeeze
oliverholworthy Nov 1, 2022
fd09644
Merge branch 'main' into transformer-retrieval-model
oliverholworthy Nov 3, 2022
3e79045
Revert change to predict next
oliverholworthy Nov 4, 2022
7fd5ae4
Remove unused ReplaceMaskedEmbeddings (only required for MLM model)
oliverholworthy Nov 4, 2022
3a41f79
Support tuple return type from model.fit `pre` argument
oliverholworthy Nov 7, 2022
a091fd5
Use predict last and use as pre instead of transform
oliverholworthy Nov 7, 2022
83b87c8
Revert changes to contrastive output
oliverholworthy Nov 7, 2022
9704c3e
Set process_lists default value to False
oliverholworthy Nov 8, 2022
22eb8f6
Add d_model and MLPBlock
oliverholworthy Nov 11, 2022
e233529
Merge branch 'main' into transformer-retrieval-model
oliverholworthy Nov 11, 2022
f9a4857
Revert change to `Encoder.encode`
oliverholworthy Nov 14, 2022
53320b0
Revert change to default value of `process_lists` in `sample_batch`
oliverholworthy Nov 14, 2022
8bb8683
Remove commented query_embeddings line
oliverholworthy Nov 14, 2022
0feea3c
update comment about prediction tuple
oliverholworthy Nov 14, 2022
e13b517
Merge branch 'main' into transformer-retrieval-model
marcromeyn Nov 14, 2022
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
5 changes: 5 additions & 0 deletions merlin/models/tf/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,11 @@ def train_step(self, data):
out = call_layer(self.train_pre, x, targets=y, features=x, training=True)
if isinstance(out, Prediction):
x, y = out.outputs, out.targets
elif isinstance(out, tuple):
assert (
len(out) == 2
), "output of `pre` must be a 2-tuple of x, y or `Prediction` tuple"
x, y = out
else:
x = out

Expand Down
55 changes: 55 additions & 0 deletions tests/unit/tf/transformers/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,61 @@ def test_import():
assert transformers is not None


@pytest.mark.parametrize("run_eagerly", [True])
def test_retrieval_transformer(sequence_testing_data: Dataset, run_eagerly):

seq_schema = sequence_testing_data.schema.select_by_tag(Tags.SEQUENCE).select_by_tag(
Tags.CATEGORICAL
)

target = sequence_testing_data.schema.select_by_tag(Tags.ITEM_ID).column_names[0]
predict_last = mm.SequencePredictLast(schema=seq_schema, target=target)
loader = Loader(sequence_testing_data, batch_size=8, shuffle=False)

query_schema = seq_schema
output_schema = seq_schema.select_by_name(target)

d_model = 48
query_encoder = mm.Encoder(
mm.InputBlockV2(
query_schema,
embeddings=mm.Embeddings(
query_schema.select_by_tag(Tags.CATEGORICAL), sequence_combiner=None
),
),
mm.MLPBlock([d_model]),
GPT2Block(d_model=d_model, n_head=2, n_layer=2),
tf.keras.layers.Lambda(lambda x: tf.reduce_mean(x, axis=1)),
)

model = mm.RetrievalModelV2(
query=query_encoder,
output=mm.ContrastiveOutput(output_schema, negative_samplers="in-batch"),
)

testing_utils.model_test(
model,
loader,
run_eagerly=run_eagerly,
reload_model=False,
metrics={},
fit_kwargs={"pre": predict_last},
)

predictions = model.predict(loader)
assert list(predictions.shape) == [100, 51997]

query_embeddings = query_encoder.predict(loader)
assert list(query_embeddings.shape) == [100, d_model]

item_embeddings = model.candidate_embeddings().compute().to_numpy()

assert list(item_embeddings.shape) == [51997, d_model]
predicitons_2 = np.dot(query_embeddings, item_embeddings.T)

np.testing.assert_allclose(predictions, predicitons_2, atol=1e-7)


def test_transformer_encoder():
NUM_ROWS = 100
SEQ_LENGTH = 10
Expand Down