NVIDIA-Merlin / Transformers4Rec

Transformers4Rec is a flexible and efficient library for sequential and session-based recommendation and works with PyTorch.
https://nvidia-merlin.github.io/Transformers4Rec/main
Apache License 2.0
1.11k stars 146 forks source link

[QST] How to retrieve the embedding of the transformer block? #711

Closed JasonDarkBlue closed 1 year ago

JasonDarkBlue commented 1 year ago

❓ Questions & Help

Details

Hello!

How can I retrieve the embedding of the transformer block for a new session from a trained T4R model, so that I can use it for downstream tasks? I attempted to reconstruct the model without the prediction task, but couldn't find an easy solution.

Thank you very much!

rnyak commented 1 year ago

@JasonDarkBlue hello. We dont have an api to export a session encoder from TF4Rec model. What you can do is to extract hidden state embeddings out of the Trainer module via model.heads[0].body(batch[0] for each batch in your dataset. This should return a 3D array size of (batch_size, sequence_length, d_model). hope that helps.

JasonDarkBlue commented 1 year ago

Thank you for your answer! It worked perfectly!