Latex写论文中,算法过长,需要分页显示的方法

本文介绍了一种在LaTeX文档中实现算法跨页显示的方法,通过定义新的环境`breakablealgorithm`来允许长算法跨越多页显示,提高了文档排版的美观性和阅读体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在写论文的过程中,我们经常遇到在写算法框架时,由于算法太长,常常占据一个页面,这样导致后续排版时候不美观同时与前文没有很好的衔接的情况,为此搜索了一下解决方法,比较有效的方法如下:

\documentclass{article}
\usepackage{algorithm,algpseudocode,float}
\usepackage{lipsum}

\makeatletter
\newenvironment{breakablealgorithm}
  {% \begin{breakablealgorithm}
   \begin{center}
     \refstepcounter{algorithm}% New algorithm
     \hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled
     \renewcommand{\caption}[2][\relax]{% Make a new \caption
       {\raggedright\textbf{\ALG@name~\thealgorithm} ##2\par}%
       \ifx\relax##1\relax % #1 is \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##2}%
       \else % #1 is not \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##1}%
       \fi
       \kern2pt\hrule\kern2pt
     }
  }{% \end{breakablealgorithm}
     \kern2pt\hrule\relax% \@fs@post for \@fs@ruled
   \end{center}
  }
\makeatother

\begin{document}前加上如上所示的代码

然后\begin{breakablealgorithm}` 和 `\end{breakablealgorithm} 替换 \begin{algorithm}` 和 `\end{algorithm} 这样就可以避免算法在一整个页面显示。
例子如下:

\documentclass{article}
\usepackage{algorithm,algpseudocode,float}
\usepackage{lipsum}

\makeatletter
\newenvironment{breakablealgorithm}
  {% \begin{breakablealgorithm}
   \begin{center}
     \refstepcounter{algorithm}% New algorithm
     \hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled
     \renewcommand{\caption}[2][\relax]{% Make a new \caption
       {\raggedright\textbf{\ALG@name~\thealgorithm} ##2\par}%
       \ifx\relax##1\relax % #1 is \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##2}%
       \else % #1 is not \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##1}%
       \fi
       \kern2pt\hrule\kern2pt
     }
  }{% \end{breakablealgorithm}
     \kern2pt\hrule\relax% \@fs@post for \@fs@ruled
   \end{center}
  }
\makeatother
\begin{document}
\section{AIS data filter algorithm}
\begin{breakablealgorithm}
        \caption{AIS filter algorithm}
        \begin{algorithmic}[1] %每行显示行号
            \Require $VesselInfoList$ the AIS data after grouping
            \Ensure  $InboundVesselList$ inbound vessel list,$OutboundVesselList$ outbound vessel list
            \Function {AIS data Extract-Transform-Load}{$VesselInfoList$}
                \State $temptable1 \gets 0$
                \While{$VesselInfoList \not\in \emptyset$}
                    \If {$VesselSpeed<0||VesselSpeed>30kn$}
                    \State $\textbf{Delete}\,these\,AIS\,data;$
                    \EndIf
                    \If {$Latitude>90^\circ||Longitude>180^\circ$}
                    \State $\textbf{Delete}\,these\,AIS\,data;$
                    \EndIf
                \State $\textbf{Save}\,the\,rest\,AIS\,data\,into\,\textbf{temptable1};$
                \EndWhile
            \EndFunction
            \State
            \Function {preliminary filtering algorithm}{$\textbf{temptable1}$}
            \State $\textbf{Get}\,the \,water \,boundary \,coordination;$
            \State $\textbf{Delete} \,the \,AIS \,data \,out \,of \,boundary \,coordination;$
            \State $\textbf{Save} \,the \,rest \,AIS \,data \,into \,\textbf{temptable2}$
             \Function {Standardization the distance}{$\textbf{temptable2}$}
             \State $\textbf{Read}\,the\,Vessel\,Length\,data\,from\,\textbf{temptable2};$
             \State $calculate\,the\,distance:$
             \State $delta D=1852.25(\sqrt{(\varphi_2-\varphi_1)^2+(\lambda_2-\lambda_1)^2})/Length;$
             \State $\text{Save}\,the\,data\,into\,\textbf{temptable3};$
             \EndFunction
             \EndFunction
             \State
             \Function{Fine filter}{$\textbf{temtable3}$}
             \State $\text{Determine}\,the compound\_channel\,direction:\varphi_3;$
             \Function {inbound/outbound determination}{$\textbf{temptable3}$}
             \State $\text{Read}\,the\,vessel's\,heading\,or\,course\,from\,\textbf{temptable3}:\varphi_4;$
             \If {$\varphi_4\in(\varphi_3\pm10^\circ)$}
             \State $Vessel inbound;$
             \State $\textbf{save}\,the\,AIS\,data\,into\,\textbf{InboundVesselList};$
             \Else {$\varphi_4\in(\varphi_3\pm180^\circ)\pm10^\circ$}
             \State $Vessel outbound;$
             \State $\textbf{Save}\,the\,AIS\,data\,into\,\textbf{OutboundVesselList};$
             \EndIf
             \EndFunction
             \Function {Process AIS data}{$\textbf{InboundVesselList/OutboundVesselList}$}
             \State$\textbf{Read}\,the\,Vessel\,speed\,and\,time\,from\,InboundVesselList\,OutboundVesselList\,pos\_time;$
             \If {$rate\_of\_change=(speed[i]-speed[i-1])/(pos_time[i]-pos_time[i-1])<threshold$}
             \State $\textbf{Delete}\,current\,AIS\,data;$
             \Else
             \State $\textbf{Save}\,the\,AIS\,data;$
             \EndIf
             \EndFunction
             \EndFunction
             \State $\textbf{Output}\,the\,InboundVesselList\,and\,OutboundVesselList;$
     \end{algorithmic}
    \end{breakablealgorithm}
\end{document}

最终显示如下:
这里写图片描述
这里写图片描述

03-30
### Swish-Gated Linear Unit (SwiGLU) 的定义 Swish-Gated Linear Unit (SwiGLU) 是一种结合了门控机制和激活函数的神经网络组件,广泛应用于自然语言处理和其他深度学习领域。它通过引入非线性和自适应加权的方式增强了模型表达能力[^4]。 具体来说,SwiGLU 可以被看作是一种特殊的门控单元形式,其中输入经过两个分支路径:一条路径应用 Swish 激活函数作为门控信号,另一条保持原始输入不变。最终输出由这两个分支的结果相乘得到: \[ \text{SwiGLU}(x) = (\sigma(W_s \cdot x + b_s) \odot W_g \cdot x + b_g) \] 在这里: - \(W_s\) 和 \(b_s\) 表示用于计算门控信号的权重矩阵和偏置项; - \(W_g\) 和 \(b_g\) 则表示用于缩放输入的权重矩阵和偏置项; - \(\sigma(x)\) 表示标准 Sigmoid 函数或其变种(如 Swish); - \(\odot\) 表示逐元素乘法操作。 这种设计允许模型动态调整不同特征的重要性,从而提升复杂模式的学习效率[^5]。 --- ### 实现细节 以下是基于 PyTorch 的 SwiGLU 实现代码片段: ```python import torch import torch.nn as nn class SwiGLU(nn.Module): def __init__(self, input_dim, output_dim): super(SwiGLU, self).__init__() self.linear_gate = nn.Linear(input_dim, output_dim) self.linear_main = nn.Linear(input_dim, output_dim) def forward(self, x): gate = torch.sigmoid(self.linear_gate(x)) # Gate signal using sigmoid/Swish main = self.linear_main(x) # Main transformation path return gate * main # Element-wise multiplication ``` 上述实现中,`linear_gate` 负责生成门控信号,而 `linear_main` 处理主要的数据流转换部分。两者的输出随后通过逐元素乘法组合在一起形成最终结果[^6]。 需要注意的是,在实际部署过程中可以根据需求替换掉默认的 Sigmoid 函数为其他更高效的替代品比如 SiLU 或 GELU 来进一步优化性能表现[^7]。 --- ### 使用场景 由于具备较强的灵活性以及良好的梯度传播特性,SwiGLU 特别适合于以下几种情况下的建模工作: 1. **序列建模**: 如 Transformer 架构中的前馈网络模块可以采用 SwiGLU 替代传统 ReLU/GeLU 层次结构来增强局部依赖关系捕捉能力。 2. **低资源环境**: 类似于预训练嵌入未能显著改善整体效果的小型数据集情境下,适当加入此类复杂的非线性变换有助于挖掘潜在规律[^8]。 3. **多任务联合训练框架内**, 当存在多个子任务共享底层表征空间时,利用 SwiGLU 提供额外自由度可以帮助平衡各目标间冲突并促进知识迁移过程顺利开展. ---
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值