GithubHelp home page GithubHelp logo

nvidia-merlin / transformers4rec Goto Github PK

View Code? Open in Web Editor NEW
1.0K 26.0 142.0 217.27 MB

Transformers4Rec is a flexible and efficient library for sequential and session-based recommendation and works with PyTorch.

Home Page: https://nvidia-merlin.github.io/Transformers4Rec/main

License: Apache License 2.0

Python 98.54% Shell 1.46%
transformer pytorch tabular-data huggingface bert gtp language-model xlnet nlp seq2seq

transformers4rec's Introduction

PyPI LICENSE Documentation

Transformers4Rec is a flexible and efficient library for sequential and session-based recommendation and can work with PyTorch.

The library works as a bridge between natural language processing (NLP) and recommender systems (RecSys) by integrating with one of the most popular NLP frameworks, Hugging Face Transformers (HF). Transformers4Rec makes state-of-the-art transformer architectures available for RecSys researchers and industry practitioners.

The following figure shows the use of the library in a recommender system. Input data is typically a sequence of interactions such as items that are browsed in a web session or items put in a cart. The library helps you process and model the interactions so that you can output better recommendations for the next item.

Sequential and Session-based recommendation with Transformers4Rec

Sequential and Session-based recommendation with Transformers4Rec

Traditional recommendation algorithms usually ignore the temporal dynamics and the sequence of interactions when trying to model user behavior. Generally, the next user interaction is related to the sequence of the user's previous choices. In some cases, it might be a repeated purchase or song play. User interests can also suffer from interest drift because preferences can change over time. Those challenges are addressed by the sequential recommendation task.

A special use case of sequential-recommendation is the session-based recommendation task where you only have access to the short sequence of interactions within the current session. This is very common in online services like e-commerce, news, and media portals where the user might choose to browse anonymously due to GDPR compliance that restricts collecting cookies or because the user is new to the site. This task is also relevant for scenarios where the users' interests change a lot over time depending on the user context or intent. In this case, leveraging the interactions for the current session is more promising than old interactions to provide relevant recommendations.

To deal with sequential and session-based recommendation, many sequence learning algorithms previously applied in machine learning and NLP research have been explored for RecSys based on k-Nearest Neighbors, Frequent Pattern Mining, Hidden Markov Models, Recurrent Neural Networks, and more recently neural architectures using the Self-Attention Mechanism and transformer architectures. Unlike Transformers4Rec, these frameworks only accept sequences of item IDs as input and do not provide a modularized, scalable implementation for production usage.

Benefits of Transformers4Rec

Transformers4Rec offers the following benefits:

  • Flexibility: Transformers4Rec provides modularized building blocks that are configurable and compatible with standard PyTorch modules. This building-block design enables you to create custom architectures with multiple towers, multiple heads/tasks, and losses.

  • Access to HF Transformers: More than 64 different Transformer architectures can be used to evaluate your sequential and session-based recommendation task as a result of the Hugging Face Transformers integration.

  • Support for multiple input features: HF Transformers only support sequences of token IDs as input because it was originally designed for NLP. Transformers4Rec enables you to use other types of sequential tabular data as input with HF transformers due to the rich features that are available in RecSys datasets. Transformers4Rec uses a schema to configure the input features and automatically creates the necessary layers, such as embedding tables, projection layers, and output layers based on the target without requiring code changes to include new features. You can normalize and combine interaction and sequence-level input features in configurable ways.

  • Seamless preprocessing and feature engineering: As part of the Merlin ecosystem, Transformers4Rec is integrated with NVTabular and Triton Inference Server. These components enable you to build a fully GPU-accelerated pipeline for sequential and session-based recommendation. NVTabular has common preprocessing operations for session-based recommendation and exports a dataset schema. The schema is compatible with Transformers4Rec so that input features can be configured automatically. You can export your trained models to serve with Triton Inference Server in a single pipeline that includes online feature preprocessing and model inference. For more information, refer to End-to-end pipeline with NVIDIA Merlin.

GPU-accelerated Sequential and Session-based recommendation

GPU-accelerated pipeline for Sequential and Session-based recommendation using NVIDIA Merlin components

Transformers4Rec Achievements

Transformers4Rec recently won two session-based recommendation competitions: WSDM WebTour Workshop Challenge 2021 (organized by Booking.com) and SIGIR eCommerce Workshop Data Challenge 2021 (organized by Coveo). The library provides higher accuracy for session-based recommendation than baseline algorithms and we performed extensive empirical analysis about the accuracy. These observations are published in our ACM RecSys'21 paper.

