arXiv:2609.37791cs.AISep 2026

Manuscript Offprint

arXiv:2609.37791

A neural network that maintains and retrieves memories based on context

Hayoung Song, JeongJun Park, Qihong Lu, Giacomo Vedovati, Monica D. Rosenberg, Zachariah M. Reagh, ShiNung Ching

ABSTRACT

Every day, people continuously infer situational context and adjust the way they understand and remember the world. Context, signaled by the prefrontal cortex, is known to modulate working memory and episodic memory, but the algorithmic understanding of this modulation remains limited. Here, we train a recurrent neural network (RNN), augmented with an episodic memory buffer, to infer context using Bayesian inference as it continuously makes predictions of upcoming scenes while watching naturalistic movies. When the inferred context modulates the RNN's recurrent connectivity (the basis of working memory) in a low-rank manner, the model's activity patterns best match neural responses in human participants who watched the same movies during fMRI. Context also modulates episodic memory retrieval, such that the model retrieves memories based on not only content similarity but also context similarity. This is implemented as a key-value system with self-attention, designed to additionally encode context and retrieve context-congruent memories. The resulting model not only better resembles human brain representations but also learns to retrieve memories like humans much faster than a model without context modulation. Together, our findings suggest a computational mechanism by which context modulates information maintenance and long-term memory retrieval in naturalistic environments.

1 INTRODUCTION

A hallmark of human intelligence is the ability to infer one's situational context and adjust thought and behavior accordingly. Context shapes every facet of cognition, including both short- and long-term memory. People maintain task-relevant information while ignoring the rest (working memory, WM; Baddeley, 1992) and selectively encode and retrieve memories of past experiences that are congruent with their goals and situations (episodic memory, EM; Tulving, 2002). At the hub of this contextual modulation is the prefrontal cortex (PFC), which represents context through distinct patterns of neural activity (Miller & Cohen, 2001) and has bidirectional connections widely across the brain.

Neurobiological work has characterized how the PFC represents context and uses this to modulate WM and EM. The lateral PFC represents context without interfering with its representation of task variables held in WM. This affords a compositional mechanism in which a shared task coding can be flexibly gated by context (Mante et al., 2013; Rigotti et al., 2013; Aoi et al., 2020; Bernardi et al., 2020; Badre et al., 2021; Flesch et al., 2022; Park et al., 2025; Tafazoli et al., 2026). The medial PFC (mPFC) also represents context and is bidirectionally connected with the ventral hippocampus. The mPFC–hippocampal interaction is known to support the retrieval of EMs congruent with context (or “schema”) while suppressing incongruent ones (van Kesteren et al., 2012; Eichenbaum, 2017; Baldassano et al., 2018; Morici et al., 2022; Varga et al., 2024; García et al., 2025; de Sousa et al., 2026).

Despite the understanding of PFC circuitry and function, the algorithms by which context modulates WM and EM remain unclear. Thus far, the two have been modeled in separate lines of research. In the WM literature, recurrent neural networks (RNNs)—whose recurrent dynamics implement WM—have been trained on a range of context-dependent task paradigms, and their dynamical solutions have been treated as candidate mechanisms of contextual modulation (Mante et al., 2013; Murray et al., 2017; Yang et al., 2019; Soldado-Magraner et al., 2024; Pagan et al., 2025). In many of these studies, context is given as input to the model—which is plausible in tasks where animals receive context as an explicit cue (e.g., the color of a fixation cross; Mante et al., 2013; Yang et al., 2019). In the EM literature, biologically plausible models of the cortico-hippocampal circuitry (Frank et al., 2003; Schapiro et al., 2017; Pilly et al., 2018; Hwu & Krichmar, 2020) and normative memory-augmented RNNs (Pritzel et al., 2017; Ritter et al., 2018; Lu et al., 2024; Li et al., 2026) have begun to simulate context-dependent encoding and retrieval. Thus far, no model has inferred context to jointly modulate WM and EM within a single framework.

In this study, a model is tasked to “watch” television episodes. While naturalistic paradigms have intrinsic confounds and murky objective functions, features such as diverse and multimodal stimuli, long-range dependencies, and structured storylines challenge the model with real-world complexity that leaves room for higher-order cognition to emerge. We directly compare the model to fMRI and behavioral data collected as human participants watched the same television episode (Song et al., 2026a), which serve as ground truth for how the model should behave and represent events.

We introduce a normative system in which context modulates both the processing and maintenance of information as well as encoding and retrieval of memories. We build on the EM-RNN, an RNN augmented with a key-value EM buffer (Song et al., 2026b), by adding a PFC-like algorithm. This is implemented using Bayesian inference, where the posterior serves as the context signal (Gershman et al., 2014; Franklin et al., 2020). We first ask at which stage of information processing context would modulate the RNN. Rather than input or output stages, context modulates the RNN's WM and does so by low-rank gating of a shared recurrent connectivity, consistent with an existing theory of PFC function (Mastrogiuseppe & Ostojic, 2018; Dubreuil et al., 2022; Boboeva et al., 2026). We found that this low-rank modulation model generates representational patterns most similar to the human brain. We then ask how context modulates EM. The key-value system in the EM-RNN, motivated by hippocampal function, stores memory in two distinct representations, keys (memory index) and values (memory content). The model now additionally encodes context (i.e., Bayesian posterior) such that it retrieves memories that are under similar context as the current one, motivated by the mPFC–hippocampal interaction (Eichenbaum, 2017). We found that this model learns to retrieve memories like human participants much faster than a baseline model, but only when retrieval is selective. Together, we present the context-modulated EM-RNN as a candidate mechanism of how the PFC modulates information processing and memory retrieval.

References (79)