lstm with attention_一文搞懂NLP中的Attention机制(附详细代码讲解)

2023-11-09

公众号关注 “ ML_NLP ” 设为 “ 星标 ”,重磅干货,第一时间送达!

5d07573a65aaecc282aebc2e1158554d.png

机器学习算法与自然语言处理出品

@公众号原创专栏作者 Don.hub

单位 | 京东算法工程师

学校 | 帝国理工大学

  • Outline

  • Intuition

  • Analysis

    • Pros

    • Cons

    • From Seq2Seq To Attention Model

      • seq2seq 很重要,但是缺陷也很明显

      • attention was born

      • Write the encoder and decoder model

  • Taxonomy of attention

    • number of sequence

      • distinctive

      • co-attention

      • self

    • number of abstraction

      • single-level

      • multi-level

    • number of positions

      • soft/global

      • hard

      • local

    • number of representations

      • multi-representational

      • multi-dimensional

    • summary

  • Networks with Attention

    • encoder-decoder

      • CNN/RNN + RNN

      • Pointer Networks

      • Transformer

    • Memory Networks

  • Applications

    • NLG

    • Classification

    • Recommendation Systems

  • ref

1. Outline

1fcacfff5ecdd069b1cc89a39110c005.png

2. Intuition

吸睛这个词就很代表attention,我们在看一张图片的时候,很容易被更重要或者更突出的东西所吸引,所以我们把更多的注意放在局部的部分上,在计算机视觉(CV)领域,就可以看作是图片的局部拥有更多的权重,比如图片生成标题,标题中的词就会主要聚焦于局部。

38ea53692deb1e440863caef7a44b98a.png

