我在使用演算法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}
一些注意事項:
輸入和輸出可以分別使用
\KwIn
和指定\KwOut
。應該是的數學對象大膽的
\mathbf
(如向量)通常使用(或\bm
,需要\usepackage{bm}
在 後)設定\usepackage{amsmath}
。使用
\repeat
而不是\while
為了在最後指定循環條件。數學相關內容需要數學模式。這通常適用於 λ、η 和 σ 等 unicode 輸入。
使用
\mid
代替|
來提供條件功能。嵌套
\For
s 以使其結構層出現在輸出中。在數學中使用
\dots
省略號而不是省略號。...