diff --git a/chapter_10_attention_mechanisms/0_attention-cues.ipynb b/chapter_10_attention_mechanisms/0_attention-cues.ipynb
index 0e6adeb..b6f379b 100644
--- a/chapter_10_attention_mechanisms/0_attention-cues.ipynb
+++ b/chapter_10_attention_mechanisms/0_attention-cues.ipynb
@@ -2,32 +2,32 @@
"cells": [
{
"cell_type": "markdown",
+ "metadata": {},
"source": [
"# Attention Cue"
- ],
- "metadata": {
- "collapsed": false
- }
+ ]
},
{
"cell_type": "code",
- "execution_count": null,
- "outputs": [],
- "source": [
- "import sys\n",
- "sys.path.append('..')"
- ],
+ "execution_count": 1,
"metadata": {
"collapsed": false,
+ "jupyter": {
+ "outputs_hidden": false
+ },
"pycharm": {
"name": "#%%\n"
}
- }
+ },
+ "outputs": [],
+ "source": [
+ "import sys\n",
+ "sys.path.append('..')"
+ ]
},
{
"cell_type": "markdown",
"metadata": {
- "collapsed": true,
"pycharm": {
"name": "#%% md\n"
}
@@ -38,30 +38,40 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "outputs": [],
- "source": [
- "from d2l import mindspore as d2l"
- ],
+ "execution_count": 3,
"metadata": {
"collapsed": false,
+ "jupyter": {
+ "outputs_hidden": false
+ },
"pycharm": {
"name": "#%%\n"
}
- }
+ },
+ "outputs": [],
+ "source": [
+ "from d2l import mindspore as d2l"
+ ]
},
{
"cell_type": "markdown",
+ "metadata": {},
"source": [
"为了可视化注意力权重,需要定义一个`show_heatmaps`函数。"
- ],
- "metadata": {
- "collapsed": false
- }
+ ]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false,
+ "jupyter": {
+ "outputs_hidden": false
+ },
+ "pycharm": {
+ "name": "#%%\n"
+ }
+ },
"outputs": [],
"source": [
"#@save\n",
@@ -82,58 +92,750 @@
" if titles:\n",
" ax.set_title(titles[j])\n",
" fig.colorbar(pcm, ax=axes, shrink=0.6);"
- ],
- "metadata": {
- "collapsed": false,
- "pycharm": {
- "name": "#%%\n"
- }
- }
+ ]
},
{
"cell_type": "markdown",
+ "metadata": {},
"source": [
"下面使用一个简单的例子进行演示"
- ],
- "metadata": {
- "collapsed": false
- }
+ ]
},
{
"cell_type": "code",
- "execution_count": null,
- "outputs": [],
- "source": [
- "attention_weights = d2l.eye(10).reshape((1, 1, 10, 10))\n",
- "show_heatmaps(attention_weights, xlabel='Keys', ylabel='Queries')"
- ],
+ "execution_count": 5,
"metadata": {
"collapsed": false,
+ "jupyter": {
+ "outputs_hidden": false
+ },
"pycharm": {
"name": "#%%\n"
}
- }
+ },
+ "outputs": [
+ {
+ "data": {
+ "image/svg+xml": [
+ "\n",
+ "\n",
+ "\n"
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "attention_weights = d2l.eye(10).reshape((1, 1, 10, 10))\n",
+ "show_heatmaps(attention_weights, xlabel='Keys', ylabel='Queries')"
+ ]
}
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3",
+ "display_name": "Python 3.10",
"language": "python",
- "name": "python3"
+ "name": "py310"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
- "version": 2
+ "version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.6"
+ "pygments_lexer": "ipython3",
+ "version": "3.10.14"
}
},
"nbformat": 4,
- "nbformat_minor": 0
-}
\ No newline at end of file
+ "nbformat_minor": 4
+}
diff --git a/chapter_10_attention_mechanisms/0_nadaraya-waston.ipynb b/chapter_10_attention_mechanisms/0_nadaraya-waston.ipynb
index 471343a..bb0c52d 100644
--- a/chapter_10_attention_mechanisms/0_nadaraya-waston.ipynb
+++ b/chapter_10_attention_mechanisms/0_nadaraya-waston.ipynb
@@ -24,7 +24,7 @@
},
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": null,
"metadata": {
"origin_pos": 2,
"tab": [
@@ -35,7 +35,8 @@
"source": [
"from d2l import mindspore as d2l\n",
"import mindspore\n",
- "from mindspore import nn, Parameter, value_and_grad"
+ "from mindspore import nn, Parameter, value_and_grad\n",
+ "from mindspore import mint"
]
},
{
@@ -72,12 +73,12 @@
],
"source": [
"n_train = 50 # 训练样本数\n",
- "x_train, _ = d2l.sort(d2l.rand(n_train) * 5) # 排序后的训练样本\n",
+ "x_train, _ = mint.sort(mint.rand(n_train) * 5) # 排序后的训练样本\n",
"\n",
"def f(x):\n",
- " return 2 * d2l.sin(x) + x**0.8\n",
+ " return 2 * mint.sin(x) + x**0.8\n",
"\n",
- "y_train = f(x_train) + d2l.normal((n_train,), 0.0, 0.5) # 训练样本的输出\n",
+ "y_train = f(x_train) + mint.normal(0.0, 0.5, (n_train,))\n",
"x_test = d2l.arange(0, 5, 0.1) # 测试样本\n",
"y_truth = f(x_test) # 测试样本的真实输出\n",
"n_test = len(x_test) # 测试样本数\n",
@@ -100,64 +101,64 @@
"\n",
"\n",
- "