\chapter{Supplementary: Sequential Memory with Temporal Predictive Coding}\label{app:seqmem}

\section{Algorithms}
In Algorithm~\ref{alg:tpc1} we present the memorizing and recalling procedures of the single-layer tPC. 
In Algorithm~\ref{alg:tpc2} we present the memorizing and recalling procedures of the 2-layer tPC.


\begin{algorithm}[ht]
\caption{Memorizing and recalling with single-layer tPC}\label{alg:tpc1}
\begin{multicols}{2}
\begin{algorithmic}[1]
    \LComment{Training/memorization}
    \While{$\bfW$ not converged}
    % \State randomly initialize ${\mathbf{x}}^{0}$
    \For{$k=1,...,K$}
        \State Input: $\mathbf{x}_k$, $\mathbf{x}_{k-1}$
        \State Update $\bfW$ (Eqs.~\ref{eq:weight-update-single-layer})
    \EndFor
    \EndWhile
    \State
    \LComment{Cued recall}
    % \State randomly initialize ${\mathbf{x}}^{0}$
    \For{$\mu=1,...,P$}
        \State Input: ${\mathbf{x}}_{k-1}$ (online) or $\hat{\mathbf{x}}_{k-1}$ (offline)
        \While{$\hat{\mathbf{x}}_k$ not converged} 
            \State Infer ${\hat{\mathbf{x}}}_k$ (Eq.~\ref{eq:single-layer-recall})
        \EndWhile
        \State $R^{tPC}(\mathbf{q}) \leftarrow \hat{\mathbf{x}}_k$
    \EndFor
\end{algorithmic}
\end{multicols}
\end{algorithm}

\begin{algorithm}[t]
\caption{Memorizing and recalling with 2-layer tPC}\label{alg:tpc2}
\begin{multicols}{2}
\begin{algorithmic}[1]
    \LComment{Training/memorization}
    \While{$\bfW$, $\bfF$ not converged}
    \State randomly initialize $\hat{\mathbf{z}}^{0}$
    \For{$k=1,...,K$}
        \State Input: $\mathbf{x}_k$, $\hat{\mathbf{z}}_{k-1}$
        \While{$\mathbf{z}_k$ not converged} 
            \State Infer ${\mathbf{z}}_k$ (Eq.~\ref{eq:infer-z})
        \EndWhile
        \State Update $\bfW$, $\bfF$ (Eqs.~\ref{eq:learn-WhWf})
        \State $\hat{\mathbf{z}}_k \leftarrow \mathbf{z}_k$
    \EndFor
    \EndWhile
    \LComment{Cued recall}
    \State randomly initialize $\hat{\mathbf{z}}^{0}$
    \For{$k=1,...,K$}
        \State Input: $\hat{\mathbf{z}}_{k-1}$
        \While{$\mathbf{z}_k$ and $\hat{\mathbf{x}}_k$ not converged} 
            \State Infer ${\mathbf{z}}_k$, ${\hat{\mathbf{x}}}_k$ (Eqs.~\ref{eq:infer-z},\ref{eq:infer-x})
        \EndWhile
        \State $\hat{\mathbf{z}}_k \leftarrow \mathbf{z}_k$
        \State $R^{tPC}(\mathbf{q}) \leftarrow \hat{\mathbf{x}}_k$
    \EndFor
\end{algorithmic}
\end{multicols}
\end{algorithm} 

It is worth noting that, although in both algorithms we used iterative inference (line $14$-$16$ in Algorithm \ref{alg:tpc1} and line $17$-$19$ in Algorithm \ref{alg:tpc2}), these inferential dynamics can be replaced by forward passes in simulation. For the single-layer model the retrieval $R^{tPC}(\mathbf{q})$ can be directly obtained by $R^{tPC}(\mathbf{q})=\bfW f(\mathbf{q})$ with the learned $\bfW$, while for the 2-layer model the retrieval ${\mathbf{x}}_{k-1}$ can be obtained by first forward passing the latent $\hat{\mathbf{z}}_k=\bfW f(\hat{\mathbf{z}}_{k-1})$ and then set the retrieval as $R^{tPC}(\mathbf{q})=\bfF f(\hat{\mathbf{z}}_k)$. Effectively, setting the retrieval directly by forward passes will result in the same retrieval as performing the inferential iterations as they are the fixed points of the inferential dynamics. However, obtaining the retrievals via iterative methods allows us to implement the computations in the plausible neural circuits in Fig.~\ref{fig:implementation} whereas forward passes cannot. 
% Code will be available upon acceptance.

\section{Proof of Property \ref{theorem1}}

