0

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 
2
  • 2
    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? Mar 4 '20 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 '20 at 8:59
4
\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

3
  • 3
    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 '20 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 '20 at 8:51
  • 2
    @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 '20 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.