DL之self-attention:self-attention自注意力机制模块思路的8个步骤及其代码实现

简介: DL之self-attention:self-attention自注意力机制模块思路的8个步骤及其代码实现


目录

代码实现


 

 

相关文章

DL之Attention:Attention的简介、应用领域之详细攻略

DL之self-attention:self-attention的简介、应用之详细攻略

 

代码实现

1. import torch
2. 
3. #1、准备输入:Input 1、2、3
4. x = [[1, 0, 1, 0], 
5.      [0, 2, 0, 2],
6.      [1, 1, 1, 1]  ]
7. x = torch.tensor(x, dtype=torch.float32)
8. 
9. 
10. #2、初始化权重
11. w_key   = [ [0, 0, 1], [1, 1, 0], [0, 1, 0], [1, 1, 0] ]
12. w_query = [ [1, 0, 1], [1, 0, 0], [0, 0, 1], [0, 1, 1] ]
13. w_value = [ [0, 2, 0], [0, 3, 0], [1, 0, 3], [1, 1, 0] ]
14. 
15. w_key   = torch.tensor(w_key,   dtype=torch.float32)
16. w_query = torch.tensor(w_query, dtype=torch.float32)
17. w_value = torch.tensor(w_value, dtype=torch.float32)
18. 
19. 
20. #3、推导键、查询和值
21. keys   = x @ w_key
22. querys = x @ w_query
23. values = x @ w_value
24. 
25. print(keys)   # tensor([[0., 1., 1.], [4., 4., 0.], [2., 3., 1.]])
26. print(querys) # tensor([[1., 0., 2.], [2., 2., 2.], [2., 1., 3.]])
27. print(values) # tensor([[1., 2., 3.], [2., 8., 0.], [2., 6., 3.]])
28. 
29. 
30. #4、计算注意力得分
31. attn_scores = querys @ keys.t()
32. # tensor([[ 2.,  4.,  4.],  # attention scores from Query 1
33. #         [ 4., 16., 12.],  # attention scores from Query 2
34. #         [ 4., 12., 10.]]) # attention scores from Query 3
35. 
36. 
37. 
38. #5、计算softmax
39. from torch.nn.functional import softmax
40. attn_scores_softmax = softmax(attn_scores, dim=-1)
41. print('attn_scores_softmax:','\n',attn_scores_softmax)
42. # tensor([[6.3379e-02, 4.6831e-01, 4.6831e-01],
43. #         [6.0337e-06, 9.8201e-01, 1.7986e-02],
44. #         [2.9539e-04, 8.8054e-01, 1.1917e-01]])
45. 
46. # For readability, approximate the above as follows
47. attn_scores_softmax = [  [0.0, 0.5, 0.5], [0.0, 1.0, 0.0], [0.0, 0.9, 0.1] ]
48. attn_scores_softmax = torch.tensor(attn_scores_softmax)
49. 
50. #6、将得分和值相乘
51. weighted_values = values[:,None] * attn_scores_softmax.t()[:,:,None]
52. print('weighted_values:','\n',weighted_values)
53. # tensor([[[0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000]],
54. #         [[1.0000, 4.0000, 0.0000], [2.0000, 8.0000, 0.0000], [1.8000, 7.2000, 0.0000]],
55. #         [[1.0000, 3.0000, 1.5000], [0.0000, 0.0000, 0.0000], [0.2000, 0.6000, 0.3000]]])
56. 
57. #7、求和加权值
58. outputs = weighted_values.sum(dim=0)
59. # tensor([[2.0000, 7.0000, 1.5000], [2.0000, 8.0000, 0.0000], [2.0000, 7.8000, 0.3000]]) # Output1、2、3
60. print('outputs:','\n',outputs)
61. 
62. 
63. 
64.

 

相关文章
|
机器学习/深度学习 自然语言处理 并行计算
Self-Attention 原理与代码实现
Self-Attention 原理与代码实现
838 0
|
存储 算法 数据可视化
Python 金融编程第二版(GPT 重译)(一)(1)
Python 金融编程第二版(GPT 重译)(一)
240 1
|
安全 网络安全 iOS开发
macOS系统安装NMAP扫描工具
macOS系统安装NMAP扫描工具
614 1
|
数据采集 存储 数据可视化
芒果tv数据采集与可视化实现
本文介绍了使用Python网络爬虫技术从芒果TV抓取电影信息(包括名称、播放量、评分等),并通过Matloplit库和Wordcloud库进行数据处理与可视化分析的研究项目。
442 0
芒果tv数据采集与可视化实现
|
IDE API 开发工具
在 VSCode 中实现 Jupyter Debug Adapter
通过本文的介绍,我们可以了解 VSCode 的 Debug Adapter 的实现原理,并以 Jupyter 为例,成功在 VSCode 中实现 Jupyter Debug Adapter,并实现完全的调试能力。 得益于 VSCode 灵活的调试注入能力,我们可以方便地为更多的语言,甚至是一些自定义框架实现调试能力,这可能可以为更多的小众语言或框架的开发者带来工作效率上的帮助。
|
机器学习/深度学习 数据可视化 API
Gymnasium的基本用法
Gymnasium的基本用法
790 0
|
机器学习/深度学习 移动开发 算法
秒懂算法 | 基于图神经网络的推荐算法
图神经网络(Graph Neural Networks,GNN)是近几年兴起的学科,用来作推荐算法自然效果也相当好,但是要学会基于图神经网络的推荐算法之前,需要对图神经网络自身有个了解。
1122 0
秒懂算法 | 基于图神经网络的推荐算法
|
算法 C语言 C++
c++游戏制作指南(三):c++剧情类文字游戏的制作
c++游戏制作指南(三):c++剧情类文字游戏的制作
1766 0
|
机器学习/深度学习 存储 人工智能
解码Transformer:自注意力机制与编解码器机制详述与代码实现
解码Transformer:自注意力机制与编解码器机制详述与代码实现
421 0
|
Shell Linux
解决脚本文件无法执行conda命令的问题:CommandNotFoundError: Your shell has not been properly configured to use
使用Linux系统时,有时候希望利用一个脚本执行多条命令来节省时间,其中如果安装有anaconda,需要切换环境或者关闭conda环境,按道理说,在终端里可以通过命令
2038 0