Implementation:Recommenders team Recommenders GRU Model
| Knowledge Sources | |
|---|---|
| Domains | Recommendation Systems, Deep Learning, Sequential Recommendation |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Implements a GRU (Gated Recurrent Unit) based sequential recommendation model that captures temporal dependencies in user behavior sequences.
Description
The GRUModel class extends SequentialBaseModel and implements _build_seq_graph for sequential recommendation using recurrent neural networks. The model concatenates item_history_embedding and cate_history_embedding into a combined history embedding, then processes this sequence through a GRUCell (from Keras) using TensorFlow's dynamic_rnn with sequence_length masking to handle variable-length sequences. The final_state of the GRU serves as the user representation, which is concatenated with the target_item_embedding to form the model output.
The class also contains an alternative _build_lstm method that follows the same architecture pattern but uses an LSTMCell instead, returning final_state[1] (the cell output) as the user representation. This LSTM variant is available but not used in the default _build_seq_graph flow.
The model is based on the work of Cho et al. (2014) on learning phrase representations using RNN encoder-decoder architectures, adapted for sequential recommendation.
Usage
Use this model as an RNN-based baseline for sequential recommendation when you want to capture temporal order in user interaction sequences. It provides a standard recurrent approach that can be compared against CNN-based (Caser) or attention-based (A2SVD, SLI_REC) alternatives.
Code Reference
Source Location
- Repository: Recommenders
- File: recommenders/models/deeprec/models/sequential/gru.py
- Lines: 1-80
Signature
class GRUModel(SequentialBaseModel):
def _build_seq_graph(self)
def _build_lstm(self)
def _build_gru(self)
Import
from recommenders.models.deeprec.models.sequential.gru import GRUModel
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| self.item_history_embedding | tf.Tensor | Yes | Embedding tensor of the user's item interaction history |
| self.cate_history_embedding | tf.Tensor | Yes | Embedding tensor of the corresponding category history |
| self.target_item_embedding | tf.Tensor | Yes | Embedding tensor of the target item being scored |
| self.iterator.mask | tf.Tensor | Yes | Binary mask tensor indicating valid sequence positions |
| self.hidden_size | int | Yes | Dimensionality of the GRU hidden state |
Outputs
| Name | Type | Description |
|---|---|---|
| return (_build_seq_graph) | tf.Tensor | Concatenation of the GRU final hidden state and target item embedding |
| return (_build_gru) | tf.Tensor | Final hidden state of the GRU representing the user |
| return (_build_lstm) | tf.Tensor | Final cell output of the LSTM representing the user |
Usage Examples
Basic Usage
from recommenders.models.deeprec.models.sequential.gru import GRUModel
from recommenders.models.deeprec.deeprec_utils import prepare_hparams
# Prepare hyperparameters from YAML config
hparams = prepare_hparams("recommenders/models/deeprec/config/gru.yaml")
# Create and train the GRU model
model = GRUModel(hparams, iterator_creator)
model.fit(train_file, valid_file)
# Evaluate the model
eval_results = model.run_eval(test_file)