diff --git a/chapter_09_recurrent-modern/0_gru.ipynb b/chapter_09_recurrent-modern/0_gru.ipynb index d62108f..7510b04 100644 --- a/chapter_09_recurrent-modern/0_gru.ipynb +++ b/chapter_09_recurrent-modern/0_gru.ipynb @@ -24,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "origin_pos": 2, "tab": [ @@ -33,13 +33,14 @@ }, "outputs": [], "source": [ - "import mindspore\n", + "import mindspore as ms\n", "import mindspore.nn as nn\n", - "import mindspore.ops as ops\n", + "import mindspore.common.initializer as initializer\n", + "from mindspore import Parameter, ParameterTuple, mint\n", "from d2l import mindspore as d2l\n", "\n", "batch_size, num_steps = 32, 35\n", - "train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)" + "train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)\n" ] }, { @@ -55,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "origin_pos": 6, "tab": [ @@ -64,27 +65,24 @@ }, "outputs": [], "source": [ - "from mindspore import Parameter, ParameterTuple\n", - "\n", "def get_params(vocab_size, num_hiddens):\n", " num_inputs = num_outputs = vocab_size\n", "\n", " def normal(shape, name):\n", - " return Parameter(d2l.tensor(d2l.randn(shape) * 0.01), name=name)\n", + " return Parameter(d2l.tensor(mint.randn(shape) * 0.01), name=name)\n", "\n", " def three(name1, name2, name3):\n", " return (normal((num_inputs, num_hiddens), name1),\n", " normal((num_hiddens, num_hiddens), name2),\n", - " Parameter(d2l.tensor(d2l.zeros(num_hiddens)), name=name3))\n", + " Parameter(d2l.tensor(mint.zeros(num_hiddens)), name=name3))\n", "\n", " W_xz, W_hz, b_z = three('W_xz', 'W_hz', 'b_z') # 更新门参数\n", " W_xr, W_hr, b_r = three('W_xr', 'W_hr', 'b_r') # 重置门参数\n", " W_xh, W_hh, b_h = three('W_xh', 'W_hh', 'b_h') # 候选隐状态参数\n", - " # 输出层参数\n", " W_hq = normal((num_hiddens, num_outputs), 'W_hq')\n", - " b_q = Parameter(d2l.tensor(d2l.zeros(num_outputs)), name='b_q')\n", + " b_q = Parameter(d2l.tensor(mint.zeros(num_outputs)), name='b_q')\n", " params = [W_xz, W_hz, b_z, W_xr, W_hr, b_r, W_xh, W_hh, b_h, W_hq, b_q]\n", - " return ParameterTuple(params)" + " return ParameterTuple(params)\n" ] }, { @@ -100,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "origin_pos": 10, "tab": [ @@ -110,7 +108,7 @@ "outputs": [], "source": [ "def init_gru_state(batch_size, num_hiddens):\n", - " return (d2l.zeros((batch_size, num_hiddens)), )" + " return (mint.zeros((batch_size, num_hiddens)), )" ] }, { @@ -126,7 +124,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "origin_pos": 14, "tab": [ @@ -140,13 +138,13 @@ " H, = state\n", " outputs = []\n", " for X in inputs:\n", - " Z = ops.sigmoid(ops.matmul(X, W_xz) + ops.matmul(H, W_hz) + b_z)\n", - " R = ops.sigmoid(ops.matmul(X, W_xr) + ops.matmul(H, W_hr) + b_r)\n", - " H_tilda = ops.tanh(ops.matmul(X, W_xh) + ops.matmul((R * H), W_hh) + b_h)\n", + " Z = mint.sigmoid(mint.matmul(X, W_xz) + mint.matmul(H, W_hz) + b_z)\n", + " R = mint.sigmoid(mint.matmul(X, W_xr) + mint.matmul(H, W_hr) + b_r)\n", + " H_tilda = mint.tanh(mint.matmul(X, W_xh) + mint.matmul((R * H), W_hh) + b_h)\n", " H = Z * H + (1 - Z) * H_tilda\n", - " Y = ops.matmul(H, W_hq) + b_q\n", + " Y = mint.matmul(H, W_hq) + b_q\n", " outputs.append(Y)\n", - " return ops.concat(outputs, axis=0), (H,)" + " return mint.cat(outputs, dim=0), (H,)" ] }, { @@ -174,9 +172,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "困惑度 1.4, 5483.6 词元/秒\n", - "time travellerit s against reason said filby who was getting bra\n", - "travellerit s against reason said filbywhat said a very you\n" + "困惑度 17.6, 10623.0 词元/秒\n", + "time traveller \n", + "traveller \n" ] }, { @@ -190,11 +188,11 @@ " \n", " \n", " \n", - " 2023-01-29T17:20:28.683651\n", + " 2025-12-06T18:22:32.176367\n", " image/svg+xml\n", " \n", " \n", - " Matplotlib v3.6.2, https://matplotlib.org/\n", + " Matplotlib v3.8.4, https://matplotlib.org/\n", " \n", " \n", " \n", @@ -226,16 +224,16 @@ " \n", " \n", + "\" clip-path=\"url(#p398c85dcd6)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -288,11 +286,11 @@ " \n", " \n", + "\" clip-path=\"url(#p398c85dcd6)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -334,11 +332,11 @@ " \n", " \n", + "\" clip-path=\"url(#p398c85dcd6)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -388,11 +386,11 @@ " \n", " \n", + "\" clip-path=\"url(#p398c85dcd6)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -429,11 +427,11 @@ " \n", " \n", + "\" clip-path=\"url(#p398c85dcd6)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -600,66 +598,178 @@ " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", - " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", @@ -767,58 +877,58 @@ " \n", " \n", " \n", - " \n", + " \n", " \n", + "\" clip-path=\"url(#p398c85dcd6)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -928,7 +1038,7 @@ " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -944,7 +1054,7 @@ ], "source": [ "vocab_size, num_hiddens = len(vocab), 256\n", - "num_epochs, lr = 500, 1\n", + "num_epochs, lr = 500, 0.01\n", "model = d2l.RNNModelScratch(len(vocab), num_hiddens, get_params,\n", " init_gru_state, gru)\n", "d2l.train_ch8(model, train_iter, vocab, lr, num_epochs)" @@ -963,7 +1073,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "metadata": { "origin_pos": 21, "tab": [ @@ -1754,9 +1864,9 @@ "metadata": { "celltoolbar": "Slideshow", "kernelspec": { - "display_name": "Python [conda env:mindspore] *", + "display_name": "Python 3.10", "language": "python", - "name": "conda-env-mindspore-py" + "name": "py310" }, "language_info": { "codemirror_mode": { @@ -1768,7 +1878,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.10.14" }, "rise": { "autolaunch": true, diff --git a/chapter_09_recurrent-modern/1_lstm.ipynb b/chapter_09_recurrent-modern/1_lstm.ipynb index fe4aa22..fefc7ae 100644 --- a/chapter_09_recurrent-modern/1_lstm.ipynb +++ b/chapter_09_recurrent-modern/1_lstm.ipynb @@ -24,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "origin_pos": 2, "tab": [ @@ -35,7 +35,7 @@ "source": [ "import mindspore\n", "import mindspore.nn as nn\n", - "import mindspore.ops as ops\n", + "import mindspore.mint as mint\n", "from d2l import mindspore as d2l\n", "\n", "batch_size, num_steps = 32, 35\n", @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "origin_pos": 6, "tab": [ @@ -70,12 +70,12 @@ " num_inputs = num_outputs = vocab_size\n", "\n", " def normal(shape, name):\n", - " return Parameter(d2l.tensor(ops.randn(shape) * 0.01), name=name)\n", + " return Parameter(d2l.tensor(mint.randn(shape) * 0.01), name=name)\n", "\n", " def three(name1, name2, name3):\n", " return (normal((num_inputs, num_hiddens), name1),\n", " normal((num_hiddens, num_hiddens), name2),\n", - " Parameter(d2l.tensor(d2l.zeros(num_hiddens)), name3))\n", + " Parameter(d2l.tensor(mint.zeros(num_hiddens)), name3))\n", "\n", " W_xi, W_hi, b_i = three('W_xi', 'W_hi', 'b_i') # 输入门参数\n", " W_xf, W_hf, b_f = three('W_xf', 'W_hf', 'b_f') # 遗忘门参数\n", @@ -83,7 +83,7 @@ " W_xc, W_hc, b_c = three('W_xc', 'W_hc', 'b_c') # 候选记忆元参数\n", " # 输出层参数\n", " W_hq = normal((num_hiddens, num_outputs), name='W_hq')\n", - " b_q = Parameter(d2l.tensor(d2l.zeros(num_outputs)), name='b_q')\n", + " b_q = Parameter(d2l.tensor(mint.zeros(num_outputs)), name='b_q')\n", " params = [W_xi, W_hi, b_i, W_xf, W_hf, b_f, W_xo, W_ho, b_o, W_xc, W_hc,\n", " b_c, W_hq, b_q]\n", " return ParameterTuple(params)" @@ -112,8 +112,8 @@ "outputs": [], "source": [ "def init_lstm_state(batch_size, num_hiddens):\n", - " return (ops.zeros((batch_size, num_hiddens)),\n", - " ops.zeros((batch_size, num_hiddens)))" + " return (mint.zeros((batch_size, num_hiddens)),\n", + " mint.zeros((batch_size, num_hiddens)))" ] }, { @@ -129,7 +129,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "metadata": { "origin_pos": 14, "tab": [ @@ -143,15 +143,15 @@ " (H, C) = state\n", " outputs = []\n", " for X in inputs:\n", - " I = ops.sigmoid(ops.matmul(X, W_xi) + ops.matmul(H, W_hi) + b_i)\n", - " F = ops.sigmoid(ops.matmul(X, W_xf) + ops.matmul(H, W_hf) + b_f)\n", - " O = ops.sigmoid(ops.matmul(X, W_xo) + ops.matmul(H, W_ho) + b_o)\n", - " C_tilda = ops.tanh(ops.matmul(X, W_xc) + ops.matmul(H, W_hc) + b_c)\n", + " I = mint.sigmoid(mint.matmul(X, W_xi) + mint.matmul(H, W_hi) + b_i)\n", + " F = mint.sigmoid(mint.matmul(X, W_xf) + mint.matmul(H, W_hf) + b_f)\n", + " O = mint.sigmoid(mint.matmul(X, W_xo) + mint.matmul(H, W_ho) + b_o)\n", + " C_tilda = mint.tanh(mint.matmul(X, W_xc) + mint.matmul(H, W_hc) + b_c)\n", " C = F * C + I * C_tilda\n", - " H = O * ops.tanh(C)\n", - " Y = ops.matmul(H, W_hq) + b_q\n", + " H = O * mint.tanh(C)\n", + " Y = mint.matmul(H, W_hq) + b_q\n", " outputs.append(Y)\n", - " return ops.concat(outputs, axis=0), (H, C)" + " return mint.cat(outputs, dim=0), (H, C)" ] }, { @@ -167,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": { "origin_pos": 17, "tab": [ @@ -179,9 +179,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "困惑度 1.7, 5950.3 词元/秒\n", - "time travellerit s against reason said filby in all in time as m\n", - "travellerit s against reason said filby in all in time as m\n" + "困惑度 1.6, 6185.3 词元/秒\n", + "time travellerit s against reason said filbycan a cube that does\n", + "travellerit would be remarkably convenient for the have tou\n" ] }, { @@ -195,11 +195,11 @@ " \n", " \n", " \n", - " 2023-01-29T23:30:23.836164\n", + " 2025-12-06T21:18:32.509022\n", " image/svg+xml\n", " \n", " \n", - " Matplotlib v3.6.2, https://matplotlib.org/\n", + " Matplotlib v3.10.7, https://matplotlib.org/\n", " \n", " \n", " \n", @@ -231,16 +231,16 @@ " \n", " \n", + "\" clip-path=\"url(#pb667d2cabe)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -284,8 +284,8 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", " \n", " \n", " \n", @@ -293,11 +293,11 @@ " \n", " \n", + "\" clip-path=\"url(#pb667d2cabe)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -330,8 +330,8 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", " \n", " \n", " \n", @@ -339,11 +339,11 @@ " \n", " \n", + "\" clip-path=\"url(#pb667d2cabe)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -384,8 +384,8 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", " \n", " \n", " \n", @@ -393,11 +393,11 @@ " \n", " \n", + "\" clip-path=\"url(#pb667d2cabe)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -425,8 +425,8 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", " \n", " \n", " \n", @@ -434,11 +434,11 @@ " \n", " \n", + "\" clip-path=\"url(#pb667d2cabe)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -472,8 +472,8 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", " \n", " \n", " \n", @@ -595,72 +595,72 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -760,70 +760,70 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", " \n", + "\" clip-path=\"url(#pb667d2cabe)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n", " \n", " \n", " \n", " \n", " \n", - " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -968,7 +968,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 9, "metadata": { "origin_pos": 21, "tab": [ @@ -980,9 +980,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "困惑度 1.6, 7590.1 词元/秒\n", - "time traveller held in his hand was a gainled bat a cubu have ar\n", - "traveller held in his hand was a gainled bat a citherast a \n" + "困惑度 1.4, 38421.9 词元/秒\n", + "time travellerit s against reason said filbywhace cerifed and ho\n", + "travellerit s against reason said filbywhace cerifed and ho\n" ] }, { @@ -996,11 +996,11 @@ " \n", " \n", " \n", - " 2023-01-29T23:01:39.338650\n", + " 2025-12-06T21:24:03.249849\n", " image/svg+xml\n", " \n", " \n", - " Matplotlib v3.6.2, https://matplotlib.org/\n", + " Matplotlib v3.10.7, https://matplotlib.org/\n", " \n", " \n", " \n", @@ -1032,16 +1032,16 @@ " \n", " \n", + "\" clip-path=\"url(#p870a14e313)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -1085,8 +1085,8 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", " \n", " \n", " \n", @@ -1094,11 +1094,11 @@ " \n", " \n", + "\" clip-path=\"url(#p870a14e313)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -1131,8 +1131,8 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", " \n", " \n", " \n", @@ -1140,11 +1140,11 @@ " \n", " \n", + "\" clip-path=\"url(#p870a14e313)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -1185,8 +1185,8 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", " \n", " \n", " \n", @@ -1194,11 +1194,11 @@ " \n", " \n", + "\" clip-path=\"url(#p870a14e313)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -1226,8 +1226,8 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", " \n", " \n", " \n", @@ -1235,11 +1235,11 @@ " \n", " \n", + "\" clip-path=\"url(#p870a14e313)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -1273,8 +1273,8 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", " \n", " \n", " \n", @@ -1396,72 +1396,72 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -1561,70 +1561,70 @@ "\" transform=\"scale(0.015625)\"/>\n", " \n", " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", " \n", + "L 44.588839 22.637756 \n", + "L 48.574554 37.043728 \n", + "L 52.560268 50.233447 \n", + "L 56.545982 62.016862 \n", + "L 60.531696 65.978344 \n", + "L 64.517411 69.909927 \n", + "L 68.503125 73.837072 \n", + "L 72.488839 76.954656 \n", + "L 76.474554 80.710568 \n", + "L 80.460268 83.325418 \n", + "L 84.445982 86.843362 \n", + "L 88.431696 89.011797 \n", + "L 92.417411 91.914353 \n", + "L 96.403125 94.932835 \n", + "L 100.388839 97.210619 \n", + "L 104.374554 99.335516 \n", + "L 108.360268 102.841225 \n", + "L 112.345982 105.760726 \n", + "L 116.331696 108.065353 \n", + "L 120.317411 111.520812 \n", + "L 124.303125 113.581956 \n", + "L 128.288839 116.179091 \n", + "L 132.274554 118.360696 \n", + "L 136.260268 121.511455 \n", + "L 140.245982 123.337241 \n", + "L 144.231696 126.56177 \n", + "L 148.217411 127.917224 \n", + "L 152.203125 128.000001 \n", + "L 156.188839 131.385423 \n", + "L 160.174554 133.69462 \n", + "L 164.160268 134.043008 \n", + "L 168.145982 135.415736 \n", + "L 172.131696 135.666659 \n", + "L 176.117411 136.105119 \n", + "L 180.103125 137.604288 \n", + "L 184.088839 136.77028 \n", + "L 188.074554 138.204986 \n", + "L 192.060268 138.471836 \n", + "L 196.045982 138.507171 \n", + "L 200.031696 138.516658 \n", + "L 204.017411 137.506929 \n", + "L 208.003125 139.322821 \n", + "L 211.988839 138.608208 \n", + "L 215.974554 138.634067 \n", + "L 219.960268 138.807076 \n", + "L 223.945982 139.5 \n", + "L 227.931696 137.521365 \n", + "L 231.917411 139.243535 \n", + "L 235.903125 139.292896 \n", + "\" clip-path=\"url(#p870a14e313)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n", " \n", " \n", " \n", " \n", " \n", - " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", @@ -1754,21 +1754,14 @@ "model = d2l.RNNModel(lstm_layer, len(vocab))\n", "d2l.train_ch8(model, train_iter, vocab, lr, num_epochs)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { - "display_name": "Python [conda env:mindspore] *", + "display_name": "Python 3.10", "language": "python", - "name": "conda-env-mindspore-py" + "name": "py310" }, "language_info": { "codemirror_mode": { @@ -1780,7 +1773,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.10.14" }, "rise": { "autolaunch": true, diff --git a/chapter_09_recurrent-modern/6_seq2seq.ipynb b/chapter_09_recurrent-modern/6_seq2seq.ipynb index ba2114c..f5020dc 100644 --- a/chapter_09_recurrent-modern/6_seq2seq.ipynb +++ b/chapter_09_recurrent-modern/6_seq2seq.ipynb @@ -24,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "origin_pos": 2, "tab": [ @@ -38,6 +38,7 @@ "import mindspore\n", "import mindspore.nn as nn\n", "import mindspore.ops as ops\n", + "import mindspore.mint as mint\n", "from d2l import mindspore as d2l" ] }, @@ -95,7 +96,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "origin_pos": 10, "tab": [ @@ -103,6 +104,13 @@ ] }, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "." + ] + }, { "data": { "text/plain": [ @@ -118,7 +126,7 @@ "encoder = Seq2SeqEncoder(vocab_size=10, embed_size=8, num_hiddens=16,\n", " num_layers=2)\n", "encoder.set_train(False)\n", - "X = d2l.zeros((4, 7), dtype=mindspore.int32)\n", + "X = mint.zeros((4, 7), dtype=mindspore.int32)\n", "output, state = encoder(X)\n", "output.shape" ] @@ -142,6 +150,13 @@ "execution_count": 5, "metadata": {}, "output_type": "execute_result" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "." + ] } ], "source": [ @@ -161,7 +176,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "origin_pos": 18, "tab": [ @@ -188,7 +203,7 @@ " X = self.embedding(X).permute(1, 0, 2)\n", " # 广播context,使其具有与X相同的num_steps\n", " context = ops.repeat_elements(state[-1:], rep=X.shape[0], axis=0)\n", - " X_and_context = d2l.concat((X, context), 2)\n", + " X_and_context = mint.cat((X, context), 2)\n", " output, state = self.rnn(X_and_context, state)\n", " output = self.dense(output).permute(1, 0, 2)\n", " # output的形状:(batch_size,num_steps,vocab_size)\n", @@ -226,6 +241,13 @@ "execution_count": 7, "metadata": {}, "output_type": "execute_result" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "." + ] } ], "source": [ @@ -275,7 +297,7 @@ "def sequence_mask(X, valid_len, value=0):\n", " \"\"\"在序列中屏蔽不相关的项\"\"\"\n", " maxlen = X.shape[1]\n", - " mask = ops.arange((maxlen), dtype=mindspore.float32)[None, :] < valid_len[:, None]\n", + " mask = mint.arange((maxlen), dtype=mindspore.float32)[None, :] < valid_len[:, None]\n", " X[~mask] = value\n", " return X\n", "\n", @@ -296,7 +318,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "origin_pos": 30, "tab": [ @@ -304,13 +326,6 @@ ] }, "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:19:51.661.294 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 43\n" - ] - }, { "data": { "text/plain": [ @@ -329,7 +344,7 @@ } ], "source": [ - "X = d2l.ones((2, 3, 4))\n", + "X = mint.ones((2, 3, 4))\n", "sequence_mask(X, mindspore.Tensor([1, 2]), value=-1)" ] }, @@ -365,7 +380,7 @@ " # label的形状:(batch_size,num_steps)\n", " # valid_len的形状:(batch_size,)\n", " def construct(self, pred, label, valid_len):\n", - " weights = ops.ones_like(label)\n", + " weights = mint.ones_like(label)\n", " weights = sequence_mask(weights, valid_len)\n", " unweighted_loss = self.softmax_ce_loss(pred.permute(0, 2, 1), label)\n", " weighted_loss = (unweighted_loss * weights).mean(axis=1)\n", @@ -394,16 +409,16 @@ }, "outputs": [ { - "name": "stderr", + "name": "stdout", "output_type": "stream", "text": [ - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:19:51.698.753 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n" + "." ] }, { "data": { "text/plain": [ - "Tensor(shape=[3], dtype=Float32, value= [ 2.30258799e+00, 1.15129399e+00, 0.00000000e+00])" + "Tensor(shape=[3], dtype=Float32, value= [ 2.30258512e+00, 1.15129256e+00, 0.00000000e+00])" ] }, "execution_count": 11, @@ -413,7 +428,7 @@ ], "source": [ "loss = MaskedSoftmaxCELoss()\n", - "loss(ops.ones((3, 4, 10)), ops.ones((3, 4), dtype=mindspore.int32),\n", + "loss(mint.ones((3, 4, 10)), mint.ones((3, 4), dtype=mindspore.int32),\n", " mindspore.Tensor([4, 2, 0]))" ] }, @@ -460,7 +475,7 @@ " X, X_valid_len, Y, Y_valid_len = [x.astype(d2l.int32) for x in batch]\n", " # print(X.shape, X_valid_len, Y.shape, Y_valid_len)\n", " bos = mindspore.Tensor([tgt_vocab['']] * Y.shape[0], dtype=mindspore.int32).reshape(-1, 1)\n", - " dec_input = ops.concat([bos, Y[:, :-1]], 1) # 强制教学\n", + " dec_input = mint.cat([bos, Y[:, :-1]], 1) # 强制教学\n", " l, grads = grad_fn(X, dec_input, X_valid_len, Y, Y_valid_len)\n", " optimizer(grads)\n", " num_tokens = Y_valid_len.sum()\n", @@ -496,77 +511,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "正在从http://d2l-data.s3-accelerate.amazonaws.com/fra-eng.zip下载../data/fra-eng.zip...\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:08.295.649 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:09.069.676 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:09.257.903 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:09.440.888 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:09.647.505 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:09.828.180 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:10.030.248 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:10.218.455 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:10.422.382 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:10.736.633 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:11.444.787 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:12.099.160 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:12.770.564 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:13.479.923 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:14.137.617 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:14.787.212 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:15.406.803 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:16.107.824 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:16.800.813 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:17.448.030 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:18.108.732 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:18.759.467 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:19.549.408 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:20.214.724 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:20.910.877 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:21.528.300 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:22.209.505 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:22.866.264 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:23.497.657 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:24.126.400 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:24.803.424 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:25.461.996 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:26.128.464 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:26.788.311 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:27.459.281 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:28.111.665 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:28.774.769 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:29.435.291 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:30.072.972 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:30.720.423 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:31.363.935 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:32.011.091 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:32.672.154 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:33.332.977 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:34.036.773 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:34.704.538 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[WARNING] CORE(25029,7f0882714080,python):2023-02-19-10:25:35.350.052 [mindspore/core/ops/fill.cc:202] FillInferValue] value type is not same as given dtype, value type id is 35 and given dtype id is 34\n", - "[ERROR] KERNEL(25029,7f0882714080,python):2023-02-19-10:25:35.729.324 [mindspore/ccsrc/plugin/device/cpu/kernel/unsorted_segment_arithmetic_cpu_kernel.cc:112] LaunchKernel] For 'UnsortedSegmentSum', segment_ids value should be [0, 184)\n" - ] - }, - { - "ename": "RuntimeError", - "evalue": "Launch kernel failed, name:Default/UnsortedSegmentSum-op2003\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/runtime/pynative/run_op_helper.cc:569 LaunchKernelsDynamic\n", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[13], line 12\u001b[0m\n\u001b[1;32m 8\u001b[0m decoder \u001b[38;5;241m=\u001b[39m Seq2SeqDecoder(\u001b[38;5;28mlen\u001b[39m(tgt_vocab), embed_size, num_hiddens, num_layers,\n\u001b[1;32m 9\u001b[0m dropout)\n\u001b[1;32m 10\u001b[0m net \u001b[38;5;241m=\u001b[39m d2l\u001b[38;5;241m.\u001b[39mEncoderDecoder(encoder, decoder)\n\u001b[0;32m---> 12\u001b[0m \u001b[43mtrain_seq2seq\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnet\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrain_iter\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnum_epochs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtgt_vocab\u001b[49m\u001b[43m)\u001b[49m\n", - "Cell \u001b[0;32mIn[12], line 23\u001b[0m, in \u001b[0;36mtrain_seq2seq\u001b[0;34m(net, data_iter, lr, num_epochs, tgt_vocab)\u001b[0m\n\u001b[1;32m 21\u001b[0m bos \u001b[38;5;241m=\u001b[39m mindspore\u001b[38;5;241m.\u001b[39mTensor([tgt_vocab[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\u001b[39m\u001b[38;5;124m'\u001b[39m]] \u001b[38;5;241m*\u001b[39m Y\u001b[38;5;241m.\u001b[39mshape[\u001b[38;5;241m0\u001b[39m], dtype\u001b[38;5;241m=\u001b[39mmindspore\u001b[38;5;241m.\u001b[39mint32)\u001b[38;5;241m.\u001b[39mreshape(\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m 22\u001b[0m dec_input \u001b[38;5;241m=\u001b[39m ops\u001b[38;5;241m.\u001b[39mconcat([bos, Y[:, :\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m]], \u001b[38;5;241m1\u001b[39m) \u001b[38;5;66;03m# 强制教学\u001b[39;00m\n\u001b[0;32m---> 23\u001b[0m l, grads \u001b[38;5;241m=\u001b[39m \u001b[43mgrad_fn\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdec_input\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mX_valid_len\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mY\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mY_valid_len\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 24\u001b[0m optimizer(grads)\n\u001b[1;32m 25\u001b[0m num_tokens \u001b[38;5;241m=\u001b[39m Y_valid_len\u001b[38;5;241m.\u001b[39msum()\n", - "File \u001b[0;32m/data/miniconda3/envs/ms_d2l/lib/python3.8/site-packages/mindspore/ops/composite/base.py:605\u001b[0m, in \u001b[0;36m_Grad.__call__..after_grad\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 604\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mafter_grad\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m--> 605\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mgrad_\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn_\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mweights\u001b[49m\u001b[43m)\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m/data/miniconda3/envs/ms_d2l/lib/python3.8/site-packages/mindspore/common/api.py:101\u001b[0m, in \u001b[0;36m_wrap_func..wrapper\u001b[0;34m(*arg, **kwargs)\u001b[0m\n\u001b[1;32m 99\u001b[0m \u001b[38;5;129m@wraps\u001b[39m(fn)\n\u001b[1;32m 100\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mwrapper\u001b[39m(\u001b[38;5;241m*\u001b[39marg, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m--> 101\u001b[0m results \u001b[38;5;241m=\u001b[39m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43marg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 102\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_python_data(results)\n", - "File \u001b[0;32m/data/miniconda3/envs/ms_d2l/lib/python3.8/site-packages/mindspore/ops/composite/base.py:584\u001b[0m, in \u001b[0;36m_Grad.__call__..after_grad\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 582\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_pynative_forward_run(fn, grad_, args, kwargs)\n\u001b[1;32m 583\u001b[0m _pynative_executor\u001b[38;5;241m.\u001b[39mgrad(fn, grad_, weights, grad_position, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m--> 584\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[43m_pynative_executor\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 585\u001b[0m out \u001b[38;5;241m=\u001b[39m _grads_divided_by_device_num_if_recomputation(out)\n\u001b[1;32m 586\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mreturn_ids \u001b[38;5;129;01mand\u001b[39;00m out:\n", - "File \u001b[0;32m/data/miniconda3/envs/ms_d2l/lib/python3.8/site-packages/mindspore/common/api.py:986\u001b[0m, in \u001b[0;36m_PyNativeExecutor.__call__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 979\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m 980\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 981\u001b[0m \u001b[38;5;124;03m PyNative executor run grad graph.\u001b[39;00m\n\u001b[1;32m 982\u001b[0m \n\u001b[1;32m 983\u001b[0m \u001b[38;5;124;03m Return:\u001b[39;00m\n\u001b[1;32m 984\u001b[0m \u001b[38;5;124;03m The return object after running grad graph.\u001b[39;00m\n\u001b[1;32m 985\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 986\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_executor\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", - "\u001b[0;31mRuntimeError\u001b[0m: Launch kernel failed, name:Default/UnsortedSegmentSum-op2003\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/runtime/pynative/run_op_helper.cc:569 LaunchKernelsDynamic\n" + "loss 0.010, 5278.9 tokens/sec\n" ] }, { @@ -575,16 +520,16 @@ "\n", "\n", - "\n", + "\n", " \n", " \n", " \n", " \n", - " 2023-02-19T10:25:36.794701\n", + " 2025-12-06T21:43:58.830663\n", " image/svg+xml\n", " \n", " \n", - " Matplotlib v3.6.2, https://matplotlib.org/\n", + " Matplotlib v3.10.7, https://matplotlib.org/\n", " \n", " \n", " \n", @@ -595,38 +540,68 @@ " \n", " \n", " \n", - " \n", " \n", " \n", " \n", - " \n", " \n", " \n", " \n", " \n", + " \n", + " \n", + " \n", " \n", - " \n", " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", " \n", + " \n", " \n", - " \n", " \n", - " \n", - " \n", - " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", - " \n", + " \n", + " \n", + " \n", + " \n", " \n", - " \n", + " \n", " \n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " \n", - " \n", + " \n", " \n", " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " \n", - " \n", + " \n", " \n", " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " \n", - " \n", + " \n", " \n", " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", - " \n", - " \n", - " \n", + " \n", " \n", - " \n", + " \n", " \n", " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", - " \n", - " \n", - " \n", + " \n", " \n", - " \n", + " \n", " \n", " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", - " \n", - " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", " \n", " \n", " \n", + " \n", + " \n", + " \n", " \n", - " \n", " \n", " \n", - " \n", " \n", " \n", - " \n", " \n", " \n", - " \n", " \n", " \n", " \n", + " \n", + " \n", + " \n", + " \n", + " \n", "\n" ], "text/plain": [ @@ -1031,7 +1313,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": { "origin_pos": 48, "tab": [ @@ -1049,11 +1331,11 @@ " enc_valid_len = mindspore.Tensor([len(src_tokens)])\n", " src_tokens = d2l.truncate_pad(src_tokens, num_steps, src_vocab[''])\n", " # 添加批量轴\n", - " enc_X = ops.unsqueeze(mindspore.Tensor(src_tokens, mindspore.int32), 0)\n", + " enc_X = mint.unsqueeze(mindspore.Tensor(src_tokens, mindspore.int32), 0)\n", " enc_outputs = net.encoder(enc_X, enc_valid_len)\n", " dec_state = net.decoder.init_state(enc_outputs, enc_valid_len)\n", " # 添加批量轴\n", - " dec_X = ops.unsqueeze(mindspore.Tensor([tgt_vocab['']], mindspore.int32), 0)\n", + " dec_X = mint.unsqueeze(mindspore.Tensor([tgt_vocab['']], mindspore.int32), 0)\n", " output_seq, attention_weight_seq = [], []\n", " for _ in range(num_steps):\n", " Y, dec_state = net.decoder(dec_X, dec_state)\n", @@ -1083,7 +1365,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": { "origin_pos": 51, "tab": [ @@ -1122,14 +1404,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": { "origin_pos": 53, "tab": [ "pytorch" ] }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "..go . => va !, bleu 1.000\n", + "i lost . => j'ai perdu ., bleu 1.000\n", + "he's calm . => elle est ., bleu 0.000\n", + "i'm home . => je suis chez moi ., bleu 1.000\n" + ] + } + ], "source": [ "engs = ['go .', \"i lost .\", 'he\\'s calm .', 'i\\'m home .']\n", "fras = ['va !', 'j\\'ai perdu .', 'il est calme .', 'je suis chez moi .']\n", @@ -1138,21 +1431,14 @@ " net, eng, src_vocab, tgt_vocab, num_steps)\n", " print(f'{eng} => {translation}, bleu {bleu(translation, fra, k=2):.3f}')" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { - "display_name": "ms_d2l", + "display_name": "Python 3.10", "language": "python", - "name": "ms_d2l" + "name": "py310" }, "language_info": { "codemirror_mode": { @@ -1164,7 +1450,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.16" + "version": "3.10.14" }, "rise": { "autolaunch": true,