Sample Code: Defining and Training the Model

Training a model with Transformers4Rec typically requires performing the following high-level steps:

  1. Provide the schema and construct an input-module.

    If you encounter session-based recommendation issues, you typically want to use the TabularSequenceFeatures class because it merges context features with sequential features.

  2. Provide the prediction-tasks.

    The tasks that are provided right out of the box are available from our API documentation.

  3. Construct a transformer-body and convert this into a model.

The following code sample shows how to define and train an XLNet model with PyTorch for next-item prediction task:

from transformers4rec import torch as tr
from transformers4rec.torch.ranking_metric import NDCGAt, RecallAt

# Create a schema or read one from disk: tr.Schema().from_json(SCHEMA_PATH).
schema: tr.Schema = tr.data.tabular_sequence_testing_data.schema

max_sequence_length, d_model = 20, 64

# Define the input module to process the tabular input features.
input_module = tr.TabularSequenceFeatures.from_schema(
    schema,
    max_sequence_length=max_sequence_length,
    continuous_projection=d_model,
    aggregation="concat",
    masking="causal",
)

# Define a transformer-config like the XLNet architecture.
transformer_config = tr.XLNetConfig.build(
    d_model=d_model, n_head=4, n_layer=2, total_seq_length=max_sequence_length
)

# Define the model block including: inputs, masking, projection and transformer block.
body = tr.SequentialBlock(
    input_module,
    tr.MLPBlock([d_model]),
    tr.TransformerBlock(transformer_config, masking=input_module.masking)
)

# Define the evaluation top-N metrics and the cut-offs
metrics = [NDCGAt(top_ks=[20, 40], labels_onehot=True),
           RecallAt(top_ks=[20, 40], labels_onehot=True)]

# Define a head with NextItemPredictionTask.
head = tr.Head(
    body,
    tr.NextItemPredictionTask(weight_tying=True, metrics=metrics),
    inputs=input_module,
)

# Get the end-to-end Model class.
model = tr.Model(head)

You can modify the preceding code to perform binary classification. The masking in the input module can be set to None instead of causal. When you define the head, you can replace the NextItemPredictionTask with an instance of BinaryClassificationTask. See the sample code in the API documentation for the class.

Installation

You can install Transformers4Rec with Pip, Conda, or run a Docker container.

Installing Transformers4Rec Using Pip

You can install Transformers4Rec with the functionality to use the GPU-accelerated Merlin dataloader. Installation with the dataloader is highly recommended for better performance. Those components can be installed as optional arguments for the pip install command.

To install Transformers4Rec using Pip, run the following command:

pip install transformers4rec[nvtabular]

-> Be aware that installing Transformers4Rec with pip does not automatically install RAPIDS cuDF. -> cuDF is required for GPU-accelerated versions of NVTabular transforms and the Merlin Dataloader.

Instructions for installing cuDF with pip are available here: https://docs.rapids.ai/install#pip-install

pip install cudf-cu11 dask-cudf-cu11 --extra-index-url=https://pypi.nvidia.com

Installing Transformers4Rec Using Conda

To install Transformers4Rec using Conda, run the following command with conda or mamba to create a new environment.

mamba create -n transformers4rec-23.04 -c nvidia -c rapidsai -c pytorch -c conda-forge \
    transformers4rec=23.04 `# NVIDIA Merlin` \
    nvtabular=23.04 `# NVIDIA Merlin - Used in example notebooks` \
    python=3.10 `# Compatible Python environment` \
    cudf=23.02 `# RAPIDS cuDF - GPU accelerated DataFrame` \
    cudatoolkit=11.8 pytorch-cuda=11.8 `# NVIDIA CUDA version`

Installing Transformers4Rec Using Docker

Transformers4Rec is pre-installed in the merlin-pytorch container that is available from the NVIDIA GPU Cloud (NGC) catalog.

Refer to the Merlin Containers documentation page for information about the Merlin container names, URLs to container images in the catalog, and key Merlin components.

Notebook Examples and Tutorials

The End-to-end pipeline with NVIDIA Merlin page shows how to use Transformers4Rec and other Merlin libraries like NVTabular to build a complete recommender system.

We have several example notebooks to help you build a recommender system or integrate Transformers4Rec into your system:

  • A getting started example that includes training a session-based model with an XLNET transformer architecture.
  • An end-to-end example that trains a model and takes the next step to serve inference with Triton Inference Server.
  • Another end-to-end example that trains and evaluates a session-based model on RNN and also serves inference with Triton Inference Server.
  • A notebook and scripts that reproduce the experiments presented in a paper for RecSys 2021.

