Comments (2)
在下面的代码中, 我觉得应该表明为什么 Q, K, V 向量序列是等于 inputs_embeds 的, 我理解的是注意力机制中的 QKV 是 embedding 与 W_Q 和 W_K , W_V 这三个矩阵相乘得到的, 这三个矩阵也是超参数, 而下面的代码是好像默认 这三个矩阵是单位矩阵. `import torch from math import sqrt
Q = K = V = inputs_embeds dim_k = K.size(-1) scores = torch.bmm(Q, K.transpose(1,2)) / sqrt(dim_k) print(scores.size())`
此外 dim_k = K.size(-1) 和下面封装的函数中不一致, 上面的 dim_k = K.size(-1), 而下面的 dim_k = query.size(-1)
`import torch import torch.nn.functional as F from math import sqrt
def scaled_dot_product_attention(query, key, value, query_mask=None, key_mask=None, mask=None): dim_k = query.size(-1) scores = torch.bmm(query, key.transpose(1, 2)) / sqrt(dim_k) if query_mask is not None and key_mask is not None: mask = torch.bmm(query_mask.unsqueeze(-1), key_mask.unsqueeze(1)) if mask is not None: scores = scores.masked_fill(mask == 0, -float("inf")) weights = F.softmax(scores, dim=-1) return torch.bmm(weights, value)`
- Because it is self-attention, Q=K=V
- If K.size(-1) != query.size(-1), how can the matrixes be multipled?
from how-to-use-transformers.
谢谢
from how-to-use-transformers.
Related Issues (17)
- transformers 用的哪个版本 HOT 2
- 如果你对 Prompting 概念不是很清楚,强烈建议先阅读一下《Prompt 方法简介》。 链接失效 HOT 1
- 请问有计划出pdf格式的文档吗? HOT 4
- 多卡训练 HOT 2
- Import "torch" could not be resolved
- question about attention mask HOT 1
- 如何基于transformers库自定义模型? HOT 1
- 大佬,第四部分什么时候出呀, 坐等 HOT 3
- 摘要评估用的Rouge,是不是有点问题?每次只取一个样本计算Rouge的值。 HOT 2
- 2,3章疑问 HOT 2
- 请问数据集有没有基于csv文件版本的?
- data/lcsts_tsv/data1.tsv 不存在 HOT 2
- end_token_index = torch.where(labels == tokenizer.eos_token_id)[1]
- torch.jit.trace追踪
- 大佬有没有继续预训练的使用方法啊? HOT 2
- 维度不对,这里的维度应该是 dxn 吧。如果是 nxd, X= (x1,x2,...,xn)^T HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from how-to-use-transformers.