Here we present the proof for Property \ref{theorem1} in the main text, that the single-layer tPC can be viewed as a ``whitened'' version of the AHN. Without loss of generality, assume a sequence of zero-mean, real-valued patterns $\mathbf{x}_k$, $k=1,...,K$ is given to the model to memorize. The step-wise objective with an identity non-linearity $f(\cdot)$ for single-layer tPC is:
\begin{equation}\label{eq:single-layer-energy-identity}
    \mathcal{F}_k (\mathbf{W}) = \Vert \mathbf{x}_k - \mathbf{W} \mathbf{x}_{k-1} \Vert_2^2
\end{equation}

The weight $\mathbf{W}$ is then updated at each time-step once, and the whole sequence is presented for multiple iterations until $\mathbf{W}$ converges. We now consider the case where we presented the sequence at once, i.e., the model now minimizes the following objective at each learning iteration:
\begin{equation}\label{eq:batch-energy}
    \mathcal{F} = \sum_{k=1}^K \Vert \mathbf{x}_{k+1} - \mathbf{W} \mathbf{x}_k \Vert_2^2
\end{equation}
which can be viewed as a ``batched'' version of Eq.~\ref{eq:single-layer-energy-identity}. Since the objective is now convex (with identity $f(\cdot)$), the fixed point obtained by these two objectives will be the same. The gradient descent update of $\mathbf{W}$ on $\mathcal{F}$ is then:
\begin{equation}
    \Delta \mathbf{W} = -\frac{\partial \mathcal{F}}{\partial \mathbf{W}} = \sum_{k=1}^K \mathbf{x}_{k+1}(\mathbf{x}_k)^\top - \mathbf{W} \sum_{k=1}^K\mathbf{x}_k(\mathbf{x}_k)^\top
\end{equation}

By setting $\Delta \mathbf{W}$ to $0$, we could obtain the optimal $\mathbf{W}$, which we call $\mathbf{W}^{tPC}$:
\begin{equation}\label{eq:final-weight}
    \mathbf{W}^{tPC} = \left(\sum_{k=1}^K \mathbf{x}_{k+1}(\mathbf{x}_k)^{\top}\right)\left(\sum_{k=1}^K \mathbf{x}_k(\mathbf{x}_k)^{\top}\right)^{-1} = \sum_{k=1}^K \mathbf{x}_{k+1}(\mathbf{x}_k)^{\top}\left(\mathbf{X}^{\top}\mathbf{X}\right)^{-1}
\end{equation}
where we define $\mathbf{X}=\big[\begin{matrix} \mathbf{x}^{1},...,\mathbf{x}^{K}\end{matrix}\big]^\top$, the $K\times N$ data matrix. Recall that when presented with a query $\mathbf{q}$, the single-layer tPC updates its value nodes to minimize:
\begin{equation}
    \mathcal{F}_k (\hat{\mathbf{x}}_k) = \Vert \hat{\mathbf{x}}_k - \mathbf{W}^{tPC} \mathbf{q} \Vert_2^2
\end{equation}
which will converge to $\mathbf{R}^{tPC}(\mathbf{q}) = \mathbf{W}^{tPC}\mathbf{q}$ due to convexity. We can now substitute $\mathbf{W}^{tPC}$ with the expression from Eq.~\ref{eq:final-weight} to obtain the retrieval:
\begin{equation}
    \mathbf{R}^{tPC}(\mathbf{q}) = \mathbf{W}^{tPC}\mathbf{q} = \sum_{k=1}^K \mathbf{x}_{k+1}(\mathbf{x}_k)^{\top}\left(\mathbf{X}^{\top}\mathbf{X}\right)^{-1}\mathbf{q}
\end{equation}

It can be immediately seen that the retrieval function of tPC is a special case of the UHN framework, where the similarity function is defined as $\simi(\mathbf{x}_k,\mathbf{q})=(\mathbf{x}_k)^{\top}\left(\mathbf{X}^{\top}\mathbf{X}\right)^{-1}\mathbf{q}$ and the separation function is identity. Notice that since we assumed a zero-mean sequence (sequences with non-zero mean can be accounted for with a bias term in the objective function Eq.~\ref{eq:batch-energy}), the term $\mathbf{X}^{\top}\mathbf{X}$ is exactly the covariance matrix of the sequence. Defining it as $\mathbf{\Sigma}$, the retrieval can be written as:
\begin{equation}\label{eq:recall-Sigma}
    \mathbf{R}^{tPC}(\mathbf{q}) = \mathbf{W}^{tPC}\mathbf{q} = \sum_{k=1}^K \mathbf{x}_{k+1}(\mathbf{x}_k)^{\top}\mathbf{\Sigma}^{-1}\mathbf{q}
