How can I create the following algorithm block in Latex? I have everything except the init block. How can I create this?

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{algorithm2e}    

\begin{document}

\begin{algorithm}[H]
\DontPrintSemicolon
\SetAlgoLined
\KwData{$D$ = $<u,i,r>$}
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
\Input{feat, iters, $\lambda$, $\eta$}
\Output{$A$, $B$}
\For{count $\leftarrow 1$ \KwTo iters}{
    $D \leftarrow$ Shuffle($D$) \;
    \For{$<u,i,r> \in D$}{
        $err_{ui} \leftarrow r - A_uB_i^T$ \;
        $A_u \leftarrow A_u + \eta(err_{ui}B_i - \lambda A_u)$ \;
        $B_i \leftarrow B_i + \eta(err_{ui}A_u - \lambda B_i)$ \;
    }
}
\caption{Stochastic Gradient Descent}
\end{algorithm}

\end{document}

Algorithm example

Is there an online editor/generator for algorithm blocks in Latex?

  • 2
    Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. • Note this is a Q&A site, not a please do this for me service. What do you have so far. Searching this side will give you some hints to start. – albert Dec 6 at 14:38
  • @albert you are right. I have provided a minimal working example. How this helps. – Dimitris Poulopoulos Dec 6 at 15:03

You can use \SetKwProg to create your own block:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{algorithm2e}    
\SetKwProg{Init}{init}{}{}
\begin{document}

\begin{algorithm}[H]
\DontPrintSemicolon
\SetAlgoLined
\KwData{$D$ = $<u,i,r>$}
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
\Input{feat, iters, $\lambda$, $\eta$}
\Output{$A$, $B$}
\init{
\For{$u \in Users(D)$}{
    $A_u \leftarrow$ Vector(size: $feat)
}
\For{count $\leftarrow 1$ \KwTo iters}{
    $D \leftarrow$ Shuffle($D$) \;
    \For{$<u,i,r> \in D$}{
        $err_{ui} \leftarrow r - A_uB_i^T$ \;
        $A_u \leftarrow A_u + \eta(err_{ui}B_i - \lambda A_u)$ \;
        $B_i \leftarrow B_i + \eta(err_{ui}A_u - \lambda B_i)$ \;
    }
}
\caption{Stochastic Gradient Descent}
\end{algorithm}

\end{document}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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