\section{Properties of the Basic Model}
\label{sec:props}

It is amazing how much the semantics of \textsection\ref{sec:model} ``gets
right'' out of the box, including value range analysis, internal reads, and
SC access, all of which can be complex in other models.  In this section, we
walk through several litmus tests, valid rewrites and invalid rewrites.  The
examples show that \ref{5a}--\ref{5d} and \ref{rf3}--\ref{rf4} are
understandable as \emph{general principles}.  The interaction of these
principles is limited to a single, global, pomset order.  We discuss tweaks
to the semantics in \textsection\ref{sec:refine}.

\subsection{Litmus Tests}
\label{sec:litmus}

\citet{PughWebsite} developed a set of litmus tests for the java memory
model.  Our model gives the expected result for all but cases 16, 19 and 20
(unrolling loops): we discuss \ref{TC16} below; \textsc{tc19} and
\textsc{tc20} involve a thread join operation, which is not expressible in
our language.  Our model also agrees with the \oota{} examples of
\citet[\textsection 4]{DBLP:conf/esop/BattyMNPS15} and the ``surprising and
controversial behaviors'' of \citet[\textsection
8]{Manson:2005:JMM:1047659.1040336}.  %See \cite{DBLP:conf/esop/PaviottiCPWOB20} for an exhaustive list of litmus tests.

\myparagraph{Buffering}

Consider the \emph{store buffering} and \emph{load buffering} litmus \labeltext[SB]{tests}{SB}:
\begin{align*}
  \taglabel{SB/LB}
  \begin{gathered}
    \PW{x}{0}\SEMI
    \PW{y}{0}\SEMI
    (
    \PW{x}{1}\SEMI\PR{y}{\aReg}
    \PAR
    \PW{y}{1}\SEMI \PR{x}{\aReg})
    \\[-1.5ex]
    \hbox{\begin{tikzinline}[node distance=.9em]
        \event{wx0}{\DW{x}{0}}{}
        \event{wy0}{\DW{y}{0}}{right=of wx0}
        \event{wx}{\DW{x}{1}}{right=3em of wy0}
        \event{ry}{\DR{y}{0}}{right=of wx}
        \event{wy}{\DW{y}{1}}{right=3em of ry}
        \event{rx}{\DR{x}{0}}{right=of wy}
        \rf[out=15,in=165]{wy0}{ry}
        \rf[out=10,in=170]{wx0}{rx}
        \wk{ry}{wy}
        \wk[out=-165,in=-15]{rx}{wx}
      \end{tikzinline}}
  \end{gathered}
  &&
  \begin{gathered}
    \PR{y}{\aReg}\SEMI \PW{x}{1}
    \PAR
    \PR{x}{\aReg}\SEMI \PW{y}{1}
    \\
    \hbox{\begin{tikzinline}[node distance=.9em]
        \event{ry}{\DR{y}{1}}{}
        \event{wx}{\DW{x}{1}}{right=of ry}
        \event{rx}{\DR{x}{1}}{right=3em of wx}
        \event{wy}{\DW{y}{1}}{right=of rx}
        \rf{wx}{rx}
        \rf[out=-165,in=-15]{wy}{ry}
      \end{tikzinline}}
  \end{gathered}
\end{align*}
Because there are no intra-thread dependencies, the desired outcomes are allowed, as shown.

\myparagraph{Publication}
\ref{rf3}--\ref{rf4} and \ref{5b}--\ref{5c} ensure correct publication,
prohibiting stale reads:
\begin{gather}
  \taglabel{Pub1}
  \begin{gathered}
    \PW{x}{0}\SEMI %\PW{y}{0}\SEMI
    \PW{x}{1}\SEMI \PW[\mRA]{y}{1} \PAR \PR[\mRA]{y}{r}\SEMI \PR{x}{s}
    \\[-.4ex]
    \nonumber
    \hbox{\begin{tikzinline}[node distance=1.5em]
        \event{wx0}{\DW{x}{0}}{}
        \event{wx1}{\DW{x}{1}}{right=of wx0}
        \event{wy1}{\DWRel{y}{1}}{right=of wx1}
        \event{ry1}{\DRAcq{y}{1}}{right=2.5em of wy1}
        \event{rx0}{\DR{x}{0}}{right=of ry1}
        \sync{wx1}{wy1}
        \sync{ry1}{rx0}
        \rf{wy1}{ry1}
        \wk{wx0}{wx1}
      \end{tikzinline}}
  \end{gathered}
\end{gather}
This pomset is disallowed, since $(\DR x0)$ fails to satisfy \ref{rf4}:
$(\DW x0) \lt (\DW x1) \lt (\DR x0)$.  Attempting to satisfy this
requirement, one might order $(\DR x0)$ before $(\DW x1)$, but this would
create a cycle.

\myparagraph{Coherence}

Our model of coherence does not correspond to either Java or C11.  We have
chosen the model to validate \ref{CSE} (unlike C11 relaxed atomics) and the
local \drfsc{} theorem (unlike Java).

Since reads are not ordered by \ref{5b},
we {allow} the following unintuitive behavior. C11 includes read-read
coherence between relaxed atomics in order to forbid this:
\begin{gather*}
  \taglabel{Co2}
  \begin{gathered}
    \PW{x}{1}\SEMI \PW{x}{2}
    \PAR
    \PW{y}{x} \SEMI \PW{z}{x}
    \\[-1ex]
    \hbox{\begin{tikzinline}[node distance=1.5em]
        \event{a}{\DW{x}{1}}{}
        \event{b}{\DW{x}{2}}{right=of a}
        \wk{a}{b}
        \event{c}{\DR{x}{2}}{right=3em of b}
        \event{d}{\DW{y}{2}}{right=of c}
        \po{c}{d}
        \event{e}{\DR{x}{1}}{right=of d}
        \event{f}{\DW{z}{1}}{right=of e}
        \po{e}{f}
        \rf{b}{c}
        \rf[out=10,in=170]{a}{e}
      \end{tikzinline}}
  \end{gathered}
\end{gather*}
Here, the reader sees $2$ then $1$, although they are written in the reverse
order.
This behavior is allowed by Java in order to validate \ref{CSE} without requiring
aliasing analysis.

However, our model is more coherent than Java, which permits the following:
\begin{gather*}
  \taglabel{TC16}
  \begin{gathered}
    \PR{x}{r}\SEMI \PW{x}{1}
    \PAR
    \PR{x}{s}\SEMI \PW{x}{2}
    \\[-1ex]
    \hbox{\begin{tikzinline}[node distance=1.5em]
        \event{a1}{\DR{x}{2}}{}
        \event{a2}{\DW{x}{1}}{right=of a1}
        \wk{a1}{a2}
        \event{b1}{\DR{x}{1}}{right=3em of a2}
        \event{b2}{\DW{x}{2}}{right=of b1}
        \wk{b1}{b2}
        \rf{a2}{b1}
        \rf[out=-165,in=-15]{b2}{a1}
      \end{tikzinline}}
  \end{gathered}
\end{gather*}
We also forbid the \labeltext{following}{page:coherence2}, which Java allows:
\begin{gather*}
  \taglabel{Co3}
  \begin{gathered}
    \PW{x}{1}\SEMI \PW[\mRA]{y}{1}
    \PAR
    \PW{x}{2}\SEMI \PW[\mRA]{z}{1}
    \PAR
    \PR[\mRA]{z}{r} \SEMI
    \PR[\mRA]{y}{r} \SEMI
    \PR{x}{r} \SEMI
    \PR{x}{r}
    \\[-1ex]
    \hbox{\begin{tikzinline}[node distance=1.5em]
        \event{a1}{\DW{x}{1}}{}
        \event{a2}{\DW[\mRA]{y}{1}}{right=of a1}
        \sync{a1}{a2}
        \event{b1}{\DW{x}{2}}{right=3em of a2}
        \event{b2}{\DW[\mRA]{\,z}{1}}{right=of b1}
        \sync{b1}{b2}
        \event{c1}{\DR[\mRA]{\,z}{1}}{right=3em of b2}
        \event{c2}{\DR[\mRA]{y}{1}}{right=of c1}
        \event{c3}{\DR{x}{2}}{right=of c2}
        \event{c4}{\DR{x}{1}}{right=of c3}
        \sync{c1}{c2}
        \sync{c2}{c3}
        \sync[out=20,in=160]{c2}{c4}
        \rf[out=8,in=172]{a2}{c2}
        \rf{b2}{c1}
        \wk[out=19,in=161]{a1}{b1}
        \wk[out=-172,in=-8]{c4}{b1}
      \end{tikzinline}}
  \end{gathered}
\end{gather*}
The order from $(\DR{x}{1})$ to $(\DW{x}{2})$ is required to fulfill
$(\DR{x}{1})$. The outcome is disallowed due to the cycle. %by fulfillment.
If this outcome were allowed, then racing writes would be visible, even after
a full synchronization; this would invalidate local reasoning about data
races (\textsection\ref{sec:sc}).

\myparagraph{MCA} We present a few examples that are hallmarks of \mca{}
architectures.
\begin{scope}
  \allowdisplaybreaks
  \begin{gather*}
    \taglabel{MCA1}
    \begin{gathered}
      \IF{z}\THEN \PW{x}{0} \FI \SEMI \PW{x}{1}
      {\PAR}
      \IF{x}\THEN \PW{y}{0} \FI \SEMI \PW{y}{1}
      {\PAR}
      \IF{y}\THEN \PW{z}{0} \FI \SEMI \PW{z}{1}
      \\[-1ex]
      \hbox{\begin{tikzinline}[node distance=1.5em]
          \event{a1}{\DR{z}{1}}{}
          \event{a2}{\DW{x}{0}}{right=of a1}
          \po{a1}{a2}
          \event{a3}{\DW{x}{1}}{right=of a2}
          \wk{a2}{a3}
          \event{b1}{\DR{x}{1}}{right=3em of a3}
          \event{b2}{\DW{y}{0}}{right=of b1}
          \po{b1}{b2}
          \event{b3}{\DW{y}{1}}{right=of b2}
          \wk{b2}{b3}
          \event{c1}{\DR{y}{1}}{right=3em of b3}
          \event{c2}{\DW{z}{0}}{right=of c1}
          \po{c1}{c2}
          \event{c3}{\DW{z}{1}}{right=of c2}
          \wk{c2}{c3}
          \rf{a3}{b1}
          \rf{b3}{c1}
          \rf[out=173,in=7]{c3}{a1}
        \end{tikzinline}}
    \end{gathered}
    \\[1ex]
    \taglabel{MCA2}
    \begin{gathered}
      \PW{x}{0}\SEMI \PW{x}{1}
      \PAR
      \PW{y}{x}
      \PAR
      \PR[\mRA]{y}{r} \SEMI \PR{x}{s}
      \\[-1ex]
      \hbox{\begin{tikzinline}[node distance=1.5em]
          \event{wx0}{\DW{x}{0}}{}
          \event{wx1}{\DW{x}{1}}{right=of wx0}
          \wk{wx0}{wx1}
          \event{rx1}{\DR{x}{1}}{right=3em of wx1}
          \event{wy1}{\DW{y}{1}}{right=of rx1}
          \po{rx1}{wy1}
          \event{ry1}{\DRAcq{y}{1}}{right=3em of wy1}
          \event{rx0}{\DR{x}{0}}{right=of ry1}
          \rf{wx1}{rx1}
          \rf{wy1}{ry1}
          \sync{ry1}{rx0}
          \wk[out=170,in=10]{rx0}{wx1}
        \end{tikzinline}}
    \end{gathered}
  \end{gather*}
\end{scope}
These candidate executions are invalid, due to cycles.
\ref{MCA1} is an example of \emph{write subsumption}
\cite[\textsection 3]{DBLP:journals/pacmpl/PulteFDFSS18}.
In \ref{MCA2}, $(\DW{x}{1})$ is delivered to the second thread, but not
the third; this is similar to the well know \iriw{} (Independent
Reads of Independent Writes) litmus test, which is also disallowed by \mca{}
architectures if the reads within each thread are ordered.