\end{equation}

Assume a positive definite covariance $\mathbf{\Sigma}$, it is possible to decompose $\mathbf{\Sigma}^{-1}$ as follows:
\begin{equation}
    \mathbf{\Sigma}^{-1} = \mathbf{M}^\top \mathbf{M}
\end{equation}

The matrix $\mathbf{M}$ is called the whitening matrix, which does not hold a unique value, e.g., $\mathbf{M}=\mathbf{\Sigma}^{-\frac{1}{2}}$ or $\mathbf{M}=\mathbf{L}^\top$ where $\mathbf{L}$ is the Cholesky decomposition of $\mathbf{\Sigma}^{-1}$ \citep{kessy2018optimal}. Here, we are agnostic about its exact value. When applied to the data sequence, it whitens the data such that (i.e., Eq.16 in the main text):
\begin{equation}
    \langle \mathbf{M}\mathbf{x}_k (\mathbf{M}\mathbf{x}_k)^\top\rangle_k = \mathbf{I}_N
\end{equation}

Therefore, the retrieval of our single-layer tPC with an identity $f(\cdot)$ can be written as:
\begin{equation}
        \mathbf{R}^{tPC}(\mathbf{q}) = \sum_{k=1}^K \mathbf{x}_{k+1}(\mathbf{M}\mathbf{x}_k)^\top \mathbf{M}\mathbf{q}
\end{equation}
by decomposing $\mathbf{\Sigma}^{-1}$ in Eq.~\ref{eq:recall-Sigma} into $\mathbf{M}^\top \mathbf{M}$, which is Eq.15 in the main text and concludes the proof.

\section{Energy comparison between tPC and AHN}
The whitening effect of single-layer tPC can be traced back to it energy function as compared to that of AHN. For simplicity, we limit our discussion here to the minimal case where a single pair of consecutive patterns, $\bfx_k$ and $\bfx_{k-1}$ are being memorized, although it can be easily generalized to arbitrarily long sequences. Recall that, in this case, the energy function of a single-layer tPC is:
\begin{equation}
\begin{split}
    \mathcal{F}^{tPC}(\bfW) &= \Vert \bfx_k - \bfW\bfx_{k-1}\Vert_2^2 \\&= \bfx_k^{\top}\bfx_k + \bfx_{k-1}^{\top}\bfW^{\top}\bfW\bfx_{k-1} - 2\bfx_k^{\top}\bfW\bfx_{k-1}
\end{split}
\end{equation}

On the other hand, the energy function of a vanilla AHN is \citep{hopfield1982neural,sompolinsky1986temporal}:
\begin{equation}
    \mathcal{F}^{AHN}(\bfW) = -\bfx_k^{\top}\bfW\bfx_{k-1}
\end{equation}

It can be seen that both energy functions have the quadratic term $\bfx_k^{\top}\bfW\bfx_{k-1}$, which tells both models to learn a \textit{cross}-covariance matrix between the two patterns ($\partial \bfx_k^{\top}\bfW\bfx_{k-1} / \partial \bfW = \bfx_k \bfx_{k-1}^{\top}$). However, tPC also has an extra term $\bfx_{k-1}^{\top}\bfW^{\top}\bfW\bfx_{k-1}$. This term essentially regularizes the energy function, so that tPC also considers the \textit{auto}-covariance while minimizing this energy ($\partial \bfx_{k-1}^{\top}\bfW^{\top}\bfW\bfx_{k-1} / \partial \bfW = 2\bfW (\bfx_{k-1}\bfx_{k-1}^{\top})$), which is the reason for the whitening effect in single-layer tPC.

\section{Generation of binary patterns}
For the experimental results in Fig.~\ref{fig:binary}A and B, the correlated binary patterns are generated following the approach mentioned in \cite{bogacz2003comparison}. For a particular pattern dimension $N$, a template $\mathbf{x}^{temp}\in\{-1,1\}^N$ is first generated. Then, for each of the $k=1,...,K$ patterns, the $i$th entry of $\mathbf{x}_k$ is equal to the $i$th entry of $\mathbf{x}^{temp}$ with probability $0.5+0.5b$ where $b$ is the parameter controlling bias. We also invert the sign of each pattern by chance to keep the level of activity constant for each neuron. This whole process is then repeated for multiple trials to average out randomness. The capacity $P_{max}$ is calculated as the maximum $K$ such that the percentage of erroneous entries across these trials is below $0.01$. It can be shown that the correlation between two features of the generated patterns $r(\mathbf{x}_{k,i}, \mathbf{x}_{k,j})$ is $-b^2$ or $b^2$, by using the identity $r(\mathbf{x}_{k,i}, \mathbf{x}_{k,j}) = \langle \mathbf{x}_{k,i} \mathbf{x}_{k,j} \rangle_k - \langle \mathbf{x}_{k,i} \rangle_k \langle \mathbf{x}_{k,j} \rangle_k$.

