赞
踩
YOLO-World 模型中的文本编码器部分主要负责将文本信息转换为可用于模型进一步处理的嵌入表示,以下是对 YOLO-World 中文本编码器主要内容。
由于在yolo-world网络中主要利用预训练的CLIP模型将输入文本(如类别名称、名词短语或对象描述)编码为文本嵌入,那么我们就着重看一下clip模型中的文本编码部分。
from transformers import GPT2Tokenizer
input_text = "A cat sits on the mat.
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
tokens = tokenizer.tokenize(input_text)
tokens_ids = tokenizer.convert_tokens_to_ids(tokens)
print(tokens) # ['A', 'Ġcat', 'Ġsits', 'Ġon', 'Ġthe', 'Ġmat', '.']
print(tokens_ids) # [32, 5778, 1374, 319, 262, 2682, 13]
Input Text: "A cat sits on the mat."
Step 1: Tokenization
Tokens: ["[CLS]", "A", "cat", "sits", "on", "the", "mat", "[SEP]"]
Step 2: Token and Position Embeddings
Token Embeddings: [e_cls, e_A, e_cat, e_sits, e_on, e_the, e_mat, e_sep]
Position Embeddings: [p_0, p_1, p_2, p_3, p_4, p_5, p_6, p_7]
Final Embeddings: [e_cls + p_0, e_A + p_1, e_cat + p_2, ...]
(vocab_size, embedding_dim)
的矩阵,其中vocab_size
是词汇表的大小,embedding_dim
是嵌入向量的维度。每个token的嵌入向量是通过查找嵌入矩阵的相应行获得的。import torch
from transformers import GPT2Model
model = GPT2Model.from_pretrained('gpt2')
input_ids = torch.tensor([tokens_ids])
with torch.no_grad():
outputs = model(input_ids)
embeddings = outputs.last_hidden_state
print(embeddings.shape)
# torch.Size([1, 7, 768]) - 1 sentence, 7 tokens, 768 dimensions
Multi-Head Self-Attention流程如下图所示:
Multi-Head Self-Attention计算过程如图所示:
下面通过具体例子进行解释:
1、input-1的查询向量为[1, 0, 2],分别乘上input-1、input-2、input-3的键向量,获得三个score为2,4,4。
2、然后对这三个score取softmax,获得了input-1、input-2、input-3各自的重要程度,获得三个重要程度为0.0,0.5,0.5。
3、然后将这个重要程度乘上input-1、input-2、input-3的值向量,求和,即
0.0 ∗ [ 1 , 2 , 3 ] + 0.5 ∗ [ 2 , 8 , 0 ] + 0.5 ∗ [ 2 , 6 , 3 ] = [ 2.0 , 7.0 , 1.5 ] 0.0 * [1, 2, 3] + 0.5 * [2, 8, 0] + 0.5 * [2, 6, 3] = [2.0, 7.0, 1.5]0.0∗[1,2,3]+0.5∗[2,8,0]+0.5∗[2,6,3]=[2.0,7.0,1.5]。
4、此时我们获得了input-1的输出 [2.0, 7.0, 1.5]。
更多细节请参考
原文链接:多模态模型学习1——CLIP对比学习 语言-图像预训练模型
# Transformer Encoder 内部过程示例 from torch.nn import functional as F def scaled_dot_product_attention(Q, K, V): d_k = Q.size(-1) scores = torch.matmul(Q, K.transpose(-2, -1)) / torch.sqrt(d_k) attention_weights = F.softmax(scores, dim=-1) output = torch.matmul(attention_weights, V) return output # Q, K, V 矩阵是通过线性变换从 embeddings 获得的 Q = torch.nn.Linear(768, 768)(embeddings) K = torch.nn.Linear(768, 768)(embeddings) V = torch.nn.Linear(768, 768)(embeddings) attention_output = scaled_dot_product_attention(Q, K, V)
from torch.nn import LayerNorm
# 残差连接
residual_output = embeddings + attention_output
# 层归一化
layer_norm = LayerNorm(768)
normalized_output = layer_norm(residual_output)
ffn = torch.nn.Sequential(
torch.nn.Linear(768, 3072),
torch.nn.ReLU(),
torch.nn.Linear(3072, 768)
)
ffn_output = ffn(normalized_output)
for _ in range(12): # 假设有12层
Q = torch.nn.Linear(768, 768)(ffn_output)
K = torch.nn.Linear(768, 768)(ffn_output)
V = torch.nn.Linear(768, 768)(ffn_output)
attention_output = scaled_dot_product_attention(Q, K, V)
residual_output = ffn_output + attention_output
normalized_output = layer_norm(residual_output)
ffn_output = ffn(normalized_output)
# GPT-2 不使用 [CLS] token,但我们可以使用第一个 token 的嵌入作为句子表示
sentence_embedding = ffn_output[:, 0, :] # 取第一个 token 的嵌入
sentence_embedding = torch.nn.functional.normalize(sentence_embedding, p=2, dim=1)
通过这些步骤,文本编码器能够将输入的文本转换为一个高维空间中的向量表示,这个向量捕捉了文本的语义信息,并可以用于后续的下游任务,如与图像特征的比较等。
下图是Transformer模块图示:
至此就实现了把一个文本词汇输出为一个词向量的完整过程(如有错误请批评指正,谢谢!)。
文本编码器是一个基于 Transformer 架构的组件,它用于将输入的文本信息转换成文本嵌入(text embeddings)。这些嵌入捕获了文本的语义特征,并且能够与图像特征相结合,以增强模型对视觉内容的理解。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。