If $y^\mRA$ is changed to $y^\mRLX$ in \ref{MCA2}, then there would be no order
from $(\DR[\mRLX]{y}{1})$ to $(\DR{x}{0})$, and the execution would be
allowed.  Since read-read dependencies do not appear in pomset order, the
execution would still be allowed if a control or address dependency were to
be introduced between the reads. See example \ref{addr2}
(\textsection\ref{sec:limits}) for further discussion.

\myparagraph{Internal Reads and Value Range Analysis}
The JMM causality test cases \citep{PughWebsite} are justified via
compiler analysis, possibly in collusion with the scheduler: If every
observed value can be shown to satisfy a precondition, then the precondition
can be dropped.  For
example, \ref{TC1} determines that the following execution should be
allowed, as it is in our model:
\begin{gather*}
  \taglabel{TC1}
  \begin{gathered}
    \PW{x}{0} \SEMI
    (\PR{x}{r}\SEMI\IF{r\geq0}\THEN \PW{y}{1} \FI
    \PAR
    \PW{x}{y})
    \\[-1ex]
    \hbox{\begin{tikzinline}[node distance=1.5em]
        \event{wx0}{\DW{x}{0}}{}
        \event{rx1}{\DR{x}{1}}{right=3em of wx0}
        \event{wy1}{0\geq0\mid\DW{y}{1}}{right=of rx1}
        \event{ry1}{\DR{y}{1}}{right=3em of wy1}
        \event{wx1}{\DW{x}{1}}{right=of ry1}
        \po{ry1}{wx1}
        \rf[out=-168,in=-12]{wx1}{rx1}
        \rf{wy1}{ry1}
        \wk[out=10,in=170]{wx0}{wx1}
        \wk{wx0}{rx1}
      \end{tikzinline}}
  \end{gathered}
\end{gather*}
In this example, $(\DW{x}{0})$ ``fulfills'' the read of $x$ that is used in
the guard of the conditional.  This is possible when prefixing $(\DR{x}{1})$
performs the substitution $[x/r]$, but does not weaken the resulting
precondition $(x\geq0\mid\DW{y}{1})$.  Subsequently prefixing $(\DW{x}{0})$
substitutes $[0/x]$, resulting in the tautological precondition
$(0\geq0\mid\DW{y}{1})$.  Note that the execution does not have an action
$(\DR{x}{0})$.

Our semantics is robust with respect to
the introduction of concurrent writes, as in \ref{TC9}:
\begin{gather*}
  \taglabel{TC9}
  \begin{gathered}
    \PW{x}{0} \SEMI
    (\PR{x}{r}\SEMI\IF{r\geq0}\THEN \PW{y}{1} \FI
    \PAR
    \PW{x}{y}
    \PAR
    \PW{x}{-2})
    \\[-1ex]
    \hbox{\begin{tikzinline}[node distance=1.5em]
        \event{wx0}{\DW{x}{0}}{}
        \event{rx1}{\DR{x}{1}}{right=3em of wx0}
        \event{wy1}{0\geq0\mid\DW{y}{1}}{right=of rx1}
        \event{ry1}{\DR{y}{1}}{right=3em of wy1}
        \event{wx1}{\DW{x}{1}}{right=of ry1}
        \event{wx2}{\DW{x}{{-2}}}{right=3em of wx1}
        \po{ry1}{wx1}
        \rf[out=-168,in=-12]{wx1}{rx1}
        \rf{wy1}{ry1}
        \wk[out=10,in=170]{wx0}{wx1}
        \wk{wx0}{rx1}
        \wk{wx1}{wx2}
      \end{tikzinline}}
  \end{gathered}
\end{gather*}
The calculation of this pomset is unchanged from \ref{TC1}.

Examples such as \ref{TC9} present substantial difficulties in other models.
When thought of in terms of compiler optimizations, \ref{TC9} is justified by
global value analysis in collusion with the thread scheduler.  This execution
is disallowed by our event structure model \cite{DBLP:conf/lics/JeffreyR16}.
It is allowed by \citet{Pichon-Pharabod:2016:CSR:2837614.2837616}, at the
cost of introducing \emph{dead reads}.

The reasoning for \ref{TC2} is similar, but in this case no value is necessary to
satisfy the precondition:
\begin{gather*}
  \taglabel{TC2}
  \begin{gathered}
    \PR{x}{r}\SEMI
    \PR{x}{s}\SEMI
    \IF{r{=}s}\THEN \PW{y}{1}\FI
    \PAR
    \PW{x}{y}
    \\[-1ex]
    \nonumber
    \hbox{\begin{tikzinline}[node distance=1.5em]
        \event{a1}{\DR{x}{1}}{}
        \event{a2}{\DR{x}{1}}{right=of a1}
        \event{a3}{(x{=}x)\land(1{=}1)\mid\DW{y}{1}}{right=of a2}
        \event{b1}{\DR{y}{1}}{right=3em of a3}
        \event{b2}{\DW{x}{1}}{right=of b1}
        \rf{a3}{b1}
        \po{b1}{b2}
        \rf[out=169,in=11]{b2}{a2}
        \rf[out=169,in=11]{b2}{a1}
      \end{tikzinline}}
  \end{gathered}
\end{gather*}
Note that in
\begin{math}
  \sem{\PR{x}{s}\SEMI
    \IF{r{=}s}\THEN \PW{y}{1}\FI},
\end{math}
the precondition on $(\DW{y}{1})$ must imply $(r{=}x \land r{=}1)$.  The
first is imposed by \ref{5a}, the second by \ref{4c}, ensuring that the two
reads see the same value.

Using \armeight{} terminology, these executions involve \emph{internal
  reads}, which are fulfilled by a sequentially preceding write.  Read
actions always generate an event that must be fulfilled, and therefore cannot
be ignored, even if they are unused.  This fact prevents internal reads from
ignoring concurrent blocking writes.
\begin{gather*}
  \taglabel{Internal1}
  \begin{gathered}
    \PW{x}{1} \SEMI
    \PW[\mRA]{a}{1} \SEMI
    \IF{z^\mRA}\THEN  \PW{y}{x} \FI
    \PAR
    \IF{a^\mRA}\THEN  \PW{x}{2}\SEMI \PW[\mRA]{z}{1} \FI
    \\
    \hbox{\begin{tikzinline}[node distance=1.2em]
        \event{a1}{\DW{x}{1}}{}
        \event{a2}{\DWRel{a}{1}}{right=of a1}
        \sync{a1}{a2}
        \event{b3}{\DRAcq{a}{1}}{below right=0em and 3em of a2}
        \rf{a2}{b3}
        \event{b4}{\DW{x}{2}}{right=of b3}
        \sync{b3}{b4}
        \event{b5}{\DWRel{z}{1}}{right=of b4}
        \sync{b4}{b5}
        \event{a6}{\DRAcq{b}{1}}{above right=0em and 3em of b5}
        \rf{b5}{a6}
        \event{a7}{\DR{x}{1}}{right=of a6}
        \sync{a6}{a7}
        \event{a8}{1{=}1\mid\DW{y}{1}}{right=of a7}
        \graypo{a7}{a8}
        \sync[out=-18,in=-162]{a6}{a8}
      \end{tikzinline}}
  \end{gathered}
\end{gather*}
Here $(\DR{x}{1})$ violates \ref{rf4}.  The precondition $(1{=}1)$ is
imposed by \ref{4c}.  The pomset becomes inconsistent if we change
$(\DR{x}{1})$ to $(\DR{x}{2})$, since the precondition would change to $(2{=}1)$.

Internal reads are notoriously difficult to get right.  Consider \cite[Ex 3.6]{DBLP:journals/pacmpl/PodkopaevLV19}:
\begin{gather*}
  \taglabel{Internal2}
  \begin{gathered}
    \PR{x}{\aReg}\SEMI
    \PW[\mRA]{y}{1}\SEMI
    \PR{y}{\bReg}\SEMI
    \PW{z}{\bReg}
    \PAR
    \PW{x}{z}
    \\[-1ex]
    \nonumber
    \hbox{\begin{tikzinline}[node distance=1.5em]
        \event{a1}{\DR{x}{1}}{}
        \event{a2}{\DWRel{y}{1}}{right=of a1}
        \sync{a1}{a2}
        \event{a3}{\DR{y}{1}}{right=of a2}
        \event{a4}{1{=}1\mid\DW{z}{1}}{right=of a3}
        \rf{a2}{a3}
        \event{b1}{\DR{z}{1}}{right=3em of a4}
        \event{b2}{\DW{x}{1}}{right=of b1}
        \po{b1}{b2}
        \rf{a4}{b1}
        \rf[out=170,in=10]{b2}{a1}
      \end{tikzinline}}
  \end{gathered}
\end{gather*}
This behavior is allowed in our model, as it is in \armeight.
Note that $\sem{\PW{z}{\bReg}}$ includes $(\bReg{=}1\mid \DW{z}{1})$.
Prepending a read,
$\sem{\PR{y}{\bReg} \SEMI \PW{z}{\bReg}}$ may update the precondition to
$(y{=}1\mid \DW{z}{1})$ without introducing order.
Further prepending
$(\DWRel{y}{1})$ results in $(1{=}1\mid \DW{z}{1})$.

Our model drops order into actions that depend on a read that can be
fulfilled {internally}, by a prefixed write.  This is natural consequence of
substitution.  The \armeight{} model has to jump through some hoops to ensure
that internal reads are handled correctly.  \armeight{} takes the symmetric
approach: rather than dropping order \emph{out of} an internal read,
\armeight{} drops the order \emph{into} it.  This difference complicates the
proof of correctness for implementing our semantics on \armeight{}
(\textsection\ref{sec:arm}).

\myparagraph{SC access}
\ref{5d} ensures that program order between SC operations is always
preserved.  Combined with \ref{rf3}--\ref{rf4}, this is
sufficient to establish that programs with only SC access have only SC
executions; for example, the executions of \ref{SB/LB} are banned when the
all actions are $\mSC$.  It is also immediate that SC
actions can be totally ordered, using any linearization of pomset order.
Just as SC access in \armeight{} is simplified by \mca, it is simplified here
by the global pomset order.

SC access is not as strict as volatile access in Java.  For example, our
model allows the following, since there is no order from
$(\DW[\mSC]{x}{2})$ to $(\DW{y}{1})$---recall that SC writes are \emph{releases}.
\begin{gather*}
  \taglabel{SC1}
  \begin{gathered}
    \PR{y}{r}\SEMI \PW[\mSC]{x}{1}\SEMI \PR{x}{s}
    \PAR
    \PW[\mSC]{x}{2} \SEMI \PW{y}{1}
    \\[-1ex]
    \hbox{\begin{tikzinline}[node distance=1.5em]
        \event{a}{\DR{y}{1}}{}
        \event{b}{\DW[\mSC]{x}{1}}{right=of a}
        \sync{a}{b}
        \event{bb}{\DR{x}{2}}{right=of b}
        \wk{b}{bb}
        \event{d}{\DW[\mSC]{x}{2}}{right=3em of bb}
        \event{e}{\DW{y}{1}}{right=of d}
        \rf{d}{bb}
        \rf[out=-170,in=-10]{e}{a}
        \wk[in=165,out=15]{b}{d}
      \end{tikzinline}}
  \end{gathered}
\end{gather*}
This execution is disallowed by
\citet[\textsection8.2]{Dolan:2018:BDR:3192366.3192421}, preventing them from
using \texttt{stlr} to implement volatile writes on \armeight{}. Our
implementation strategy does use \texttt{stlr} for SC writes, as is standard.
For further discussion, see examples \ref{past} and \ref{future} in
\textsection\ref{sec:sc}.

