2024 07 23, 414 pm 今日內容
2024-07-23, 4:14 PM-今日內容
# 2024-07-23-embedding
2024-07-23-tanh的作用和图像
2024-07-23-anh 函数在LSTM里面有什么作用吗? 他和new hidden state, previous hidden…
2024-07-23-环神经网络(RNN)在处理长序列数据时,存在“短期记忆”的问题,即它们在长时间步之后难以保留早期的信息。这…
2024-07-23-请解释为什么new weights = weight - learning rate * gradient…
2024-07-23-fully connected layer 为什么是another name of a basic vanilla…
2024-07-23-encoder和decoder的时候,要提到LSTM
encoder和decoder的时候,要提到LSTM
2024-07-23-embedding和encoding解惑
Embedding为每个词创建了2个嵌入值(即向量的维度是2)
2024-07-23-browser的设置问题
🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️下一個筆記🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️
2024-07-23-embedding
layout: post title: ‘embedding’ date: 2024-07-23T16:06:46-04:00
🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️下一個筆記🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️
2024-07-23-tanh的作用和图像
layout: post title: ‘tanh的作用和图像’ date: 2024-07-23T16:02:06-04:00
Tanh, 把任意输入的从-inifi 到+infinit的value压缩到-1 和+1 之间

#tanh #图像 #三角函数
🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️下一個筆記🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️
2024-07-23-anh 函数在LSTM里面有什么作用吗? 他和new hidden state, previous hidden…
2024-07-23-anh 函数在LSTM里面有什么作用吗? 他和new hidden state, previous hidden state concatenation有什么联系吗
layout: post title: ‘anh 函数在LSTM里面有什么作用吗? 他和new hidden state, previous hidden state concatenation有什么联系吗’ date: 2024-07-23T15:56:27-04:00
在 LSTM 中,tanh 函数扮演着重要的角色,主要用于控制记忆单元(cell state)和隐藏状态(hidden state)的输出。让我们详细解释 tanh 函数在 LSTM 中的作用及其与新隐藏状态和前一个隐藏状态的关系。
tanh 函数的作用
tanh 函数(双曲正切函数)是一个常见的激活函数,其输出范围为 -1 到 1。它在 LSTM 中的主要作用如下:
- 标准化输出:
tanh函数将输入值压缩到 -1 到 1 的范围内,有助于稳定网络的训练过程。 - 引入非线性:通过非线性激活函数,LSTM 可以捕捉更复杂的模式和关系,而不仅仅是线性关系。
LSTM 中的 tanh 使用
在 LSTM 中,tanh 函数主要出现在两个地方:
-
计算新的细胞状态(cell state)候选值: [ \tilde{C}t = \tanh(W_C \cdot [h{t-1}, x_t] + b_C) ] 这里,
tanh函数用于生成新的候选细胞状态 ( \tilde{C}_t ),其值范围在 -1 到 1 之间,有助于控制更新量。 -
计算新的隐藏状态(hidden state): [ h_t = o_t \times \tanh(C_t) ] 这里,
tanh函数应用于更新后的细胞状态 ( C_t ),输出新的隐藏状态 ( h_t )。这样可以确保隐藏状态的值在 -1 到 1 之间,提供稳定的激活信号。
新隐藏状态和前一个隐藏状态的关系
在 LSTM 中,隐藏状态的计算依赖于前一个隐藏状态、当前输入以及各种门控机制的组合。具体来说,隐藏状态的更新步骤如下:
-
前一个隐藏状态和当前输入的连接: [ [h_{t-1}, x_t] ] 在计算门控值(如输入门、遗忘门、输出门和候选细胞状态)时,前一个隐藏状态 ( h_{t-1} ) 和当前输入 ( x_t ) 通常会被连接起来作为输入。
-
计算新的候选细胞状态: [ \tilde{C}t = \tanh(W_C \cdot [h{t-1}, x_t] + b_C) ] 这里,
tanh函数用于生成新的候选细胞状态。 -
更新细胞状态: [ C_t = f_t \times C_{t-1} + i_t \times \tilde{C}t ] 细胞状态结合了前一个细胞状态 ( C{t-1} ) 和新的候选细胞状态 ( \tilde{C}_t ),通过遗忘门 ( f_t ) 和输入门 ( i_t ) 的控制来更新。
-
计算新的隐藏状态: [ h_t = o_t \times \tanh(C_t) ] 新的隐藏状态通过
tanh函数应用于更新后的细胞状态 ( C_t ),并通过输出门 ( o_t ) 的控制来产生。
通过这种方式,LSTM 能够利用前一个隐藏状态和当前输入的信息,结合非线性变换(tanh 函数)和门控机制,有效地捕捉序列数据中的长距离依赖关系。
图示化解释
- 输入序列:
x_t和前一个隐藏状态h_{t-1}被连接并输入到各个门(输入门、遗忘门、输出门)。 - 遗忘门:决定丢弃多少过去的信息。
- 输入门:决定添加多少新的信息。
- 候选细胞状态:通过
tanh函数计算,生成新的候选细胞状态。 - 细胞状态更新:综合前一个细胞状态和新的候选细胞状态,得到更新的细胞状态。
- 隐藏状态更新:通过
tanh函数应用于更新后的细胞状态,并结合输出门的控制,生成新的隐藏状态。
补充学习资源
为了更深入理解这些概念,可以参考以下资源:
- 博客和教程
- 视频教程
- 在线课程
- Coursera: Sequence Models by Andrew Ng
这些资源将帮助你更好地理解 LSTM 中 tanh 函数的作用及其在隐藏状态和细胞状态更新中的角色。
Shortcuts for Next Step:
- input “1” for a practical implementation of LSTM with detailed explanations of each step
- input “2” for an in-depth tutorial on gate mechanisms in LSTM
- input “3” for further exploration of LSTM variants like GRU
Or, you can just type “continue” or “c”, I will continue automatically.
#tanh #lstm #hidden_state #concatenation
🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️下一個筆記🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️
2024-07-23-环神经网络(RNN)在处理长序列数据时,存在“短期记忆”的问题,即它们在长时间步之后难以保留早期的信息。这…
2024-07-23-环神经网络(RNN)在处理长序列数据时,存在“短期记忆”的问题,即它们在长时间步之后难以保留早期的信息。这主要是因为 RNN 中的信息在每个时间步都会被更新,随着时间步数的增加,早期的信息逐渐被后来的信息所覆盖。此外,RNN 容易受到梯度消失和梯度爆炸问题的影响,使得它们在训练过程中难以有效地捕捉长距离依赖关系。
layout: post title: ‘环神经网络(RNN)在处理长序列数据时,存在“短期记忆”的问题,即它们在长时间步之后难以保留早期的信息。这主要是因为 RNN 中的信息在每个时间步都会被更新,随着时间步数的增加,早期的信息逐渐被后来的信息所覆盖。此外,RNN 容易受到梯度消失和梯度爆炸问题的影响,使得它们在训练过程中难以有效地捕捉长距离依赖关系。’ date: 2024-07-23T15:49:57-04:00
RNN 的短期记忆问题
普通的循环神经网络(RNN)在处理长序列数据时,存在“短期记忆”的问题,即它们在长时间步之后难以保留早期的信息。这主要是因为 RNN 中的信息在每个时间步都会被更新,随着时间步数的增加,早期的信息逐渐被后来的信息所覆盖。此外,RNN 容易受到梯度消失和梯度爆炸问题的影响,使得它们在训练过程中难以有效地捕捉长距离依赖关系。
LSTM 如何解决短期记忆问题
LSTM(长短期记忆网络)通过引入一种更复杂的结构,包括记忆单元(cell state)和三个门控机制(输入门、遗忘门和输出门),有效地解决了 RNN 的短期记忆问题。
1. 记忆单元(Cell State)
LSTM 引入了一个直接流经整个序列的“记忆单元”或“细胞状态”,可以看作是一个高速公路,信息可以在其中相对不变地流动。LSTM 通过门控机制来控制信息的流入、流出和保留,从而有效地保留长期依赖信息。
2. 门控机制
LSTM 使用三个门来控制信息的流动,这些门是:
-
遗忘门(Forget Gate):决定遗忘多少来自前一时间步的状态。 [ f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + b_f) ] 遗忘门输出的值在 0 和 1 之间,表示要保留多少过去的记忆。
-
输入门(Input Gate):决定当前时间步的输入信息有多少需要保存到记忆单元中。 [ i_t = \sigma(W_i \cdot [h_{t-1}, x_t] + b_i) ] [ \tilde{C}t = \tanh(W_C \cdot [h{t-1}, x_t] + b_C) ] 输入门控制的值在 0 和 1 之间,决定了当前输入信息对记忆单元更新的影响程度。
-
输出门(Output Gate):决定从记忆单元中提取多少信息作为当前时间步的输出。 [ o_t = \sigma(W_o \cdot [h_{t-1}, x_t] + b_o) ] [ h_t = o_t \times \tanh(C_t) ] 输出门的值在 0 和 1 之间,决定了当前记忆单元状态有多少用于计算当前时间步的输出。
3. LSTM 的工作流程
每个时间步中,LSTM 通过以下步骤更新和计算状态:
-
遗忘上一步的状态: [ C_t = f_t \times C_{t-1} ]
-
将当前输入的信息写入状态: [ C_t = C_t + i_t \times \tilde{C}_t ]
-
根据当前状态和输出门的结果计算新的隐藏状态: [ h_t = o_t \times \tanh(C_t) ]
为什么 LSTM 有效
LSTM 的这种结构允许它在长序列中保留重要信息,而不会因为时间步数的增加而丢失这些信息。具体来说:
- 遗忘门让模型可以决定丢弃哪些不再重要的信息。
- 输入门允许模型选择哪些新的信息需要加入记忆单元。
- 输出门控制了哪些部分的记忆需要输出。
通过这些门控机制,LSTM 可以在长时间步上保留和利用信息,解决了普通 RNN 中存在的短期记忆问题。
补充学习资源
为了更深入地理解这些概念,可以参考以下资源:
- 博客和教程
- Understanding LSTM Networks - 这篇文章提供了关于 LSTM 的深入而形象的解释。
- 视频教程
- YouTube - LSTM Explained - 这段视频详细解释了 LSTM 的内部工作原理。
- 在线课程
- Coursera: Sequence Models by Andrew Ng - 该课程详细讲解了序列模型,包括 RNN、LSTM 和 GRU。
通过这些资源,你可以更深入地了解 LSTM 是如何解决 RNN 的短期记忆问题的。
Shortcuts for Next Step:
- input “1” for a practical implementation of LSTM in a sequence modeling task
- input “2” for detailed explanation of GRU and its comparison with LSTM
- input “3” for further exploration of neural network architectures in sequence tasks
Or, you can just type “continue” or “c”, I will continue automatically.
#lstm #RNN #GRU #RNN-LSTM-GRU
🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️下一個筆記🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️
2024-07-23-请解释为什么new weights = weight - learning rate * gradient…
2024-07-23-请解释为什么new weights = weight - learning rate * gradient (gradient update rule )用中文。我想知道为啥LSTM, GRU和vanilla NN 有关系。 同时请告诉我back propagation是啥。我们为什么要用
layout: post title: ‘请解释为什么new weights = weight - learning rate * gradient (gradient update rule )用中文。我想知道为啥LSTM, GRU和vanilla NN 有关系。 同时请告诉我back propagation是啥。我们为什么要用’ date: 2024-07-23T15:49:16-04:00
梯度更新规则解释
梯度更新规则中的公式:
[ \text{new weights} = \text{weights} - \text{learning rate} \times \text{gradient} ]
这个公式解释了如何更新神经网络中的权重:
- 权重(weights):神经网络中的参数,决定了输入数据如何经过网络的层层计算,最终产生输出。
- 学习率(learning rate):一个超参数,用来控制每次权重更新的步幅大小。学习率过大可能导致训练不稳定,学习率过小则可能导致收敛速度过慢。
- 梯度(gradient):损失函数对权重的偏导数,表示在当前点上,权重变化对损失的影响方向和大小。
通过每次迭代减去学习率乘以梯度,可以逐步调整权重,使得损失函数逐步减小,从而优化模型。
LSTM, GRU 和 Vanilla NN 的关系
-
Vanilla NN(普通神经网络):普通的前馈神经网络,每一层的神经元与下一层的所有神经元相连,没有循环或时间序列处理能力。它们主要用于处理静态数据,例如图像分类。
-
RNN(循环神经网络):在普通神经网络基础上增加了循环连接,可以处理序列数据。每个时间步的输出依赖于前一个时间步的状态,因此适用于时间序列、自然语言处理等任务。但 RNN 存在梯度消失和梯度爆炸问题,处理长序列时效果不好。
-
LSTM(长短期记忆网络):对 RNN 的改进,引入了门控机制(输入门、遗忘门、输出门),使得它能更好地捕捉长距离依赖关系,缓解了梯度消失和爆炸问题。
-
GRU(门控循环单元):对 LSTM 的简化,引入了更新门和重置门,保留了捕捉长距离依赖的能力,同时减少了参数数量,计算更高效。
反向传播(Backpropagation)
反向传播是神经网络训练中的一种算法,用于计算损失函数对各层权重的梯度,并通过这些梯度更新权重。反向传播步骤包括:
- 前向传播(Forward Propagation):计算输入数据经过网络后得到的输出,并根据实际值计算损失。
- 反向传播(Backward Propagation):从输出层开始,逐层计算损失函数对各层权重的梯度,利用链式法则将误差反向传播到每一层。
- 权重更新:利用梯度下降法,根据计算出的梯度和学习率,更新每一层的权重。
反向传播的重要性在于:
- 高效计算梯度:通过链式法则,可以高效地计算每一层的梯度。
- 自动化训练:使得神经网络的训练过程能够自动优化,逐步减小损失函数,从而提高模型的准确性。
总结
- 梯度更新规则:通过权重减去学习率乘以梯度,逐步优化模型。
- LSTM、GRU 和 Vanilla NN 的关系:LSTM 和 GRU 是对 RNN 的改进,增强了处理长序列的能力,Vanilla NN 则是基本的前馈神经网络。
- 反向传播:通过前向传播计算损失,再通过反向传播计算梯度,最终更新权重,以优化模型。
这些概念的掌握有助于理解神经网络的训练过程和优化原理。希望这些解释对你有所帮助。
Shortcuts for Next Step:
- input “1” for a detailed tutorial on gradient descent and backpropagation
- input “2” for practical implementation of LSTM and GRU models
- input “3” for further resources on neural network optimization techniques
Or, you can just type “continue” or “c”, I will continue automatically.
#lstm #GRU #vanillaNN
🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️下一個筆記🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️
2024-07-23-fully connected layer 为什么是another name of a basic vanilla…
2024-07-23-fully connected layer 为什么是another name of a basic vanilla neural network?你能讲一下为什么我们把一个句子输入(包含很多的word) 他是怎么通过LSTM的encoder decoder的。我想知道具体的数据和矩阵操作变换,还有这些数据是怎么通过fully connected layer 的。 请帮我把这些概念和数据变换过程搞清楚。
layout: post title: ‘fully connected layer 为什么是another name of a basic vanilla neural network?你能讲一下为什么我们把一个句子输入(包含很多的word) 他是怎么通过LSTM的encoder decoder的。我想知道具体的数据和矩阵操作变换,还有这些数据是怎么通过fully connected layer 的。 请帮我把这些概念和数据变换过程搞清楚。’ date: 2024-07-23T15:38:34-04:00
Fully Connected Layer and Vanilla Neural Network
A fully connected layer, also known as a dense layer, is called another name for a basic vanilla neural network because it forms the foundational building block of many neural networks. In a fully connected layer, every neuron is connected to every neuron in the previous layer, meaning all inputs affect all outputs.
LSTM Encoder-Decoder Architecture
Let’s break down how an LSTM-based encoder-decoder model processes a sentence, including the data transformations and matrix operations involved, and how the fully connected layer (FC layer) fits into this.
1. Input Sentence and Embedding Layer
- Input Sentence: “Hello, how are you?”
- Word Embedding: Each word in the sentence is converted into a dense vector representation using an embedding layer.
- Example:
- “Hello” -> [0.1, 0.2, …, 0.3]
- “how” -> [0.4, 0.5, …, 0.6]
- “are” -> [0.7, 0.8, …, 0.9]
- “you” -> [0.2, 0.3, …, 0.4]
- Example:
2. Encoder LSTM
The embedded sentence is fed into the encoder LSTM one word at a time.
- Input Sequence:
- X = [x_hello, x_how, x_are, x_you]
- LSTM Equations (for simplicity, omitting bias terms):
- ( f_t = \sigma(W_f \cdot [h_{t-1}, x_t]) ) (forget gate)
- ( i_t = \sigma(W_i \cdot [h_{t-1}, x_t]) ) (input gate)
- ( \tilde{C}t = \tanh(W_C \cdot [h{t-1}, x_t]) ) (cell candidate)
- ( C_t = f_t * C_{t-1} + i_t * \tilde{C}_t ) (cell state)
- ( o_t = \sigma(W_o \cdot [h_{t-1}, x_t]) ) (output gate)
- ( h_t = o_t * \tanh(C_t) ) (hidden state)
Each word’s embedding vector ( x_t ) is processed, and the hidden state ( h_t ) and cell state ( C_t ) are updated at each time step.
3. Context Vector
After processing all words, the final hidden state ( h_T ) and cell state ( C_T ) form the context vector, encapsulating the entire input sentence.
4. Decoder LSTM
The context vector is passed to the decoder LSTM, which generates the output sequence (e.g., translated sentence) one word at a time.
- Initial State:
- ( h_0^{dec} = h_T^{enc} )
- ( C_0^{dec} = C_T^{enc} )
- Output Sequence Generation:
- The decoder uses the previous word ( y_{t-1} ) (or
at start) and the current hidden state to generate the next word. - ( x_t^{dec} ) (input embedding of decoder) is processed similarly to the encoder, but also involves the fully connected layer for final word prediction.
- The decoder uses the previous word ( y_{t-1} ) (or
5. Fully Connected Layer
The fully connected layer takes the hidden state from the decoder LSTM and maps it to the output vocabulary size, producing the logits for each possible next word.
- Equation:
- ( y_t = \text{softmax}(W_{fc} \cdot h_t^{dec} + b_{fc}) )
Here, ( W_{fc} ) and ( b_{fc} ) are the weights and biases of the fully connected layer.
Data Flow and Matrix Operations:
- Embedding: Converts words to vectors.
- Matrix multiplication between word indices and embedding matrix.
- LSTM Cell: Processes each word embedding.
- Multiple matrix multiplications for gates and state updates.
- Context Vector: Final hidden and cell states from encoder.
- Decoder Input: Initial states from context vector.
- Decoder LSTM: Generates sequence.
- Fully Connected Layer: Converts decoder hidden state to vocabulary logits.
- Matrix multiplication and softmax for word prediction.
Visual Explanation and Further Learning
For a more visual and detailed explanation, including interactive elements, I recommend the following resources:
- Understanding LSTM Networks (Blog): Colah’s Blog on LSTMs
- This blog provides an excellent visual and intuitive explanation of how LSTMs work.
- The Illustrated Transformer (Blog): The Illustrated Transformer
- Though focused on transformers, it includes visual explanations of attention mechanisms that complement understanding of sequence models.
- Deep Learning Specialization (Course): Coursera - Sequence Models by Andrew Ng
- This course covers sequence models in-depth, including LSTM and encoder-decoder architectures.
- Video Tutorial: YouTube - LSTM and Encoder-Decoder Video
- This video tutorial explains the working of LSTMs and their use in encoder-decoder models.
These resources will help you understand the intricacies of LSTM-based encoder-decoder architectures, their data transformations, and the role of fully connected layers.
Shortcuts for Next Step:
- input “1” for a detailed code example of LSTM encoder-decoder
- input “2” for an in-depth tutorial on LSTM gates and operations
- input “3” for further resources on advanced sequence modeling
Or, you can just type “continue” or “c”, I will continue automatically.
#lstm #encoder #decoder #scratch #vanillaNN #fully_connected_layer
🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️下一個筆記🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️
2024-07-23-encoder和decoder的时候,要提到LSTM
layout: post title: ‘encoder和decoder的时候,要提到LSTM’ date: 2024-07-23T14:57:45-04:00
Encoder 和 Decoder 中的 LSTM 0. Encoder 部分:编码器的任务是将输入序列转换为一个固定大小的上下文向量。LSTM 的长距离依赖处理能力使其能够有效地捕捉输入序列中的关键信息,并将其编码成上下文向量。 0. Decoder 部分:解码器的任务是从上下文向量生成输出序列。LSTM 能够利用上下文向量和之前生成的输出灵活地生成新输出,使其在机器翻译等任务中表现出色。 举例子:
输入句子 (英语): [Hello, how are you?] ↓ (Encoder LSTM) ↓ 上下文向量 (Context Vector) ↓ (Decoder LSTM) ↓ 输出句子 (法语): [Bonjour, comment ça va?]
具体示例代码
以下是一个使用 LSTM 作为 Encoder 和 Decoder 的简单 Seq2Seq 模型的 PyTorch 示例:
import torch
import torch.nn as nn
class Encoder(nn.Module):
def __init__(self, input_dim, emb_dim, hidden_dim, n_layers, dropout):
super(Encoder, self).__init__()
self.embedding = nn.Embedding(input_dim, emb_dim)
self.lstm = nn.LSTM(emb_dim, hidden_dim, n_layers, dropout=dropout)
def forward(self, src):
embedded = self.embedding(src)
outputs, (hidden, cell) = self.lstm(embedded)
return hidden, cell
class Decoder(nn.Module):
def __init__(self, output_dim, emb_dim, hidden_dim, n_layers, dropout):
super(Decoder, self).__init__()
self.embedding = nn.Embedding(output_dim, emb_dim)
self.lstm = nn.LSTM(emb_dim, hidden_dim, n_layers, dropout=dropout)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, trg, hidden, cell):
embedded = self.embedding(trg).unsqueeze(0)
output, (hidden, cell) = self.lstm(embedded, (hidden, cell))
prediction = self.fc(output.squeeze(0))
return prediction, hidden, cell
class Seq2Seq(nn.Module):
def __init__(self, encoder, decoder):
super(Seq2Seq, self).__init__()
self.encoder = encoder
self.decoder = decoder
def forward(self, src, trg, teacher_forcing_ratio=0.5):
hidden, cell = self.encoder(src)
outputs = torch.zeros(trg.size(0), trg.size(1), self.decoder.fc.out_features).to(trg.device)
input = trg[0, :]
for t in range(1, trg.size(0)):
output, hidden, cell = self.decoder(input, hidden, cell)
outputs[t] = output
teacher_force = torch.rand(1).item() < teacher_forcing_ratio
input = trg[t] if teacher_force else output.argmax(1)
return outputs
#lstm #encoder #decoder #LSTM-encoder-decoder
🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️下一個筆記🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️
encoder和decoder的时候,要提到LSTM Encoder 和 Decoder 中的 LSTM 1. Encoder 部分:编码器的任务是将输入序列转换为一个固定大小的上下文向量。LSTM 的长距离依赖处理能力使其能够有效地捕捉输入序列中的关键信息,并将其编码成上下文向量。 2. Decoder 部分:解码器的任务是从上下文向量生成输出序列。LSTM 能够利用上下文向量和之前生成的输出灵活地生成新输出,使其在机器翻译等任务中表现出色。 举例子:
输入句子 (英语): [Hello, how are you?] ↓ (Encoder LSTM) ↓ 上下文向量 (Context Vector) ↓ (Decoder LSTM) ↓ 输出句子 (法语): [Bonjour, comment ça va?]
具体示例代码
以下是一个使用 LSTM 作为 Encoder 和 Decoder 的简单 Seq2Seq 模型的 PyTorch 示例:
import torch
import torch.nn as nn
class Encoder(nn.Module):
def __init__(self, input_dim, emb_dim, hidden_dim, n_layers, dropout):
super(Encoder, self).__init__()
self.embedding = nn.Embedding(input_dim, emb_dim)
self.lstm = nn.LSTM(emb_dim, hidden_dim, n_layers, dropout=dropout)
def forward(self, src):
embedded = self.embedding(src)
outputs, (hidden, cell) = self.lstm(embedded)
return hidden, cell
class Decoder(nn.Module):
def __init__(self, output_dim, emb_dim, hidden_dim, n_layers, dropout):
super(Decoder, self).__init__()
self.embedding = nn.Embedding(output_dim, emb_dim)
self.lstm = nn.LSTM(emb_dim, hidden_dim, n_layers, dropout=dropout)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, trg, hidden, cell):
embedded = self.embedding(trg).unsqueeze(0)
output, (hidden, cell) = self.lstm(embedded, (hidden, cell))
prediction = self.fc(output.squeeze(0))
return prediction, hidden, cell
class Seq2Seq(nn.Module):
def __init__(self, encoder, decoder):
super(Seq2Seq, self).__init__()
self.encoder = encoder
self.decoder = decoder
def forward(self, src, trg, teacher_forcing_ratio=0.5):
hidden, cell = self.encoder(src)
outputs = torch.zeros(trg.size(0), trg.size(1), self.decoder.fc.out_features).to(trg.device)
input = trg[0, :]
for t in range(1, trg.size(0)):
output, hidden, cell = self.decoder(input, hidden, cell)
outputs[t] = output
teacher_force = torch.rand(1).item() < teacher_forcing_ratio
input = trg[t] if teacher_force else output.argmax(1)
return outputs
🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️下一個筆記🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️
2024-07-23-embedding和encoding解惑
layout: post title: ‘embedding和encoding解惑’ date: 2024-07-23T14:53:04-04:00
Embedding为每个词创建了2个嵌入值(即向量的维度是2)
嵌入(Embedding) 嵌入(Embedding) 是将高维离散变量(如词语)转换为低维连续向量表示的方法。每个词语在嵌入层中都被表示为一个向量,这个向量通常有数百或数千个维度,用于捕捉词语的语义信息和上下文关系。 举例说明 假设我们有一个简单的句子:”Hello world”。我们通常会对每个词进行嵌入。嵌入层会将词语转换为向量,例如: “Hello” -> [0.5, -0.3] “world” -> [0.1, 0.8] 在这个例子中,我们为每个词创建了2个嵌入值(即向量的维度是2)。但是,在实际应用中,我们通常会使用更高维度的嵌入,例如100维、300维甚至更多: “Hello” -> [0.5, -0.3, 0.2, …, 0.1] “world” -> [0.1, 0.8, -0.4, …, 0.7] 使用更高维度的嵌入可以更好地捕捉词语之间的复杂关系和语义信息。
Encoding 和embedding的区别是,encoding是把词汇对应成为为一个ID, 但是embedding是吧token转换成为高纬度的向量。
Transformer = encoder+decoder。 encoder和decoder都是有很多的layer组成的, 每个layer都有自己的sublayer。这里的sublayer一般只有两个,也就是multi-headed self-attention mechanism 和feedforward neural network.(FFNN)
⁃ Multihead是对输入序列进行加权求和,以捕捉序列中不同位置的依赖关系。
⁃ FFNN是对自注意力机制的输出进行下一步的非线性变换。FFNN= 两个线性变换(全连接层)+非线性激活函数(ReLU) : [ FFN(x) = \max(0, xW_1 + b_1)W_2 + b_2 ]
• 其中,(W_1)、(W_2) 是权重矩阵,(b_1)、(b_2) 是偏置向量,(\max(0, x)) 是 ReLU 激活函数。 FFNN: Linear -> ReLU -> Linear
#encoding #decoding #FFNN #ReLU #multiheadead_self_attention
🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️下一個筆記🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️
Embedding为每个词创建了2个嵌入值(即向量的维度是2) 嵌入(Embedding) 嵌入(Embedding) 是将高维离散变量(如词语)转换为低维连续向量表示的方法。每个词语在嵌入层中都被表示为一个向量,这个向量通常有数百或数千个维度,用于捕捉词语的语义信息和上下文关系。 举例说明 假设我们有一个简单的句子:”Hello world”。我们通常会对每个词进行嵌入。嵌入层会将词语转换为向量,例如: “Hello” -> [0.5, -0.3] “world” -> [0.1, 0.8] 在这个例子中,我们为每个词创建了2个嵌入值(即向量的维度是2)。但是,在实际应用中,我们通常会使用更高维度的嵌入,例如100维、300维甚至更多: “Hello” -> [0.5, -0.3, 0.2, …, 0.1] “world” -> [0.1, 0.8, -0.4, …, 0.7] 使用更高维度的嵌入可以更好地捕捉词语之间的复杂关系和语义信息。
Encoding 和embedding的区别是,encoding是把词汇对应成为为一个ID, 但是embedding是吧token转换成为高纬度的向量。
Transformer = encoder+decoder。 encoder和decoder都是有很多的layer组成的, 每个layer都有自己的sublayer。这里的sublayer一般只有两个,也就是multi-headed self-attention mechanism 和feedforward neural network.(FFNN)
⁃ Multihead是对输入序列进行加权求和,以捕捉序列中不同位置的依赖关系。
⁃ FFNN是对自注意力机制的输出进行下一步的非线性变换。FFNN= 两个线性变换(全连接层)+非线性激活函数(ReLU) : [ FFN(x) = \max(0, xW_1 + b_1)W_2 + b_2 ]
• 其中,(W_1)、(W_2) 是权重矩阵,(b_1)、(b_2) 是偏置向量,(\max(0, x)) 是 ReLU 激活函数。 FFNN: Linear -> ReLU -> Linear
🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️下一個筆記🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️🐿️
2024-07-23-browser的设置问题
layout: post title: ‘browser的设置问题 ‘ date: 2024-07-23T13:20:20-04:00
safari用来看YouTube可以翻译中文。 因为有插件immersive translator。iPad可以用,比较干净,如果有YouTube上的学习资源,可以在safari的reading list上继续观看,同时0.75倍速去学习这个材料。
Google上可以对YouTube ,Udemy, Netflix 都进行翻译中文,比较适合日常的每一项工作,所以defaul browser还是应该选择Google。 插件的名字叫language reactor, 和udemy subtitle translate
#browser #google #safari #extension #匯總今日內容