LaTeX實戰經驗:如何寫算法


LaTeX中實現算法的呈現主要有兩種方式:

  • 使用宏包algorithm2e, 這個宏包有很多可選項進行設定。
  • 使用宏包algorithm 與 algorithmic

使用宏包algorithm2e

\usepackage[linesnumbered,boxed,ruled,commentsnumbered]{algorithm2e}%%算法包,注意設置所需可選項

 

例子:

\IncMargin{1em} % 使得行號不向外突出

\begin{algorithm}

 

    \SetAlgoNoLine % 不要算法中的豎線

    \SetKwInOut{Input}{\textbf{輸入}}\SetKwInOut{Output}{\textbf{輸出}} % 替換關鍵詞

 

    \Input{

        \\

        The observed user-item pair set $S$\;\\

        The feature matrix of items $F$\;\\

        The content features entities $A := \{A^u,A^v\}$\;\\}

    \Output{

        \\

        $\Theta \  := \{Y^u,Y^v\}$\;\\

        $W := \{W^u,W^v\}$\;\\}

    \BlankLine

 

    initialize the model parameter $\Theta$ and $W$ with uniform $\left(-\sqrt{6}/{k},\sqrt{6}/{k}\right)$\; % 分號 \; 區分一行結束

    standarized $\Theta$\;

    Initialize the popularity of categories $\rho$ randomly\;

    \Repeat

        {\text{convergence}}

        {Draw a triple $\left(m,i,j\right)$ with 算法\ref{al2}\;

            \For {each latent vector $\theta \in \Theta$}{

                $\theta \leftarrow \theta - \eta\frac{\partial L}{\partial \theta}$

            }

            \For {each $W^e \in W$}{

                Update $W^e$ with the rule defined in Eq.\ref{equ:W}\;

            }  

        }

    \caption{Learning paramters for BPR\label{al3}}

\end{algorithm}

\DecMargin{1em}

 

 

使用宏包algorithmalgorithmic

\usepackage{algorithm, algorithmic}

 

\begin{algorithm}

         \renewcommand{\algorithmicrequire}{\textbf{Input:}}

         \renewcommand{\algorithmicensure}{\textbf{Output:}}

         \caption{Bayesian Personalized Ranking Based Latent Feature Embedding Model}

         \label{alg:1}

         \begin{algorithmic}[1]

                   \REQUIRE latent dimension $K$, $G$, target predicate $p$

                   \ENSURE $U^{p}$, $V^{p}$, $b^{p}$

                   \STATE Given target predicate $p$ and entire knowledge graph $G$, construct its bipartite subgraph, $G_{p}$

                   \STATE $m$ = number of subject entities in $G_{p}$

                   \STATE $n$ = number of object entities in $G_{p}$

                   \STATE Generate a set of training samples $D_{p} = \{(s_p, o^{+}_{p}, o^{-}_{p})\}$ using uniform sampling technique

                   \STATE Initialize $U^{p}$ as size $m \times K$ matrix with $0$ mean and standard deviation $0.1$

                   \STATE Initialize $V^{p}$ as size $n \times K$ matrix with $0$ mean and stardard deviation $0.1$

                   \STATE Initialize $b^{p}$ as size $n \times 1$ column vector with $0$ mean and stardard deviation $0.1$

                   \FORALL{$(s_p, o^{+}_{p}, o^{-}_{p}) \in D_{p}$}

                   \STATE Update $U_{s}^{p}$ based on Equation~\ref{eq:sgd1}

                   \STATE Update $V_{o^{+}}^{p}$ based on Equation~\ref{eq:sgd2}

                   \STATE Update $V_{o^{-}}^{p}$ based on Equation~\ref{eq:sgd3}

                   \STATE Update $b_{o^{+}}^{p}$ based on Equation~\ref{eq:sgd4}

                   \STATE Update $b_{o^{-}}^{p}$ based on Equation~\ref{eq:sgd5}

                   \ENDFOR

                   \STATE \textbf{return} $U^{p}$, $V^{p}$, $b^{p}$

         \end{algorithmic} 

\end{algorithm}

 

重新定義require和ensure命令對應的關鍵字(此處將默認的Require/Ensure自定義為Input/Output)

\renewcommand{\algorithmicrequire}{\textbf{Input:}} 
\renewcommand{\algorithmicensure}{\textbf{Output:}}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM