Context-aware Session-based Recommendation with Graph Neural Networks
Session-based recommendation (SBR) is a task that aims to predict items based
on anonymous sequences of user behaviors in a session. While there are methods
that leverage rich context information in sessions for SBR, most of them have
the following limitations: 1) they fail to distinguish the item-item edge types
when constructing the global graph for exploiting cross-session contexts; 2)
they learn a fixed embedding vector for each item, which lacks the flexibility
to reflect the variation of user interests across sessions; 3) they generally
use the one-hot encoded vector of the target item as the hard label to predict,
thus failing to capture the true user preference. To solve these issues, we
propose CARES, a novel context-aware session-based recommendation model with
graph neural networks, which utilizes different types of contexts in sessions
to capture user interests. Specifically, we first construct a multi-relation
cross-session graph to connect items according to intra- and cross-session
item-level contexts. Further, to encode the variation of user interests, we
design personalized item representations. Finally, we employ a label
collaboration strategy for generating soft user preference distribution as
labels. Experiments on three benchmark datasets demonstrate that CARES
consistently outperforms state-of-the-art models in terms of P@20 and MRR@20.
Our data and codes are publicly available at
https://github.com/brilliantZhang/CARES.
Zhihui Zhang, JianXiang Yu, Xiang Li
# Context-aware Session-based Recommendation with Graph Neural Networks

###### Abstract

