如何在Latex中的算法环境中编写算法?

如何在Latex中的算法环境中编写算法?

如何在 Latex 的算法环境中编写以下算法/伪代码?

train_ANN(fi,wi,oj)
For epochs from 1 to N
       While (j<=m)
       Randomly initialize wi={w1,w2…..wn}
       input oj={o1,o2…..om} in the input layer
       forward propagate (fi*wi) through layers until getting the predicted result y
       compute e=y-y^
       back propagate e from right to left through layers
            update wi
       end 

答案1

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}

\begin{document}

\begin{algorithm}
    \caption{Text of the caption}\label{your_label}
    \begin{algorithmic}
        \STATE  $\mathrm{train\_ANN} (f_i,w_i,o_j)$
        \FOR{epochs = $1$ to $N$}
            \WHILE{$(j\le m)$}
                \STATE Randomly initialize $w_i=\{w_1,w_2,\dots,w_n\}$
                \STATE input $o_j=\{o_1,o_2,\dots,o_m\}$ in the input layer
                \STATE forward propagate $(f_i\cdot w_i)$ through layers until getting the predicted result $y$
                \STATE compute $e=y-y^2$
                \STATE back propagate $e$ from right to left through layers
                \STATE update $w_i$
            \ENDWHILE
        \ENDFOR
    \end{algorithmic}
\end{algorithm}

\end{document}

注意使用关键字(必须大写!)\STATE\FOR\ENDFOR、和\WHILE\ENDWHILE以及 LaTeX 数学符号,例如 \le代替<=\dots代替…..和显式下标符号,例如w_i代替wi。最后,要显示花括号,请使用\{\}、而不是{}

在此处输入图片描述

相关内容