LATEX编写算法的代码

>> 点击此处查看 【 LaTeX实战经验:新手须知 】

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

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

  • 使用宏包algorithm 与 algorithmic, 好像挺多人喜欢用,周志华老师的<<机器学习>>一书中的算法描述应该就是使用的这两个宏包。

使用宏包algorithm2e

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

algorithm


  
  
  1. \IncMargin{1em} % 使得行号不向外突出
  2. \begin{algorithm}
  3. \SetAlgoNoLine % 不要算法中的竖线
  4. \SetKwInOut{Input}{\textbf{输入}}\SetKwInOut{Output}{\textbf{输出}} % 替换关键词
  5. \Input{
  6. \\
  7. The observed user-item pair set $S$\;\\
  8. The feature matrix of items $F$\;\\
  9. The content features entities $A := \{A^u,A^v\}$\;\\}
  10. \Output{
  11. \\
  12. $\Theta \ := \{Y^u,Y^v\}$\;\\
  13. $W := \{W^u,W^v\}$\;\\}
  14. \BlankLine
  15. initialize the model parameter $\Theta$ and $W$ with uniform $\left(-\sqrt{6}/{k},\sqrt{6}/{k}\right)$\; % 分号 \; 区分一行结束
  16. standarized $\Theta$\;
  17. Initialize the popularity of categories $\rho$ randomly\;
  18. \Repeat
  19. {\text{convergence}}
  20. {Draw a triple $\left(m,i,j\right)$ with 算法\ref{al2}\;
  21. \For {each latent vector $\theta \in \Theta$}{
  22. $\theta \leftarrow \theta - \eta\frac{\partial L}{\partial \theta}$
  23. }
  24. \For {each $W^e \in W$}{
  25. Update $W^e$ with the rule defined in Eq.\ref{equ:W}\;
  26. }
  27. }
  28. \caption{Learning paramters for BPR\label{al3}}
  29. \end{algorithm}
  30. \DecMargin{1em}

使用宏包algorithmalgorithmic

\usepackage{algorithm, algorithmic}
  
  

这里写图片描述


  
  
  1. \begin{algorithm}
  2. \renewcommand{\algorithmicrequire}{\textbf{Input:}}
  3. \renewcommand{\algorithmicensure}{\textbf{Output:}}
  4. \caption{Bayesian Personalized Ranking Based Latent Feature Embedding Model}
  5. \label{alg:1}
  6. \begin{algorithmic}[1]
  7. \REQUIRE latent dimension $K$, $G$, target predicate $p$
  8. \ENSURE $U^{p}$, $V^{p}$, $b^{p}$
  9. \STATE Given target predicate $p$ and entire knowledge graph $G$, construct its bipartite subgraph, $G_{p}$
  10. \STATE $m$ = number of subject entities in $G_{p}$
  11. \STATE $n$ = number of object entities in $G_{p}$
  12. \STATE Generate a set of training samples $D_{p} = \{(s_p, o^{+}_{p}, o^{-}_{p})\}$ using uniform sampling technique
  13. \STATE Initialize $U^{p}$ as size $m \times K$ matrix with $0$ mean and standard deviation $0.1$
  14. \STATE Initialize $V^{p}$ as size $n \times K$ matrix with $0$ mean and stardard deviation $0.1$
  15. \STATE Initialize $b^{p}$ as size $n \times 1$ column vector with $0$ mean and stardard deviation $0.1$
  16. \FORALL{$(s_p, o^{+}_{p}, o^{-}_{p}) \in D_{p}$}
  17. \STATE Update $U_{s}^{p}$ based on Equation~\ref{eq:sgd1}
  18. \STATE Update $V_{o^{+}}^{p}$ based on Equation~\ref{eq:sgd2}
  19. \STATE Update $V_{o^{-}}^{p}$ based on Equation~\ref{eq:sgd3}
  20. \STATE Update $b_{o^{+}}^{p}$ based on Equation~\ref{eq:sgd4}
  21. \STATE Update $b_{o^{-}}^{p}$ based on Equation~\ref{eq:sgd5}
  22. \ENDFOR
  23. \STATE \textbf{return} $U^{p}$, $V^{p}$, $b^{p}$
  24. \end{algorithmic}
  25. \end{algorithm}
此文章转载自:https://blog.csdn.net/simple_the_best/article/details/52710794
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值