Session-based recommendation (SBR) is a task that aims to predict items based on anonymous sequences of user behaviors in a session. While there are methods that leverage rich context information in sessions for SBR, most of them have the following limitations: 1) they fail to distinguish the item-item edge types when constructing the global graph for exploiting cross-session contexts; 2) they learn a fixed embedding vector for each item, which lacks the flexibility to reflect the variation of user interests across sessions; 3) they generally use the one-hot encoded vector of the target item as the hard label to predict, thus failing to capture the true user preference. To solve these issues, we propose CARES, a novel context-aware session-based recommendation model with graph neural networks, which utilizes different types of contexts in sessions to capture user interests. Specifically, we first construct a multi-relation cross-session graph to connect items according to intra- and cross-session item-level contexts. Further, to encode the variation of user interests, we design personalized item representations. Finally, we employ a label collaboration strategy for generating soft user preference distribution as labels. Experiments on three benchmark datasets demonstrate that CARES consistently outperforms state-of-the-art models in terms of P@20 and MRR@20. Our data and codes are publicly available at [https://github.com/brilliantZhang/CARES](https://github.com/brilliantZhang/CARES).

 Session-based recommendation, Graph neural networks, Collaborative learning

## I Introduction

Recommendation systems play a crucial role in various fields because they provide users with personalized information to complete a task in the midst of a large amount of information. At present, many recommendation models have achieved great success, but most of them usually need to use user profiles. However, as the number of users on the platform grows and privacy awareness increases, user profiling may not be available in certain applications. Without obtaining user profiles as well as long-term historical user behaviors, it is hard to accurately model portraits of users. Consequently, session-based recommendation (SBR) has recently attracted more attention. Here, a session can generate interactive behavior sequence (e.g., clicks in e-commerce scenarios) in a short period of time, and SBR aims to predict the _next item_ based on an anonymous short-term behavior sequence.

To address the SBR problem, some existing methods [9, 10, 11, 19] utilize the rich context information in sessions, which generally includes both _intra-session_ and _cross-session_ ones. For the former, we can further divide it into _item-level context_, which characterizes the neighboring items in the behavior sequence for an item, and _session-level context_, which refers to the complete sequence information in a session. Similarly, the latter includes collaborative information from sessions with similar behavioral patterns for both item and session. Details on the division of contexts are given in Figure 1. Early studies [4, 5, 7] for SBR employ intra-session contexts only, whose performance could be adversely affected when the behavior sequence in a session is very sparse. Recently, there are also methods [9, 10, 11, 19] that leverage both intra-session and cross-session contexts, which aim to incorporate contextual information from relevant sessions to enrich the representation of a given session. In particular, some methods [11, 19] propose to construct a global graph to link items from various sessions according to the intra- and cross-session item-level context information, and then learn item/session embeddings based on the graph. Despite the success, most of these methods suffer from three major limitations. First, when constructing the global graph, they fail to distinguish the item-item edge types. Since it has been verified in [29, 30] that integrating item attributes can improve the recommendation performance, the categorical attributes of items (e.g., "shirts" and "pants" belong to the apparel category), can be used to distinguish item relations. For example, if products in two categories are frequently interacted by users, the relation between the two item categories is of more importance. Second, they learn a fixed embedding vector for each item. However, since user interest could vary across sessions, the embedding of an item should be learned to reflect the variation of user interests and personalized w.r.t. different sessions. Third, they generally use the one-hot encoded vector of the target item as the hard label to be predicted, which may not reflect the true user preference. However, the true distribution of user preferences is usually unknown, as only a limited number of items are exposed to users. Simply regarding the one-hot encoded vector of the
target item as the true distribution could induce bias and lead to the overfitting problem [15].

In this paper, to address these problems, we propose a novel context-aware session-based recommendation model CARES, which leverages the four types of contexts introduced earlier. Specifically, we first construct a multi-relation cross-session graph to connect items according to intra- and cross-session item-level contexts, where edge relations are defined based on item categories. Then based on the graph, we learn general item embeddings with graph neural networks (GNNs). Further, to encode the variation of user interests, we also learn personalized item representations w.r.t. sessions with a gating mechanism. After that, we unify item embeddings with item positions and session length to learn session representations. Finally, to alleviate the bias induced by the hard label of one-hot encoded vector of the target item in a session, we employ internal- and external-session-level contexts, and present a label collaboration strategy, which uses most similar historical sessions to the current session for collaborative filtering and generates soft label of user preferences to be predicted. We next summarize our main contributions in this paper as follows:

* We propose a novel context-aware session-based recommendation model CARES.
* We design personalized item embeddings w.r.t. sessions to capture the variation of user interests across sessions.
* We propose a simple and effective label collaboration method that generates soft user preference distribution as labels.
* We conduct extensive experiments on three public benchmark datasets to show the superiority of our method over other state-of-art models.

## II Related Work

### _Session-based Recommendation_

Early studies [3] on SBR use the similarity between the last item of the session and candidate items to make recommendations. However, they omit the sequential information in the session. While the Markov-chain-based method [4] can bridge the gap, the number of states and the computational complexity increase exponentially as the problem scale increases. Recently, owing to the powerful representation capability of deep learning, many deep-learning-based methods [5, 6, 7] have been successfully applied to SBR. In particular, some approaches [5, 7] exploit Recurrent Neural Networks (RNNs) to characterize the item's sequential information in the session. However, these RNN-based methods are incapable of capturing long-term item dependencies.

Recently, Graph Neural Network (GNN) [8, 12, 16] has attracted more and more attention due to their powerful learning ability for graph structure data representation. To explore the complex transition relation between items in the session, GNN-based SBR constructs sessions into graphs and utilizes GNNs to model the session graph. For instance, SR-GNN [8] first converts the session into a graph and utilizes Gated GNN [23] to model the session graph to explore the complex transition relations between items in the session. After that, GC-SAN [13] further extends SR-GNN by adding self-attention mechanism. However, constructing sessions into graphs will introduce noise and lose the sequential order information, so some GNN-based methods are proposed to alleviate these problems. LESSER [14] improves the way of graph construction for sessions, taking into account the relative order of nodes in sessions. SGNN-HN [12] alleviates the long-range dependency problem by introducing a Star GNN, which improves the information propagation mechanism between items. All these methods only focus on utilizing the internal information in a session.

### _Cross-session Learning in SBR_

Utilizing the current session only to make recommendations is constrained by its limited information. To incorporate collaborative information from external sessions, some collaborative filtering-based SBR methods are proposed to enhance the current session representation. For example, CSRM [9] incorporates the relevant information contained in the neighborhood sessions by adopting a memory module to obtain more accurate session representations. CoSAN [10] utilizes multi-head attention mechanism to fuse item representations in collaborative sessions by building dynamic item representations. GCE-GNN [11] simultaneously constructs local session graphs and a global graph, then extracts information related to the current session from the global graph. MTD [32] constructs a global graph connecting adjacent items in each session and utilizes graphical mutual information maximization to capture global item-wise transition information to enhance the current session's representation. \(S^{2}\)-DHCN [38] utilizes hypergraph convolutional networks to capture high-order item relations and constructs two types of hypergraphs to learn information from inter- and intra-session. The view augmentation in COTREC [37] enables the model to capture beyond-pairwise relations among items across sessions.

### _Multi-relation Learning in SBR_

Heterogeneous graphs have proven effective in handling information by modeling complex high-order dependencies

Figure 1: Contexts referred to in this paper.
among heterogeneous information. They can extract user interests more accurately through global item relations across sessions [16, 34, 35]. AutoGSR [16] uses Network Architecture Search (NAS) techniques to automatically search for better GNN architectures that capture information on local-context relations and various item-transition semantics. MGIR [35] utilizes item relations of incompatible and co-occurrence relations to generate enhanced session representations, while CoHHN [34] proposes a heterogeneous hypergraph network to model price preferences. These works either built multiple relation graphs or used hypergraphs to model artificial features or side information as auxiliary information in modeling user actions. However, constructing sessions into multiple relation graphs is cumbersome. Therefore, we propose a new approach that models association analysis of items' categories in SBR based on a single heterogeneous graph.

## III Preliminaries

In this section, we introduce the problem statement of SBR and the definition of item-side information.

### _Problem Statement_

We formally formulate the task of session-based recommendation (SBR). Let \(V=\{v_{1},v_{2},\ldots,v_{m}\}\) be all of items, where \(m\) is the number of items in \(V\). Assuming that all sessions are denoted as \(U=\{S_{1},S_{2},\ldots,S_{n}\}\), where \(n\) is the number of sessions. Each anonymous session \(S_{\tau}\) in \(U\), which is denoted by \(S_{\tau}=\{v_{1}^{\tau},v_{2}^{\tau},\ldots,v_{t}^{\tau}\}\), consists of a sequence of interactions in chronological order, where \(v_{t}^{\tau}\) denotes the item that the user interacted with at the \(t\)-th timestamp in the session \(S_{\tau}\), and the length of \(S_{\tau}\) is \(t\). The goal of SBR is to recommend the next item from \(V\) that is most probably interacted with by the user given the current session \(S_{\tau}\). We call the item that interacted at the \(t+1\)-th timestamp the target item or the ground truth item of the session, i.e., \(\left(\left[v_{1},v_{2},\ldots,v_{t}\right],v_{t+1}\right)\) is a session and its target item pair.

### _Item-side Information_

Item-side information describes the item itself and can provide extra complementary information for the recommendation. For each item \(v_{i}\), we use its category \(c_{v_{i}}\) as the item-side information to assist in learning user preference. Let \(C=\{c_{1},c_{2},\ldots,c_{l}\}\) be all of categories of items, where \(l\) is the number of categories of items in \(C\). Each category of item \(v_{i}\) is encoded into an unified embedding space, i.e., \(h_{i}^{c}\in\mathbb{R}^{d}\).

## IV The Proposed Method

This section elaborates on our proposed novel Context-aware Graph Neural Networks for Session-based Recommendation (CARES). We first give an overview of CARES, which is illustrated in Figure 2. Next, we describe each component in detail.

### _Multi-relation Cross-session Graph_

Most early graph-based methods [8, 12, 13, 14, 16] model the item transition patterns in a single session into graphs only and ignore the global-level relations between items in different sessions. Therefore, we propose to build connections between different sessions to employ global-level item transition relations further. Specifically, we follow [11] to build these edge connections based on \(\varepsilon\)-neighbor sets of items in all sessions, which are formally defined as follows.

**Definition 1**: \(\varepsilon\)_-Neighbor set [11]. Given a set of sessions \(U\), for an item \(v_{i}^{x}\) in session \(S_{x}\), its \(\varepsilon\)-Neighbor set is a set of items with:_

\[\mathcal{N}_{\varepsilon}(v_{i}^{x})=\left\{v_{j}^{y}|\,j\in[k- \varepsilon,k+\varepsilon],\forall v_{k}^{y}=v_{i}^{x},v_{k}^{y}\in S_{y},S_{y} \in U\right\},\]

_where \(i,j,k\) are positions of items in corresponding sessions, respectively. Further, \(\varepsilon\) is used to control the neighboring range of item transition._

Based on the \(\varepsilon\)-Neighbor set, items from different sessions can be linked. Some existing works [11, 15] consider all the item transitions as one type of relation, while we distinguish item transitions by taking the categorical attribute of items into consideration. Intuitively, if a user successively clicks on items \(v_{1}\) and \(v_{2}\) whose categories are different, we cannot simply consider these two items are related, because this could also indicate the drift of user interest. Further, if a user sequentially clicks on items \(v_{1}\) and \(v_{2}\) with the same category, it is more likely that the two items are highly related. This is because a user usually views a number of similar items before picking the one to buy. Therefore, we propose to construct a multi-relation cross-session graph based on item context and category. Formally, it is defined as \(\mathcal{G}=(\mathcal{V},\mathcal{E})\), where \(\mathcal{V}\) denotes the node set that contains all items in \(V\) and \(\mathcal{E}=\{(v_{i},r_{ij},v_{j})|v_{i}\in V,v_{j}\in\mathcal{N}_{\varepsilon}(v _{i})\}\) represents the edge set in the graph. We use \(r_{ij}=(c_{i},c_{j})\) to denote the edge type, which captures the contextual relation between items of categories \(c_{i}\) and \(c_{j}\). Further, similar as in [31], for the edge between \(v_{i}\) and \(v_{j}\), we give a weight \(e_{ij}=\frac{Freq(v_{i}\in\mathcal{N}_{\varepsilon}(v_{i}))}{(log(Freq(v_{i})^ {x})+1)+\epsilon(log(Freq(v_{j})^{x})+1)}\). Here, \(Freq(\cdot)\) is the frequency counting function over all the sessions. To alleviate the dominant effect of a frequently occurred item, we also introduce a hyper-parameter \(\alpha\), whose value is set to 0.75 in our experiments. To speed up the model efficiency, for each item \(v_{i}\) in the graph, we only keep the top-\(N\) neighbors in each relation that have the largest weights with it. To further simplify the graph, we only retain the top-\(Q\) most frequent contextual relations in the graph. For others, we uniformly set \(r_{ij}=\texttt{Same}\) when \(c_{i}=c_{j}\); \(r_{ij}=\texttt{Drift}\), when \(c_{i}\neq c_{j}\). Figure 3 shows a toy example on converting sessions into a multi-relation cross-session graph.

### _Item Representation Learning_

After the multi-relation cross-session graph is constructed, we next learn item representations. We first use an embedding look-up table to initialize embedding \(\mathbf{h}_{i}\in\mathbb{R}^{d}\) for item \(v_{i}\). After that, we employ the attention mechanism [22] to generate the general embedding vector for each item based
on GNNs. Then we use a gating mechanism to further learn a personalized embedding vector for each item w.r.t. a given session.

**Learning General Item Representations.** Based on the multi-relation cross-session graph, we can easily capture both the intra-session and cross-session item-level context information. To learn the representation of an item, since its \(\varepsilon\)-Neighbors have different importance, we then introduce item-level attention. Note that for each item, it has various contextual relations. Therefore, when computing attention scores, we need to distinguish edge relations. Specifically, in the \(k\)-th layer, the representation of item \(v_{i}\) is derived by neighborhood aggregation, which is formulated as:

\[\mathbf{h}_{i}^{(k)}=\alpha_{ii}\mathbf{W}_{1}\mathbf{h}_{i}^{(k-1)}+\sum_{v_{ j}\in\mathcal{N}(v_{i})}\alpha_{ij}\mathbf{W}_{1}\mathbf{h}_{j}^{(k-1)}. \tag{1}\]

Here, the attention score \(\alpha_{ij}\) is computed by

\[\alpha_{ij}=\frac{\exp\left(\mathbf{a}^{\top}\sigma\left(\mathbf{W}_{1}| \mathbf{h}_{i}^{(k-1)}\,\|\,\mathbf{h}_{j}^{(k-1)}\,\|\,e_{ij}\|\,\mathbf{r}_ {ij}\|\right)\right)}{\sum_{v_{k}\in\mathcal{N}(v_{i})\cup\{v_{i}\}}\exp\left( \mathbf{a}^{\top}\sigma\left(\mathbf{W}_{1}|\mathbf{h}_{i}^{(k-1)}\,\|\,e_{ ik}\|\,\mathbf{r}_{ik}\right)\right)},\]

where \(\sigma\) is the LeakyReLU function, \(\mathbf{a}\) and \(\mathbf{W}_{1}\) are trainable parameters, and \(\|\) denotes the concatenation operator. We also take edge weight \(e_{ij}\) and edge relation embedding \(r_{ij}\in\mathbb{R}^{d}\) as edge features.

Figure 3: Illustration of the construction of the cross-session graph. Here, we set \(\varepsilon\) = 2.

Figure 2: The overview of CARES.
**Learning Personalized Item Representations w.r.t. Sessions.** Note that \(\mathbf{h}_{i}^{(k)}\) in Eq. 1 leverages item-level context and reflects the general embedding vector of item \(v_{i}\). Since an item is generally contained in various sessions, we can further enrich the representation of an item w.r.t. a session. Given a session \(S\) and an item \(v_{i}\in S\), \(S\) could contain many items that are not in \(\mathcal{N}_{\varepsilon}(v_{i})\) and all the items in \(S\) reflect the user interest in the current session. Therefore, we introduce an embedding vector \(\mathbf{h}_{i}^{s}\) for item \(v_{i}\) that is personalized for the session \(S\). Inspired by [28], we add a virtual node \(\tilde{v}\) that is linked to all the items in the session \(S\), whose embedding vector \(\tilde{\mathbf{h}}\) is used to capture the information of all the items in \(S\). After that, we apply a gating mechanism to fuse \(\mathbf{h}_{i}\) and \(\tilde{\mathbf{h}}\) to generate \(\mathbf{h}_{i}^{s}\):

\[\mathbf{h}_{i}^{s_{i}(k)}=(1-\delta_{i})\,\mathbf{h}_{i}^{(k)}+\delta_{i} \tilde{\mathbf{h}}^{(k-1)}, \tag{2}\]

where the gating score \(\delta_{i}\) is computed by:

\[\delta_{i}=\texttt{Sigmoid}\left(\frac{\left(\mathbf{W}_{2}\mathbf{h}_{i}^{ (k)}\right)^{\top}\left(\mathbf{W}_{3}\tilde{\mathbf{h}}^{(k-1)}\right)}{ \sqrt{d}}\right), \tag{3}\]

where \(\mathbf{W}_{2}\),\(\mathbf{W}_{3}\in\mathbb{R}^{d\times d}\) are learnable parameters, and \(\sqrt{d}\) is the scaling coefficient. In this way, we can generate a personalized embedding vector \(\mathbf{h}_{i}^{s}\) for item \(v_{i}\) w.r.t. the session \(S\). When \(\delta_{i}\) is small, \(\mathbf{h}_{i}^{s}\) will be close to the general representation of item \(v_{i}\); otherwise, \(\mathbf{h}_{i}^{s}\) will be more indicative to the information in the current session \(S\). Finally, the embedding vector \(\tilde{\mathbf{h}}^{(k)}\) for \(\tilde{v}\) in the \(k\)-th layer is updated as:

\[\tilde{\mathbf{h}}^{(k)}=\sum_{v_{i}\in S}\beta_{i}\mathbf{h}_{i}^{s_{i}(k)}, \tag{4}\]

where the weight \(\beta_{i}\) is calculated by:

\[\beta_{i}=\texttt{Softmax}\left(\frac{\left(\mathbf{W}_{4}\mathbf{h}_{i}^{s_ {i}(k)}\right)^{\top}\left(\mathbf{W}_{5}\tilde{\mathbf{h}}^{(k-1)}\right)}{ \sqrt{d}}\right). \tag{5}\]

Note that \(\mathbf{W}_{4}\),\(\mathbf{W}_{5}\in\mathbb{R}^{d\times d}\) are trainable parameters.

### _Session Representation Learning_

Given a session \(S\), although the embedding of the virtual node \(\tilde{v}\) contains the information of all the items in \(S\), it omits the temporal information and cannot be simply taken as the representation of the session. In the previous section, for each item \(v_{i}\), we have computed its general embedding \(\mathbf{h}_{i}\) and personalized embedding \(\mathbf{h}_{i}^{s}\) w.r.t. a session \(S\), respectively. For notation brevity, we overload the embedding of item \(v_{i}\) as \(\mathbf{h}_{i}\) and next show how to calculate session representations based on item representations.

To leverage item sequence in a session, in addition to item embeddings, we further incorporate the positional information of items and the length of the session. For all the sessions, we use a shared position embedding look-up table \(\mathbf{P}\), where the \(r\)-th row \(\mathbf{p}_{r}\in\mathbb{R}^{d}\) represents the embedding vector for the \((t-r)\)-th reverse position in a session of length \(t\). Note that we choose a reverse order for positions because the most recent items could be more useful for the prediction of the next item in the session. We also introduce a shared session length embedding look-up table \(\mathbf{L}\), where the \(t\)-th row \(\mathbf{l}_{t}\in\mathbb{R}^{d}\) corresponding to the embedding for the length \(t\) of a session. Note that we limit the maximum length of a session to be \(t_{max}\). After that, for the \(i\)-th item in a session of length \(t\), we unify both the information of item position and session length into the item embedding \(\mathbf{h}_{i}\), and output an updated embedding \(\mathbf{z}_{i}\) for \(v_{i}\):

\[\mathbf{z}_{i}=\mathbf{h}_{i}+\mathbf{p}_{t-i}+\mathbf{l}_{t}. \tag{6}\]

To calculate the representation of a session, we can also employ item categories in the session. For all the items, we further define a shared item category embedding look-up table, where each row indicates an embedding vector of an item category. Given a session \(S\) of length \(t\), we unify item categories in the session as:

\[\mathbf{h}_{c}=\texttt{Mean}(\{\mathbf{h}_{i}^{c}\}_{i=1}^{t},\forall v_{i} \in\mathcal{S}), \tag{7}\]

where \(\mathbf{h}_{i}^{c}\) represents the category embedding of the item \(v_{i}\). Then we use the attention mechanism to fuse the information of all the items in \(S\), and have:

\[\bar{\mathbf{z}}_{s}=\sum_{i=1}^{t}\gamma_{i}\mathbf{z}_{i}, \tag{8}\]

where the attention weight \(\gamma_{i}\) can be calculated by a two-layer MLP:

\[\gamma_{i}=\texttt{MLP}(\mathbf{z}_{i}\,\|\,\mathbf{z}_{t}\,\|\,\tilde{ \mathbf{h}}\,\|\,\mathbf{h}_{c}) \tag{9}\]

Here, \(\tilde{\mathbf{h}}\) is the embedding of the virtual node \(\tilde{v}\) in Equation 4. we also use the embedding \(\mathbf{z}_{t}\) of the last item in \(S\) because it could be highly related to the prediction of the next item.

After that, we combine \(\bar{\mathbf{z}}_{s}\) and \(\mathbf{z}_{t}\) to capture user interests in session \(S\):

\[\mathbf{z}_{s}=\mathbf{W}_{6}\left[\bar{\mathbf{z}}_{s}\|\mathbf{z}_{t}\right], \tag{10}\]

where \(\mathbf{W}_{6}\) is a weight parameter. Further, inspired by the skip connection technique in [27], we directly derive embedding of item \(v_{i}\) from the look-up table and rerun Equations 6-10 to generate a new \(\mathbf{z}_{s}\) (we denote it as \(\mathbf{z}_{s}^{\prime}\) for difference) without the item representation learning stage in Section IV-B. Finally, the representation of session \(S\) is computed by:

\[\mathbf{h}_{s}=\mathbf{z}_{s}+\mathbf{z}_{s}^{\prime}. \tag{11}\]

### _Label Collaboration_

Most existing works [8, 9, 10, 11, 12] use the one-hot encoded vector of the target item as the hard label of user preference, which may not reflect the true preference. The intuition is that users are generally only exposed to a limited number of items, so the lack of other items could induce a bias to user interest. Further, user preference is also influenced by different time periods and contextual scenarios, which can deviate from historical data over time. Therefore, to address the problem, we employ the session-level contexts and propose a label collaboration strategy, which aims to explicitly utilize the target items of
historical sessions with most similar behavioral patterns to the current session as collaborative label information.

**Collaborative Sessions Retrieval.** Given a session \(S\), our target is to first retrieve \(K\) sessions that are most similar to \(S\) from a fixed-size candidate session pool with \(M\) most recent sessions. Intuitively, the more sessions we retrieve, the more accurate the user preference could be estimated, and the larger computation cost will be induced. Therefore, we further utilize _SimHash_[1] to speed up the model efficiency. The SimHash function takes the session representation as input and generates its binary fingerprint, where each entry is either 0 or 1. It has been pointed out in [2] that the outputs of SimHash satisfy the _locality-sensitive properties_ that the outputs are similar if the input vectors are similar to each other. Specifically, we first project embeddings of \(S\) and other candidate sessions into binary fingerprints by multiplying the input embedding vectors with a hash function, which is set to be a fixed random projection matrix \(H\in\mathbb{R}^{d\times m}\), where \(m<d\). As a result, similar session embedding vectors can get the same hashing output. After that, we calculate the _hamming distance_ between the output vectors and select the top-\(K\) most similar sessions to \(S\) from \(M\) candidate sessions by:

\[N_{S},W_{S}=\texttt{topK}(-\texttt{HammingDistance}\left(\mathbf{e},\hat{ \mathbf{e}}\right)),\]

where \(\mathbf{e}=\texttt{SimHash}(S)\), \(\hat{\mathbf{e}}=\texttt{SimHash}(\hat{S})\), and \(\hat{S}\) is derived from \(M\) candidate sessions. The weights \(W_{S}\) are then normalized to ensure that they sum to 1. We denote the set of one-hot encoded labels of selected sessions as \(N_{S}=\left\{\mathbf{y}_{1}^{S},\mathbf{y}_{2}^{S},\ldots,\mathbf{y}_{K}^{S}\right\}\) and the set of corresponding weights as \(W_{S}=\left\{w_{1}^{s},w_{2}^{S},\ldots,w_{K}^{S}\right\}\), which will be used for label collaboration of session \(S\). Further, the pool is updated by a slide window scheme: removing the oldest sessions and adding the most recent ones in the next batch. Therefore, compared to the time complexity \(\mathcal{O}\left(MBd\right)\) of retrieval by cosine similarity in [9], the time complexity of our retrieval is \(\mathcal{O}\left(Bm\right)\), where \(B\) is the batch size, \(M\) is the pool size and \(m\) is smaller than session representation dimensionality \(d\).

**Collaborative Label Generation.** After \(K\) most similar sessions are retrieved, we next construct the soft label for session \(S\). These \(K\) sessions can help provide more comprehensive estimation for user interests than using \(S\) only. Therefore, we obtain the collaborative label for \(S\) by a weighted sum of the one-hot encoded label of each retrieved session:

\[\tilde{\mathbf{y}}=\sum_{i=1}^{K}w_{i}^{S}\mathbf{y}_{i}^{S}. \tag{12}\]

### _Prediction Layer_

The prediction layer is used to output the probability distribution of items that the user will interact at the next timestamp in the current session. Due to the long-tail distribution problem [24] in the data for recommendation, we normalize item embeddings and session embeddings in each layer. Finally, we feed them into a prediction layer, where the inner product and the Softmax function are applied to generate the output:

\[\hat{\mathbf{y}}_{i}=\texttt{Softmax}(\mathbf{h}_{s}^{\top}\mathbf{h}_{i}), \tag{13}\]

where \(\hat{\mathbf{y}}_{i}\) denotes the probability of interacting with item \(v_{i}\) in the next timestamp. The total loss function consists of two components: a cross-entropy loss based on the hard label \(\mathbf{y}\) and a KL-divergence loss based on the soft label \(\tilde{\mathbf{y}}\):

\[\mathcal{L}=\text{CrossEntropy}(\hat{\mathbf{y}},\mathbf{y})+\lambda\text{KLD} (\hat{\mathbf{y}},\tilde{\mathbf{y}}), \tag{14}\]

where \(\lambda\) is a trade-off parameter that is used to control the importance of the two components.

## V Experiments

In this section, we conduct extensive experiments on three publicly available datasets to show the effectiveness of our method. We preprocess these datasets as in [8]. First, we arrange all the sessions in the chronological order and split the data into training data and test data by the timestamps of sessions. Second, we filter out items that appear less than 5 times or only appear in the test set, and also the sessions of length one. Third, we perform data augmentation with a temporal-window shifting to generate more data samples in a session, e.g., \(\left(\left[v_{1},v_{2},\ldots,v_{n-1}\right],v_{n}\right),\ldots,\left(\left[v _{1},v_{2}\right],v_{3}\right),\left(\left[v_{1}\right],v_{2}\right)\) for session \(\left[v_{1},v_{2},\ldots,v_{n}\right]\). Further, we adopt two widely used evaluation metrics in information retrieval: _Precision (P@20)_ and _Mean Reciprocal Rank (MRR@20)_ for evaluating the performance.

### _Datasets_

The following datasets are utilized to evaluate our model. The statistics of the processed datasets are shown in Table I.

\(\bullet\)**Diginetica1** contains anonymous user transaction information extracted from e-commerce search engine logs for five months. The dataset is from CIKM Cup 2016.

Footnote 1: [http://cikm2016.cs.iupu.edu/cikm-cup](http://cikm2016.cs.iupu.edu/cikm-cup)

\(\bullet\)**Tmall2** records the anonymized users' shopping logs on the online shopping platform called Tmall. The dataset comes from the IJCAI15 competition.

Footnote 2: [https://tianchi.aliyun.com/dataset/dataDetail?dataId=42](https://tianchi.aliyun.com/dataset/dataDetail?dataId=42)

Footnote 3: [http://2015.recsymboleng.com/challenge](http://2015.recsymboleng.com/challenge)

\(\bullet\)**Yoochoose1_643** was built by YOOCHOOSE GmbH to support RecSys Challenge 2015. It records users' clicks from an e-commerce website. We follow Wu [8] by using the most recent proportion \(1/64\) of the training sessions.

Footnote 3: [http://2015.recsymboleng.com/challenge](http://2015.recsymboleng.com/challenge)

### _Hyper-parameter Setup_

Following [8, 12], the dimension of the latent vectors is fixed to 256, and the batch size is set to 100. We use the Adam optimizer with the initial learning rate of 0.001, which will decay by 0.8 after every 3 epochs. The \(l_{2}\) penalty is set to

\begin{table}
\begin{tabular}{c|c|c|c} \hline \hline Dataset & Diginetica & Tmall & Yoochoose1\_64 \\ \hline \#Train sessions & \(719,470\) & \(351,268\) & \(369,859\) \\ \#Test sessions & \(60,858\) & \(25,898\) & \(55,898\) \\ \#Items & \(43,097\) & \(40,728\) & \(16,766\) \\ Avg. lengths & \(5.12\) & \(6.69\) & \(6.16\) \\ \hline \hline \end{tabular}
\end{table}
Table I: datasets statstics
\(10^{-5}\) and the dimension of the hash matrix in SimHash is set to 64. The candidate number of sessions is set to 1500 in the label collaboration strategy. We set the parameter \(\lambda\) for adjusting the loss weights to 0.1 for Deginetica 5 for Yoochoose1_64, and 10 for Tmall. We vary the number of retrieved target items in label collaboration from \(\{10,30,50,70,90\}\) and the number of frequent contextual relations from \(\{0,5,10,20,30\}\) to study their effects.

### _Baselines_

To verify the performance of our proposed model, we compared our model with 17 other methods, which can be grouped into three categories. Readers are referred to Section II for more details.

**(Single Session methods): POP** recommends the most popular items. **Item-KNN**[3] recommends items based on the cosine similarity between items in the current session and candidate items. **FPMC**[4] uses both Markov chain and Matrix Factorization to consider the user's personalized and general information. **GRU4REC**[7] exploits the memory of GRUs by characterizing the entire sequence. **NARM**[5] and **STAMP**[6] further utilize attention mechanism additionally, which aims to capture the current interest and general interest of the user. **SRGNN**[8], **LESSER**[14], **SGNN-HN**[12], convert each session into a graph and do not utilize cross-session information.

**(Cross Session methods): CSRM**[9] incorporates the relevant information in the neighborhood sessions through the memory network. **CoSAN**[10] utilizes multi-head attention mechanism to build dynamic item representations by fusing item representations in collaborative sessions. **GCE-GNN**[11] and **MTD**[32] simultaneously focus on cross-session and intra-session dependencies. **COTREC**[37] and \(S^{2}\)**-DHCN**[38] employ a global argumentation view of items to mine informative self-supervision signals.

**(Multi-relation methods): AutoGSR**[16] and **MGIR**[35] both learn multi-faceted item relations to enhance session representation. Note that MGIR utilizes cross-session information while AutoGSR does not.

### _Overall performance_

From the experimental results on the three datasets in Table II, we have the following observations: (1) It is observed that methods utilizing RNNs or attention mechanisms perform better than early methods such as Item-KNN and FPMC because they are both suitable for dealing with sequential data with temporal information without losing the interal-session-level context. Methods such as CSRM and CoSAN offer higher performance for introducing auxiliary information from historical sessions than single session methods like GRU4Rec, NARM and STAMP. This confirms the effectiveness of leveraging external-session-level contexts. The current best-performing methods such as SGNN-HN, COTREC and MGIR are GNN-based approaches because GNNs are good at capturing complex item-transitions across sessions, which shows the effectiveness of introducing cross-session item-level context by graph modeling.

(2) CARES outperforms other GNN-based models SRGNN, LESSER, AotoGSR, and SGNN-HN. This is because all these methods are designed for local sessions without considering cross-session information in the global view. While the cross-session method COTREC leverages self-supervision for enhancing session representation, it ignores heterogeneity and is outperformed by CARES.

(3) The leading performance of CARES and COTREC over GCE-GNN implies that it is useful to capture the internal-session-level context in the global graph because the latter only considers the cross-session item-level context of item-transitions and lacks diversity in its collaborative information. Therefore, COTREC employs self-supervised learning to impose a divergence constraint on global view and internal-session view of item embedding, while CARES further introduces personalized item representation w.r.t sessions. This demonstrates the significance of the internal-session-level context in global graph modeling.

(4) Our approach achieves the best performance in all the datasets, which shows the importance of making full use of contexts in sessions. Further, our model has a significant improvement in terms of MRR@20 on Diginetica and Yoochoose1_64, indicating that the item relevant to users' interests can be ranked higher, which is critical for user experience improvement and confirms the superiority of our model.

(5) To ensure a fair comparison, we conducted experiments with an additional variant model that does not use side information to construct the graph. As shown in Table II, even without utilizing the side information of the item's category (aka CARES_ns), our method still performs well across different datasets.

### _Ablation Study_

We conduct an ablation study on CARES to understand the characteristics of its main components. One variant updates items' embeddings by directly capturing information from intra-session without utilizing general information to model item-transition relationships on the global graph. This helps us understand the importance of including cross-session item-level context in SBR. We call this variant **CARES_ng** (**no g**eneral information). Another variant learns items' embedding without personalized information w.r.t sessions. We call this variant **CARES_np** (**no** personalized information), which helps us evaluate the effectiveness of internal-session-level context. To show the importance of the label collaboration strategy, we train the model with cross-entropy loss only and call this variant **CARES_nl** (**no** label collaboration). **CARES_ns** (**no** side information) represents the variant of CARES without considering category information of items to understand the effect of items' category association in SBR.

From the experimental results in Figure 4, the following observations are made. (i) Compared with **CARES_ng**, CARES leverages cross-session item-level context and thus can utilize diverse collaborative information from the global
graph and outperform **CARES_ng**. (ii) It can also be observed that CARES with learning personalized information beats **CARES_np** on all the datasets. This indicates that internal-session-level context can effectively preserve user intent through adding personalized information w.r.t sessions. (iii) CARES performs better than **CARES_nl**, and this indicates that utilizing the target items of historical sessions with similar behavioral patterns to the current session as external-session-level context can mitigate the bias in the user preference distribution. (iv) CARES also defeats **CARES_ns**, indicating that items' category plays an important role in learning users' preferences. Additionally, although side information improves recommendation accuracy, our model still performs well without it, as shown in Table II.

### _Influence of Contextual Relations_

In this section, we study how contextual relations affect the performance of the proposed method. Due to the limited space, we only show the results in terms of MRR@20. The results are shown in Figure 5. From the results, we can see that the models that do not use contextual relations always have lower performance. This is because contextual relations can help the model capture more complex item context, which indicates disentangling the relation semantics of sessions is a promising direction for further exploiting the information across sessions. For different datasets, the optimal number of contextual relations is different. For the dataset Yoochoose1_64, the score hits the highest when the relation number is set to 30. For the other two datasets, the optimal relation number is 5 and we can see that increasing the number of relations does not always result in a better performance. This is because only the relation between items' categories with enough high frequency can be considered a context.

### _Sensitivity Analysis of Hyper-Parameters_

We end this section with a sensitivity analysis on the hyper-parameters of CARES. In particular, we study two hyper-parameters: the hash matrix dimension \(m\) and the number of retrieved sessions \(K\). In our experiments, we vary one parameter each time with others fixed. Fig 6 illustrates the results with w.r.t. P@20 and MRR@20 scores on the datasets of Tmall and Yoochoose1_64. (Results on other datasets scores exhibit similar trends, and thus are omitted for space reasons.) From the figure, we see that

(1) A larger dimension \(m\) can slightly improve the performance of the model. Since the model is not very sensitive to the hash matrix dimension, setting a small size of \(m\) can also guarantee the performance of the model.

(2) Fewer retrieved sessions in label collaboration are not sufficient to provide enough information for the current session. And there is also a performance drop when retrieving more sessions, which shows that a large number of collaborative sessions could contain noise that adversely affects the recommendation performance. So, an appropriate number of retrieved sessions \(K\) is essential.

## VI Conclusion

In this paper, we propose a novel method named CARES for session-based recommendation based on graph neural network. Specifically, it converts the session sequences into a global graph with item attributes as context. The general item representations are generated by various contextual relations through item-level attention. After that, we apply a gating mechanism to further enrich the representations of items with personalized information w.r.t sessions. Then the intra- and cross-session context information are subsequently combined to enhance the recommendation performance. Finally, it incorporates label collaboration to generate soft user preference

\begin{table}
\begin{tabular}{c c c c c c c} \hline \hline  & \multicolumn{2}{c}{Diginetica} & \multicolumn{2}{c}{Tmall} & \multicolumn{2}{c}{Yoochoose1\_64} \\ \cline{2-7} Method & P@20 & MRR@20 & P@20 & MRR@20 & P@20 & MRR@20 \\ \hline POP & 1.18 & 0.28 & 2.00 & 0.90 & 6.71 & 0.58 \\ Item-KNN & 35.75 & 11.57 & 9.15 & 3.31 & 51.60 & 21.81 \\ FPMC & 22.14 & 6.66 & 16.06 & 7.32 & 45.62 & 15.01 \\ GRU4Rec & 30.79 & 8.22 & 10.93 & 5.89 & 60.64 & 22.89 \\ NARM & 48.32 & 16.00 & 23.30 & 10.70 & 68.32 & 28.63 \\ STAMP & 46.62 & 15.13 & 26.47 & 13.36 & 68.74 & 29.67 \\ SR-GNN & 50.73 & 17.59 & 27.57 & 13.72 & 70.57 & 30.94 \\ LESSR & 51.71 & 18.15 & 23.53 & 9.56 & 70.05 & 30.59 \\ SGNN-HN & 55.67 & 19.45 & – & – & 72.06 & 32.61 \\ \hline CSRM & 48.49 & 17.13 & 29.46 & 13.96 & – & – \\ CoSAN & 51.97 & 17.92 & 32.68 & 14.09 & – & – \\ GCE-GNN & 54.22 & 19.04 & 33.42 & 15.42 & 70.91 & 30.63 \\ \(S^{2}\)-DHCN & 53.18 & 18.44 & 31.42 & 15.05 & – & – \\ MTD & 51.82 & 17.26 & 29.12 & 13.73 & 71.88 & 31.32 \\ COTREC & 54.18 & 19.07 & 36.35 & 18.04 & – & – \\ \hline AutoGR & 54.56 & 19.20 & 33.71 & 15.87 & 71.77 & 31.02 \\ MGIR & – & – & 36.41 & 17.42 & – & – \\ \hline CARES\_ns & \(55.29\) & 21.04 & 38.17 & 17.79 & 71.82 & 33.05 \\ CARES & \(\mathbf{56.49}\) & \(\mathbf{23.22}\) & \(\mathbf{38.77}\) & \(\mathbf{18.37}\) & \(\mathbf{72.21}\) & \(\mathbf{34.40}\) \\ Improv. & \(1.47\%\) & 19.30\% & 6.48\% & 1.82\% & 0.20\% & \(5.48\%\) \\ \hline \hline \end{tabular}
\end{table}
Table II: Overall performance comparison on three datasets. For fairness, we directly report the results of baseline methods from their original papers, where “-” indicates the absence of corresponding results in the original papers.
distribution as labels and thus empowers the proposed model to alleviate the overfitting problem. Comprehensive experiments demonstrate that our proposed model can make full use of contexts in sessions, especially those cross-session ones, thus achieving state-of-the-art performance over three real-world datasets consistently.
