2

As a part of a homework assignment in a Latex course, I have been assigned to write this algorithm in Latex. AdaBoost Algorithm

I have got most of the part done with this code:-

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx, blindtext}
\usepackage{algorithm2e}
\usepackage{amsmath}

\begin{document}
    \RestyleAlgo{boxed}
    \begin{algorithm}[h!]
        \DontPrintSemicolon
        \SetKwInput{kwInput}{Input}
        \SetKwInput{kwInit}{Initialization}
        \SetKwInput{kwMain}{Forward Inclusion}
        \SetKwInput{kwOutput}{Output}
        \kwInput{
        \par
        \vspace{0.25cm}
            Training examples $ Z = \{(x_1 , y_1 ), . . . , (x_N , y_N )\} $, where $N = a + b$, of which $a$ examples have $y_i = 1$ and $b$ examples have $ y_i = −1 $\;
            \vspace{0.25cm}
            The number $M$ of weak classifiers to be combined.

        }
        \kwInit{
        \par
        \vspace{0.25cm}
        \Indp{
            $
                w_i^{(0)}= \begin{cases}
                    \frac{1}{2a} & \text{$for\ those\ examples\ with\ y_i=1$} \\
                    \frac{1}{2b} & \text{$otherwise$}
                \end{cases}
            $
        }
        \vspace{0.25cm}
        }
        \kwMain{
        \par
        \vspace{0.25cm}
            \For{m = 1, . . . , M}{
                Choose optimal $h_m$ to minimize the weighted error\;
                Choose $a_m$ according to (2.17)\;
                Update $w_i^{(m)} \gets w_i^{(m-1)} e^{-y_ia_mh_m(x_i)}$ and normalize to $\sum_i{w_i^{(m)}} = 1$\;
            }
        \vspace{0.25cm}
        }
        \kwOutput{
        \par
            Classification Function: $H_M(x)$ as in (2.20)\;
            Class Label Prediction: $\hat{y}(x) = sgn(H_M(x))$\;
        }
        \caption{AdaBoost Learning Algorithm [35]}
    \end{algorithm}

\end{document}

However, I am unable to create a new Keyword (Like \kwInit in the code above) which has its own associated end, and auto-indented code segment.

Being new to the Latex typesetting system, any help is highly welcome.

For reference, my code produces output that looks like this:-

My AdaBoost Latex Code O/p

1

Just replace the \SetKwInput{word} with \SetKwBlock{beginword}{endword}. See algorithm2emanual Section 11.2 page 35. This modification provides

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx, blindtext}
\usepackage{algorithm2e}
\usepackage{amsmath}

\begin{document}
    \RestyleAlgo{boxed}
    \begin{algorithm}[h!]
        \DontPrintSemicolon
        \SetKwInput{kwInput}{Input}
        \SetKwBlock{kwInit}{Initialization}{end}
        \SetKwBlock{kwMain}{Forward Inclusion}{end}
        \SetKwInput{kwOutput}{Output}
        \kwInput{
        \par
        \vspace{0.25cm}
            Training examples $ Z = \{(x_1 , y_1 ), . . . , (x_N , y_N )\} $, where $N = a + b$, of which $a$ examples have $y_i = 1$ and $b$ examples have $ y_i = −1 $\;
            \vspace{0.25cm}
            The number $M$ of weak classifiers to be combined.

        }
        \kwInit{
        \par
        \vspace{0.25cm}
        \Indp{
            $
                w_i^{(0)}= \begin{cases}
                    \frac{1}{2a} & \text{$for\ those\ examples\ with\ y_i=1$} \\
                    \frac{1}{2b} & \text{$otherwise$}
                \end{cases}
            $
        }
        \vspace{0.25cm}
        }
        \kwMain{
        \par
        \vspace{0.25cm}
            \For{m = 1, . . . , M}{
                Choose optimal $h_m$ to minimize the weighted error\;
                Choose $a_m$ according to (2.17)\;
                Update $w_i^{(m)} \gets w_i^{(m-1)} e^{-y_ia_mh_m(x_i)}$ and normalize to $\sum_i{w_i^{(m)}} = 1$\;
            }
        \vspace{0.25cm}
        }
        \kwOutput{
        \par
            Classification Function: $H_M(x)$ as in (2.20)\;
            Class Label Prediction: $\hat{y}(x) = sgn(H_M(x))$\;
        }
        \caption{AdaBoost Learning Algorithm [35]}
    \end{algorithm}

\end{document}

enter image description here

2
  • Thanks! I have been stuck for hours trying to wade through the docs for this. P.S. You've put my code and your output in the answer ;)
    – saketk21
    Nov 5 '18 at 17:10
  • @saketk21 Happy to help, this is the very purpose of this place. I corrected the code.
    – BambOo
    Nov 5 '18 at 17:14

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.