[Tex/LaTex] How to write an algorithm in algorithm environment in Latex

algorithm2ealgorithmicalgorithmsalgpseudocodelists

How do I write the following algorithm/pseudocode in algorithm environment in 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 

Best Answer

\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}

Observe the use of the keywords (uppercase essential!) \STATE, \FOR and \ENDFOR, and \WHILE and \ENDWHILE as well as of LaTeX math notation, e.g., \le in place of <=, \dots in place of ….., and explicit subscript notation, e.g., w_i instead of wi. Finally, to display curly braces, use \{ and \}, not { and }.

enter image description here

Related Question