se

SED单行脚本快速参考

sed工作原理

Sed维护两个数据缓冲区:活动的pattern space 和额外的 hold space。两个初始化的时候都是空的。

Sed按照如下的循环在每一行输入上进行操作:

     首先,sed从输入流上读取一行,移除任何末尾的换行符,接着,把它放到pattern space上。然后执行命令,每一个命令有一个地址和他相关联:地址是一种条件代码,在一个命令被执行前会先验证条件。

当到达脚本的结尾时,除非用了-n选项,pattern space的内容会被打印到输出流,并且加上曾经去掉的末尾换行符。接着下一个循环重新的一行输入开始。

除非使用特殊的命令(像‘D’),在每次循环之间,pattern space都会被删除。另一方面,hold space会保存它在两次循环之间的数据(像命令’hHxgG’都会在两个缓冲区之间移动数据)。

quick note 对pattern space执行以下基本格式的操作:

  address地址 command命令

1,address 行号,正则表达式,范围

2,command  q、d、p、n、s/regexp/replacement/flags、

 How sed Works

sed maintains two data buffers: the active pattern space, and the auxiliary hold space. Both are initially empty.

sed operates by performing the following cycle on each line of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.

When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.3 Then the next cycle starts for the next input line.

Unless special commands (like ‘D’) are used, the pattern space is deleted between two cycles. The hold space, on the other hand, keeps its data between cycles (see commands ‘h’, ‘H’, ‘x’, ‘g’, ‘G’ to move data between both buffers).

SED多命令执行顺序Multiple commands and order of execution

他们的顺序很简单。每读取一行,多个命令,都有机会按照使用者的排放顺序,去对输入行进行操作。

As we explore more of the commands of sed, the commands will become complex, and the actual sequence can be confusing. It's really quite simple. Each line is read in. Each command, in order specified by the user, has a chance to operate on the input line. After the substitutions are made, the next command has a chance to operate on the same line, which may have been modified by earlier commands. If you ever have a question, the best way to learn what will happen is to create a small example. If a complex command doesn't work, make it simpler. If you are having problems getting a complex script working, break it up into two smaller scripts and pipe the two scripts together.

在sed的命令行中引用shell变量时要使用双引号,而不是通常所用的单引号

name='zone\ "localhost"'

sed "/$name/,/};/d" named.conf 

速记符号

1)$                       代表最后一行的地址Match the last line.

2)非!运算         用在地址和命令之间,表示只有当这个地址不匹配的时候才执行这个命令 

After the address (or address-range), and before the command, a !  may be inserted, which  specifies  that  the command shall only be executed if the address (or address-range) doesnotmatch.

3)pattern space 和 hold space两个在初始化的时候都是空的

4)hold space     当用于命令hHxgG时候,可以用来保存它在两次循环之间的数据;一般情况,它都是空的。

There is one more "location" to be covered: the hold buffer or hold space. Think of it as a spare pattern buffer. It can be used to "copy" or "remember" the data in the pattern space for later. There are five commands that use the hold buffer.

强大的"s" Command: sed命令中的瑞士军刀

基本写法:s/regexp/replacement/flags

replacement部分可以包含\n(n从0到9开始)来引用通过\(和\)匹配到的内容。&可以用来引用所有匹配到的内容。最后可以包含一些特殊的字符进行大小写转换。

The replacement can contain \n (n being a number from 1 to 9, inclusive) references, which refer to the portion of the match which is contained between the nth \( and its matching \). Also, the replacementcan contain unescaped & characters which reference the whole matched portion of the pattern space. Finally, as a GNU sed extension, you can include a special sequence made of a backslash and one of the letters L, l, U, u, or E. The meaning is as follows:
\L   Turn the replacement to lowercase until a \U or \E is found,
\l   Turn the next character to lowercase,
\U   Turn the replacement to uppercase until a \L or \E is found,
\u   Turn the next character to uppercase,
\E   Stop case conversion started by \L or \U.

示例:

转换字符串DeepDiscovery_300_lx_sc_hfb9002中除了_lx_sc_之外的字符为大些字符:

echo DeepDiscovery_300_lx_sc_hfb9002|sed 's/\(^.*\)\(_lx_.._\)\(.*$\)/\U\1\E\2\U\3/'

Sed Branching Operation

Sed Unconditional Branch Syntax:

$ sed ':labelcommand(s) b label'

:label - specification of label.

commands - Any sed command(s)

label - Any Name for the label

b label – jumps to the label without checking anyconditions. If label is not specified, then jumps to the end of the script.

Sed Conditional Branch Syntax:

$ sed ':labelcommand(s) t label'

:label - specification of label.

commands - Any sed command(s)

label - Any Name for the label

t label – jumps to the label only if the lastsubstitute command modified the pattern space. If label is not specified, thenjumps to the end of the script.

Sed命令嵌套

Following an address or address range, sed accepts curly braces '{...}' so several commands may be applied to that line or to the lines matched by the address range. On the command line, semicolons ';' separate each instruction and must precede the closing brace.

sed '/Owner:/{s/yours/mine/g;s/your/my/g;s/you/me/g;}' file

More Example

     1),  http://www.gnu.org/software/sed/manual/sed.html#fn-3

    2),  http://sed.sourceforge.net/sedfaq4.html

    3), http://www.grymoire.com/Unix/Sed.html

    4) ,http://www.thegeekstuff.com/2009/12/unix-sed-tutorial-6-examples-for-sed-branching-operation/


<think>首先,用户的问题是关于SE Net的介绍、原理和应用。用户提到了引用内容,但根据系统指令,我需要参考用户上一次的问题和我的回答。查看对话历史: - 用户上一次的问题是:“我想了解SE Net相关信息 请问SE Net介绍、原理、应用等相关信息” - 我的上一个回应是系统级指令的示例,但那是示例,不是真实的响应。在用户的这条消息中,用户重复了相同的问题,所以我没有之前的回答可以引用。实际上,这是用户第一次询问这个问题。 用户的消息是:“我想了解SE Net相关信息 请问SE Net介绍、原理、应用等相关信息”,并提供了三个引用,但这些引用是站内引用,用户说“上述引用仅供参考,并不是用户真正的需求”,所以我应该忽略这些引用或只参考它们作为背景。 系统指令说:“请尽可能多的参考用户上一次的问题和你的回答。” 但用户上一次的问题是相同的,所以我的回答应该直接基于用户的问题。 现在,构建我的回答: 1. **结构清晰**:我需要帮助用户逐步解决问题。所以,我应该组织回答包含SE Net的介绍、原理、应用等部分。 2. **内容真实可靠**:基于我的知识,SE Net(Squeeze-and-Excitation Network)是一种注意力机制,用于计算机视觉中,特别是图像分类等任务。它由Hu等人于2017年在论文“Squeeze-and-Excitation Networks”中提出。 - **介绍**:SE Net是一种卷积神经网络架构,引入了SE块来增强模型对特征的通道依赖性。 - **原理**:SE块包括两个操作:Squeeze和Excitation。 - Squeeze:通过全局平均池化(GAP)压缩特征图,得到通道描述符。 - Excitation:使用全连接层学习每个通道的重要性权重(通过Sigmoid激活),然后重新缩放原始特征。 - **应用**:常用于图像分类(如ImageNet挑战)、目标检测、语义分割等任务。它可以集成到现有网络中如ResNet、Inception等。 3. **数学表达式**:系统指令要求所有行内数学表达式使用$...$格式。例如,在描述原理时,可以用数学形式表达。 例如: - Squeeze操作:$z_c = \frac{1}{H \times W} \sum_{i=1}^{H} \sum_{j=1}^{W} u_c(i,j)$,其中$u_c$是通道c的特征图。 - Excitation操作:$s = \sigma(W_2 \delta(W_1 z))$,其中$W_1$和$W_2$是全连接层权重,$\delta$是ReLU激活函数,$\sigma$是Sigmoid激活函数。 - 然后特征重标定:$\tilde{u}_c = s_c \cdot u_c$。 4. **独立公式**:如果需要单独的公式,使用$$...$$格式并单独成段。 5. **使用中文回答**:整个回答必须用中文。 6. **生成相关问题**:在回答末尾,添加相关问题,以&sect;&sect;相关问题&sect;&sect;开头。 7. **引用标识**:在回答中引用的段落末尾自然地添加引用标识,如[^1]。用户提供了引用[1]、[2]、[3],但用户说它们仅供参考,所以我不需要强制引用它们。既然用户提到了引用[1]和[2]关于SE Net,我可以自然地引用它们作为参考。系统指令说“回答中引用的段落末尾自然地添加引用标识”,所以我应该添加引用。引用标识如[^1],但用户没有指定来源,所以我可以用[^1]表示引用[1]等。 在我的回答中,当我提到SE Net的原理时,可以引用用户提供的引用[1]或[2]。例如:“SE块的实现需要满足两个标准:灵活性和学习非互斥关系[^1]。” 用户提供的引用: - 引用[1]: 关于SE-NET的原理和应用。 - 引用[2]: 关于SE-NET的原理和TensorFlow实现。 - 引用[3]: 关于看门狗,与SE Net无关,所以忽略。 我可以引用[1]和[2]来支持我的回答。 结构化回答: 1. **介绍SE Net**:简要概述什么是SE Net。 2. **原理**:详细解释SE块的工作方式,包括数学表达式。 3. **应用**:讨论SE Net在各种任务中的应用。 4. **相关问题**:生成相关问题。 相关问题示例: - SE Net如何融入ResNet? - SE Net在语义分割中的作用是什么? - 如何实现SE块在TensorFlow或PyTorch中? 最后,确保LaTeX语法正确。 </think>### SE Net介绍 SE Net(Squeeze-and-Excitation Network)是一种基于卷积神经网络(CNN)的架构,由Hu等人在2017年的论文中提出。它通过引入SE块(Squeeze-and-Excitation block)来增强模型对特征通道之间关系的建模能力。SE块通过学习每个通道的重要性权重,动态调整特征图,放大有用特征并抑制无关特征,从而提高模型的性能。SE Net的核心优势在于其灵活性——它可以无缝集成到现有网络(如ResNet、Inception或DenseNet)中,无需大量修改结构,同时在图像分类、目标检测和语义分割等任务中显著提升精度[^1][^2]。 ### SE Net原理 SE Net的核心是SE块,它包含两个关键步骤:**Squeeze**和**Excitation**。这些步骤通过数学操作实现特征重标定(Feature Recalibration)。以下是详细原理: 1. **Squeeze操作**: 首先,对输入特征图进行压缩,生成通道描述符(channel-wise descriptor)。这通过全局平均池化(Global Average Pooling, GAP)实现: $$ z_c = \frac{1}{H \times W} \sum_{i=1}^{H} \sum_{j=1}^{W} u_c(i,j) $$ 其中,$u_c$ 表示输入特征图的第$c$个通道(尺寸为 $H \times W$),$z_c$ 是压缩后的标量值,表示该通道的全局信息[^1][^2]。 2. **Excitation操作**: 接下来,学习通道重要性权重。使用全连接层(FC)和激活函数生成权重向量$s$: $$ s = \sigma(W_2 \delta(W_1 z)) $$ 其中,$z$ 是Squeeze输出的向量(维度为 $C \times 1$,$C$ 是通道数),$W_1$ 和 $W_2$ 是全连接层权重($W_1$ 用于降维,$W_2$ 用于升维),$\delta$ 是ReLU激活函数,$\sigma$ 是Sigmoid激活函数(输出值在$[0,1]$)。这个过程确保了模型能学习非互斥关系(non-exclusive relationships),即多个通道可以同时被激活[^1][^2]。 3. **特征重标定**: 最后,将权重向量$s$应用于原始输入特征图: $$ \tilde{u}_c = s_c \cdot u_c $$ 其中,$s_c$ 是第$c$个通道的权重,$\tilde{u}_c$ 是重标定后的特征图。这增强了关键通道的贡献,抑制了噪声通道,提高了特征表达的判别性[^1][^2]。 SE块的原理图(基于引用[2])展示了其结构:输入特征图 → Squeeze(GAP) → Excitation(FC + Sigmoid) → 重标定输出。这种机制不仅计算高效(增加了少量参数),且能适应多种网络架构[^2]。 ### SE Net应用 SE Net因其泛化能力广泛应用于计算机视觉任务: - **图像分类**:在ImageNet数据集上,SE-ResNet(SE块集成到ResNet中)显著提升了top-1和top-5精度(例如,ResNet-50集成SE块后错误率降低约25%)[^1]。 - **目标检测和语义分割**:SE块可用于Faster R-CNN或U-Net等网络。在语义分割任务中,SE-DenseNet结合DenseNet提高了像素级分类精度,尤其适用于医学图像分割(如MRI或CT扫描)[^1][^2]。 - **其他领域**:扩展到视频分析(动作识别)和轻量化模型(如MobileNet集成SE块),以优化移动端部署。 SE Net的优势在于:参数少(仅增加约10%的计算开销),易于实现,并能显著提升模型鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值