1 问题设置
-
1.1 数据集和预处理
-
1.2 概览整个模型
-
2.1 在优化循环中梯度裁剪
-
2.2 采样
-
3.1 梯度下降
-
3.2 训练模型
-
如何存储文本数据以使用RNN进行处理
-
如何通过在每个时间步采样预测并将其传递给下一个RNN单元来合成数据
-
如何构建字符级文本生成循环神经网络
-
为什么剪裁渐变很重要
-
读取恐龙名称的数据集
-
创建唯一字符列表(例如a-z)
-
计算数据集和词汇量。
data = open('dinos.txt', 'r').read() # 加载数据集
data= data.lower() # 将所有数据变小写
chars = list(set(data)) # 创建唯一字符列表,data中用了什么字符
data_size, vocab_size = len(data), len(chars) # 数据集的大小和词汇量
print('There are %d total characters and %d unique characters in your data.' % (data_size, vocab_size))
['\n', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
{ 0: '\n', 1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
-
初始化参数
-
运行优化循环
-
前向传播以计算损失函数
-
向后传播根据损失函数计算梯度
-
梯度裁剪以避免梯度爆炸
-
使用梯度下降规则更新你的参数
-
返回学习到的参数
-
每一个时间步长, RNN都会根据之前的字符来预测下一个字符是什么。
-
数据集 \mathbf{X} = (x^{\langle 1 \rangle}, x^{\langle 2 \rangle}, ..., x^{\langle T_x \rangle}) 是训练集中的字符列表。
-
\mathbf{Y} = (y^{\langle 1 \rangle}, y^{\langle 2 \rangle}, ..., y^{\langle T_x \rangle}) 是相同的字符列表,但是向前移动了一个字符。
-
每个时间步长 t, y^{\langle t \rangle} = x^{\langle t+1 \rangle}. 在时间 t 处的预测值和时间 t + 1 处的输入值相同.
-
梯度裁剪:避免梯度爆炸
-
采样:一种用于生成字符的技术
-
当梯度值非常大时,称为“梯度爆炸”。
-
梯度爆炸使训练过程更加困难,因为更新可能太大,以至于在反向传播过程中“overshoot”了最佳值。
-
向前传播
-
计算成本函数
-
向后传播
-
参数更新
-
剪切渐变有多种方法。
-
我们将使用简单的按元素裁剪程序,其中将梯度向量的每个元素裁剪为位于某个范围[-N,N]之间。
-
例如,如果N = 10
-
元素的取值范围是[-10,10]
-
如果任何在梯度向量中大于10的元素,则将其设置为10。
-
如果任何在梯度向量中小于-10的元素,则将其设置为-10。
-
如果任何在-10到10之间的元素,则它们保持原始值。
def clip(gradients, maxValue):
'''
Clips the gradients' values between minimum and maximum.
Arguments:
gradients -- a dictionary containing the gradients "dWaa", "dWax", "dWya", "db", "dby"
maxValue -- everything above this number is set to this number, and everything less than -maxValue is set to -maxValue
Returns:
gradients -- a dictionary with the clipped gradients.
'''
dWaa, dWax, dWya, db, dby = gradients['dWaa'], gradients['dWax'], gradients['dWya'], gradients['db'], gradients['dby']
### START CODE HERE ###
# clip to mitigate exploding gradients, loop over [dWax, dWaa, dWya, db, dby]. (≈2 lines)
for gradient in [dWax, dWaa, dWya, db, dby]:
np.clip(gradient, -maxValue, maxValue, out=gradient)
### END CODE HERE ###
# gradients = {"dWaa": dWaa, "dWax": dWax, "dWya": dWya, "db": db, "dby": dby}
return gradients
-
Step 1: 输入0的 "dummy" 向量x^{\langle 1 \rangle} = \vec{0}.
-
这是我们生成任何字符之前的默认输入。 我们还设置了 a^{\langle 0 \rangle} = \vec{0}
-
Step 2: 执行一次向前传播即可获得 a^{\langle 1 \rangle} 和 \hat{y}^{\langle 1 \rangle}. 这里是公式:
-
关于 的一些细节:
-
注意 是一个概率向量 (softmax) (它的每个元素值在0到1之间,且总和为1).
-
表示由“ i”索引的字符是下一个字符的概率。
-
我们提供了 softmax() 函数供你使用.
-
x^{\langle 1 \rangle} 在代码中是 x.当创建one-hot向量时,请创建一个由零的组成numpy数组,其中行数等于唯一字符数,列数等于1。 它是2D而不是1D数组。
-
a^{\langle 0 \rangle} 在代码中是a_prev. 它是一个由零组成的numpy数组,其中行数为 n_ {a} ,列数为1。它也是2D数组。 通过获取 W_ {aa} 中的列数来得到 n_ {a} (这些数字必须匹配,以便矩阵乘法W_{aa}a^{\langle t \rangle}起作用 。
-
a ^ {\ langle 0 \ rangle} 在代码中为a_prev。
-
numpy.dot
-
numpy.tanh
-
你可能想知道为什么我们强调x^{\langle 1 \rangle}和a^{\langle 0 \rangle}是2D数组而不是1D向量。
-
对于numpy中的矩阵乘法,如果将2D矩阵与1D向量相乘,则最终得到1D数组。
-
当我们将两个数组相加时,期望它们具有相同形状,这将成为一个问题。
-
当两个具有不同维数的数组加在一起时,Python将会执行“广播broadcasts”。
-
这是一些示例代码,显示了使用1D和2D数组之间的区别。
import numpy as np
matrix1 = np.array([[1,1],[2,2],[3,3]]) # (3,2)
matrix2 = np.array([[0],[0],[0]]) # (3,1)
vector1D = np.array([1,1]) # (2,)
vector2D = np.array([[1],[1]]) # (2,1)
np.dot(matrix1,vector1D) # 2D 和 1D 数组相乘: 结果是1D数组 [2 4 6]
np.dot(matrix1,vector2D) # 2D 和 2D 数组相乘: 结果是2D数组 [[2], [4], [6]]
np.dot(matrix1,vector2D) + matrix2 # (3 x 1) 向量和(3 x 1)向量相加是(3 x 1) 向量,这个是我们想要的。 [[2] [4] [6]]
np.dot(matrix1,vector1D) + matrix2 # (3,) 向量和(3 x 1)向量相加,这会在第二维上广播1D的数组,这不是我们想要的!
-
Step 3: 抽样:
-
注意我们已经有了y^{\langle t+1 \rangle}, 我们想选择恐龙名称中的下一个字母。如果我们选择最有可能的情况,那么在给定起始字母的情况下,模型将始终产生相同的结果。
-
为了使结果更有趣,我们将使用np.random.choice选择可能但并非总是相同的下一个字母。
-
采样是从一组值中选择一个值,其中每个值都有一定概率被选择。
-
采样使我们能够生成随机的序列值。
-
根据\hat{y}^{\langle t+1 \rangle }中的概率分布选择下一个字符的索引。
-
这就意味着如果\hat{y}^{\langle t+1 \rangle }_i = 0.16, 你将以16%的概率选择索引“ i”。
-
你可以查看np.random.choice.
-
这就意味着你会根据分布来选择索引:
-
注意p的值是1D向量。
-
注意 \hat{y}^{\langle t+1 \rangle}在代码中用 y 表示, 它是2维数组。
-
Step 4: 更新 x^{\langle t \rangle }
-
sample()函数的最后一步就是更新变量x, 它当前存储的是 x^{\langle t \rangle }, 换成x^{\langle t + 1 \rangle }.
-
你将选择作为预测字符相对应的one-hot矢量来代表x^{\langle t + 1 \rangle }。
-
你将接着在步骤1中向前传播x^{\langle t + 1 \rangle },并继续重复该过程直到获得“ \n”字符,它表明你已经到达恐龙名称的末尾。
# GRADED FUNCTION: sample def sample(parameters, char_to_ix, seed): """ Sample a sequence of characters according to a sequence of probability distributions output of the RNN Arguments: parameters -- python dictionary containing the parameters Waa, Wax, Wya, by, and b. char_to_ix -- python dictionary mapping each character to an index. seed -- used for grading purposes. Do not worry about it. Returns: indices -- a list of length n containing the indices of the sampled characters. """ # Retrieve parameters and relevant shapes from "parameters" dictionary Waa, Wax, Wya, by, b = parameters['Waa'], parameters['Wax'], parameters['Wya'], parameters['by'], parameters['b'] vocab_size = by.shape[0] n_a = Waa.shape[1] ### START CODE HERE ### # Step 1: Create the a zero vector x that can be used as the one-hot vector # representing the first character (initializing the sequence generation). (≈1 line) x = np.zeros((vocab_size, 1)) # 可以看看上面为什么这里是二维 # Step 1': Initialize a_prev as zeros (≈1 line) a_prev = np.zeros((n_a, 1)) # Create an empty list of indices, this is the list which will contain the list of indices of the characters to generate (≈1 line) indices = [] # idx is the index of the one-hot vector x that is set to 1 # All other positions in x are zero. # We will initialize idx to -1 idx = -1 # Loop over time-steps t. At each time-step: # sample a character from a probability distribution # and append its index (`idx`) to the list "indices". # We'll stop if we reach 50 characters # (which should be very unlikely with a well trained model). # Setting the maximum number of characters helps with debugging and prevents infinite loops. counter = 0 newline_character = char_to_ix['\n'] while (idx != newline_character and counter != 50): # Step 2: Forward propagate x using the equations (1), (2) and (3) a = np.tanh(np.dot(Wax, x) + np.dot(Waa, a_prev) + b) z = np.dot(Wya, a) + by y = softmax(z) # for grading purposes np.random.seed(counter+seed) # Step 3: Sample the index of a character within the vocabulary from the probability distribution y # (see additional hints above) idx = np.random.choice(list(range(vocab_size)), p=y.ravel()) # Append the index to "indices" indices.append(idx) # Step 4: Overwrite the input x with one that corresponds to the sampled index `idx`. # (see additional hints above) x = np.zeros((vocab_size, 1)) x[idx] = 1 # Update "a_prev" to be "a" a_prev = a # for grading purposes seed += 1 counter +=1 ### END CODE HERE ### if (counter == 50): indices.append(char_to_ix['\n']) return indices
-
在本节中,你将实现一个函数,该函数执行一步随机梯度下降(带有修剪的梯度)。
-
你将一次查看一个训练示例,因此优化算法将是随机梯度下降。
-
通过RNN向前传播以计算损耗
-
随时间向后传播以计算相对于参数的损耗梯度
-
梯度裁剪
-
使用梯度下降更新参数
def rnn_forward(X, Y, a_prev, parameters): """ Performs the forward propagation through the RNN and computes the cross-entropy loss. It returns the loss' value as well as a "cache" storing values to be used in backpropagation.""" .... return loss, cache def rnn_backward(X, Y, parameters, cache): """ Performs the backward propagation through time to compute the gradients of the loss with respect to the parameters. It returns also all the hidden states.""" ... return gradients, a def update_parameters(parameters, gradients, learning_rate): """ Updates parameters using the Gradient Descent Update Rule.""" ... return parameters Recall that you previously implemented the clip function: def clip(gradients, maxValue) """Clips the gradients' values between minimum and maximum.""" ... return gradients
-
请注意,即使parameters不是optimize函数的返回值之一,parameters字典中的权重和偏差也会通过优化进行更新。参数字典通过引用传递到函数中,因此即使在函数外部访问该字典,对字典的更改也会对参数字典做出更改。
-
Python字典和列表是“按引用传递”,这意味着,如果将字典传递给函数并在函数内修改字典,则这将更改同一字典。
def optimize(X, Y, a_prev, parameters, learning_rate = 0.01): """ Execute one step of the optimization to train the model. Arguments: X -- list of integers, where each integer is a number that maps to a character in the vocabulary. Y -- list of integers, exactly the same as X but shifted one index to the left. a_prev -- previous hidden state. parameters -- python dictionary containing: Wax -- Weight matrix multiplying the input, numpy array of shape (n_a, n_x) Waa -- Weight matrix multiplying the hidden state, numpy array of shape (n_a, n_a) Wya -- Weight matrix relating the hidden-state to the output, numpy array of shape (n_y, n_a) b -- Bias, numpy array of shape (n_a, 1) by -- Bias relating the hidden-state to the output, numpy array of shape (n_y, 1) learning_rate -- learning rate for the model. Returns: loss -- value of the loss function (cross-entropy) gradients -- python dictionary containing: dWax -- Gradients of input-to-hidden weights, of shape (n_a, n_x) dWaa -- Gradients of hidden-to-hidden weights, of shape (n_a, n_a) dWya -- Gradients of hidden-to-output weights, of shape (n_y, n_a) db -- Gradients of bias vector, of shape (n_a, 1) dby -- Gradients of output bias vector, of shape (n_y, 1) a[len(X)-1] -- the last hidden state, of shape (n_a, 1) """ ### START CODE HERE ### # Forward propagate through time (≈1 line) loss, cache = rnn_forward(X, Y, a_prev, parameters) # Backpropagate through time (≈1 line) gradients, a = rnn_backward(X, Y, parameters, cache) # Clip your gradients between -5 (min) and 5 (max) (≈1 line) gradients = clip(gradients, 5) # Update parameters (≈1 line) parameters = update_parameters(parameters, gradients, learning_rate) ### END CODE HERE ### return loss, gradients, a[len(X)-1]
-
给定恐龙名称数据集,我们将数据集的每一行(一个名称)用作一个训练样本。
-
每100步随机梯度下降,你将抽样10个随机选择的名称,以查看算法的运行情况。
-
请记住要对数据集进行混洗,以便随机梯度下降以随机顺序访问样本。
-
使用for循环,在“示例”列表中浏览经过排序的恐龙名称列表。
-
如果有100个示例,并且for循环将索引从100开始递增,请考虑如何使索引循环回到0,以便我们可以在j为100、101,等等
-
提示:101除以100为零,余数为1。
-
%是python中的模运算符。
def model(data, ix_to_char, char_to_ix, num_iterations = 35000, n_a = 50, dino_names = 7, vocab_size = 27): """ Trains the model and generates dinosaur names. Arguments: data -- text corpus ix_to_char -- dictionary that maps the index to a character char_to_ix -- dictionary that maps a character to an index num_iterations -- number of iterations to train the model for n_a -- number of units of the RNN cell dino_names -- number of dinosaur names you want to sample at each iteration. vocab_size -- number of unique characters found in the text (size of the vocabulary) Returns: parameters -- learned parameters """ # Retrieve n_x and n_y from vocab_size n_x, n_y = vocab_size, vocab_size # Initialize parameters parameters = initialize_parameters(n_a, n_x, n_y) # Initialize loss (this is required because we want to smooth our loss) loss = get_initial_loss(vocab_size, dino_names) # Build list of all dinosaur names (training examples). with open("dinos.txt") as f: examples = f.readlines() examples = [x.lower().strip() for x in examples] # Shuffle list of all dinosaur names np.random.seed(0) np.random.shuffle(examples) # Initialize the hidden state of your LSTM a_prev = np.zeros((n_a, 1)) # Optimization loop for j in range(num_iterations): ### START CODE HERE ### # Set the index `idx` (see instructions above) index = j % len(examples) # Set the input X (see instructions above) single_example = examples[index] single_example_chars = [ch for ch in single_example] single_example_ix = [char_to_ix[ch] for ch in single_example_chars] X = [None] + single_example_ix # Set the labels Y (see instructions above) ix_newline = char_to_ix["\n"] Y = X[1:] + [ix_newline] # Perform one optimization step: Forward-prop -> Backward-prop -> Clip -> Update parameters # Choose a learning rate of 0.01 curr_loss, gradients, a_prev = optimize(X, Y, a_prev, parameters) ### END CODE HERE ### # Use a latency trick to keep the loss smooth. It happens here to accelerate the training. loss = smooth(loss, curr_loss) # Every 2000 Iteration, generate "n" characters thanks to sample() to check if the model is learning properly if j % 2000 == 0: print('Iteration: %d, Loss: %f' % (j, loss) + '\n') # The number of dinosaur names to print seed = 0 for name in range(dino_names): # Sample indices and print them sampled_indices = sample(parameters, char_to_ix, seed) print_sample(sampled_indices, ix_to_char) seed += 1 # To get the same result (for grading purposes), increment the seed by one. print('\n') return parameters