算法的文字描述

算法的文字描述

我想用非常冗长的方式编写一个算法。我想编写几个for循环,其余的几乎都是文本。我在这里包含了我正在使用的代码,algpseudocode如下所示:

\usepackage[noend]{algpseudocode}
\usepackage{algorithm}

算法代码:

\begin{algorithm}
  \caption{Backpropagation learning algorithm}
  \begin{algorithmic}
  \For {d in data}
    Forwards Pass \hfill \\
      Starting from the input layer, use eq. \ref{update} to do a forward pass trough the network, computing the activities of the neurons at each layer.
    Backwards Pass \hfill \\
    Compute the derivatives of the error function with respect to the output layer activities
      \For {layer in layers}
        Compute the derivatives of the error function with respect to the inputs of the upper layer neurons
        Compute the derivatives of the error function with respect to the weights between the outer layer and the layer below
        Compute the derivatives of the error function with respect to the activities of the layer below
      \EndFor
      Updates the weights.
\EndFor
\end{algorithmic}

\end{algorithm}

当然,这是可行的,但它完全是黑客行为,而且渲染效果非常糟糕。从该软件包的文档来看,我认为它不支持这一点。

我说得对吗?我应该使用其他包吗?

编辑:我希望最终的输出看起来像这样:

for e in epochs:
  for d in data:
     Forward pass:
        Starting from the input layer, use eq. \ref{update} to do a forward pass trough the network, computing the activities of the neurons at each layer.
     Backward pass:
          Compute the derivatives of the error function with respect to the output layer activities 
          for layer in layers:
              Compute the derivatives of the error function with respect to the inputs of the upper layer neurons
              Compute the derivatives of the error function with respect to the weights between the outer layer and the layer below
              Compute the derivatives of the error function with respect to the activities of the layer below

          Updates the weights.

谢谢你!

答案1

您可以使用标准algorithmic环境(如果您坚持的话,我想您也可以这样做algpseudocode)。

如果您希望标题“前向传递”和“后向传递”按照问题中的格式进行格式化,则必须提供额外的缩进;为此,我定义了两个额外的命令(旨在嵌套),并使用带下划线的小写字母作为标题。

\usepackage{algorithmic}
\usepackage{algorithm}

% Define a \HEADER{Title} ... \ENDHEADER block
\makeatletter
\newcommand{\HEADER}[1]{\ALC@it\underline{\textsc{#1}}\begin{ALC@g}}
\newcommand{\ENDHEADER}{\end{ALC@g}}
\makeatother

进而:

\begin{algorithm}
  \caption{Backpropagation learning algorithm}
  \begin{algorithmic}
  \FOR{d in data}
    \HEADER{Forwards Pass}
      \STATE Starting from the input layer, use eq.~\ref{update} to do
      a forward pass trough the network, computing the activities of the
      neurons at each layer.
    \ENDHEADER
    \HEADER{Backwards Pass}
      \STATE Compute the derivatives of the error function with respect
      to the output layer activities
      \FOR{layer in layers}
        \STATE Compute the derivatives of the error function with respect
        to the inputs of the upper layer neurons
        \STATE Compute the derivatives of the error function with respect
        to the weights between the outer layer and the layer below
        \STATE Compute the derivatives of the error function with respect
        to the activities of the layer below
      \ENDFOR
      \STATE Updates the weights.
    \ENDHEADER
  \ENDFOR
\end{algorithmic}
\end{algorithm}

结果


更新:如果您想要注释中提到的额外缩进,您可以在序言中添加一个与...\STATEI一起的命令,并在有多行的地方使用它:\HEADER\ENDHEADER\STATE

% Define a \STATE command with hanging indentation
\newcommand{\STATEI}[1]{\STATE
  \begin{tabular}{@{}p{\dimexpr \textwidth-\labelwidth-\ALC@tlm}@{}}%
    \hangindent \algorithmicindent
    \hangafter 1
    #1
  \end{tabular}
}

然后像这样使用它:

\STATEI{Compute the derivatives of the error function with respect
        to the output layer activities.}

更新结果

相关内容