Algorithm2e 缺少 $ 插入。$ \標題

Algorithm2e 缺少 $ 插入。$ \標題

我在使用演算法2e包時遇到問題。以下是我的乳膠代碼:

\begin{algorithm}[H]
\SetAlgoLined
\Comment{Input: Training data S, regularization parameters λ, learning rate η, initialization σ }

\Comment{Output: $\Theta = (w_{0},\textbf{w},\textbf{V})$}
initialization\; 
\While{stopping criterion is not met}{
    \For{\ (x,y) \in S}
    {
        w_{0}\leftarrow w_{0}-$η$(\frac{\partial}{\partialw_{0}}l(y(x|\Theta),y)                                +2\lambda^{0}w_{0});
    }
    \For{\ i \in \{1,...p\}  \wedge x_{i} \neq 0 }
    {
    w_{i}\leftarrow w_{i} - $η$(\frac{\partial}{\partialw_{i}}l(y(x|\Theta),y)+2\lambda_{\pi}^{w}w_{i});
    }
    For{ f \in \{1,...k\} }     
    {
    v_{i,f}\leftarrow v_{i,f}-$η$(\frac{\partial}{\partialv_{i,f}l(y(x|\Theta),y)+2\lambda_{f,\pi(i)}^{v}v_{i,f});
    }
    }
}
 \caption{Stochastic gradient descent}
\end{algorithm}

我遇到如標題這樣的錯誤,結果應該是這樣的,謝謝。

在此輸入影像描述

答案1

以下複製了預期的輸出:

在此輸入影像描述

\documentclass{article}

\usepackage{amsmath}

\usepackage[noline,ruled]{algorithm2e}
\setlength{\algomargin}{7.5pt}

\begin{document}

\begin{algorithm}[H]
 \caption{Stochastic Gradient Descent (SGD)}
  \KwIn{Training data $S$, regularization parameters $\lambda$, learning rate $\eta$, initialization $\sigma$}
  \KwOut{Model parameters $\Theta = (w_0,\mathbf{w},\mathbf{V})$}
  $w_0 \leftarrow 0$; $\mathbf{w} \leftarrow (0,\dots,0)$; $\mathbf{V} \sim \mathcal{N}(0,\sigma)$\; 
  \Repeat{stopping criterion is not met}{
    \For{$(x,y) \in S$}
    {
      $w_0 \leftarrow w_0 - \eta(\frac{\partial}{\partial w_0} l( y(\mathbf{x} \mid \Theta), y) + 2\lambda^0 w_0)$\;
      \For{$i \in \{1,\dots,p\} \wedge x_i \neq 0$}
      {
        $w_i \leftarrow w_i - \eta(\frac{\partial}{\partial w_i} l( y(x \mid \Theta), y) + 2\lambda_{\pi}^w w_i)$\;
        \For{$f \in \{1,\dots,k\}$}     
        {
          $v_{i,f} \leftarrow v_{i,f} - \eta(\frac{\partial}{\partial v_{i,f}} l( y(x \mid \Theta), y) + 2\lambda_{f,\pi(i)}^v v_{i,f})$\;
        }
      }
    }
  }
\end{algorithm}

\end{document}

一些注意事項:

  1. 輸入和輸出可以分別使用\KwIn和指定\KwOut

  2. 應該是的數學對象大膽的\mathbf(如向量)通常使用(或\bm,需要\usepackage{bm}在 後)設定\usepackage{amsmath}

  3. 使用\repeat而不是\while為了在最後指定循環條件。

  4. 數學相關內容需要數學模式。這通常適用於 λ、η 和 σ 等 unicode 輸入。

  5. 使用\mid代替|來提供條件功能。

  6. 嵌套\Fors 以使其結構層出現在輸出中。

  7. 在數學中使用\dots省略號而不是省略號。...

相關內容