NLP领域,可以想象我们在做阅读理解的时候,我们在看文章的时候,往往是带着问题去寻找答案,所以文章中的每个部分是需要不同的注意力的。例如我们在做评论情感分析的时候,一些特定的情感词,例如amazing等,我们往往需要特别注意,因为它们是很重要的情感词,往往决定了评论者的情感。如下图(Yang et al., 何老师团队 HAN

3825fa64a3ae3ed4fbc425b32af114dd.png

直白地说,attention就是一个权重的vector。

3. Analysis

3.1 Pros

attention的好处主要是具有很好的解释性,并且极大的提高了模型的效果,已经是很多SOTA 模型必备的模块,特别是transformer(使用了self / global/ multi-level/ multihead/ attention)的出现极大得改变了NLP的格局。

3.2 Cons

没法捕捉位置信息,需要添加位置信息。当然不同的attention机制有不同的当然如果说transformer的坏处,其最大的坏处是空间消耗大,这是因为我们需要储存attention score(N*N)的维度,所以Sequence length(N)不能太长,这就导致,我们seq和seq之间没有关联。(具体参照XLNET以及XLNET的解决方式)

3.3 From Seq2Seq To Attention Model

为什么会有attention?attention其实就是为了翻译任务而生的(但最后又不局限于翻译任务),我们来看看他的具体演化。

3.3.1 seq2seq 很重要,但是缺陷也很明显

Seq2Seq model 是有encoder和decoder组成的,它主要的目的是将输入的文字翻译成目标文字。其中encoder和decoder都是RNN,(可以是RNN/LSTM/或者GRU或者是双向RNN)。模型将source的文字编码成一串固定长度的context编码,之后利用这段编码,使用decoder解码出具体的输出target。这种转化任务可以适用于:翻译,语音转化,对话生成等序列到序列的任务。

5b8e78a8e6238c2e675a2c87563a2e18.png

但是这种模型的缺点也很明显:- 首先所有的输入都编码成一个固定长度的context vector,这个长度多少合适呢?很难有个确切的答案,一个固定长度的vector并不能编码所有的上下文信息,导致的是我们很多的长距离依赖关系信息都消失了。- decoder在生成输出的时候,没有一个与encoder的输入的匹配机制,对于不同的输入进行不同权重的关注。- Second, it is unable to model alignment between input and output sequences, which is an essential aspect of structured output tasks such as translation or summarization [Young et al., 2018]. Intuitively, in sequence-to-sequence tasks, each output token is expected to be more influenced by some specific parts of the input sequence. However, decoder lacks any mechanism to selectively focus on relevant input tokens while generating each output token.

3.3.2 attention was born

NMT【paper】 【code】最早提出了在encoder以及decoder之间追加attention block,最主要就是解决encoder 以及decoder之间匹配问题。

56e577d3294709934c683d4ce5ac9ce5.png
  • 其中 1cff3869-1431-eb11-8da9-e4434bdf6706.svg 是decoder的初始化hidden state,是随机初始化的,相比于seq2seq(他是用context vector作为decoder的hidden 初始化), 1eff3869-1431-eb11-8da9-e4434bdf6706.svg 是decoder的hidden states。

  • 21ff3869-1431-eb11-8da9-e4434bdf6706.svg 代表的是第j个encoder位置的输出hidden states

  • 23ff3869-1431-eb11-8da9-e4434bdf6706.svg 代表的是第i个decoder的位置对对j个encoder位置的权重

  • 27ff3869-1431-eb11-8da9-e4434bdf6706.svg 是第i个decoder的位置的输出,就是经过hidden state输出之后再经过全连接层的输出

  • 29ff3869-1431-eb11-8da9-e4434bdf6706.svg 代表的是第i个decoder的context vector,其实输出hidden output的加权求和

  • decoder的输入是由自身的hidden state以及 2cff3869-1431-eb11-8da9-e4434bdf6706.svg 这两个的concat结果

3.3.3 Write the encoder and decoder model

详细的实现可以参照tensorflow的repo使用的是tf1.x Neural Machine Translation (seq2seq) tutorial. 这里的代码用的是最新的2.x的代码 code.

输入经过encoder之后得到的hidden states 的形状为 (batch_size, max_length, hidden_size) , decoder的 hidden state 形状为 (batch_size, hidden_size).

以下是被implement的等式:

5fcf1af6a940a192a897a8f79fe8e3f4.png

This tutorial uses Bahdanau attention for the encoder. Let's decide on notation before writing the simplified form:

  • FC = Fully connected (dense) layer

  • EO = Encoder output

  • H = hidden state

  • X = input to the decoder

And the pseudo-code:

  • score = FC(tanh(FC(EO) + FC(H)))

  • attention weights = softmax(score, axis = 1). Softmax by default is applied on the last axis but here we want to apply it on the 1st axis, since the shape of score is (batch_size, max_length, hidden_size)Max_length is the length of our input. Since we are trying to assign a weight to each input, softmax should be applied on that axis.

  • context vector = sum(attention weights * EO, axis = 1). Same reason as above for choosing axis as 1.

  • embedding output = The input to the decoder X is passed through an embedding layer.

  • merged vector = concat(embedding output, context vector)

  • This merged vector is then given to the GRU

class BahdanauAttention(tf.keras.layers.Layer):
def __init__(self, units):
super(BahdanauAttention, self).__init__()
self.W1 = tf.keras.layers.Dense(units)
self.W2 = tf.keras.layers.Dense(units)
self.V = tf.keras.layers.Dense(1)

def call(self, query, values):
# hidden shape == (batch_size, hidden size)
# hidden_with_time_axis shape == (batch_size, 1, hidden size)
# we are doing this to perform addition to calculate the score
hidden_with_time_axis = tf.expand_dims(query, 1)

# score shape == (batch_size, max_length, 1)
# we get 1 at the last axis because we are applying score to self.V
# the shape of the tensor before applying self.V is (batch_size, max_length, units)
score = self.V(tf.nn.tanh(
self.W1(values) + self.W2(hidden_with_time_axis)))

# attention_weights shape == (batch_size, max_length, 1)
attention_weights = tf.nn.softmax(score, axis=1)

# context_vector shape after sum == (batch_size, hidden_size)
context_vector = attention_weights * values
context_vector = tf.reduce_sum(context_vector, axis=1)

return context_vector, attention_weights
class Encoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz):
super(Encoder, self).__init__()
self.batch_sz = batch_sz
self.enc_units = enc_units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.gru = tf.keras.layers.GRU(self.enc_units,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform')

def call(self, x, hidden):
x = self.embedding(x)
output, state = self.gru(x, initial_state = hidden)
return output, state

def initialize_hidden_state(self):
return tf.zeros((self.batch_sz, self.enc_units))
class Decoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz):
super(Decoder, self).__init__()
self.batch_sz = batch_sz
self.dec_units = dec_units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.gru = tf.keras.layers.GRU(self.dec_units,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform')
self.fc = tf.keras.layers.Dense(vocab_size)

# used for attention
self.attention = BahdanauAttention(self.dec_units)

def call(self, x, hidden, enc_output):
# enc_output shape == (batch_size, max_length, hidden_size)
context_vector, attention_weights = self.attention(hidden, enc_output)

# x shape after passing through embedding == (batch_size, 1, embedding_dim)
x = self.embedding(x)

# x shape after concatenation == (batch_size, 1, embedding_dim + hidden_size)
x = tf.concat([tf.expand_dims(context_vector, 1), x], axis=-1)

# passing the concatenated vector to the GRU
output, state = self.gru(x)

# output shape == (batch_size * 1, hidden_size)
output = tf.reshape(output, (-1, output.shape[2]))

# output shape == (batch_size, vocab)
x = self.fc(output)

return x, state, attention_weights

4. Taxonomy of attention

根据不同的分类标准,可以将attention分为多个类别,但是具体来说都是q(query)k(key)以及v(value)之间的交互,通过q以及k计算score,这个score的计算方法各有不同如下表,再经过softmax进行归一化。最后在将计算出来的score于v相乘加和(或者取argmax 参见pointer network)。

Below is a summary table of several popular attention mechanisms and corresponding alignment score functions:

09095ca47b69b1fea7127579b86c7887.png

(*) Referred to as “concat” in Luong, et al., 2015 and as “additive attention” in Vaswani, et al., 2017. (^) It adds a scaling factor 1/n‾√1/n, motivated by the concern when the input is large, the softmax function may have an extremely small gradient, hard for efficient learning.

以下的分类不是互斥的,比如说HAN模型,就是一个multi-level,soft,的attention model(AM)。

4.1 number of sequence

根据我们的query以及value来自的sequence来分类。

4.1.1 distinctive

attention的query和value分别来自不同两个不同的input sequence和output sequence,例如我们上文提到的NMT,我们的query来自于decoder的hidden state,我们的value来自去encoder的hidden state。

4.1.2 co-attention

co-attention 模型对多个输入sequences进行联合学习权重,并且捕获这些输入的交互作用。例如visual question answering 任务中,作者认为对于图片进行attention重要,但是对于问题文本进行attention也同样重要,所以作者采用了联合学习的方式,运用attention使得模型能够同时捕获重要的题干信息以及对应的图片信息。

4.1.3 self

例如文本分类或者推荐系统,我们的输入是一个序列,输出不是序列,这种场景下,文本中的每个词,就去看与自身序列相关的词的重要程度关联。如下图

8aef9c14052be656e2fec12fb4975664.png

我们可以看看bert的self attention的实现的函数说明,其中如果from tensor= to tensor,那就是self attention

def attention_layer(from_tensor,
to_tensor,
attention_mask=None,
num_attention_heads=1,
size_per_head=512,
query_act=None,
key_act=None,
value_act=None,
attention_probs_dropout_prob=0.0,
initializer_range=0.02,
do_return_2d_tensor=False,
batch_size=None,
from_seq_length=None,
to_seq_length=None):
"""Performs multi-headed attention from `from_tensor` to `to_tensor`. This is an implementation of multi-headed attention based on "Attention is all you Need". If `from_tensor` and `to_tensor` are the **same**, then this is self-attention. Each timestep in `from_tensor` attends to the corresponding sequence in `to_tensor`, and returns a fixed-with vector """

4.2 number of abstraction

这是根据attention计算权重的层级来划分的。

4.2.1 single-level

在最常见的case中,attention都是在输入的sequence上面进行计算的,这就是普通的single-level attention。

4.2.2 multi-level

但是也有很多模型,例如HAN,模型结构如下。模型是hierarchical的结构的,它的attention也是作用在多层结构上的。我们介绍一下这个模型的作用,它主要做的是一个文档分类的问题,他提出,文档是由句子组成的,句子又是由字组成的,所以他就搭建了两级的encoder(双向GRU)表示,底下的encoder编码字,上面的encoder编码句子。在两个encoder之间,连接了attention层,这个attention层是编码字层级上的注意力。在最后输出作文本分类的时候,也使用了一个句子层级上的attention,最后输出来Dense进行句子分类。需要注意的是,这里的两个query 38ff3869-1431-eb11-8da9-e4434bdf6706.svg以及 39ff3869-1431-eb11-8da9-e4434bdf6706.svg 都是随机初始化,然后跟着模型一起训练的,score方法用的也是Dense方法,但是这边和NMT不同的是,他是self attention。

04f6b021d2225767dd8564122258be68.png

4.3 number of positions

根据attention 层关注的位置不同,我们可以把attention分为三类,分别是global/soft(这两个几乎一样),local以及hard attention。Effective Approaches to Attention-based Neural Machine Translation. 提出了local global attention,Show, Attend and Tell: Neural Image Caption Generation with Visual Attention. 提出了hard soft attention

4.3.1 soft/global

global/soft attention 指的是attention 的位置为输入序列的所有位置,好处在与平滑可微,但是坏处是计算量大。

4.3.2 hard

hard attention 的context vector是从采样出来的输入序列hidden states进行计算的,相当于将hidden states进行随机选择,然后计算attention。这样子可以减少计算量,但是带来的坏处就是计算不可微,需要采用强化学习或者其他技巧例如variational learning methods。

4.3.3 local

local的方式是hard和soft的折中 - 首先从input sequence中找到一个需要attention的点或者位置 - 在选择一个窗口大小,create一个local的soft attention 这样做的好处在于,计算是可微的,并且减少了计算量

4.4 number of representations

通常来说single-representation是最常见的情况,which means 一个输入只有一种特征表示。但是在其他场景中,一个输入可能有多种表达,我们按输入的representation方式分类。

4.4.1 multi-representational

在一些场景中,一种特征表示不足以完全捕获输入的所有信息,输入特征可以进行多种特征表示,例如Show, attend and tell: Neural image caption generation with visual attention. 这篇论文就对文本输入进行了多种的word embedding表示,然后最后对这些表示进行attention的权重加和。再比如,一个文本输入分别词,语法,视觉,类别维度的embedding表示,最后对这些表示进行attention的权重加和。

4.4.2 multi-dimensional

顾名思义,这种attention跟维度有关。这种attention的权重可以决定输入的embedding向量中不同维度之间的相关性。其实embedding中的维度可以看作一种隐性的特征表示(不像one_hot那种显性表示直观,虽然缺少可解释性,但是也算是特征的隐性表示),所以通过计算不同维度的相关性就能找出起作用最大的特征维度。尤其是解决一词多义时,这种方式非常有效果。所以,这种方法在句子级的embedding表示、NLU中都是很有用的。

5. summary

8ad1af73d461b73d969056cc5a704ef9.png

6. Networks with Attention

介绍了那么多的attention类别,那么attention通常是运用在什么网络上的呢,我们这边总结了两种网络,一种是encoder-decoder based的一种是memory network。

6.1 encoder-decoder

encoder-decoder网络+attention是最常见的+attention的网络,其中NMT是第一个提出attention思想的网络。这边的encoder和decoder是可以灵活改变的,并不绝对都是RNN结构。

6.1.1 CNN/RNN + RNN

对于图片转文字这种任务,可以将encoder换成CNN,文字转文字的任务可以使用RNN+RNN。

6.1.2 Pointer Networks

并不是所有的序列输入和序列输出的问题都可以使用encoder-decoder模型解决,(e.g. sorting or travelling salesman problem). 例如下面这个问题:我们想要找到一堆的点,能够将图内所有的点包围起来。我们期望得到的效果是,输入所有的点 40ff3869-1431-eb11-8da9-e4434bdf6706.svg 最后输出的是 42ff3869-1431-eb11-8da9-e4434bdf6706.svg

b533c2c65ff699ccc632f98009d12736.png

如果直接下去训练的话,下图所示:input 4个data point的坐标,得到一个红色的vector,再把vector放到decoder中去,得到distribution,再做sample(比如做argmax,决定要输出token 1...),最终看看work不work,结果是不work。比如:训练的时候有50 个点,编号1-50,但是测试的时候有100个点,但是它只能选择 1-50编号的点,后面的点就选不了了。

d090afe2370e8f78bf4346ca9a101bf5.png

改进:attention,可以让network动态的决定输出的set有多大

x0,y0代表END这些词,每一个input都会得到一个attention的weight=output的distribution。

05d1c255207d3f16d0b182ea18fd2884.png

最后的模型的结束的条件就是 4eff3869-1431-eb11-8da9-e4434bdf6706.svg 点的概率最高

84271c10b377b8f71dd9564e3be5423d.png

6.1.3 Transformer

transformer网络使用的是encoder+decoder网络,其主要是解决了RNN的计算速度慢的问题,通过并行的self attention机制,提高了计算效率。但是与此同时也带来了计算量大,空间消耗过大的问题,导致sequence length长度不能过长的问题,解决参考transformerXL。(之后会写一篇关于transformer的文章) - multihead的作用:有点类似与CNN的kernel,主要捕获不同的特征信息

6.2 Memory Networks

像是question answering,或者聊天机器人等应用,都需要传入query以及知识数据库。End-to-end memory networks.通过一个memroy blocks数组储存知识数据库,然后通过attention来匹配query和答案。memory network包含四部分内容:query(输入)的向量、一系列可训练的map矩阵、attention权重和、多hop推理。这样就可以使用KB中的fact、使用history中的关键信息、使用query的关键信息等进行推理,这在QA和对话中至关重要。(这里需要补充)

7. Applications

7.1 NLG

  • MT:计算机翻译

  • QA:problems have made use of attention to (i) better understand questions by focusing on relevant parts of the question [Hermann et al., 2015], (ii) store large amount of information using memory networks to help find answers [Sukhbaatar et al., 2015], and (iii) improve performance in visual QA task by modeling multi-modality in input using co-attention [Lu et al., 2016].

  • Multimedia Description(MD):is the task of generating a natural language text description of a multimedia input sequence which can be speech, image and video [Cho et al., 2015]. Similar to QA, here attention performs the function of finding relevant acoustic signals in speech input [Chorowski et al., 2015] or relevant parts of the input image [Xu et al., 2015] to predict the next word in caption. Further, Li et al. [2017] exploit the temporal and spatial structures of videos using multi-level attention for video captioning task. The lower abstraction level extracts specific regions within a frame and higher abstraction level focuses on small subset of frames selectively.

7.2 Classification

  • Document classification:HAN

  • Sentiment Analysis:

  • Similarly, in the sentiment analysis task, self attention helps to focus on the words that are important for determining the sentiment of input. A couple of approaches for aspect based sentiment classification by Wang et al. [2016] and Ma et al. [2018] incorporate additional knowledge of aspect related concepts into the model and use attention to appropriately weigh the concepts apart from the content itself. Sentiment analysis application has also seen multiple architectures being used with attention such as memory networks [Tang et al., 2016] and Transformer [Ambartsoumian and Popowich, 2018; Song et al., 2019].

7.3 Recommendation Systems

Multiple papers use self attention mechanism for finding the most relevant items in user’s history to improve item recommendations either with collaborative filtering framework [He et al., 2018; Shuai Yu, 2019], or within an encoderdecoder architecture for sequential recommendations [Kang and McAuley, 2018; Zhou et al., 2018].

Recently attention has been used in novel ways which has opened new avenues for research. Some interesting directions include smoother incorporation of external knowledge bases, pre-training embeddings and multi-task learning, unsupervised representational learning, sparsity learning and prototypical learning i.e. sample selection.

8. ref

  • 写作风格很好,最后模型那块可以再补充到本篇文章

  • 非常好的综述An Attentive Survey of Attention Models

  • wildml.com/2016/01/atte

  • 图文详解NMT(decoder那边有点错误,因为decoder的初始化的embedding 是 估计是定义不通,然后初始化的用的是encoder的hidden output作为attention score的key,然后其实是concat context和embedding作为输入)

  • NMT代码

  • pointer network

  • pointer slides

  • All Attention You Need还没看完

51a18b06a97f79b2c55864a3cc44b2bf.png

推荐阅读:

深度解析LSTM神经网络的设计原理

图卷积网络(GCN)新手村完全指南

论文赏析[ACL18]基于Self-Attentive的成分句法分析

d5eadf0828b679511ccb23b87e69367c.png

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

lstm with attention_一文搞懂NLP中的Attention机制(附详细代码讲解) 的相关文章

  • 经理人必看的十个管理网站

    经理人必看的十个管理网站 管理这玩艺远远看着如同象牙塔中的佛牙舍利 可观而不可玩 其实身在其中无非就是一张窗户纸 没有什么大不了的 管理这玩艺远远看着如同象牙塔中的佛牙舍利 可观而不可玩 其实身在其中无非就是一张窗户纸 没有什么大不了的 网
  • C# NPOI写excel文件,设置某个单元格为自动筛选

    https blog csdn net qq 40467670 article details 118102078 如标题所示 附上几行代码 HSSFWorkbook workbook new HSSFWorkbook 创建工作表 var
  • Uva 10474 Where is the Marble?(排序与检索)

    本题若掌握了sort 和lower bound 两个函数 就无难点 include
  • 通关6级之词汇(2021.05.29)

    前言 这篇词汇是通关6级系列第一篇文章 这篇文章和4级有大部分内容是一样的 所以如果学了4级的课程再学这个会很轻松 更多相关文章点击阅读 通关4级之阅读理解 通关4级之听力 通关4级之写作 通关4级之翻译 通关4级之词汇 通关6级之词汇 点
  • STM32 ST-LINK Utility程序烧录方法

    打开软件过后点击Target connect 出现以下界面表示STlink链接成功 如果出现没有检测到stlink的话 首先查看是否安装驱动程序 再重新插拔电脑usb口的stlink连接线 或者链接到主机后方的usb口 再重复以上步骤链接
  • Fire Net

    点击打开链接 Problem Description Suppose that we have a square city with straight streets A map of a city is a square board wi
  • 利用python对b站某GPT-4解说视频的近万条弹幕进行爬取、数据挖掘、数据分析、弹幕数量预测及情绪分类

    目录 一 利用Python爬取弹幕 二 利用几行代码直接生成词云 三 将弹幕属性和内容放入mysql当中 四 分析弹幕在视频各节点的数量 1 分析视频各个片段出现的弹幕数量 2 分析视频各大章节出现的弹幕数量 3 分析视频各小节出现的弹幕数
  • Learning Java language Fundamentals

    Chapter 2 Learning Java language fundamentals exercises 1 What is Unicode Unicode is a computing industry standard for c
  • 炒股新手技巧

    技巧一 关于止损和止赢的问题 我觉得很重要 止赢和止损的设置对股民来说尤为重要 有很多散户会设立止损 但是不会止赢 今天有必要和大家探讨一下 止损的设立大家都知道 设定一个固定的亏损率 到达位置严格执行 但是止赢 一般的散户都不会 为什么说
  • 用mybatis批处理, 编程式事务,CompletableFuture异步处理,多线程,线程池,list 分页,实现多张表大批量插入

    目录 概述 准备工作 创建数据库表 创建Java实体类 创建MyBatis映射文件和DAO接口 编写Java代码实现多张表大批量插入的功能 总结 1 概述 在实际的开发中 我们经常需要将大量的数据插入到数据库中 如果使用单条插入的方式 会导
  • vs2017 找不到源文件stdio.h解决方法

    这个问题网上又不少人提出 我的vs出现这个问题是因为我电脑重装系统了 原来的项目所采用windows SDK 已经发生了变化 因此解决的办法是 项目 gt 属性 gt 配置属性 gt 常规 gt windows SDK版本 将其换成你现在的
  • 【论文阅读】【因果关系】

    文章目录 因果理论的三个层级 张含望 因果推理在计算机视觉中的进展 浙江大学助理教授况琨 因果推理和稳定学习 因果理论的三个层级 该篇是本人入门因果 CV的随笔 期间会借鉴记录别人的理解 引用的部分会给出原始连接 如有侵权请联系我删除 Ju
  • EEPROM AT24C08的操作

    EEPROM应该是学习IIC总线时候最先接触的东西了 EEPROM的优点是可以随机存取 不像Flash存储器一样需要先擦除在能写入 而且擦写次数多存储时间长 但是缺点是存储空间非常有限 像我这用的Atmel的AT24C08只有8Kbit的存
  • Java异常机制Throwable

    Java中异常的概念以及处理异常 在Java程序运行期间出现了一个错误 这个错误可能是由于文件包含了错误信息 或者是由于网络连接出现问题 也可以是因为使用了无效的数组下标 或者是试图使用一个没有被赋值的对象引用而造成的 我们称这样的错误为异
  • canvas 简单用法

    canvas使用方法 1 首先要获取页面中的画布 var canvas document querySelector canvas 2 创建画笔 var context canvas getContext 2d 3 选择要绘画的方式 进行绘
  • Linux C/C++ Openssl RSA Encrypt/Decrypt(加密/解密) 简单示例教程

    PEM文件有以下格式 PEM私钥文件格式 BEGIN RSA PRIVATE KEY END RSA PRIVATE KEY 生成该密钥的Linux命令 OpenSSL gt genrsa out privateKey pem 1024 读
  • 100天精通Python(基础篇)——第2天:注释

    文章目录 一 注释的作用 二 单行注释如何定义 三 多行注释 一 注释的作用 1 注释是代码中的解释型语句 用来对代码内容进行注释 2 注释不是代码 不会被程序执行 二 单行注释如何定义 通过 号定义 建议在 和注释内容之间 间隔一个空格
  • 锚点

    在制作网页时 我们常常遇到需要添加侧边导航 通过点击导航让页面自动滚动到指定位置 如 电商网站的楼层 这就涉及到 a 标签的锚点应用 把它称为 锚点标签 一 锚点 我们想要让页面跳转到的位置 就是锚点 锚点是一种超链接 只不过它是页面内部的
  • 使用Html做一个简单的登陆页面

    目录 绪论 一 新建一个html项目 二 制作整体框架 三 使用CSS进行修饰 绪论 html作为一个常用的前端语言 使用的人群范围是很大的 如果你想要成为一个前端工程师 那必不可少的就要做一个登陆页面 登录页面一般就是账号和密码 另外还需

随机推荐

  • 一文读懂:什么是RFID

    在物联网领域 我们经常听到 RFID 这个词 接下来讲解一下 什么是 RFID 个人技术公众号 解决方案工程师 欢迎同领域的朋友关注 相互交流 RFID 全称为 Radio Frequency Identification 中文称 射频识别
  • VRRP与DHCP

    目录 一 VRRP 1 VRRP基本概述 2 VRRP基本机构 3 设备类型 4 工作原理 5 VRRP主备备份过程 二 DHCP 1 应用场景 2 工作原理 一 VRRP 1 VRRP基本概述 VRRP 虚拟路由器冗余协议 VRRP能够在
  • 软件架构之架构视图

    软件架构设计运用RUP4 1视图方法进行设计 4 1架构视图模型是1995年Philippe kruchen在 IEEE software 上发表的题为 The 4 1 View Model of Architecture 文 主要包括的架
  • Linux shell编程(三)shell脚本中的特殊变量详解

    1 环境变量 全局变量 环境变量一般使用export内置命令导出的变量 用于定义shell运行环境 保证shell命令能够正确执行 shell通过环境变量来确定登录的用户名等信息 所有的环境变量都是系统的全局变量 环境变量可以在命令行中创建
  • JMeter快速入门知识系列(12)----JMeter集合点

    12 1 集合点的定义 在性能测试过程中 为了真实模拟多个用户同时进行操作以度量服务器的处理能力 可以考虑同步虚拟用户以便恰好在同一时刻执行操作或发送请求 通过插入集合点可以较真实模拟多个用户并发操作 注意 虽然通过加入集合点可以约束请求同
  • Oracle为用户设置读权限

    Oracle 数据库中创建表只读用户 并为其设置密码永不过期 同义词 1 创建用户 create user test identified by 123456 default tablespace db temporary tablespa
  • Maven详细入门

    Maven 一 是什么 二 干什么 1 方便的依赖管理 2 统一的项目结构 3 标准的项目构建流程 三 怎么用 1 Maven坐标 2 依赖管理 2 1 依赖配置 2 2 依赖传递 2 3 依赖范围 2 4 生命周期 2 5 依赖原则 3
  • 利用cin.get()的特性实现输入的错误处理

    cin get读取数据cin get ch cin get读取字符串cin get 字符串名 长度 当括号内无参数时 读掉缓存取的一个数据 cin clear 重置错误输入标记 接受新的输入 如若cin位于测试条件中 则将被转换为bool类
  • 矩阵求导中的分母布局与分子布局

    最近在处理一些优化问题时 我才注意到 在不同的书籍 资料中函数 f x R n
  • Kubernetes轻量级日志工具Loki安装及踩坑记录

    Loki简介 Loki是Grafana出品的一个轻量级日志系统 熟悉ELK的都知道ELK使用起来的成本 而且仅仅是日志检索使用ELK的话有点大材小用了 Loki8技术栈中使用了以下组件 Promtail 用来将容器日志发送到 Loki 或者
  • Linux进程隐藏问题————显示隐藏进程

    阿里云云监控到有两台redis服务器CPU被某进程消耗400 cpu资源 系统查看Top 情况并未找到高消耗进程X7但CPU100 ni Netstat 查找到了一些异常请求 初步判断出组件被提权入侵了 尝试查找异常进程X7关联的文件 排查
  • 解决flutter showDialog下拉框,复选框等无法及时响应的问题

    使用StatefulBuilder showDialogr showDialog context context builder BuildContext ctx return StatefulBuilder builder BuildCo
  • Web Worker API

    Web Worker API Web Worker 使得在一个独立于 Web 应用程序主执行线程的后台线程中运行脚本操作成为可能 这样做的好处是可以在独立线程中执行费时的处理任务 使主线程 通常是 UI 线程 的运行不会被阻塞 放慢 Web
  • Kali 更换源(超详细,附国内优质镜像源地址)

    1 进入管理员下的控制台 2 输入密码后点击 授权 3 在控制台内输入下面的内容 vim etc apt sources list 4 敲击回车后会进入下面的页面 5 来到这个页面后的第一部是按键盘上的 i 键 左下角出现 插入 后说明操作
  • 使用APIKey定向加密对外接口案例

    使用API Key定向加密对外接口案例 整体思路 前端通过某种方式生成一个动态字段 例如时间戳 随机数 UUID 等 前端将动态字段和其他请求参数一起发送给后端 并对请求参数进行加密 后端通过相同的加密算法 使用动态字段生成 API KEY
  • Python解析库lxml与xpath用法总结

    本文主要围绕以xpath和lxml库进行展开 一 xpath 概念 xpath节点 xpath语法 xpath轴 xpath运算符 二 lxml的安装 lxml的使用 lxml案例 一 xpath 1 xpath概念 XPath 是一门在
  • 通用分页的详解(Java后端常用)

    目录 一 通用分页 1 1通用分页是什么 1 2使用通用分页有什么好处 1 3经常在哪使用通用分页 二 往常的通用分页实现及代码 三 通用分页的讲解 思路 具体实现代码 四 JUnit框架的使用 4 1JUnit框架是什么 4 2JUnit
  • 动态加载 JS 文件

    动态加载JS文件是指在网页运行过程中通过JavaScript代码向页面中动态添加外部JS文件 这种方式能够提高页面加载速度和用户体验 并且可以帮助网站实现更多的功能和特效 本文将详细介绍动态加载JS文件的基本原理 优势 注意事项以及具体实现
  • MySQL Workbench 报错 Cannot connect to Database Server

    MySQL Workbench是一款专为MySQL设计的ER 数据库建模工具 可以使用MySQL Workbench设计和创建新的数据库图示 建立数据库文档 以及进行复杂的MySQL迁移 MySQL Workbench是下一代的可视化数据库
  • lstm with attention_一文搞懂NLP中的Attention机制(附详细代码讲解)

    公众号关注 ML NLP 设为 星标 重磅干货 第一时间送达 机器学习算法与自然语言处理出品 公众号原创专栏作者 Don hub 单位 京东算法工程师 学校 帝国理工大学 Outline Intuition Analysis Pros Co