How to write an algorithm in algorithm environment in Latex?

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

• Hi Anurag, could you put your exact question in your posting? I'm guessing you're struggeling to write pseudocode/an algorithm in the algorithm environment? – user1729210 Mar 4 at 5:53
• Hello, please review the proposed answer, If you like it and/or it was helpful, please consider upvoting (by clicking on the arrows next to the score) and/or marking it as the accepted answer (by clicking on the checkmark ✓) – BambOo Mar 27 at 8:59

1 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 }.

• Please add a complete example that starts with \documentclass and ends with \end{document}, and can be compiled. If you post code here, put the code in the cursor and press the {} button to mark it as code. – user194703 Mar 4 at 7:31
• @KersouMan - Many thanks for editing the answer and for bringing the code closer to being compilable. Unfortunately, it's still not fully compilable, as information is missing on what to do with \State, \For, \While, etc. – Mico Mar 4 at 8:51
• @Mico - Normally, with the last edit, it should work. The algorithmic environment requires full capital letters commands (\STATE, \FOR, etc.) to work. – KersouMan Mar 4 at 11:32