\section{Additional results with CIFAR10 and MovingMNIST}

\begin{figure}[t]
    \centering
    \includegraphics[width=\linewidth]{figs/chap5/SM-cifar10examples.pdf}
    \caption{\textbf{Visual results with CIFAR10 sequences.}}
    \label{fig:SM-cifar}
    % \vspace*{-2.5ex}
\end{figure}

\begin{figure}[h]
    \centering
    \includegraphics[width=\linewidth]{figs/chap5/SM-online-recalls.pdf}
    % \vspace*{-5ex}
    \caption{\textbf{Online recalls with} A: MovingMNIST; B: CIFAR10; C: UCF101.}
    \label{fig:SM-online}
    % \vspace*{-2.5ex}
\end{figure}


\begin{figure}[t]
    \centering
    \includegraphics[width=\linewidth]{figs/chap5/SM-numerical.pdf}
    \caption{\textbf{Numerical results with CIFAR10 and MovingMNIST.} A: \emph{Online} recall MSE of random CIFAR10 sequences; B: \emph{Offline} recall MSE of random CIFAR10 sequences, with a $\texttt{tanh}$ nonlinearity; C: \emph{Offline} recall MSE of movingMNIST dataset, with a $\texttt{tanh}$ nonlinearity.}
    \label{fig:SM-numerical}
    % \vspace*{-2.5ex}
\end{figure}

In Fig.~\ref{fig:SM-cifar} we present additional visual results when MCAHN and single-layer tPC are trained to memorize a random CIFAR10 sequence of $32$ images and are queried both online and offline during recall. It can be seen that MCAHN, like what we have shown in the main text, recalls memories preceded with images with large pixel values (images are presented to the models as $32\times 32\times 3 = 3072$-dimensional vectors where $3$ represents the RGB channels) in both online and offline recall regimes, whereas tPC does not suffer from this problem because of the whitening procedure. However, it can be seen that the recall by tPC will gradually become more blurry and noisier when queried offline because the recall errors will accumulate temporally. 

These observations are consistent with our numerical results shown in Fig.~\ref{fig:SM-numerical}. In Fig.~\ref{fig:SM-numerical}A we show the online recall MSE of CIFAR10 sequences by single-layer tPC (with linear $f(\cdot)$) and MCAHN. MCAHN has a much larger MSE than that of the tPC because of the entirely wrong recalls. In offline recalls in Fig.~\ref{fig:SM-numerical}B however, tPC will have exploding MSE as soon as $P$ reaches $64$ because of the accumulating recall errors. It is worth mentioning that for these experiments, we used a $\texttt{tanh}$ nonlinearity, as the recall error will accumulate to infinity with an identity $f(\cdot)$. This is the only case where identity and $\texttt{tanh}$ are different in our experiments. 

In Fig.~\ref{fig:SM-online} we also present the online recall results of the models in MovingMNIST, CIFAR10 and UCF101. The results with CIFAR10 are consistent with the discussions above, and the results with UCF101 is clearer with online queries than those with offline queries. Moreover, although in MovingMNIST MCAHN still suffers from the wrong attractor problem (red triangle in Fig.~\ref{fig:SM-online}A), the online query can prevent it from staying in the wrong attractor. This is consistent with our numerical obervation in Fig.~\ref{fig:SM-numerical}C, where the performance of MCAHN in online recall of MovingMNIST is better than that of the tPC models.

\section{A natural example of aliased sequences from UCF101}

\begin{figure}[t]
    \centering
    \includegraphics[width=0.8\linewidth]{figs/chap5/aliased-appendix.pdf}
    % \vspace*{-5ex}
    \caption{\textbf{A natural aliased example from the UCF101 dataset, showing a human doing push-ups.}}
    \label{fig:push-up}
\end{figure}

In Fig~\ref{fig:push-up} we show a natural example of aliased sequences where a movie of a human doing push-ups is memorized and recalled by the model. The frames at the second and the fifth steps are almost identical, leading to inaccurate predictions of the single-layer models (including MCAHN and single-layer tPC) at the next time steps. On the other hand, the 2-layer tPC performs well and produces sharp and correct recalls.