\citet[\textsection3.1]{DBLP:conf/pldi/WattPPBDFPG20} noticed a similar
difficulty in Javascript \cite[\textsection27]{ecma2019}:
\begin{gather*}
  \taglabel{SC2}
  \begin{gathered}
    \PW[\mSC]{x}{1} \SEMI \PR[\mSC]{y}{r}
    \PAR
    \PW[\mSC]{y}{1} \SEMI \PW[\mSC]{y}{2} \SEMI \PW{x}{2} \SEMI \PR[\mSC]{x}{s}
    \\[-1ex]
    \hbox{\begin{tikzinline}[node distance=1.5em]
        \event{a}{\DW[\mSC]{x}{1}}{}
        \event{b}{\DR[\mSC]{y}{1}}{right=of a}
        \event{c}{\DW[\mSC]{y}{1}}{right=3em of b}
        \event{d}{\DW[\mSC]{y}{2}}{right=of c}
        \event{e}{\DW{x}{2}}{right=of d}
        \event{f}{\DR[\mSC]{x}{1}}{right=of e}
        \sync{a}{b}
        \sync{c}{d}
        \sync[out=15,in=165]{d}{f}
        \rf{c}{b}
        \rf[out=-8,in=-172]{a}{f}
        \wk[in=10,out=170]{e}{a}
        \wk{e}{f}
      \end{tikzinline}}
  \end{gathered}
\end{gather*}
This execution is allowed both by our semantics and by \armeight{} (using
\texttt{stlr} for SC writes and \texttt{ldar} for SC reads).  However, it is
not allowed by Javascript 2019.  In Javascript, the rules relating SC and
relaxed access are subtle.  As result of these interactions, Javascript 2019
fails to satisfy \drfsc{} \cite{DBLP:journals/pacmpl/WattRP19}.  The rules
are even more complex in C11; see \ref{SC3} and \ref{SC4} in
\textsection\ref{sec:variants} for a discussion of SC fences in C11.
In our model, only \ref{5d} is required to explain SC access.

\subsection{Valid and Invalid Rewrites}
\label{sec:valid}

