Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
798 changes: 750 additions & 48 deletions chapter_10_attention_mechanisms/0_attention-cues.ipynb

Large diffs are not rendered by default.

3,086 changes: 1,575 additions & 1,511 deletions chapter_10_attention_mechanisms/0_nadaraya-waston.ipynb

Large diffs are not rendered by default.

549 changes: 263 additions & 286 deletions chapter_10_attention_mechanisms/1_attention-scoring-functions.ipynb

Large diffs are not rendered by default.

1,281 changes: 305 additions & 976 deletions chapter_10_attention_mechanisms/2_bahdanau-attention.ipynb

Large diffs are not rendered by default.

67 changes: 40 additions & 27 deletions chapter_10_attention_mechanisms/3_multihead-attention.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"origin_pos": 2,
"tab": [
Expand All @@ -35,7 +35,8 @@
"source": [
"from d2l import mindspore as d2l\n",
"import mindspore\n",
"from mindspore import nn"
"from mindspore import nn\n",
"import mindspore.mint as mint"
]
},
{
Expand Down Expand Up @@ -71,24 +72,28 @@
" self.W_k = nn.Dense(key_size, num_hiddens, has_bias=has_bias)\n",
" self.W_v = nn.Dense(value_size, num_hiddens, has_bias=has_bias)\n",
" self.W_o = nn.Dense(num_hiddens, num_hiddens, has_bias=has_bias)\n",
" # mint 中没有 expand_dims,保留 tile 和 reshape\n",
" self.tile = mint.tile \n",
" self.reshape = mint.reshape\n",
" # 移除 self.expand_dims = mint.expand_dims,将在 construct 中使用 Tensor 方法\n",
"\n",
" def construct(self, queries, keys, values, valid_lens):\n",
" # queries,keys,values的形状:\n",
" # (batch_size,查询或者“键-值”对的个数,num_hiddens)\n",
" # valid_lens 的形状:\n",
" # (batch_size,)或(batch_size,查询的个数)\n",
" # 经过变换后,输出的queries,keys,values 的形状:\n",
" # (batch_size*num_heads,查询或者“键-值”对的个数,\n",
" # num_hiddens/num_heads)\n",
" queries = transpose_qkv(self.W_q(queries), self.num_heads)\n",
" keys = transpose_qkv(self.W_k(keys), self.num_heads)\n",
" values = transpose_qkv(self.W_v(values), self.num_heads)\n",
"\n",
" if valid_lens is not None:\n",
" # 在轴0,将第一项(标量或者矢量)复制num_heads次,\n",
" # 然后如此复制第二项,然后诸如此类。\n",
" valid_lens = d2l.repeat(\n",
" valid_lens, repeats=self.num_heads, axis=0)\n",
" \n",
" # 1. 扩展 valid_lens 形状至 (batch_size, 1)\n",
" valid_lens = valid_lens.expand_dims(1)\n",
" \n",
" # 2. 沿着轴 1 (新增维度) 重复 num_heads 次,形状变为 (batch_size, num_heads)\n",
" valid_lens = self.tile(valid_lens, (1, self.num_heads))\n",
" \n",
" # 3. 展平为 (batch_size * num_heads,)\n",
" valid_lens = self.reshape(valid_lens, (-1,))\n",
"\n",
" # output的形状:(batch_size*num_heads,查询的个数,\n",
" # num_hiddens/num_heads)\n",
Expand Down Expand Up @@ -167,18 +172,26 @@
]
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[WARNING] ME(42683:281472939304512,MainProcess):2025-12-08-17:59:06.972.000 [mindspore/nn/layer/basic.py:174] For Dropout, this parameter `keep_prob` will be deprecated, please use `p` instead.\n",
"[WARNING] ME(42683:281472939304512,MainProcess):2025-12-08-17:59:06.984.000 [mindspore/nn/layer/basic.py:200] For Dropout, this parameter `keep_prob` will be deprecated, please use `p` instead.\n"
]
},
{
"data": {
"text/plain": [
"MultiHeadAttention<\n",
" (attention): DotProductAttention<\n",
" (dropout): Dropout<keep_prob=0.5>\n",
" >\n",
" (W_q): Dense<input_channels=100, output_channels=100>\n",
" (W_k): Dense<input_channels=100, output_channels=100>\n",
" (W_v): Dense<input_channels=100, output_channels=100>\n",
" (W_o): Dense<input_channels=100, output_channels=100>\n",
" >"
"MultiHeadAttention(\n",
" (attention): DotProductAttention(\n",
" (dropout): Dropout(keep_prob=0.5)\n",
" )\n",
" (W_q): Dense(input_channels=100, output_channels=100)\n",
" (W_k): Dense(input_channels=100, output_channels=100)\n",
" (W_v): Dense(input_channels=100, output_channels=100)\n",
" (W_o): Dense(input_channels=100, output_channels=100)\n",
")"
]
},
"execution_count": 5,
Expand All @@ -195,7 +208,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {
"origin_pos": 16,
"tab": [
Expand All @@ -217,18 +230,18 @@
"source": [
"batch_size, num_queries = 2, 4\n",
"num_kvpairs, valid_lens = 6, d2l.tensor([3, 2], mindspore.int32)\n",
"X = d2l.ones((batch_size, num_queries, num_hiddens))\n",
"Y = d2l.ones((batch_size, num_kvpairs, num_hiddens))\n",
"X = mint.ones((batch_size, num_queries, num_hiddens))\n",
"Y = mint.ones((batch_size, num_kvpairs, num_hiddens))\n",
"attention(X, Y, Y, valid_lens).shape"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3.10",
"language": "python",
"name": "python3"
"name": "py310"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -240,7 +253,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
"version": "3.10.14"
},
"rise": {
"autolaunch": true,
Expand All @@ -251,4 +264,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
Loading