Implementation:Recommenders team Recommenders Caser Model
| Knowledge Sources | |
|---|---|
| Domains | Recommendation Systems, Deep Learning, Sequential Recommendation |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Implements the Caser (Convolutional Sequence Embedding) model for top-N sequential recommendation using both vertical and horizontal convolution operations over user interaction history.
Description
The CaserModel class extends SequentialBaseModel and uses CNN-based sequence modeling. The constructor initializes key hyperparameters: L (history sequence length for convolution), T (prediction shape), n_v (number of vertical convolution filters), n_h (number of horizontal convolution filters), and lengths (filter widths from 1 to L for horizontal convolutions).
The _build_seq_graph method calls _caser_cnn, which applies two parallel CNN branches: one for item_history_embedding and one for cate_history_embedding. Each branch uses the _add_cnn method which consists of:
- Vertical convolutions: The embedding matrix is transposed and processed with n_v filters of full-embedding-width via _build_cnn, then flattened. This captures point-level sequential patterns across the entire feature dimension.
- Horizontal convolutions: Multiple convolutions with filter widths ranging from 1 to L, each using n_h filters with ReLU activation via tf.compat.v1.layers.conv1d. Max-pooling over the time dimension is applied to each output, and all pooled results are concatenated. This captures union-level sequential patterns of varying lengths.
The vertical and horizontal outputs are concatenated, and the item and category CNN outputs are further concatenated into cnn_output. The final model output combines this with target_item_embedding.
Usage
Use this model when you want a CNN-based approach to sequential recommendation that captures both individual item signals (vertical) and multi-item sequential patterns (horizontal). It provides an alternative to RNN-based approaches like GRU for sequence modeling.
Code Reference
Source Location
- Repository: Recommenders
- File: recommenders/models/deeprec/models/sequential/caser.py
- Lines: 1-108
Signature
class CaserModel(SequentialBaseModel):
def __init__(self, hparams, iterator_creator, seed=None)
def _build_seq_graph(self)
def _add_cnn(self, hist_matrix, vertical_dim, scope)
def _caser_cnn(self)
def _build_cnn(self, history_matrix, nums, shape)
Import
from recommenders.models.deeprec.models.sequential.caser import CaserModel
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| hparams | HParams | Yes | Hyperparameters object containing L, T, n_v, n_h, and other model configuration |
| iterator_creator | object | Yes | An iterator class for loading sequential data |
| seed | int | No | Random seed for reproducibility |
| hist_matrix (_add_cnn) | tf.Tensor | Yes | History sequential embedding tensor |
| vertical_dim (_add_cnn) | int | Yes | Embedding dimension for vertical convolution |
| scope (_add_cnn) | str | Yes | Variable scope name for the CNN operations |
Outputs
| Name | Type | Description |
|---|---|---|
| return (_build_seq_graph) | tf.Tensor | Concatenation of CNN-encoded sequence representation and target item embedding |
| return (_add_cnn) | tf.Tensor | Concatenated vertical and horizontal convolution outputs |
| return (_caser_cnn) | tf.Tensor | Concatenated item and category CNN outputs |
| return (_build_cnn) | tf.Tensor | Output of a single 1D convolution layer with ReLU activation |
Usage Examples
Basic Usage
from recommenders.models.deeprec.models.sequential.caser import CaserModel
from recommenders.models.deeprec.deeprec_utils import prepare_hparams
# Prepare hyperparameters from YAML config
hparams = prepare_hparams(
"recommenders/models/deeprec/config/caser.yaml",
L=5, # history sequence length
T=3, # prediction length
n_v=4, # number of vertical filters
n_h=16, # number of horizontal filters
)
# Create and train the Caser model
model = CaserModel(hparams, iterator_creator)
model.fit(train_file, valid_file)
# Evaluate the model
eval_results = model.run_eval(test_file)