zoukankan      html  css  js  c++  java
  • LaTeX实战经验:如何写算法

    LaTeX中实现算法的呈现主要有两种方式:

    • 使用宏包algorithm2e, 这个宏包有很多可选项进行设定。
    • 使用宏包algorithm 与 algorithmic

    使用宏包algorithm2e

    usepackage[linesnumbered,boxed,ruled,commentsnumbered]{algorithm2e}%%算法包,注意设置所需可选项

     

    例子:

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

    egin{algorithm}

        SetAlgoNoLine % 不要算法中的竖线

        SetKwInOut{Input}{ extbf{输入}}SetKwInOut{Output}{ extbf{输出}} % 替换关键词

        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} ight)$; % 分号 ; 区分一行结束

        standarized $Theta$;

        Initialize the popularity of categories $ ho$ randomly;

        Repeat

            { ext{convergence}}

            {Draw a triple $left(m,i,j ight)$ with 算法 ef{al2};

                For {each latent vector $ heta in Theta$}{

                    $ heta leftarrow heta - etafrac{partial L}{partial heta}$

                }

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

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

                }  

            }

        caption{Learning paramters for BPRlabel{al3}}

    end{algorithm}

    DecMargin{1em}

    使用宏包algorithmalgorithmic

    usepackage{algorithm, algorithmic}

    egin{algorithm}

             enewcommand{algorithmicrequire}{ extbf{Input:}}

             enewcommand{algorithmicensure}{ extbf{Output:}}

             caption{Bayesian Personalized Ranking Based Latent Feature Embedding Model}

             label{alg:1}

             egin{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 imes K$ matrix with $0$ mean and standard deviation $0.1$

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

                       STATE Initialize $b^{p}$ as size $n imes 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~ ef{eq:sgd1}

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

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

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

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

                       ENDFOR

                       STATE extbf{return} $U^{p}$, $V^{p}$, $b^{p}$

             end{algorithmic} 

    end{algorithm}

    重新定义require和ensure命令对应的关键字(此处将默认的Require/Ensure自定义为Input/Output)

    enewcommand{algorithmicrequire}{ extbf{Input:}} 
    enewcommand{algorithmicensure}{ extbf{Output:}}

  • 相关阅读:
    极光推送SDK通过泰尔终端实验室检测,符合统一推送接口标准
    极光小课堂|手把手教你做接口测试
    一键登录怎么在iOS端实现?这篇文章教会你!
    一键登录已成大势所趋,Android端操作指南来啦!
    极光一键登录:更快捷、安全的登录认证方式,简单集成即可实现
    跨浏览器问题的五种解决方案
    Laravel 搭建 Composer 包,实现配置 Config、门面 Facade、服务 Service、发布到 Packagist
    How to Install ClamAV on Ubuntu 20.04 and Scan for Vulnerabilities
    单点登录(SSO)看这一篇就够了
    一口气说出 OAuth2.0 的四种授权方式
  • 原文地址:https://www.cnblogs.com/USTBlxq/p/6509969.html
Copyright © 2011-2022 走看看