{ "cells": [ { "cell_type": "markdown", "id": "52df7352", "metadata": { "origin_pos": 0 }, "source": [ "# 循环神经网络的从零开始实现\n", ":label:`sec_rnn_scratch`\n", "\n", "本节将根据 :numref:`sec_rnn`中的描述,\n", "从头开始基于循环神经网络实现字符级语言模型。\n", "这样的模型将在H.G.Wells的时光机器数据集上训练。\n", "和前面 :numref:`sec_language_model`中介绍过的一样,\n", "我们先读取数据集。\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "dafdcbcb", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:01.555032Z", "iopub.status.busy": "2023-08-18T07:18:01.554199Z", "iopub.status.idle": "2023-08-18T07:18:04.803287Z", "shell.execute_reply": "2023-08-18T07:18:04.802073Z" }, "origin_pos": 2, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "%matplotlib inline\n", "import math\n", "import torch\n", "from torch import nn\n", "from torch.nn import functional as F\n", "from d2l import torch as d2l" ] }, { "cell_type": "code", "execution_count": 2, "id": "be4f5d93", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:04.809116Z", "iopub.status.busy": "2023-08-18T07:18:04.808214Z", "iopub.status.idle": "2023-08-18T07:18:05.026750Z", "shell.execute_reply": "2023-08-18T07:18:05.025592Z" }, "origin_pos": 5, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "batch_size, num_steps = 32, 35\n", "train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)" ] }, { "cell_type": "markdown", "id": "80db8a1f", "metadata": { "origin_pos": 7 }, "source": [ "## [**独热编码**]\n", "\n", "回想一下,在`train_iter`中,每个词元都表示为一个数字索引,\n", "将这些索引直接输入神经网络可能会使学习变得困难。\n", "我们通常将每个词元表示为更具表现力的特征向量。\n", "最简单的表示称为*独热编码*(one-hot encoding),\n", "它在 :numref:`subsec_classification-problem`中介绍过。\n", "\n", "简言之,将每个索引映射为相互不同的单位向量:\n", "假设词表中不同词元的数目为$N$(即`len(vocab)`),\n", "词元索引的范围为$0$到$N-1$。\n", "如果词元的索引是整数$i$,\n", "那么我们将创建一个长度为$N$的全$0$向量,\n", "并将第$i$处的元素设置为$1$。\n", "此向量是原始词元的一个独热向量。\n", "索引为$0$和$2$的独热向量如下所示:\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "c5725a77", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:05.032457Z", "iopub.status.busy": "2023-08-18T07:18:05.031682Z", "iopub.status.idle": "2023-08-18T07:18:05.042971Z", "shell.execute_reply": "2023-08-18T07:18:05.041878Z" }, "origin_pos": 9, "tab": [ "pytorch" ] }, "outputs": [ { "data": { "text/plain": [ "tensor([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0],\n", " [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n", " 0, 0, 0, 0]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "F.one_hot(torch.tensor([0, 2]), len(vocab))" ] }, { "cell_type": "markdown", "id": "b5d08204", "metadata": { "origin_pos": 12 }, "source": [ "我们每次采样的(**小批量数据形状是二维张量:\n", "(批量大小,时间步数)。**)\n", "`one_hot`函数将这样一个小批量数据转换成三维张量,\n", "张量的最后一个维度等于词表大小(`len(vocab)`)。\n", "我们经常转换输入的维度,以便获得形状为\n", "(时间步数,批量大小,词表大小)的输出。\n", "这将使我们能够更方便地通过最外层的维度,\n", "一步一步地更新小批量数据的隐状态。\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "60a49de8", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:05.047886Z", "iopub.status.busy": "2023-08-18T07:18:05.047143Z", "iopub.status.idle": "2023-08-18T07:18:05.054936Z", "shell.execute_reply": "2023-08-18T07:18:05.053897Z" }, "origin_pos": 14, "tab": [ "pytorch" ] }, "outputs": [ { "data": { "text/plain": [ "torch.Size([5, 2, 28])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = torch.arange(10).reshape((2, 5))\n", "F.one_hot(X.T, 28).shape" ] }, { "cell_type": "markdown", "id": "32469879", "metadata": { "origin_pos": 17 }, "source": [ "## 初始化模型参数\n", "\n", "接下来,我们[**初始化循环神经网络模型的模型参数**]。\n", "隐藏单元数`num_hiddens`是一个可调的超参数。\n", "当训练语言模型时,输入和输出来自相同的词表。\n", "因此,它们具有相同的维度,即词表的大小。\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "a8ad7abe", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:05.059740Z", "iopub.status.busy": "2023-08-18T07:18:05.059023Z", "iopub.status.idle": "2023-08-18T07:18:05.067363Z", "shell.execute_reply": "2023-08-18T07:18:05.066318Z" }, "origin_pos": 19, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "def get_params(vocab_size, num_hiddens, device):\n", " num_inputs = num_outputs = vocab_size\n", "\n", " def normal(shape):\n", " return torch.randn(size=shape, device=device) * 0.01\n", "\n", " # 隐藏层参数\n", " W_xh = normal((num_inputs, num_hiddens))\n", " W_hh = normal((num_hiddens, num_hiddens))\n", " b_h = torch.zeros(num_hiddens, device=device)\n", " # 输出层参数\n", " W_hq = normal((num_hiddens, num_outputs))\n", " b_q = torch.zeros(num_outputs, device=device)\n", " # 附加梯度\n", " params = [W_xh, W_hh, b_h, W_hq, b_q]\n", " for param in params:\n", " param.requires_grad_(True)\n", " return params" ] }, { "cell_type": "markdown", "id": "037e51a5", "metadata": { "origin_pos": 22 }, "source": [ "## 循环神经网络模型\n", "\n", "为了定义循环神经网络模型,\n", "我们首先需要[**一个`init_rnn_state`函数在初始化时返回隐状态**]。\n", "这个函数的返回是一个张量,张量全用0填充,\n", "形状为(批量大小,隐藏单元数)。\n", "在后面的章节中我们将会遇到隐状态包含多个变量的情况,\n", "而使用元组可以更容易地处理些。\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "e310bbed", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:05.072206Z", "iopub.status.busy": "2023-08-18T07:18:05.071312Z", "iopub.status.idle": "2023-08-18T07:18:05.076740Z", "shell.execute_reply": "2023-08-18T07:18:05.075653Z" }, "origin_pos": 24, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "def init_rnn_state(batch_size, num_hiddens, device):\n", " return (torch.zeros((batch_size, num_hiddens), device=device), )" ] }, { "cell_type": "markdown", "id": "d5c7e392", "metadata": { "origin_pos": 27 }, "source": [ "[**下面的`rnn`函数定义了如何在一个时间步内计算隐状态和输出。**]\n", "循环神经网络模型通过`inputs`最外层的维度实现循环,\n", "以便逐时间步更新小批量数据的隐状态`H`。\n", "此外,这里使用$\\tanh$函数作为激活函数。\n", "如 :numref:`sec_mlp`所述,\n", "当元素在实数上满足均匀分布时,$\\tanh$函数的平均值为0。\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "84a46eb3", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:05.081883Z", "iopub.status.busy": "2023-08-18T07:18:05.080930Z", "iopub.status.idle": "2023-08-18T07:18:05.088343Z", "shell.execute_reply": "2023-08-18T07:18:05.087321Z" }, "origin_pos": 29, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "def rnn(inputs, state, params):\n", " # inputs的形状:(时间步数量,批量大小,词表大小)\n", " W_xh, W_hh, b_h, W_hq, b_q = params\n", " H, = state\n", " outputs = []\n", " # X的形状:(批量大小,词表大小)\n", " for X in inputs:\n", " H = torch.tanh(torch.mm(X, W_xh) + torch.mm(H, W_hh) + b_h)\n", " Y = torch.mm(H, W_hq) + b_q\n", " outputs.append(Y)\n", " return torch.cat(outputs, dim=0), (H,)" ] }, { "cell_type": "markdown", "id": "b99d272d", "metadata": { "origin_pos": 32 }, "source": [ "定义了所有需要的函数之后,接下来我们[**创建一个类来包装这些函数**],\n", "并存储从零开始实现的循环神经网络模型的参数。\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "a45ae30c", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:05.093357Z", "iopub.status.busy": "2023-08-18T07:18:05.092334Z", "iopub.status.idle": "2023-08-18T07:18:05.101515Z", "shell.execute_reply": "2023-08-18T07:18:05.100380Z" }, "origin_pos": 34, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "class RNNModelScratch: #@save\n", " \"\"\"从零开始实现的循环神经网络模型\"\"\"\n", " def __init__(self, vocab_size, num_hiddens, device,\n", " get_params, init_state, forward_fn):\n", " self.vocab_size, self.num_hiddens = vocab_size, num_hiddens\n", " self.params = get_params(vocab_size, num_hiddens, device)\n", " self.init_state, self.forward_fn = init_state, forward_fn\n", "\n", " def __call__(self, X, state):\n", " X = F.one_hot(X.T, self.vocab_size).type(torch.float32)\n", " return self.forward_fn(X, state, self.params)\n", "\n", " def begin_state(self, batch_size, device):\n", " return self.init_state(batch_size, self.num_hiddens, device)" ] }, { "cell_type": "markdown", "id": "34b19f1b", "metadata": { "origin_pos": 37 }, "source": [ "让我们[**检查输出是否具有正确的形状**]。\n", "例如,隐状态的维数是否保持不变。\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "83809e58", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:05.106127Z", "iopub.status.busy": "2023-08-18T07:18:05.105766Z", "iopub.status.idle": "2023-08-18T07:18:07.615027Z", "shell.execute_reply": "2023-08-18T07:18:07.613950Z" }, "origin_pos": 39, "tab": [ "pytorch" ] }, "outputs": [ { "data": { "text/plain": [ "(torch.Size([10, 28]), 1, torch.Size([2, 512]))" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num_hiddens = 512\n", "net = RNNModelScratch(len(vocab), num_hiddens, d2l.try_gpu(), get_params,\n", " init_rnn_state, rnn)\n", "state = net.begin_state(X.shape[0], d2l.try_gpu())\n", "Y, new_state = net(X.to(d2l.try_gpu()), state)\n", "Y.shape, len(new_state), new_state[0].shape" ] }, { "cell_type": "markdown", "id": "3baefc97", "metadata": { "origin_pos": 42 }, "source": [ "我们可以看到输出形状是(时间步数$\\times$批量大小,词表大小),\n", "而隐状态形状保持不变,即(批量大小,隐藏单元数)。\n", "\n", "## 预测\n", "\n", "让我们[**首先定义预测函数来生成`prefix`之后的新字符**],\n", "其中的`prefix`是一个用户提供的包含多个字符的字符串。\n", "在循环遍历`prefix`中的开始字符时,\n", "我们不断地将隐状态传递到下一个时间步,但是不生成任何输出。\n", "这被称为*预热*(warm-up)期,\n", "因为在此期间模型会自我更新(例如,更新隐状态),\n", "但不会进行预测。\n", "预热期结束后,隐状态的值通常比刚开始的初始值更适合预测,\n", "从而预测字符并输出它们。\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "a98020e1", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:07.619431Z", "iopub.status.busy": "2023-08-18T07:18:07.619151Z", "iopub.status.idle": "2023-08-18T07:18:07.626388Z", "shell.execute_reply": "2023-08-18T07:18:07.625321Z" }, "origin_pos": 44, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "def predict_ch8(prefix, num_preds, net, vocab, device): #@save\n", " \"\"\"在prefix后面生成新字符\"\"\"\n", " state = net.begin_state(batch_size=1, device=device)\n", " outputs = [vocab[prefix[0]]]\n", " get_input = lambda: torch.tensor([outputs[-1]], device=device).reshape((1, 1))\n", " for y in prefix[1:]: # 预热期\n", " _, state = net(get_input(), state)\n", " outputs.append(vocab[y])\n", " for _ in range(num_preds): # 预测num_preds步\n", " y, state = net(get_input(), state)\n", " outputs.append(int(y.argmax(dim=1).reshape(1)))\n", " return ''.join([vocab.idx_to_token[i] for i in outputs])" ] }, { "cell_type": "markdown", "id": "375db47c", "metadata": { "origin_pos": 47 }, "source": [ "现在我们可以测试`predict_ch8`函数。\n", "我们将前缀指定为`time traveller `,\n", "并基于这个前缀生成10个后续字符。\n", "鉴于我们还没有训练网络,它会生成荒谬的预测结果。\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "8ea33551", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:07.630956Z", "iopub.status.busy": "2023-08-18T07:18:07.630335Z", "iopub.status.idle": "2023-08-18T07:18:07.646754Z", "shell.execute_reply": "2023-08-18T07:18:07.645688Z" }, "origin_pos": 48, "tab": [ "pytorch" ] }, "outputs": [ { "data": { "text/plain": [ "'time traveller aaaaaaaaaa'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "predict_ch8('time traveller ', 10, net, vocab, d2l.try_gpu())" ] }, { "cell_type": "markdown", "id": "596cfaf3", "metadata": { "origin_pos": 50 }, "source": [ "## [**梯度裁剪**]\n", "\n", "对于长度为$T$的序列,我们在迭代中计算这$T$个时间步上的梯度,\n", "将会在反向传播过程中产生长度为$\\mathcal{O}(T)$的矩阵乘法链。\n", "如 :numref:`sec_numerical_stability`所述,\n", "当$T$较大时,它可能导致数值不稳定,\n", "例如可能导致梯度爆炸或梯度消失。\n", "因此,循环神经网络模型往往需要额外的方式来支持稳定训练。\n", "\n", "一般来说,当解决优化问题时,我们对模型参数采用更新步骤。\n", "假定在向量形式的$\\mathbf{x}$中,\n", "或者在小批量数据的负梯度$\\mathbf{g}$方向上。\n", "例如,使用$\\eta > 0$作为学习率时,在一次迭代中,\n", "我们将$\\mathbf{x}$更新为$\\mathbf{x} - \\eta \\mathbf{g}$。\n", "如果我们进一步假设目标函数$f$表现良好,\n", "即函数$f$在常数$L$下是*利普希茨连续的*(Lipschitz continuous)。\n", "也就是说,对于任意$\\mathbf{x}$和$\\mathbf{y}$我们有:\n", "\n", "$$|f(\\mathbf{x}) - f(\\mathbf{y})| \\leq L \\|\\mathbf{x} - \\mathbf{y}\\|.$$\n", "\n", "在这种情况下,我们可以安全地假设:\n", "如果我们通过$\\eta \\mathbf{g}$更新参数向量,则\n", "\n", "$$|f(\\mathbf{x}) - f(\\mathbf{x} - \\eta\\mathbf{g})| \\leq L \\eta\\|\\mathbf{g}\\|,$$\n", "\n", "这意味着我们不会观察到超过$L \\eta \\|\\mathbf{g}\\|$的变化。\n", "这既是坏事也是好事。\n", "坏的方面,它限制了取得进展的速度;\n", "好的方面,它限制了事情变糟的程度,尤其当我们朝着错误的方向前进时。\n", "\n", "有时梯度可能很大,从而优化算法可能无法收敛。\n", "我们可以通过降低$\\eta$的学习率来解决这个问题。\n", "但是如果我们很少得到大的梯度呢?\n", "在这种情况下,这种做法似乎毫无道理。\n", "一个流行的替代方案是通过将梯度$\\mathbf{g}$投影回给定半径\n", "(例如$\\theta$)的球来裁剪梯度$\\mathbf{g}$。\n", "如下式:\n", "\n", "(**$$\\mathbf{g} \\leftarrow \\min\\left(1, \\frac{\\theta}{\\|\\mathbf{g}\\|}\\right) \\mathbf{g}.$$**)\n", "\n", "通过这样做,我们知道梯度范数永远不会超过$\\theta$,\n", "并且更新后的梯度完全与$\\mathbf{g}$的原始方向对齐。\n", "它还有一个值得拥有的副作用,\n", "即限制任何给定的小批量数据(以及其中任何给定的样本)对参数向量的影响,\n", "这赋予了模型一定程度的稳定性。\n", "梯度裁剪提供了一个快速修复梯度爆炸的方法,\n", "虽然它并不能完全解决问题,但它是众多有效的技术之一。\n", "\n", "下面我们定义一个函数来裁剪模型的梯度,\n", "模型是从零开始实现的模型或由高级API构建的模型。\n", "我们在此计算了所有模型参数的梯度的范数。\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "997a02ea", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:07.651414Z", "iopub.status.busy": "2023-08-18T07:18:07.650745Z", "iopub.status.idle": "2023-08-18T07:18:07.657007Z", "shell.execute_reply": "2023-08-18T07:18:07.655964Z" }, "origin_pos": 52, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "def grad_clipping(net, theta): #@save\n", " \"\"\"裁剪梯度\"\"\"\n", " if isinstance(net, nn.Module):\n", " params = [p for p in net.parameters() if p.requires_grad]\n", " else:\n", " params = net.params\n", " norm = torch.sqrt(sum(torch.sum((p.grad ** 2)) for p in params))\n", " if norm > theta:\n", " for param in params:\n", " param.grad[:] *= theta / norm" ] }, { "cell_type": "markdown", "id": "726d638a", "metadata": { "origin_pos": 55 }, "source": [ "## 训练\n", "\n", "在训练模型之前,让我们[**定义一个函数在一个迭代周期内训练模型**]。\n", "它与我们训练 :numref:`sec_softmax_scratch`模型的方式有三个不同之处。\n", "\n", "1. 序列数据的不同采样方法(随机采样和顺序分区)将导致隐状态初始化的差异。\n", "1. 我们在更新模型参数之前裁剪梯度。\n", " 这样的操作的目的是,即使训练过程中某个点上发生了梯度爆炸,也能保证模型不会发散。\n", "1. 我们用困惑度来评价模型。如 :numref:`subsec_perplexity`所述,\n", " 这样的度量确保了不同长度的序列具有可比性。\n", "\n", "具体来说,当使用顺序分区时,\n", "我们只在每个迭代周期的开始位置初始化隐状态。\n", "由于下一个小批量数据中的第$i$个子序列样本\n", "与当前第$i$个子序列样本相邻,\n", "因此当前小批量数据最后一个样本的隐状态,\n", "将用于初始化下一个小批量数据第一个样本的隐状态。\n", "这样,存储在隐状态中的序列的历史信息\n", "可以在一个迭代周期内流经相邻的子序列。\n", "然而,在任何一点隐状态的计算,\n", "都依赖于同一迭代周期中前面所有的小批量数据,\n", "这使得梯度计算变得复杂。\n", "为了降低计算量,在处理任何一个小批量数据之前,\n", "我们先分离梯度,使得隐状态的梯度计算总是限制在一个小批量数据的时间步内。\n", "\n", "当使用随机抽样时,因为每个样本都是在一个随机位置抽样的,\n", "因此需要为每个迭代周期重新初始化隐状态。\n", "与 :numref:`sec_softmax_scratch`中的\n", "`train_epoch_ch3`函数相同,\n", "`updater`是更新模型参数的常用函数。\n", "它既可以是从头开始实现的`d2l.sgd`函数,\n", "也可以是深度学习框架中内置的优化函数。\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "4b5e10db", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:07.661288Z", "iopub.status.busy": "2023-08-18T07:18:07.660940Z", "iopub.status.idle": "2023-08-18T07:18:07.671838Z", "shell.execute_reply": "2023-08-18T07:18:07.670625Z" }, "origin_pos": 57, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "#@save\n", "def train_epoch_ch8(net, train_iter, loss, updater, device, use_random_iter):\n", " \"\"\"训练网络一个迭代周期(定义见第8章)\"\"\"\n", " state, timer = None, d2l.Timer()\n", " metric = d2l.Accumulator(2) # 训练损失之和,词元数量\n", " for X, Y in train_iter:\n", " if state is None or use_random_iter:\n", " # 在第一次迭代或使用随机抽样时初始化state\n", " state = net.begin_state(batch_size=X.shape[0], device=device)\n", " else:\n", " if isinstance(net, nn.Module) and not isinstance(state, tuple):\n", " # state对于nn.GRU是个张量\n", " state.detach_()\n", " else:\n", " # state对于nn.LSTM或对于我们从零开始实现的模型是个张量\n", " for s in state:\n", " s.detach_()\n", " y = Y.T.reshape(-1)\n", " X, y = X.to(device), y.to(device)\n", " y_hat, state = net(X, state)\n", " l = loss(y_hat, y.long()).mean()\n", " if isinstance(updater, torch.optim.Optimizer):\n", " updater.zero_grad()\n", " l.backward()\n", " grad_clipping(net, 1)\n", " updater.step()\n", " else:\n", " l.backward()\n", " grad_clipping(net, 1)\n", " # 因为已经调用了mean函数\n", " updater(batch_size=1)\n", " metric.add(l * y.numel(), y.numel())\n", " return math.exp(metric[0] / metric[1]), metric[1] / timer.stop()" ] }, { "cell_type": "markdown", "id": "59eb7b57", "metadata": { "origin_pos": 60 }, "source": [ "[**循环神经网络模型的训练函数既支持从零开始实现,\n", "也可以使用高级API来实现。**]\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "3fe4738f", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:07.676190Z", "iopub.status.busy": "2023-08-18T07:18:07.675912Z", "iopub.status.idle": "2023-08-18T07:18:07.684203Z", "shell.execute_reply": "2023-08-18T07:18:07.683026Z" }, "origin_pos": 62, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "#@save\n", "def train_ch8(net, train_iter, vocab, lr, num_epochs, device,\n", " use_random_iter=False):\n", " \"\"\"训练模型(定义见第8章)\"\"\"\n", " loss = nn.CrossEntropyLoss()\n", " animator = d2l.Animator(xlabel='epoch', ylabel='perplexity',\n", " legend=['train'], xlim=[10, num_epochs])\n", " # 初始化\n", " if isinstance(net, nn.Module):\n", " updater = torch.optim.SGD(net.parameters(), lr)\n", " else:\n", " updater = lambda batch_size: d2l.sgd(net.params, lr, batch_size)\n", " predict = lambda prefix: predict_ch8(prefix, 50, net, vocab, device)\n", " # 训练和预测\n", " for epoch in range(num_epochs):\n", " ppl, speed = train_epoch_ch8(\n", " net, train_iter, loss, updater, device, use_random_iter)\n", " if (epoch + 1) % 10 == 0:\n", " print(predict('time traveller'))\n", " animator.add(epoch + 1, [ppl])\n", " print(f'困惑度 {ppl:.1f}, {speed:.1f} 词元/秒 {str(device)}')\n", " print(predict('time traveller'))\n", " print(predict('traveller'))" ] }, { "cell_type": "markdown", "id": "a744039a", "metadata": { "origin_pos": 65 }, "source": [ "[**现在,我们训练循环神经网络模型。**]\n", "因为我们在数据集中只使用了10000个词元,\n", "所以模型需要更多的迭代周期来更好地收敛。\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "60e0712a", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:18:07.688319Z", "iopub.status.busy": "2023-08-18T07:18:07.688050Z", "iopub.status.idle": "2023-08-18T07:19:36.858051Z", "shell.execute_reply": "2023-08-18T07:19:36.857197Z" }, "origin_pos": 66, "tab": [ "pytorch" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "困惑度 1.0, 67212.6 词元/秒 cuda:0\n", "time traveller for so it will be convenient to speak of himwas e\n", "travelleryou can show black is white by argument said filby\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", " \n", " 2023-08-18T07:19:36.820413\n", " image/svg+xml\n", " \n", " \n", " Matplotlib v3.5.1, https://matplotlib.org/\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "num_epochs, lr = 500, 1\n", "train_ch8(net, train_iter, vocab, lr, num_epochs, d2l.try_gpu())" ] }, { "cell_type": "markdown", "id": "7f058da1", "metadata": { "origin_pos": 68 }, "source": [ "[**最后,让我们检查一下使用随机抽样方法的结果。**]\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "e672f727", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:19:36.861442Z", "iopub.status.busy": "2023-08-18T07:19:36.861161Z", "iopub.status.idle": "2023-08-18T07:20:58.207471Z", "shell.execute_reply": "2023-08-18T07:20:58.206663Z" }, "origin_pos": 69, "tab": [ "pytorch" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "困惑度 1.5, 65222.3 词元/秒 cuda:0\n", "time traveller held in his hand was a glitteringmetallic framewo\n", "traveller but now you begin to seethe object of my investig\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", " \n", " 2023-08-18T07:20:58.170314\n", " image/svg+xml\n", " \n", " \n", " Matplotlib v3.5.1, https://matplotlib.org/\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "net = RNNModelScratch(len(vocab), num_hiddens, d2l.try_gpu(), get_params,\n", " init_rnn_state, rnn)\n", "train_ch8(net, train_iter, vocab, lr, num_epochs, d2l.try_gpu(),\n", " use_random_iter=True)" ] }, { "cell_type": "markdown", "id": "11df14f3", "metadata": { "origin_pos": 72 }, "source": [ "从零开始实现上述循环神经网络模型,\n", "虽然有指导意义,但是并不方便。\n", "在下一节中,我们将学习如何改进循环神经网络模型。\n", "例如,如何使其实现地更容易,且运行速度更快。\n", "\n", "## 小结\n", "\n", "* 我们可以训练一个基于循环神经网络的字符级语言模型,根据用户提供的文本的前缀生成后续文本。\n", "* 一个简单的循环神经网络语言模型包括输入编码、循环神经网络模型和输出生成。\n", "* 循环神经网络模型在训练以前需要初始化状态,不过随机抽样和顺序划分使用初始化方法不同。\n", "* 当使用顺序划分时,我们需要分离梯度以减少计算量。\n", "* 在进行任何预测之前,模型通过预热期进行自我更新(例如,获得比初始值更好的隐状态)。\n", "* 梯度裁剪可以防止梯度爆炸,但不能应对梯度消失。\n", "\n", "## 练习\n", "\n", "1. 尝试说明独热编码等价于为每个对象选择不同的嵌入表示。\n", "1. 通过调整超参数(如迭代周期数、隐藏单元数、小批量数据的时间步数、学习率等)来改善困惑度。\n", " * 困惑度可以降到多少?\n", " * 用可学习的嵌入表示替换独热编码,是否会带来更好的表现?\n", " * 如果用H.G.Wells的其他书作为数据集时效果如何,\n", " 例如[*世界大战*](http://www.gutenberg.org/ebooks/36)?\n", "1. 修改预测函数,例如使用采样,而不是选择最有可能的下一个字符。\n", " * 会发生什么?\n", " * 调整模型使之偏向更可能的输出,例如,当$\\alpha > 1$,从$q(x_t \\mid x_{t-1}, \\ldots, x_1) \\propto P(x_t \\mid x_{t-1}, \\ldots, x_1)^\\alpha$中采样。\n", "1. 在不裁剪梯度的情况下运行本节中的代码会发生什么?\n", "1. 更改顺序划分,使其不会从计算图中分离隐状态。运行时间会有变化吗?困惑度呢?\n", "1. 用ReLU替换本节中使用的激活函数,并重复本节中的实验。我们还需要梯度裁剪吗?为什么?\n" ] }, { "cell_type": "markdown", "id": "c810dbc6", "metadata": { "origin_pos": 74, "tab": [ "pytorch" ] }, "source": [ "[Discussions](https://discuss.d2l.ai/t/2103)\n" ] } ], "metadata": { "language_info": { "name": "python" }, "required_libs": [] }, "nbformat": 4, "nbformat_minor": 5 }