When $\sem{\aCmd} \supseteq \sem{\aCmd'}$, we say that $\aCmd'$ is a
\emph{valid transformation} of $\aCmd$.  In this subsection, we show the
validity of specific optimizations.
Let $\free(\aCmd)$ be the set of locations and registers that occur in $\aCmd$.

The semantics validates many peephole optimizations.  Most apply only to
relaxed access.
\begin{align*}
  \taglabel{RR}
  \sem{\PR{\aLoc}{\aReg} \SEMI \PR{\bLoc}{\bReg}\SEMI\aCmd} &=
  \sem{\PR{\bLoc}{\bReg}\SEMI \PR{\aLoc}{\aReg}\SEMI\aCmd} &&\text{if } \aReg\neq\bReg
  \\
  \taglabel{WW}
  \sem{\aLoc \GETS \aExp \SEMI \bLoc  \GETS \bExp\SEMI\aCmd} &=
  \sem{\bLoc  \GETS \bExp\SEMI \aLoc \GETS \aExp\SEMI\aCmd} &&\text{if } \aLoc\neq\bLoc
  \\
  \taglabel{RW}
  \sem{\aLoc \GETS \aExp  \SEMI \PR{\bLoc}{\bReg} \SEMI\aCmd} &=
  \sem{\PR{\bLoc}{\bReg} \SEMI\aLoc \GETS \aExp\SEMI\aCmd} &&\text{if }
  \aLoc\neq\bLoc \textand \bReg\not\in\free(\aExp)%\disjoint{{\free(\aLoc \GETS \aExp)}}{{\free(\PR{\bLoc}{\bReg})}}
\end{align*}
\ref{5} imposes no order between events in \ref{RR}--\ref{RW}.  %Note that \ref{RR} allows aliasing.
Using augmentation closure, \ref{5} also validates roach-motel reorderings \cite{SevcikThesis}.  For
example, on read/write pairs:
\begin{align*}
  \tag{\textsc{roach1}}\label{AcqW}
  \sem{x^\amode \GETS \aExp \SEMI\PR{y}{\bReg} \SEMI\aCmd} &\supseteq
  \sem{\PR{y}{\bReg}  \SEMI x^\amode\GETS \aExp \SEMI \aCmd}
  &&\text{if }
  \aLoc\neq\bLoc \textand \bReg\not\in\free(\aExp)%\disjoint{{\free(\aLoc \GETS \aExp)}}{{\free(\PR{\bLoc}{\bReg})}}
  \\
  \tag{\textsc{roach2}}\label{RelW}
  \sem{x \GETS \aExp \SEMI\PR[\amode]{y}{\bReg} \SEMI\aCmd} &\supseteq
  \sem{\PR[\amode]{y}{\bReg}  \SEMI x\GETS \aExp \SEMI \aCmd}
  &&\text{if }
  \aLoc\neq\bLoc \textand \bReg\not\in\free(\aExp)%\disjoint{{\free(\aLoc \GETS \aExp)}}{{\free(\PR{\bLoc}{\bReg})}}
\end{align*}

Redundant load elimination \eqref{RL} follows
from \ref{1}, taking $\bEv\in\Event$, regardless of the access mode:
\begin{align*}
  \taglabel{RL}
  \sem{\PR[\amode]{\aLoc}{\aReg} \SEMI \PR[\amode]{\aLoc}{\bReg}\SEMI\aCmd} &\supseteq
  \sem{\PR[\amode]{\aLoc}{\aReg} \SEMI \bReg  \GETS \aReg\SEMI\aCmd}
\end{align*}

Since \ref{5b} does not impose order between reads of the same
location, \ref{RR} can allow the possibility that $\aLoc=\bLoc$.  As a
result, read optimizations are not limited by the power of aliasing
analysis.  By composing \ref{RR} and \ref{RL}, we validate \ref{CSE}:
\begin{align*}
  \taglabel{CSE}
  \sem{r_1\GETS \aLoc \SEMI
    s\GETS \bLoc \SEMI
    r_2\GETS \aLoc\SEMI\aCmd}
  \supseteq
  \sem{r_1\GETS \aLoc \SEMI
    r_2\GETS r_1\SEMI
    s\GETS \bLoc \SEMI\aCmd}
  &&\textif \aReg_2\neq\bReg&&\hbox{}
\end{align*}

Many laws hold for the conditional, such as dead code elimination \eqref{DC}
and code lifting \eqref{CL}:
\begin{align*}
  \taglabel{DC}
  \sem{\IF{\aExp}\THEN\aCmd\ELSE\bCmd\FI} &=
  \sem{\aCmd}
  &&\textif \aExp \text{ is a tautology}
  \\
  \taglabel{CL}
  \sem{\IF{\aExp}\THEN\aCmd\ELSE\aCmd\FI} &\supseteq
  \sem{\aCmd}
\end{align*}
Code lifting also applies to program fragments inside a conditional.  For example:
\begin{align*}
  \sem{\IF{\aExp}\THEN x\GETS \bExp \SEMI\aCmd\ELSE x\GETS \bExp \SEMI\bCmd\FI} &\supseteq
  \sem{x\GETS \bExp \SEMI \IF{\aExp}\THEN\aCmd\ELSE\bCmd\FI}
\end{align*}
We discuss the inverse of \ref{CL} in \textsection\ref{sec:refine}.

As expected, %sequential and
parallel composition commutes with conditionals and declarations, and
conditionals and declarations commute with each other.  For example,
we have \emph{scope extrusion}~\cite{Milner:1999:CMS:329902}:
\begin{align*}
  \taglabel{SE}
  \sem{\aCmd\PAR \VAR\aLoc\SEMI\bCmd} &=
  \sem{\VAR\aLoc\SEMI(\aCmd\PAR\bCmd)}
  &&\text{if } \aLoc\not\in\free(\aCmd)
\end{align*}

\myparagraph{Invalid Rewrites}

The definition of location binding does not validate renaming of locations:
if $\aLoc\neq\bLoc$ then
$\sem{\VAR\bLoc\SEMI\aCmd}\neq\sem{\VAR\aLoc\SEMI\aCmd[\aLoc/\bLoc]}$, even
if $\aCmd$ does not mention~$\aLoc$.  This is consistent with support for
address calculation, which is required by realistic memory allocators.

\ref{Internal2} shows that---like most relaxed models---our model
fails to validate \emph{thread inlining}.  The given execution is impossible
if the first thread is split, as in
\begin{math}
  \sem{\PR{x}{\aReg}\SEMI
    \PW[\mRA]{y}{1}\PAR
    \PR{y}{\bReg}\SEMI
    \PW{z}{\bReg}
    \PAR
    \PW{x}{z}}.
\end{math}
The write in the first thread cannot discharge the precondition in the
second, now separate.

Some rewrites are invalid in a concurrent setting, such as
relevant read introduction:
\begin{displaymath}
  \sem{\PR{\aLoc}{\aReg} \SEMI \IF{\aReg {\neq} \aReg} \THEN \PW{y}{1} \FI}
  \not\supseteq
  \sem{\PR{\aLoc}{\aReg} \SEMI \PR{\aLoc}{\bReg}  \SEMI \IF{\aReg {\neq}\bReg} \THEN \PW{y}{1} \FI}
\end{displaymath}
Observationally, these are distinguished by the context %
\begin{math}
  \hole{} \PAR \PW{x}{1}\PAR \PW{x}{2}.
\end{math}

Write introduction is also invalid, even when duplicating an existing write:
\begin{displaymath}
  \sem{\PW{\aLoc}{1}}
  \not\supseteq
  \sem{\PW{\aLoc}{1} \SEMI \PW{\aLoc}{1}}
\end{displaymath}
These are distinguished by the context:
\begin{math}
  \hole{} \PAR
  \PR{x}{r} \SEMI
  \PW{x}{2} \SEMI
  \PR{x}{s}\SEMI
  \IF{\aReg {=} \bReg} \THEN \PW{\cLoc}{1} \FI.
\end{math}


\part{Algorithm} \label{part:algorithm}
% Define the structuring auto-encoder.

\chapter{Background} \label{chap:background}
% Motivation.
% Why ? Neural nets, deep learning, unsupervised feature extraction, representation learning.
% The Need for Non-Local Generalization and Distributed Representations
% Feature learning, representation learning.

%\section{Representation learning}
%\section{Feature / representation learning}
% NN is well suited for representation learning
% We want unsupervised feature learning vs supervised vs hand-crafted
% Representation learning
% Learning Distributed Representations: Bengio learning deep ai
% Representation Learning: A Review and New Perspectives

\section{Neural networks}
% Why ? Distributed representation and computation (e.g. training on GPU, clusters) --> much what symbolic AI is not (mainly search algorithms, SAT solvers)
% Started in the late 1950s with the perceptron.

\gls{ANNs} are a family of statistical learning models inspired by biological neural networks and are used to estimate or approximate functions that can depend on a large number of inputs and are generally unknown. It is presented as a network of interconnected neurons whose connections have numeric weights that can be tuned based on experience. It makes the neural nets adaptive to inputs and capable of learning.

% What is it ? In general.
Such a network is composed by an input layer, a number of hidden layers and an output layer. The activation of the neurons in the input layer corresponds to the input vector, e.g. an image for computer vision, a song for \gls{MIR}, a word vector for machine translation and sentiment analysis. A weight matrix, followed by a non-linear activation function, then transforms the vector in another representation. The output vector of the first layer is the input vector of the second, and so on until the output layer is reached. The activation of the neurons in the output layer may represent classes, probability distributions, or the estimated value of an unknown function to be learned.

%{\color{red} Picture ?}

% Types: feed-forward or recurrent
In a feed-forward network, the connections go from one layer to the next, i.e. information only goes in one direction, forward, from the input nodes, through the hidden nodes (if any) and to the output nodes. Such networks are known to be able to approximate any function. The perceptron, the \gls{MLP} and the \gls{CNN} are examples of this class of networks.
By introducing backward connections, i.e. the connections between units form a directed cycle, we obtain a so called \gls{RNN}. This creates an internal state of the network which allows it to exhibit dynamic temporal behavior. Unlike feed-forward neural networks, an \gls{RNN} can use its internal memory to process arbitrary sequences of inputs. It is known to be able to approximate any program. Such networks have proven very successful for machine translation.

% How to train, supervised.
In a supervised learning setting, the network is trained by back-propagating the error, gradient based learning method, from the output layer to the input through all the hidden layers.
The vanishing gradient problem, where errors shrink exponentially with the number of layers as they propagate from layer to layer, is a major issue of the algorithm \cite{hochreiter2001vanishingGradient}. Various methods, like unsupervised pre-training or \gls{LSTM} \cite{hochreiter1997LSTM}, were developed to work around this problem.

% How to train, unsupervised.
However, in an unsupervised learning setting, there is no desired output, which implies that there is no error to back-propagate. The training algorithm should thus optimize for another objective, which represent desired properties about the output. We will introduce next such an algorithm, called an auto-encoder.

\section{Auto-encoders} \label{sec:auto_encoders}
% For unsupervised learning, i.e. feature extraction.
% Auto-encoders as a manifold learning tool (Bengio review).

An auto-encoder, auto-associator or Diabolo network is an artificial neural network composed of $n$ input and output units and $m$ hidden units. It is used for learning efficient codings \cite{bourlard1988autoencoder, hinton1994autoencoder}. The aim of an auto-encoder is to learn a distributed representation (encoding) for a set of data. An auto-encoder is trained to encode the input $\x \in \R^n$ into some representation $\z \in \R^m$ so that the input can be reconstructed from that representation. It is thus a generative model. Hence the target output of the auto-encoder is the auto-encoder input itself. Auto-encoders may further be stacked to form a \gls{DBN}, while each layer can be trained separately \cite{bengio2007DBN, ranzato2007stackedSparseAutoencoders}.
% Stacked Auto-Encoders, not DBN

%{\color{red} Picture ?}

% if linear activations are used, or only a single sigmoid hidden layer, then the optimal solution to an auto-encoder is strongly related to principal component analysis (PCA).[5]
If there is one linear hidden layer and the mean squared error criterion is used to train the network, then the $k$ hidden units learn to project the input in the span of the first $k$ principal components of the data \cite{bourlard1988autoencoder}. If the hidden layer is non-linear, the auto-encoder behaves differently from \gls{PCA}, with the ability to capture multi-modal aspects of the input distribution \cite{japkowicz2000autoencoderPCA}.

The hope is that the code $\z$ is a distributed representation that captures the main factors of variation in the data: because $\z$ is viewed as a lossy representation of $\x$, it cannot be a good representation (with small loss) for all $\x$. So learning drives it to be one that is a good representation in particular for training examples, and hopefully for others as well (and that is the sense in which an auto-encoder generalizes), but not for arbitrary inputs.

It can typically be used for dimensionality reduction by learning a compressed ($m<n$) representation of the data. Another application is feature extraction before classification, for which we want an higher dimensionality ($m>n$) for easier separability. One serious issue with this approach is that if there is no other constraint, then an auto-encoder with $n$-dimensional input and an encoding of dimension $m \geq n$ could potentially just learn the identity function. There are different ways that an auto-encoder with more hidden units than inputs could be prevented from learning the identity, and still capture something useful about the input in its hidden representation $\z$.

\paragraph{Sparse auto-encoders.}
One strategy, based on the concept of sparse coding, is to add a sparsity constraint on the code. While an ordinary auto-encoder or an \gls{RBM} has an encoder part which computes $P(\z|\x)$ and a decoder part which computes $P(\x|\z)$, sparse coding systems only parametrize the decoder: the encoder is implicitly defined as the solution of an optimization. A middle ground between ordinary auto-encoders and sparse coding was proposed in \cite{lecun2006sparseAutoencoders, ranzato2007stackedSparseAutoencoders} and applied to pattern recognition and machine vision tasks. They propose to let the codes $\z$ be free (as in sparse coding algorithms), but include a parametric encoder (as in an ordinary auto-encoder or \gls{RBM}) and a penalty for the difference between the free non-parametric codes $\z$ and the outputs of the parametric encoder. In this way, the optimized codes $\z$ try to satisfy two objectives: reconstruct well the input (like in sparse coding), while not being too far from the output of the encoder (which is stable by construction, because of the simple parametrization of the encoder). See \secref{encoder} for the definition of our encoder.

\paragraph{Denoising auto-encoders.}
Another strategy is to add noise in the encoding. The denoising auto-encoder thus minimizes the error in reconstructing the input from a stochastically corrupted transformation of the input \cite{bengio2008denoisingAutoencoders}. Intuitively, a denoising auto-encoder does two things: try to encode the input (preserve the information about the input), and try to undo the effect of a corruption process stochastically applied to the input of the auto-encoder. This is essentially what a \gls{RBM} does \cite{hinton2002RBM}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\chapter{Model} \label{chap:model}
% Step by step construction: sparse coding, dictionary learning, encoder learning, manifold learning.
% Xavier: maybe not useful as it is just the enumeration of the sub-titles of chapter 2 --> give instead a global motivation.

This chapter presents the proposed structured auto-encoder. Built on linear regression, the model increases in complexity as desired properties about its internal representation are progressively integrated in the form of regularizations.

% Plan.
%This chapter presents the proposed structured auto-encoder. \secref{assumptions} states the assumptions of the model. \secref{linear_regression} reviews the basics of linear regression, the foundation of our model. \secref{sparse_coding} introduces sparse coding, \secref{dictionary_learning} introduces a trainable dictionary and \secref{manifold_learning} introduces manifold learning. Last but not least, \secref{encoder} introduces the trainable encoder. Finally, \secref{energy_formulation} reviews the whole energy-based formulation and brings some new insights.

\section{Assumptions} \label{sec:assumptions}
% Xavier: needs to be improved.

\paragraph{Sparse representation.}
We make the hypothesis that a set of sample signals drawn from the same distribution can be sparsely represented in some frame\footnote{A frame of a vector space is a set of vectors which may be linearly dependent. It is a generalization of a basis.}. Each signal should be approximately reconstructed by a linear combinations of a few atoms from a suitable dictionary. As we shall see, this dictionary may be adaptive and learned directly from the data. Many approaches have been developed to achieve sparse representations, e.g. sparse \gls{PCA} \cite{aspremont2007sparsePCA}, sparse NMF \cite{hoyer2004sparseNMF}, K-SVD \cite{aharon2006KSVD}. Sparse coding \cite{olshausen1996SparseV1, mairal2008sparseCoding} is however the most popular one. Sparsity has become a concept of great interest recently, not only in machine learning but also in statistics and signal processing, in particular with the work on \gls{CS} \cite{candes2005CS, donoho2006CS}.
% There is a huge body of literature on sparse coding and compressed sensing to support this hypothesis.
% --> sparse coding.

% Structured: adding similarity information between inputs.
% Manifold: not all possible signals are reasonable / feasible.
% Xavier: a definition of auto-encoder should be given before in the report.
% We further assume that the subspace spanned by the data is much smaller than the Euclidean space.
\paragraph{Structured data.}
Our second hypothesis is that the dataset holds some structure, in the sense that related samples are close to each other (with respect to some metric). Given a large enough training set, the structure of the data distribution should be able to be captured; i.e. a new valid sample (e.g. from the testing set) should be close to the seen examples.
It suggests that the data is drawn from sampling a probability distribution that has support on or near to a submanifold of the ambient space.
% It suggests that the data lie on a lower dimensional manifold embedded in a high dimensional vector space.
This manifold is however often unknown and must be learned. \gls{SOM} \cite{kohonen1982SOM}, \gls{LLE} \cite{roweis2000LLE}, Laplacian Eigenmaps \cite{belkin2001laplacianEigenmaps} and ISOMAP \cite{tenenbaum2000isomap} are some popular techniques which learn manifolds in a \gls{NLDR} framework. All these algorithms use the so-called locally invariant idea \cite{lecun2006invariantMapping}, i.e. the nearby points are likely to have similar embeddings. Auto-encoders used for dimensionality reduction are able to learn a map from high to low-dimensional space with fewer hidden units than inputs. They are trained to learn to optimally encode the input vectors into a small number of dimensions and decode them back into the original space with minimal error \cite{bourlard1988autoencoder}.
% --> manifold learning.

\paragraph{Encoder.}
We further make the assumption that a simple encoder can be trained to avoid the need of an optimization process that extracts the features during testing, i.e. after the training phase. The encoder shall be more efficient, in the computational sense, than the optimization process while not degrading too much the quality of the extracted features. Note that even if this hypothesis is not verified, the algorithm is still an auto-encoder; although with an implicit encoder.
% --> auto-encoder.

\section{Linear regression} \label{sec:linear_regression}

\paragraph{Model.}
Given a set $\set{\x}{N} \in \R^n$ of $N$ signals, the subspace $\Xx = \spanning \{\x_i\}_{i=1}^N \subset \R^n$ is defined as the subspace spanned by the data. Then, given a signal $\x \in \Xx$ and a frame $\D \in \R^{n \times m}$, we want to find a representation $\z \in \R^m$ which satisfies the linear regression model
\begin{equation} \label{eqn:linear_regression}
	\x = \D \z + \eps,
\end{equation}
where $\mathbf{\epsilon} \in \R^n$ is the reconstruction error, which is not negligible as long as the frame $\D$ is not complete on $\Xx$.

\paragraph{Capacity.} The hyper-parameter $m$ defines the learning capacity of the auto-encoder. A capacity $m < n$ is good for dimensionality reduction as it exploits the statistical regularities present in the training set while being more compact. A capacity $m > n$ is good for classification as it allows for an easier (linear) separability enabled by the higher dimensional space.

\begin{figure}[ht]
	\centering
	\includegraphics[height=6cm]{img/overcomplete_frame}
	\caption[]{An example of overcomplete frame. While the data lies in a two-dimensional space, a four-dimensional space supported by an overcomplete frame allows a better representation.\footnotemark}
	\label{fig:overcomplete_frame}
\end{figure}
\footnotetext{Figure from Wikipedia.}

\paragraph{Completeness.}
A frame is complete if it can represent any vector $\x \in \Xx$. It is overcomplete if the removal of a vector from the frame results in a complete frame. A set of $n < m$ linearly independent vectors would indeed be overcomplete on the whole space $\R^n$ and a set of $m=n$ linearly independent vectors, like the Fourier transform, would form a basis\footnote{A basis is a set of linearly independent vectors who span the entire space, i.e. it is a linearly independent spanning set.} of $\R^n$, which is obviously complete.

% Complete.
A complete frame which is not overcomplete allows bidirectional lossless transformations. Such problems are well-posed as there exists a unique solution to \eqnref{linear_regression} with $\eps=0$.

% Not complete.
If the frame is not complete, there exist no solution to \eqnref{linear_regression} with $\eps=0$ as the system is overdetermined. An error measure, like \eqnref{least_square} for the \gls{OLS} method, should instead be minimized.

% Overcomplete.
In different research, such as signal processing and function approximation, overcomplete representations have been advocated because they have greater robustness in the presence of noise, can be sparser, and can have greater flexibility in matching structure in the data. However, because of this redundancy, a signal can have multiple expressions under an overcomplete frame \cite{lewicki2000overcompleteRepresentation}. See \figref{overcomplete_frame} for an example of the flexibility of an overcomplete frame to represent a dataset. As there is then an infinite number of solutions to \eqnref{linear_regression} with $\eps=0$, i.e. the problem is ill-posed, a regularization over $\z$ shall be introduced. Optimization techniques are then used to find the optimal solution which minimizes the sum of the error measure and the regularization term, controlled by an hyper-parameter.

\paragraph{Ordinary least squares.}
The method of least squares is a standard approach in regression analysis to the approximate solution of overdetermined systems. "Least squares" means that the overall solution minimizes the sum of the squares of the errors made in the results of every single equation, i.e. it finds
\begin{equation} \label{eqn:least_square}
	\z^* = \argmin{\z} \normT{\x - \D\z},
\end{equation}
where $\normT{\cdot}$ denotes the squared $\ell_2$, or Euclidean, norm. This problem has the closed-form solution
\begin{equation}
	\z^* = (\D^T\D)^{-1} \D^T \x,
\end{equation}
where $T$ denotes the matrix transpose. The primary assumption of \gls{OLS} is that there are zero or negligible errors in the independent variable $\D$, since this method only attempts to minimize the mean squared error in the dependent variable $\x$. That is not an issue in an auto-encoder setting where $\D$ is either hand-crafted or learned.

% underdetermined == ill-posed ?
\paragraph{Regularization.}
% L2.
Tikhonov regularization \cite{tikhonov1963tikhonovRegularization}, or ridge regression, is the most commonly used method of regularization of ill-posed problems. It adds a prior of the form $\lambda \normT{\gam \z}$ to the minimization problem \eqnref{least_square} as follows:
\begin{equation} \label{eqn:tikhonov_regularization}
	\z^* = \argmin{\z} \normT{\x - \D\z} + \lambda \normT{\gam \z},
\end{equation}
where $\gam$ is the Tikhonov matrix. This matrix is often chosen to be a multiple of the identity matrix, i.e. $\gam = \alpha \mathbf{I}$, giving preference to solutions with smaller norms. In a Bayesian context, this is equivalent to placing a zero-mean normally distributed prior on $\z$ \cite{vogel2002inverseProblems}. In other cases, lowpass operators, e.g. a difference operator or a weighted Fourier operator, may be used to enforce smoothness if the underlying vector is believed to be mostly continuous. A regularization of this kind will be introduced in our model in \secref{manifold_learning}. An explicit solution is given by
\begin{equation}
	\z^* = (\D^T\D + \gam^T\gam)^{-1} \D^T \x.
\end{equation}

% L1.
Another commonly used regularization is the \gls{LASSO} \cite{tibshirani1996Lasso}, which adds the prior $\lambda \normO{\z}$ to the minimization problem \eqnref{least_square} as follows:
\begin{equation} \label{eqn:lasso_regularization}
	\z^* = \argmin{\z} \normT{\x - \D\z} + \lambda \normO{\z},
\end{equation}
where $\normO{\z} = \sum_{i=1}^{m} |z_i|$ is the $\ell_1$ norm of $\z$, also called the Taxicab or Manhattan norm.
In a Bayesian context, this is equivalent to placing a zero-mean Laplace prior distribution on $\z$ \cite{park2008BayesianLasso}. The advantage of the \gls{LASSO} is that it promotes the simplest solutions, i.e. the solutions with many zeros. Driving parameters to zero effectively deselects the features from the regression. \gls{LASSO} thus automatically selects the most relevant features, whereas ridge regression never fully discards any. For this reason, the \gls{LASSO} and its variants are fundamental to the field of \gls{CS}. A regularization of this kind will be introduced in our model in \secref{sparse_coding}.

% L1 + L2.
An extension of this approach is the elastic net regularization \cite{zou2005ElasticNet} which linearly combines the $\ell_1$ and $\ell_2$ penalties of the \gls{LASSO} and ridge methods as follows:
\begin{equation} \label{eqn:elasticnet_regularization}
	\z^* = \argmin{\z} \normT{\x - \D\z} + \lambda_2 \normT{\z} + \lambda_1 \normO{\z}.
\end{equation}
This regularization overcomes some limitations of the $\ell_1$ penalty, e.g. the saturation which happens for high-dimensional data with few examples, or the fact that the \gls{LASSO} tends to select only one variable and ignore the others if there is a group of highly correlated variables.

\section{Sparse coding} \label{sec:sparse_coding}
% What it is, why it works, why it's good.
% paper who demonstrates that L1 penalty leads to same solution as L0 while being convex, not combinatorial

% Sparse coding.
The main idea behind sparse coding \cite{olshausen1996SparseV1, mairal2008sparseCoding} is to express the signal $\x \in \Xx \subset \R^n$ as a sparse linear combination of basis functions $\set{\d}{m} \in \R^n$, or atoms, from an overcomplete dictionary $\D \in \R^{n \times m}$. The sparse code $\z^* \in \R^m$ is given by
\begin{equation} \label{eqn:sparsecoding}
	\z^* = \argmin{\z} \frac{\lambda_d}{2} \normT{\x - \D \z} + \lambda_z \normZ{\z},
\end{equation}
where $\normZ{\z}$ denotes the number of non-zero elements in $\z$.  $\lambda_d$ and $\lambda_z$ are the (redundant) hyper-parameters setting the trade-off between the data term, an accurate reconstruction, and the prior, a sparse solution. Overcomplete sparse representations tend to be good features for classification systems as they provide a succinct representation of the signal, are robust to noise and are more likely to be linearly separable due to their high dimensionality.

% Faster sparse coding approximations.
Finding the sparse code $\z^*$ however requires a combinatorial search which is an NP-hard problem \cite{natarajan1995sparseNPhard}, intractable in high dimensional spaces. Various approximations have thus been proposed. \gls{MP} \cite{mallat1993MatchingPursuit} offers a greedy approximation to the solution while \gls{BP} \cite{chen1998BasisPursuit} is the popular convex approximation
\begin{equation} \label{eqn:basispursuit}
	\z^* = \argmin{\z} \frac{\lambda_d}{2} \normT{\x - \D \z} + \lambda_z \normO{\z},
\end{equation}
which is the \gls{LASSO} regularized least square problem introduced in \eqnref{lasso_regularization}. As is now well understood \cite{candes2005CS, donoho2006CS}, the $\ell_1$ norm is a very good proxy for the $\ell_0$ pseudo-norm and naturally induces sparse results. It can even be shown to recover exactly the true sparse code, i.e. the solution of \eqnref{sparsecoding} (if there is one), under mild conditions \cite{donoho2003OptSparse}.
%{\color{red}link with \gls{RIP}}.
A number of algorithms have been proposed to efficiently solve this problem \cite{chen1998BasisPursuit, beck2009FISTA, ng2006EfficientSparse, li2009Coordinate}. They however still rely on computationally expensive iterative procedures which limit the system's scalability and real-time applications. While a direct method will always be preferred for feature extraction, iterative methods will still be necessary during training. Distributed computing with \gls{GPU} or via cloud computing will hopefully accelerate the process.

\section{Dictionary learning} \label{sec:dictionary_learning}

% Why ?
\paragraph{Model.}
In classical sparse coding, the dictionary is composed of known functions such as sinusoids \cite{bracewell1965fourier}, wavelets \cite{mallat1999wavelet}, Gabors \cite{gabor1946gabor}, curvelets \cite{candes2002curvelet} or contourlets \cite{vetterli2003contourlet}; i.e. hand-crafted features. One may also want to learn a dictionary that is adaptive to the type of data at hand. This approach may allow an even more compact representation and may lead to the discovery of previously unknown discriminative features.
% gammatones\footnote{A gammatone is a sinusoid (a pure tone) with an amplitude envelope which is a scaled gamma distribution function. It is used to build cochlear models.} \cite{holdsworth1992gammatone}

% How ?
To use the dictionary $\D$ as an unknown variable, all the training data shall be part of the objective function as the dictionary depends on all of them. The energy function, composed by an $\ell_2$ fidelity term and an $\ell_1$ penalty, becomes
\begin{equation} \label{eqn:en_dict}
	\Eone = \frac{\lambda_d}{2} \normF{\X - \D \Z} + \lambda_z \normO{\Z},
\end{equation}
where $\normF{\cdot}$ denotes the squared Frobenius norm, $\X = \set{\x}{N} \in \R^{n \times N}$ is the set of training vectors and $\Z = \set{\z}{N} \in \R^{m \times N}$ their associated sparse codes. $N$ is naturally the number of training vectors, which should be much greater than the size $m$ of the dictionary to avoid the trivial solution where examples are copied in the dictionary. The problem to solve is then
\begin{equation} \label{eqn:pr_dict}
	\minimize{\Z,\D} \Eone \st \cstd,
\end{equation}
where the $\ell_2$ ball constraint (usually implemented by rescaling the columns $\d_i$ of $\D$ at each iteration) prevents the trivial solution where the code coefficients go to zero while the bases are scaled up. While this problem is not convex, a good approximate solution can be found by iteratively minimizing for $\Z$ and $\D$ \cite{olshausen1996SparseV1}.
% Simple gradient descent or more sophisticated methods \cite{chen1998BasisPursuit, beck2009FISTA, li2009Coordinate} for $\z$, stochastic gradient descent for $\D$.

\paragraph{Completeness.}
The learned dictionary may be seen as an overcomplete frame of the subspace $\Xx$ spanned by the training data. The overcompleteness of the learned dictionary could indeed be tested: the frame should be able to perfectly represent any training sample, i.e. in the absence of the $\ell_1$ regularization, the reconstruction error $\eps$ of \eqnref{linear_regression} should be zero.

% Motivation: learned dictionaries resemble brain processing stages.
\paragraph{Biological motivation.}
There is evidence that sparse coding may be a strategy employed by the brain in the early stages of visual and auditory processing \cite{olshausen1996SparseV1, olshausen1997SparseV1, smith2006SparseAudio}. Basis functions learned on natural images have been shown to resemble the receptive fields of neurons in the visual cortex \cite{olshausen1996SparseV1, olshausen1997SparseV1}. Basis functions learned on natural sounds were found to be highly similar to gammatone functions \cite{smith2006SparseAudio} which have been used to model the action of the basilar membrane in the inner ear. Moreover, learning on natural time-varying stimuli such as speech or video has been shown to produce localized bases \cite{lewicki2000SparseSpeech, olshausen2000SparseVideo}.

\section{Manifold learning} \label{sec:manifold_learning}
% Further structure, geometry learning.
% Three parts: manifold, graph and model.

% Why ?
\paragraph{Motivation.}
% Paper: graph regularized sparse coding.
Most of the existing approaches to sparse coding do not consider the geometrical structure of the data space. The data is however more likely to reside on a low-dimensional submanifold embedded in the high-dimensional ambient space.
%It has been shown that the geometrical information of the data is important for discrimination \cite{zheng2011StructuredSparse}.
It has been shown that the learning performance of a sparse coding scheme can be significantly enhanced if the geometrical structure is exploited and the local invariance is considered \cite{zheng2011StructuredSparse}.

% General introduction.
%\paragraph{Manifold.}
%A manifold is a topological space that resembles Euclidean space near each point, i.e. each point of an $d$-dimensional manifold has a neighborhood that is homeomorphic to the Euclidean space of dimension $d$. Lines and circles, but not figure eights, are one-dimensional manifolds. Two-dimensional manifolds are also called surfaces. Examples include the plane, the sphere, and the torus, which can all be embedded in three-dimensional real space. \figref{manifolds} shows examples of 1D and 2D manifolds.

%\begin{figure}[ht]
%	\centering
%	\begin{subfigure}[b]{0.49\textwidth}
%		\centering
%		\includegraphics[height=6cm]{img/circle_manifold_charts}
%		\caption{}
%	\end{subfigure}
%	\begin{subfigure}[b]{0.49\textwidth}
%		\centering
%		\includegraphics[height=6cm]{img/klein_bottle}
%		\caption{}
%	\end{subfigure}
%	\caption[]{Examples of manifolds.\footnotemark (a) The circle, a 1D manifold, can be mapped by four charts. (b) The klein bottle is a 2D manifold that cannot be embedded in a 3D space without self-intersection.}
%	\label{fig:manifolds}
%\end{figure}
%\footnotetext{Figures from Wikipedia.}

% Example close to our case.
%Let's imagine a single handwritten digit recognition system. The input is an image of $n \times n$ pixels which is represented by a vector $\x \in \R^{n \times n}$. However, not all vectors $\x \in \R^{n \times n}$ represent meaningful data. The vast majority indeed represents garbage. We may make the hypothesis that the set of plausible digits lies on a $d$-dimensional manifold where $d \leq n \times n$.

% Approximate a manifold.
%The problem is that we often ignore the shape of the embedded manifold. We only have at our disposal some samples which are drawn from it, e.g. a few examples of handwritten digits. Another example with object scanning: we obtain a discrete point cloud sampled from the surface. We do know some points on the surface, but we don't know the exact surface.

%We use graphs as discrete approximations of low-dimensional manifolds embedded in high-dimensional spaces. The set of data are samples drawn from this unknown manifold.

% General formulation.
% What a graph is, generally. Not only a manifold approximation.
\paragraph{Similarity graphs.}
% Paper: Pierre's review
Graphs are generic data representation forms which are useful for describing the geometric structures of data domains in numerous applications, including social, energy, transportation, sensor, and neuronal networks \cite{pierre2013graphs}. The connectivity and weight associated with each edge in the graph is either dictated by the physics of the problem at hand or inferred from the data.
Weighted graphs are commonly used to represent similarities between data points in statistical learning problems for applications such as machine vision \cite{lowe1999graphSimilarity} and automatic text classification \cite{apte1994graphSimilarity}.

%\begin{figure}[ht]
%	\centering
%	\includegraphics[height=6cm]{img/example_graph}
%	\caption[]{A random positive graph signal on the vertices of the Petersen graph. The height of each blue bar represents the signal value at the vertex where the bar originates.\footnotemark}
%	\label{fig:example_graph}
%\end{figure}
%\footnotetext{Figure from \cite{pierre2013graphs}.}

% We first review some basic definitions and notations from spectral graph theory and then show how we can use a graph to approximate a manifold.

% Graph definition.
From the set of training vectors $\X$, we can construct an undirected, connected and weighted graph $\G = \{ \V, \mathcal{E}, \W \}$ which consists of a finite set of vertices $\V$ with $|\V| = N$, a set of edges $\mathcal{E}$, and a weighted adjacency matrix $\W = (w_{ij}) \in \R^{N \times N}$. Each vertex $i \in \V$ represents a training vector $\x_i$. If there is an edge $e = (i, j)$ connecting vertices $i$ and $j$, the entry $w_{ij}$ represents the weight of the edge; otherwise, $w_{ij} = 0$. The set of sparse codes $\Z$ is a signal which resides on the graph, i.e. a signal with one sample $\z_i$ at each vertex $i$ of the graph.

% Distance metric: euclidean (small dim) and cosine (angular, high dim).
% ref for why cosine is better in high dim
% Intuition: cosine is euclidean when data projected to sphere.
% Kernel: Gaussian or polynomial.
While there exist several ways to define the edge weights when they are not naturally defined by the application, they often represent the similarity between the two vertices they connect \cite{pierre2013graphs}. For instance, the edge weight may be inversely proportional to the Euclidean distance between the vectors:
\begin{equation}
	w_{ij} = \exp \left( - \frac{\normT{\x_i - \x_j}}{2\sigma^2} \right) \in [0,1],
\end{equation}
where the Gaussian kernel width $\sigma$ controls the width of the neighborhoods and $\inner{\cdot}{\cdot}$ denotes the scalar product. While the Euclidean distance is a good choice for low-dimensional data, its discriminative power vanishes in higher dimensional space \cite{aggarwal2001highDimDistance, domingos2012pitfallsML}. An option is then to use the cosine similarity as the edge weight:
\begin{equation}
	w_{ij} = \frac{1}{2} + \frac{1}{2} \cos(\theta) = \frac{1}{2} \left(1 + \frac{\inner{\x_i}{\x_j}}{\|\x_i\|_2 \|\x_j\|_2} \right) \in [0,1],
\end{equation}
where $\theta$ is the angle between the two vectors $\x_i$ and $\x_j$. Another reason for the popularity of the cosine similarity is that it is very efficient to evaluate, especially for sparse vectors, as only the non-zero dimensions need to be considered. See \cite{grady2010graphs} for other graph
construction methods.

% Type: KNN vs epsilon.
A fully connected graph is usually not wanted: the number of edges are often artificially limited in order to reduce the storage and computational cost associated with the graph manipulation, effectively sparsifying the weight matrix $\W$.
The $\epsilon$-neighborhood graph is the popular approach which sets to 0 any weigh $w_{ij} < \epsilon$ for some threshold $\epsilon$.
A second common method is to connect each vertex to its $k$-nearest neighbors only and drop the smallest weights; in which case \cite{zelnik2004scale} suggests to set the Gaussian kernel scale $\sigma$ to the mean of the $k^{\text{st}}$ distances.

\paragraph{Graph Laplacian.}
% The graph (normalized) Laplacian is an approximation of the Laplace-Beltrami operator. --> Dirichlet energy
% normalized vs un-normalized, cite normalized approximates the Laplace-Beltrami operator
% A way to test if the Laplacian is well constructed is... (see blog)
% Another regularization on Z. Like Thikonov with the graph Laplacian as the Tikhonov matrix (difference operator). It enforces smoothness on the graph / manifold.
% The graph Laplacian as a difference operator.
% \L = U \Lambda U^*, U Fourier basis, Lamb eigenvalues / frequencies, U* inverse Fourier.
The unnormalized graph Laplacian, also called the combinatorial graph Laplacian, is defined as
\begin{equation}
	\L = \A - \W,
\end{equation}
where the degree matrix $\A=(a_{ij}) \in \R^{N \times N}$ is a diagonal matrix whose $i$th diagonal element $a_{ii}$ is equal to the sum of the weights of all the edges incident to vertex $i$:
\begin{equation}
	a_{ii} = \sum\limits_{j=1}^{N} w_{ij}.
\end{equation}
The graph Laplacian is a difference operator as it satisfies
\begin{equation}
	\L\z_i = \sum\limits_{j=1}^{N} w_{ij} (\z_i - \z_j).
\end{equation}

\paragraph{Dirichlet energy.}
The Dirichlet energy is a measure of the smoothness of a graph signal given by
\begin{equation} \label{eqn:dirichlet_energy}
	\tr(\Z\L\Z^T) = \frac{1}{2} \sum\limits_{i=1}^{N} \sum\limits_{j=1}^{N} w_{ij} \normT{\z_i - \z_j} \geq 0,
\end{equation}
which is a suitable candidate for regularization \cite{belkin2006manifoldRegularization}.
%\paragraph{Manifold assumption.}
The assumption that the representations $\Z$ should be smooth on the similarity graph $\G$ constructed by the training vectors $\X$, usually referred to as the manifold assumption\footnote{Because the graph is used as a proxy for the manifold.}, plays an essential role in various kinds of algorithms including dimensionality reduction algorithms \cite{belkin2001laplacianEigenmaps}, clustering algorithms \cite{ng2002spectralClustering} and semi-supervised learning algorithms \cite{belkin2006manifoldRegularization, zhou2004manifoldRegularization}.
Note that the Euler-Lagrange of the Dirichlet energy is precisely the graph Laplacian.
% exploit the geometrical information in the data by using the manifold assumption which has been shown effective in classification and clustering tasks \cite{belkin2006manifoldRegularization}.

% Why normalized Laplacian ? Consistency.
% Descrete graph Laplacian converges to continuous Laplace-Beltrami operator.
% Reference: Erdos.
% Why graphs ? To approximate the unknown manifold.
% Correct approximation because of the consistency principle.
% While the dataset $\X$ is supposedly drawn from a manifold, the manifold itself is unknown.
\paragraph{Consistency.}
Two normalized graph Laplacians are found in the literature \cite{chung1997spectralGraphTheory}:
\begin{equation}
	\L_{sym} = \A^{-1/2} \L \A^{-1/2} = \I - \A^{-1/2} \W \A^{-1/2},
\end{equation}
and
\begin{equation}
	\L_{rw} = \A^{-1} \L = \I - \A^{-1} \W,
\end{equation}
where $\I$ denotes the identity matrix.
While there is no convergence guarantee for the unnormalized graph Laplacian, these two normalized Laplacian can be shown to converge to the continuous Laplace-Beltrami operator as the number of samples increase \cite{vonluxburg2008consistency}. The similarity graph is indeed a good approximation of the unknown manifold.

\paragraph{Model.}
% Paper: By using graph Laplacian as a smooth operator, the obtained sparse representations vary smoothly along the geodesics of the data manifold.
The geometrical information about the data is encoded in a similarity graph constructed by the training vectors $\X$ and the graph Laplacian is used as a smooth operator to preserve the local manifold structure. Introducing the Dirichlet energy into the objective function as an additional $\ell_2$ regularization, similar to the Tikhonov regularization presented in \secref{linear_regression}, gives
\begin{equation}
	\Etwo = \frac{\lambda_d}{2} \normF{\X - \D \Z} + \lambda_z \normO{\Z} + \frac{\lambda_g}{2} \tr(\Z^T \L \Z).
\end{equation}
This regularization promotes a smooth variations of the representations along the geodesics of the data manifold.

\section{Encoder} \label{sec:encoder}
% Auto-encoder: explicit encoder instead of implicit encoder.
% Problem: these methods are slow at inferring sparse codes as they need iterations
% --> train an encoder

\paragraph{Motivation.}
In order to avoid the iterative procedure typically required to infer the sparse code, we aim at an explicit encoder which can quickly map inputs to approximations of their sparse code. Several works \cite{lecun2010PSD, lecun2010LISTA, lecun2013DrSAE} have been done in this direction. The addition of an explicit encoder to the sparse coding scheme bridges the gap between auto-encoders and sparse coding and is often referred to as sparse auto-encoders \cite{bengio2009learningDeepAI}.
% {\color{red} [others not from LeCun?]}
% Xavier: cite Andrew Ng. But iterative ?

% 2nd motivation.
Moreover, adding structure to the problem should enhance the behavior of the loss function and help sparse recovery \cite{kowalski2009sparse, baraniuk2010modelCS, huang2011LearningStructuredSparsity, jenatton2011structured}.
% {\color{red}Then \cite{donoho2003OptSparse} should not be strong enough.}

% How ?
\paragraph{Model.}
Introducing a trainable encoder $\E \in \R^{m \times n}$, designed to predict sparse codes from input vectors with minimum error, into our model gives the energy function
\begin{equation} \label{eqn:en_encoder}
	\Ethree = \frac{\lambda_d}{2} \normF{\X - \D \Z} + \lambda_z \normO{\Z} + \frac{\lambda_g}{2} \tr(\Z^T \L \Z) + \frac{\lambda_e}{2} \normF{\Z - \E \X}, %{\color{red} + \lambda_s \normO{\E \X}},
\end{equation}
%where $\lambda_e$ and $\lambda_s$ are two additional hyper-parameters which control the sparsity versus fidelity tradeoff. The problem is then defined by
where $\lambda_e$ is an additional hyper-parameter which controls the relative weight of the prediction error. The problem is then defined by
\begin{equation} \label{eqn:pr_encoder}
	\minimize{\Z,\D,\E} \Ethree \st \cst{\d}{i} , \cst{\e}{k} ,
	\ \forallx{i}{m} , \ \forallx{k}{n},
\end{equation}
where $\e_k$ are the columns of $\E$. %Again, the $\ell_1$ penalty $\normO{\E \X}$ regularizes the least square problem, i.e. the \gls{LASSO} method.

\paragraph{Energy.}
While it is often a good idea to control the energy, the constraint on the columns of $\E$ is not needed in practice. While the columns of $\D$ are constrained to a norm smaller than one, they are in practice normalized because of the $\normO{\Z}$ objective. The energy of a vector transformed by $\D$ does thus not change, which means that the inferred sparse code $\z_i$ as the same energy as its corresponding vector $\x_i$. The encoder does then not need to add energy and will have a column norm smaller than one, even without the constraint. It has been verified empirically.

\section{Auto-encoder}
% Reviews the whole energy-based formulation and brings some new insights.
% Identify parts of the energy functions to equations in regression, e.g. tik, lasso, elastic.

\paragraph{Energy formulation.}
The energy function \eqnref{en_encoder}, which defines our model, may be rewritten as a sum of functions of the variables $\Z$, $\D$ and $\E$ as follows:
\begin{equation} \label{eqn:objective}
	E(\Z, \D, \E) =
	\underbrace{\frac{\lambda_d}{2} \normF{\X - \D \Z}}_{\fd} +
	\underbrace{\lambda_z \normO{\Z}}_{\fz} +
	\underbrace{\frac{\lambda_g}{2} \tr(\Z^T \L \Z)}_{\fg} +
	\underbrace{\frac{\lambda_e}{2} \normF{\Z - \E \X}}_{\fe}.
\end{equation}
An advantage of energetic formulations is that it is easy to control the relative importance of the sub-objectives, whether they are fidelity or prior terms. As seen through this chapter, the model can be easily constructed by sequentially adding terms to the objective. They are as easily removed, or muted, when experimentations require it. Other advantages include good understanding, robustness, existence of solutions, design and analysis of optimization algorithms.

\paragraph{Auto-encoder model.}
Training the model is akin to minimize the objective function \eqnref{objective} over the variables, or model parameters $\Z$, $\D$ and $\E$:
\begin{multline} \label{eqn:model}
	%\text{given} \lambda_d, \lambda_z, \lambda_g, \lambda_e \geq 0, \X
	\minimize{\Z,\D,\E} \fd + \fz + \fg + \fe \\
	\st \cst{\d}{i} ,\ \cst{\e}{k} ,\ \forallx{i}{m} ,\ \forallx{k}{n}.
\end{multline}
Posing the problem as the minimization of an objective function is a sound expression of the model. Indeed, many laws of nature are nothing but optimality conditions, often expressed in terms of a minimum energy principle.

Note that the regularizations $\fz$ and $\fg$ on the internal representation are preventing the auto-encoder to learn the identity $\D = \E = \I$.

\paragraph{Encoder.}
The proposed model is an auto-encoder. Given its hyper-parameters $\lambda_d, \lambda_z, \lambda_g, \lambda_e \geq 0$ and a training set $\X$, model \eqnref{model} learns the auto-encoder variables, i.e. the dictionary $\D$ and the encoder $\E$. Then, given $\D$ and $\E$, the sparse and structured internal representation $\z^*$ of an unseen sample $\x$ is given by
\begin{equation} \label{eqn:z_exact}
	\z^* = \argmin{\z} \frac{\lambda_d}{2} \normT{\x - \D \z} + \lambda_z \normO{\z} + \frac{\lambda_g}{2} \inner{\z}{\L \z} + \frac{\lambda_e}{2} \normT{\z - \E \x},
\end{equation}
where the graph Laplacian $\L$ is constructed from the data.

\paragraph{Decoder.}
Similarly, the mapping of a representation $\z$ back to the input domain is given by
\begin{equation} \label{eqn:x_exact}
	\x^* = \argmin{\x} \frac{\lambda_d}{2} \normT{\x - \D \z} + \frac{\lambda_e}{2} \normT{\z - \E \x}.
\end{equation}

\section{Approximate schemes} \label{sec:approximate_schemes}
% In the next chapter ? It is not really a part of the model, but a use of it. Not really: third assumption.

\paragraph{Motivation.}
While encoder \eqnref{z_exact} extracts the exact representation given the auto-encoder model \eqnref{model}, it is computationally heavy. We aim at a faster encoder model to infer an approximate representation $\tilde{\z} \approx \z^*$.

\paragraph{Direct encoder.}
Neglecting some of the terms in \eqnref{z_exact} because of our model's third assumption given in \secref{assumptions}, the approximation:
\begin{equation} \label{eqn:z_approximate}
	\tilde{\z} = \argmin{\z} \frac{\lambda_e}{2} \normF{\z - \E \x} + \lambda_z \normO{\z}
\end{equation}
is able to infer good enough representations. Problem \eqnref{z_approximate} holds a closed-form solution:
\begin{equation} \label{eqn:z_direct}
	\tilde{\z} = h_{\lambda_z / \lambda_e} (\E \x),
\end{equation}
where $h_\lambda$ is the shrinkage function defined here by
\begin{equation} \label{eqn:shrinkage}
	h_\lambda(\x)_k = \sign(x_k) \left( |x_k| - \lambda \right)_+ ,
\end{equation}
where $(\cdot)_+ = \max(\cdot, 0)$. Therefore, the explicit encoder formulation introduced in \secref{encoder} allows a direct inference of the representation.

Note that although the dictionary $\D$ and the graph Laplacian $\L$ are not used in the approximate scheme \eqnref{z_direct}, they however add structure to the problem because $\E$ is learned simultaneously with $\D$ and a regularization which makes use of $\L$. Additional structure has been shown to enhance the behavior of the loss function and help sparse recovery \cite{kowalski2009sparse, baraniuk2010modelCS, huang2011LearningStructuredSparsity, jenatton2011structured}.

\paragraph{Direct decoder.}
Although we care less about decoder \eqnref{x_exact}, a similar approach may be used to approximate $\x^*$:
\begin{equation} \label{eqn:x_direct}
	\tilde{\x} = \argmin{\x} \frac{\lambda_d}{2} \normT{\x - \D \z} = \D \z.
\end{equation}

\chapter{Related works}
% That is clearly not a part of the model.
% How do we compare with other auto-encoders ?
% Our overall model may be thought as an hybrid between a sparse and a denoising auto-encoder.

\paragraph{Standard auto-encoders.}
Auto-encoders, as introduced by \cite{bourlard1988autoencoder, hinton1994autoencoder}, are defined by one linear hidden layer and the mean squared error criterion is used to train the network, i.e. their model is \eqnref{model} without any regularization.
Because of the lack of constraint, an auto-encoder with $n$-dimensional input and an encoding of dimension at least $n$ could potentially just learn the identity function. However, it as been shown that \gls{SGD} with early stopping is similar to an $\ell_2$ regularization of the parameters \cite{zinkevich2003SGDl2regularization}.

\paragraph{Sparse auto-encoders.}
Direct decoder \eqnref{x_direct} is the definition used for sparse auto-encoders \cite{lecun2006sparseAutoencoders, ranzato2007stackedSparseAutoencoders}. While the encoder definition may vary, it includes at least the first two terms of encoder \eqnref{z_exact}, related to sparse coding.

\paragraph{Predictive sparse decomposition.}
A technique introduced in \cite{lecun2010PSD} which, similarly to us, adds an explicit encoder to the sparse coding scheme. Their encoder architecture is very close to direct encoder \eqnref{z_direct}. Using sparse coding, their decoder is defined by direct decoder \eqnref{x_direct}.

\paragraph{Denoising auto-encoders.}
Denoising auto-encoders share the same model as the standard auto-encoders, but are trained with stochastically corrupted data  \cite{bengio2008denoisingAutoencoders}. They thus learn to undo the effect of the corruption. In model \eqnref{model}, the Dirichlet energy term promotes smooth variations along the data manifold. It has the effect of pushing noisy samples toward the manifold, effectively denoising them.
%This is essentially what a \gls{RBM} does \cite{hinton2002RBM}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\chapter{Optimization} \label{chap:optimization}
% Learning / Training
% Non-convex problem where each sub-problem / independant variable is a convex problem --> outer loop --> objective monotically decreasing.
% Batch (FISTA, primal-dual) and online (stochastic gradient descent) training
% Transductive learning
% Non-convex problem composed of convex sub-problems.
% Derive prox and gradients.
% Why we can use FISTA an PD ?
% Derive FISTA and primal-dual formulations.

% Three properties of non-convex optimization: monotony (of the energy function), convergence (local, global, critical points) and speed. Convergence is hard to prove.

The whole process of training the auto-encoder is to solve the non-convex optimization problem which defines model \eqnref{model}. Because of its non-convexity, there is no guarantee to find its global minimum.
%While there exist techniques for non-convex optimization, they are very slow.
The minimization will end up in a local minimum depending on the initialization. That is, given the same training data, different random initializations of the variables will lead to different solutions.
%{\color{red} Is there anything we can say about good enough local minima ? Or that many local minima are actually similar and we don't care in which we fall ?}

\paragraph{Convex sub-problems.}
As problem \eqnref{model} is non-convex only when all the variables are taken together, a natural way is to decompose it into three convex sub-problems:
\begin{align}
	\label{eqn:minZ}
	\minimize{\Z} & \fd + \fz + \fg + \fe , \\
	\label{eqn:minD}
	\minimize{\D} & \fd \st \cst{\d}{i} ,\ \forallx{i}{m} , \\
	\label{eqn:minE}
	\minimize{\E} & \fe \st \cst{\e}{k} ,\ \forallx{k}{n}.
\end{align}
Sub-problem \eqnref{minZ} is an $\ell_1$ and $\ell_2$ regularized least squares problem while sub-problems \eqnref{minD} and \eqnref{minD} are constrained least squares problems. Both of which can efficiently be solved by several convex optimization methods \cite{combettes2011proximalSplitting, beck2009FISTA, chambolle2011PrimalDual}.

\paragraph{Iterative scheme.}
The idea is to iteratively solve \eqnref{minZ}, \eqnref{minD} and \eqnref{minE}, i.e. to minimize the objective for one variable at a time while fixing the two others. While we have no convergence guarantee, the overall loss function is guaranteed to decrease monotonically if each convex sub-problem is optimally solved, which is the case if we let each sub-minimization converge.

\paragraph{Proximal splitting.}
Proximal splitting methods are a class of algorithms designed to solve convex problems of the form
\begin{equation} \label{eqn:convex_prob}
	\minimize{\x} f_1(\x) + f_2(\x),
\end{equation}
where $f_1$ is a convex but non-smooth function and $f_2$ is convex and differentiable with a $\beta$-Lipschitz continuous gradient $\nabla f_2$, i.e.,
\begin{equation}
	\forall(\x,\y) \ \ \|\nabla f_2(\x) - \nabla f_2(\y) \|_2 \leq \beta \| \x - \y \|_2 ,
\end{equation}
where $\beta \in \ ]0,+\infty[$.

It can be shown \cite{combettes2005forwardBackward} that problem \eqnref{convex_prob} admits at least one solution and that, for any $\gamma \in \ ]0,+\infty[$, its solutions are characterized by the fixed point equation
\begin{equation} \label{eqn:fixed_point}
	x = \prox{\gamma f_1} \left( \x - \gamma \nabla f_2(\x) \right),
\end{equation}
where $\prox{f}$ denotes the proximity operator of $f$, defined as the minimization problem
\begin{equation}
	\prox{f} \x = \minimize{\y} f(\y) + \frac{1}{2} \normT{\x - \y},
\end{equation}
which is an extension of the notion of a projection operator \cite{moreau1962prox}.
The fixed point equation \eqnref{fixed_point} suggests the possibility of iterating
\begin{equation}
	\x^{t+1} = \underbrace{\prox{\gamma_t f_1}}_\text{backward step} \underbrace{ \left( \x^t - \gamma \nabla f_2(\x^t) \right) }_\text{forward step}
\end{equation}
for values of the step-size parameter $\gamma_t$ in a suitable bounded interval which depends on $\beta$. This type of scheme is known as a \textit{forward-backward} splitting algorithm. It can be broken up into a forward (explicit) gradient
step using the function $f_2$, and a backward (implicit) step using the function $f_1$ \cite{combettes2011proximalSplitting}.

The \gls{FISTA} \cite{beck2009FISTA} is an efficient forward-backward scheme which exploits variable time steps and multiple points. It achieves an optimal \cite{nemirovsky1983optimalConvergence} $O(1/t^2)$ rate of convergence of the objective function.

\paragraph{Sub-problems casting.}
To be solved via \gls{FISTA}, each of the minimization sub-problems \eqnref{minZ}, \eqnref{minD} and \eqnref{minE} has to be cast to the form of \eqnref{convex_prob} and provide the gradient $\nabla f_2$, its Lipschitz constant $\beta$ and the proximity operator of $f_1$.

Sub-problem \eqnref{minZ} is split into a smooth and a non-smooth part:
\begin{equation}
	\minimize{\Z} \underbrace{\fd + \fg + \fe}_{f_2(\Z)} + \underbrace{\fz}_{f_1(\Z)}.
\end{equation}
The gradient $\nabla f_2$, its Lipschitz constant $\beta$ and the proximity operator of $f_1$ are as follows:
\begin{align}
	\nabla f_2(\Z) &= \lambda_d \D^T (\X - \D \Z) + \lambda_e (\Z - \E \X) + \lambda_g \L \Z \\
	\beta &\geq \lambda_e + \lambda_d \| \D^T \D \|_2 + \lambda_g \|\L\|_2 \\
	\prox{\beta^{-1} f_1}(\D) &= h_{\lambda_z / \beta} (\Z)
\end{align}
where $h_\lambda$ is the shrinkage function \eqnref{shrinkage}.

The constraint of \eqnref{minD} can be integrated in the loss function via the Lagrange multiplier method:
\begin{equation}
	\minimize{\D} \underbrace{\frac{\lambda_d}{2} \normF{\X^T - \Z^T\D^T}}_{f_2(\D)} + \underbrace{\iota_C(\D)}_{f_1(\D)},
\end{equation}
where $\iota_C$ is the indicator function of the subset $C \in \R^{n \times m}$ defined as
\begin{equation}
	\iota_C(\D) = \left\{ \begin{array}{rl}
	0, & \text{if } \cstd; \\
	+\infty, & \text{otherwise}.
	\end{array}\right.
\end{equation}
The gradient $\nabla f_2$, its Lipschitz constant $\beta$ and the proximity operator of $f_1$ are as follows:
\begin{align}
	\nabla f_2(\D) &= \lambda_d \Z (\X^T - \Z^T \D^T) \\
	\beta &\geq \lambda_d \| \Z \Z^T \|_2 \\
	\prox{\beta^{-1} f_1}(\D) &= \left\{ \frac{\d_i}{\max(1, \|\d_i\|_2)} \right\}_{i=1}^m.
\end{align}

Similarly, for \eqnref{minE}:
\begin{align}
	\nabla f_2(\E) &= \lambda_e \X (\Z^T - \X^T \E^T) \\
	\beta &\geq \lambda_e \| \X \X^T \|_2 \\
	\prox{\beta^{-1} f_1}(\E) &= \left\{ \frac{\e_k}{\max(1, \|\e_k\|_2)} \right\}_{k=1}^n.
\end{align}

%\section{Algorithm}
% How to write algorithms (with a frame) in Latex ?

%Given the hyper-parameters $\lambda_d \geq 0$, $\lambda_z \geq 0$, $\lambda_g \geq 0$, $\lambda_e \geq 0$ and a training set $\X \in \R^{n \times N}$.

%\paragraph{Initialization}
%Randomly initialize $\D$ and rescale each column to unit-norm to satisfy the constraint of problem \eqnref{model}. Initialize $\Z$ to zero. % sparse enough, cannot practically init to full because of mem.

%\section{Convex problem}
% What is a convex problem (objective and constraints).
% Methods to solve: analytically or numerical schemes.
%\section{Numerical solvers}
% Numerical: gradient descent --> slow --> second order methods like Newton
% Stochastic gradient descent
%\section{Proximal splitting methods}
% But our objective function is not differentiable
% Ridge regression is, but not the LASSO regularization.
% --> rely on a sub-gradient method, the proximity / proximal operator --> proximal splitting
%\subsection{Forward-backward}
% Different basic methods to solve these problems: forward-backward, douglas-rachford
% Explain the intuition
%\subsection{Douglas-Rachford}
%\subsection{FISTA}
% Advanced (faster) algorithms who exlpoit variable time steps and multiple points: FISTA
%\section{Dual problem}
% Introducing the dual problem (paper playing with duality)
%\subsection{Primal-dual}


\documentclass[utf8x,hyperref={pdfpagelabels=false}]{beamer}

\usepackage[utf8x]{inputenc}
\usepackage[OT1]{fontenc}
\usepackage{amsmath,graphicx}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{tikz}

\usetikzlibrary{arrows}
\usetikzlibrary{calc}

\usetheme{Malmoe}  % Now it's a beamer presentation with the lisa theme!
\usecolortheme{beaver}
\setbeamertemplate{footline}[page number]
\setbeamertemplate{navigation symbols}{}
\mode<presentation>

\title{Recurrent Neural Networks in Theano}

\author{%
    \footnotesize
    \newline
    \newline
Phil\'{e}mon Brakel \newline
Institut des algorithmes d'apprentissage de Montréal \newline
Montreal Institute for Learning Algorithms \newline
Université de Montréal
}
\date{August 11th, Deep Learning Summer School 2015, Montréal}

\definecolor{termblue}{RGB}{51, 51, 179}
\newcommand{\term}[1]{\textcolor{termblue}{#1}}

\begin{document}

\begin{frame}[plain]
 \titlepage
 \vspace{-2em}
 \includegraphics[width=.8in]{mila.png}
 \hfill
 \includegraphics[width=.8in]{UdeM_logo}
\end{frame}

\setcounter{page}{1}

\section{Introduction}

\begin{frame}{Recurrent Neural Networks}
    Neural networks that can process sequences of inputs
    \begin{itemize}
        \item Used to process speech, language, music,\dots
        \item Recurrent Neural Networks are very powerful:
            \begin{itemize}
            \item Non-linear
            \item Distributed representations
            \item No Markov assumptions
            \end{itemize}
        \item However:
            \begin{itemize}
            \item Optimization can be challenging
            \item Learning \emph{long-term dependencies} is difficult
            \item Computations are not as easy to \emph{parallelize}
            \end{itemize}
    \end{itemize}
\end{frame}

\begin{frame}
    \frametitle{Standard Architecture}
    \begin{align*}
        h_t &= q(W_{xh}x_t + W_{hh}h_{t-1} + b_h)\\
        y_t &= r(W_{hy}h_{t} + b_y)
    \end{align*}
\begin{figure}[t]
\centering
\begin{tikzpicture}[->,thick]
\scriptsize
\tikzstyle{main}=[circle, minimum size = 7mm, thick, draw =black!80, node distance = 12mm]
\foreach \name in {1,...,6}
    \node[main, fill = white!100] (y\name) at (\name,1.5) {$\mathbf{y}_\name$};
\foreach \name in {1,...,6}
    \node[main, fill = white!100] (h\name) at (\name,0) {$\mathbf{h}_\name$};
\foreach \name in {1,...,6}
    \node[main, fill = white!100] (x\name) at (\name,-1.5) {$\mathbf{x}_\name$};
\foreach \h in {1,...,6}
       {
        \path (x\h) edge (h\h);
        \path (h\h) edge (y\h);
       }
\foreach \current/\next in {1/2,2/3,3/4,4/5,5/6}
       {
        \path (h\current) edge (h\next);
       }
    %\node[main] (G-\name) at (\x,0) {$\name$};
\end{tikzpicture}
\caption{A simple Recurrent Neural Network}
\end{figure}
\end{frame}

\begin{frame}
\frametitle{Long Short-Term Memory (LSTM)}
\begin{figure}[t]
\centering
\includegraphics[scale=.7]{lstmjozefowicz15}
\caption{LSTM: Learn long term dependencies by asserting control over what goes in and out of \emph{memory cells}.\footnotemark}
\end{figure}
\footnotetext{Figure Taken from Jozefowicz et al. (2015)}
\end{frame}

\begin{frame}
\frametitle{Long Short-Term Memory (LSTM)}
\begin{figure}[t]
\centering
\includegraphics[scale=1.0]{lstmgraves}
\caption{Another LSTM\footnote{Figure from Graves et al. (2013)}}
\end{figure}
\end{frame}

\begin{frame}
    \frametitle{Update Equations}
    \begin{align*}
        i_t &= \tanh(W_{xi}x_t + W_{hi}h_{t-1} + b_i)\\
        j_t &= \sigma(W_{xj}x_t + W_{hj}h_{t-1} + b_j)\\
        f_t &= \sigma(W_{xf}x_t + W_{hf}h_{t-1} + b_f)\\
        o_t &= \sigma(W_{xo}x_t + W_{ho}h_{t-1} + b_o)\\
        c_t &= f_t \otimes c_{t-1} + i_t \otimes j_t\\
        h_t &= \tanh(c_t) \otimes o_t
    \end{align*}
\end{frame}

\begin{frame}{RNNs can be Stacked}
\begin{figure}[t]
\centering
\begin{tikzpicture}[->,thick]
\scriptsize
\tikzstyle{main}=[circle, minimum size = 7mm, thick, draw =black!80, node distance = 12mm]
\foreach \name in {1,...,6}
    \node[main, fill = white!100] (l\name) at (\name,2.3) {$\mathbf{h}^2_\name$};
\foreach \name in {1,...,6}
    \node[main, fill = white!100] (k\name) at (\name,3.1) {$\hat{\mathbf{h}}^2_\name$};
\foreach \name in {1,...,6}
    \node[main, fill = white!100] (i\name) at (\name,.8) {$\hat{\mathbf{h}}^1_\name$};
\foreach \name in {1,...,6}
    \node[main, fill = white!100] (h\name) at (\name,0) {$\mathbf{h}^1_\name$};
\foreach \name in {1,...,6}
    \node[main, fill = white!100] (x\name) at (\name,-1.5) {$\mathbf{x}_\name$};
\foreach \h in {1,...,6}
       {
        \path (x\h) edge [bend right] (i\h);
        \path (x\h) edge (h\h);
        \path (i\h) edge [bend left] (k\h);
        \path (h\h) edge [bend right] (l\h);
        \path (h\h) edge [bend left] (k\h);
        \path (i\h) edge (l\h);
       }
\foreach \current/\next in {1/2,2/3,3/4,4/5,5/6}
       {
        \path (i\next) edge (i\current);
        \path (h\current) edge (h\next);
        \path (k\next) edge (k\current);
        \path (l\current) edge (l\next);
       }
    %\node[main] (G-\name) at (\x,0) {$\name$};
\end{tikzpicture}
\caption{Two Bidirectional Recurrent Neural Networks stacked on top of each
other.}
\label{fig:birnn}
\end{figure}
\end{frame}

\section{RNNs In Practice}
\begin{frame}
    \frametitle{Parallelizing RNN computations}
    Apply RNNs to \emph{batches} of sequences\\
        Present the data as a 3D tensor of $(T \times B \times F)$.
        Each dynamic update will now be a matrix multiplication.
\begin{figure}
\centering
\resizebox{4cm}{2.5cm}{
\begin{tikzpicture}
\tikzset{every cell/.style={draw=white}}
\tikzset{every cell 1/.style={fill=gray}}

   \foreach \row [count=\y] in {%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}}
     \foreach \cell [count=\x] in \row  {
        \path [every cell/.try, every cell \cell/.try]
        (\x,-\y) rectangle ++(1,1);
       }
\end{tikzpicture}
}
\end{figure}
\end{frame}

\begin{frame}{Binary Masks}
    A \emph{mask} matrix may be used to aid with computations that ignore the padded zeros.
    In Theano this may be required to keep computations \emph{differentiable}.
\begin{figure}
\centering
\resizebox{8cm}{5cm}{
\begin{tikzpicture}
\tikzset{every cell/.style={draw=black}}
\tikzset{every cell 1/.style={draw=black}}

   \foreach \row [count=\y] in {%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}}
     \foreach \cell [count=\x] in \row  {
        \path [every cell/.try, every cell \cell/.try]
        (\x,-\y) rectangle ++(1,1) node[below left] {$\cell$};
       }
\end{tikzpicture}
}
\end{figure}
\end{frame}

\begin{frame}{Binary Masks}
    It may be necessary to (partially) sort your data.
\begin{figure}
\centering
\resizebox{8cm}{5cm}{
\begin{tikzpicture}
\tikzset{every cell/.style={draw=black}}
\tikzset{every cell 1/.style={fill=gray}}

   \foreach \row [count=\y] in {%
     {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},%
     {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},%
     {1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},%
     {1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},%
     {1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},%
     {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}
     \foreach \cell [count=\x] in \row  {
        \path [every cell/.try, every cell \cell/.try]
        (\x,-\y) rectangle ++(1,1);
       }
\end{tikzpicture}
}
\end{figure}
\end{frame}

\begin{frame}{Final Notes}
    \begin{itemize}
        \item<1-> Fuel has a transformer that automatically padds a batch of sequences and adds a mask
        \item<1-> Since masks are often used for multiplication, their type should often be floating point
        \item<1-> Be careful that your implementation doesn't nest \texttt{scan} nodes
        \item<2-> Have fun!
    \end{itemize}
\end{frame}

\end{document}