Feedback and Support

If you'd like to make direct contributions to Transformers4Rec, refer to Contributing to Transformers4Rec. We're particularly interested in contributions or feature requests for our feature engineering and preprocessing operations. To further advance our Merlin roadmap, we encourage you to share all the details regarding your recommender system pipeline by going to https://developer.nvidia.com/merlin-devzone-survey.

If you're interested in learning more about how Transformers4Rec works, refer to our Transformers4Rec documentation. We also have API documentation that outlines the specifics of the available modules and classes within Transformers4Rec.

transformers4rec's People

Contributors

ajschmidt8 avatar albert17 avatar ayodeawe avatar bbozkaya avatar benfred avatar edknv avatar emmaqiaoch avatar evenoldridge avatar gabrielspmoreira avatar jperez999 avatar karlhigley avatar leej35 avatar lgardenhire avatar marcromeyn avatar masoncusack avatar mikemckiernan avatar murphp15 avatar nv-alaiacano avatar nzarif avatar oliverholworthy avatar radekosmulski avatar rnyak avatar sararb avatar woosukkwon avatar zanussbaum avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

transformers4rec's Issues

Extend TabularFeature with 3-D sequence inputs

  • The current version returns 2D tensor where sequential inputs are combined using EmbeddingBag with 'mean' combiner. Instead, we should support returning a 3-D tensor to build item interaction embedding for session-based task.

  • Requires fixing the issue #67

The related files are :

Retrieve itemid embedding table from block

When mf_constrained_embeddings is set to True in SequentialPrediction task, the output layer is tied to the embedding table of item-id. the prediction head should therefore get this table from the model block.

Multi-GPU support with NVTabular data loader with our wrapper

Currently we use PyTorch DataParallel, which is not optimal. Move to DistributedParallel (recommended) using the Multi-GPU support of NVT Data loaders and check which Transformer architectures work

References:
Context on DataParallel / Distributed Parallel: https://github.com/rapidsai/recsys/tree/main/transformers4recsys/codes#2-run-the-training--evaluation-script
NVT with Horovod: https://nbviewer.jupyter.org/github/NVIDIA/NVTabular/blob/main/examples/multi-gpu-movielens/01-03b-MultiGPU-Download-Convert-ETL-with-NVTabular-Training-with-PyTorch.ipynb"

Benchmark the new PyT data loader (with sparse tensors support) scalability with multi-GPU and larger datasets

Benchmark the new PyT data loader with the REES46 ecommerce dataset, using multiple GPUs

Train set: All train.parquet files for 31 days (1 parquet file by week). P.s. Set row group size accordingly
Eval set: All valid.parquet files concatenated

  • Create a recsys_main.py variation for non-incremental training
  • Train with 3 weeks and evaluate on the last week
  • Run experiments varying the number of GPUs: Single GPU, Multi-GPU Data Parallel, Multi-GPU Distributed DataParallel

Finalize design for build with shape in torch

Keras propagates static shape information. This allows things like a Dense layer where you can just specify the hidden-dimension, and then we it builds the layer with the shapes in can init the weights. We would like to have something similar in the torch-side of the library so that we could enable things like a MLPBlock.

Initial version of documentation

Documentation of Transformers4Rec: how to install with docker and conda, example commands to run preproc and training, API documentation

Create client.py example to serve Triton server

This is a requirement for RecSys21 tutorial. Once we load the models to the Triton server, we then should be able to create a client.py or an example code in jp notebook to send request to the server, and generate final prediction results for the next item to be clicked.

First end-to-end example of pytorch-API

Create a notebook for training a Transformer model with pre-processed yoochoose dataset using NVTabular and Transformers4Rec API. The steps are :

  • #111
  • Load features from the Tagging schema
  • Define the sequential aggregator to create interaction embeddings
  • Instantiate the masking class for a given language modelling task
  • Instantiate the sequential model : Load tf4rec config and init the related HF class
  • Instantiate the sequential prediction task with related metrics and loss
  • Run training using two first day
  • Run evaluation on the third day

N.B: This notebook is the simpler version of the Transformers4Rec tasks.

Create an API for the Transformers4Rec modules

Create an API for the Transformers4Rec modules (Meta-Architecture, Evaluation, Logging), allowing users to define their own training and evaluation pipeline (not necessarily using our recsys_main.py script)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.