数据集包含了所有恐龙的名字,构建一个字符级语言模型来创建新的恐龙名称,算法能够学习不同的名称模式,并随机生成新的名称。

完成这项作业能够学到:

  • 如何存储文本数据以便使用RNN进行处理
  • 如何合成数据,通过在每个时间步采样预测值并将其传递给下一个RNN单元
  • 如何构建一个字符级文本生成循环神经网络
  • 为什么剪裁梯度很重要

1 - Problem Statement

1.1 - Dataset and Preprocessing

运行下面单元来读取恐龙名字的数据集,创建一个唯一字符的列表,计算数据集和词表大小。

data = open('dinos.txt', 'r').read()
data= data.lower()
chars = list(set(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))
There are 19909 total characters and 27 unique characters in your data.

字符包括a-z,加上\n,它在这节作业中的作用类似于<EOS>,代表恐龙名字的结束而不是一行的结束。创建一个python字典来映射每个索引和相应的字符,这会帮助找到softmax层的概率分布输出中的字符对应的索引。

char_to_ix = { ch:i for i,ch in enumerate(sorted(chars)) }
ix_to_char = { i:ch for i,ch in enumerate(sorted(chars)) }
print(ix_to_char)
{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'}

1.2 - Overview of the model

模型有以下结构:

  1. 初始化参数
  2. 运行优化的循环:前向传播计算损失,反向传播计算损失的梯度,裁剪梯度避免梯度爆炸,使用梯度下降更新策略更新参数
  3. 返回学习到的参数

在每个时间步,RNN尝试预测给定之前的字符后下一个字符是什么,是训练集,是测试集,其中,

2 - Building blocks of the model

构建两个模块:

  • 梯度裁剪:避免梯度爆炸
  • 采样:用来生成字符

2.1 - Clipping the gradients in the optimization loop

实现一个在优化循环内部调用的clip函数,整个循环结构包括前向传播、损失计算、反向传播、参数更新。参数更新之前,需要时要执行梯度裁剪,来确保梯度不会爆炸。

该clip函数接收一个梯度的字典,返回一个裁剪版本的梯度,如果需要的话。裁剪梯度有不同的方法,使用一个简单的element-wise裁剪过程,梯度向量的每一个元素都被裁剪为介于某个范围[-N,N]的值,一般来说,需要提供一个最大值(比如10)。在本例中,如果梯度向量的任何分量大于10,则设为10,如果梯度向量的任何分量小于-10,则设为-10。

练习:实现下列函数来返回裁剪后的梯度。

### GRADED FUNCTION: clipdef 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 -maxValueReturns: 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

2.2 - Sampling

生成新文本的过程:

练习:实现采样函数,需要以下四步:

  1. 设置
  2. 运行一步前向传播得到,公式为:,其中是一个softmax概率向量,代表下标为i的字符是下一个字符的概率值。
  3. 采样:根据概率分布选取下一个字符的索引,如果,则选取索引“i”的概率为16%,可以使用np.random.choice:
  4. 最后一步是覆盖x,该变量当前存储的值,通过创建一个与选择的预测字符对应的one-hot向量来表示,前向传播​​​​​​​,不断重复直到得到一个\n字符,表示已到达恐龙名字的结尾。
# GRADED FUNCTION: sampledef sample(parameters, char_to_ix, seed):"""Sample a sequence of characters according to a sequence of probability distributions output of the RNNArguments: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 indexes of the sampled characters."""# Retrieve parameters and relevant shapes from "parameters" dictionaryWaa, Wax, Wya, by, b = parameters['Waa'], parameters['Wax'], parameters['Wya'], parameters['by'], parameters['b']# Waa:(n_a, n_a)# Wax:(n_a, vocab_size)# Wya:(vocab_size, n_a)# by:(vocab_size, 1)# b:(n_a, 1)vocab_size = by.shape[0]n_a = Waa.shape[1]### START CODE HERE #### Step 1: Create the one-hot vector x for 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 indexes of the characters to generate (≈1 line)indices = []# Idx is a flag to detect a newline character, we initialize it to -1idx = -1 # Loop over time-steps t. At each time-step, sample a character from a probability distribution and append # its index to "indexes". We'll stop if we reach 50 characters (which should be very unlikely with a well # trained model), which helps debugging and prevents entering an infinite loop. counter = 0newline_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) + byy = softmax(z)# for grading purposesnp.random.seed(counter+seed) # Step 3: Sample the index of a character within the vocabulary from the probability distribution yidx = np.random.choice(list(range(vocab_size)), p=y.ravel())# Append the index to "indices"indices.append(idx)# Step 4: Overwrite the input character as the one corresponding to the sampled index.x = np.zeros((vocab_size, 1))x[idx] = 1# Update "a_prev" to be "a"a_prev = a# for grading purposesseed += 1counter +=1### END CODE HERE ###if (counter == 50):indices.append(char_to_ix['\n'])return indices

3 - Building the language model

3.1 - Gradient descent

实现一个函数来执行一步随机梯度下降(使用裁剪梯度),每次都要遍历整个训练样本,所以优化算法是随机梯度下降,RNN中通常的优化循环有以下步骤:

  • 前向传播计算损失
  • 通过时间的反向传播计算损失相对于参数的梯度
  • 裁剪梯度,如果必要
  • 使用梯度下降更新参数

练习:实现优化过程(随机梯度下降的一步)

提供以下函数:

# GRADED FUNCTION: optimizedef 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]

3.2 - Training the model

给定了恐龙名字的数据集,使用数据集的每一行(一个名字)作为一个训练样本,每100步随机梯度下降,抽样10个随机选择的名字,看看算法如何运作的,记得打乱数据集,随机梯度下降就可以随机访问样本。

练习:根据说明实现model(),当examples[index]包含了一个恐龙名字,为了构建样本(X,Y),可以:

# GRADED FUNCTION: modeldef 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 corpusix_to_char -- dictionary that maps the index to a characterchar_to_ix -- dictionary that maps a character to an indexnum_iterations -- number of iterations to train the model forn_a -- number of hidden neurons in the softmax layerdino_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 vocabularyReturns:parameters -- learned parameters"""# Retrieve n_x and n_y from vocab_sizen_x, n_y = vocab_size, vocab_size# Initialize parametersparameters = initialize_parameters(n_a, n_x, n_y)# Initialize loss (this is required because we want to smooth our loss, don't worry about it)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 namesshuffle(examples)# Initialize the hidden state of your LSTMa_prev = np.zeros((n_a, 1))# Optimization loopfor j in range(num_iterations):### START CODE HERE #### Use the hint above to define one training example (X,Y) (≈ 2 lines)index = j % len(examples)X = [None] + [char_to_ix[ch] for ch in examples[index]]Y = X[1:] + [char_to_ix['\n']]# Perform one optimization step: Forward-prop -> Backward-prop -> Clip -> Update parameters# Choose a learning rate of 0.01curr_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 properlyif j % 2000 == 0:print('Iteration: %d, Loss: %f' % (j, loss) + '\n')# The number of dinosaur names to printseed = 0for name in range(dino_names):# Sample indexes and print themsampled_indexes = sample(parameters, char_to_ix, seed)print_sample(sampled_indexes, ix_to_char)seed += 1  # To get the same result for grading purposed, increment the seed by one. print('\n')return parameters

生成恐龙名字的效果:

deeplearning.ai——字符级语言模型-恐龙岛相关推荐

  1. 当莎士比亚遇见Google Flax:教你用​字符级语言模型和归递神经网络写“莎士比亚”式句子...

    作者 | Fabian Deuser 译者 | 天道酬勤 责编 | Carol 出品 | AI科技大本营(ID:rgznai100) 有些人生来伟大,有些人成就伟大,而另一些人则拥有伟大. -- 威廉 ...

  2. 吴恩达deeplearning.ai系列课程笔记+编程作业(13)序列模型(Sequence Models)-第一周 循环序列模型(Recurrent Neural Networks)

    第五门课 序列模型(Sequence Models) 第一周 循环序列模型(Recurrent Neural Networks) 文章目录 第五门课 序列模型(Sequence Models) 第一周 ...

  3. pytorch dropout_手把手带你使用字符级RNN生成名字 | PyTorch

    作者 | News 编辑 | 奇予纪 出品 | 磐创AI团队出品 [磐创AI 导读]:本篇文章讲解了PyTorch专栏的第五章中的使用字符级RNN生成名字.查看专栏历史文章,请点击下方蓝色字体进入相应 ...

  4. 完结撒花!吴恩达DeepLearning.ai《深度学习》课程笔记目录总集

    作者: 大树先生 博客: http://blog.csdn.net/koala_tree 知乎:https://www.zhihu.com/people/dashuxiansheng GitHub:h ...

  5. 学不学吴恩达deeplearning.ai课程,看完这篇你就知道了

    吴恩达的网课 deeplearning.ai 到底适不适合自己呢? 快来看看学员 Thomas 怎么说的! AI 科技评论按:本文的作者是 Thomas Treml,是一名具有社会学背景的数据科学自由 ...

  6. 吴恩达deeplearning.ai系列课程笔记+编程作业(15)序列模型(Sequence Models)-第三周 序列模型和注意力机制

    第五门课 序列模型(Sequence Models) 第三周 序列模型和注意力机制(Sequence models & Attention mechanism) 文章目录 第五门课 序列模型( ...

  7. L5W1作业2 字母级语言模型 - Dinosaurus land¶

    欢迎来到恐龙大陆! 6500万年前,恐龙就已经存在,并且在该作业下它们又回来了.假设你负责一项特殊任务,领先的生物学研究人员正在创造新的恐龙品种,并计划将它们带入地球,而你的工作就是为这些新恐龙起名字 ...

  8. 一文读懂神经网络初始化!吴恩达Deeplearning.ai最新干货

    来源:新智元 本文约3000字,建议阅读5分钟. 本文是deeplearning.ai的一篇技术博客,对初始化值的大小选取不当,可能造成梯度爆炸或梯度消失等问题,并提出了针对性的解决方法. 神经网络的 ...

  9. 独家 | 一文读懂序列建模(deeplearning.ai)

    作者:Pulkit Sharma,2019年1月21日 翻译:陈之炎 校对:丁楠雅 本文约11000字,建议阅读10+分钟. 本文为你详细介绍序列模型,并分析其在不同的真实场景中的应用. 简介 如何预 ...

  10. 吴恩达deeplearning.ai五项课程完整笔记了解一下?

    来源:机器之心 本文共3744字,建议阅读8分钟. 通过本文为大家解读如何构建自然语言.音频和其他序列数据的模型. 自吴恩达发布 deeplearning.ai 课程以来,很多学习者陆续完成了所有专项 ...

最新文章

  1. python报错'str' object is not callable
  2. JVM:常用调优命令
  3. UVa 12167 HDU 2767 强连通分量 Proving Equivalences
  4. InvocationHandler的invoke方法如何被调用?
  5. 休眠锁定模式– PESSIMISTIC_FORCE_INCREMENT锁定模式如何工作
  6. * 类描写叙述:字符串工具类 类名称:String_U
  7. Jquery Ajax +.ashx XML数据格式
  8. python第三周笔记_Python第四周 学习笔记(1)
  9. app error login.php,如何解决uniapp登录错误提示问题
  10. 快速确定HIve表中数据是否重复
  11. 二元偏导数存在的条件_偏导数连续怎么证明
  12. tomcat实现多端口、多域名访问(只针对一个tomcat)
  13. html5视频播放器 二 (功能实现及播放优化)
  14. matlab灰度分段线性变换优缺点,matlab分段线性变换
  15. 想不想修真鸿蒙源液哪里买,想不想修真悟道茶在哪买
  16. matlab 不显示图中坐标轴(不显示x、y、z轴)
  17. 人脸表情识别——fer2013
  18. 天猫否认“大数据杀熟” 部分用户不买账联系消协:会员体系或受影响!
  19. 0x800700c1添加语言,win10检查更新失败,错误代码 0x800700c1
  20. 计算机网络第一章作业(第8版 谢希仁)

热门文章

  1. 学习yii2.0框架阅读代码(九)
  2. Andorid Kernel 编译测试
  3. iOS开发多线程篇—NSOperation基本操作
  4. 关于JavaScript中apply与call的用法意义及区别(转)
  5. 【经验】使用Oracle的SQL Developer创建用户方法
  6. Silverlight+WCF 新手实例 象棋 棋子移动-规则补充(三十七)
  7. 《机器学习Python实践》第7章——数据可视化
  8. matplotlib.pyplot库解析
  9. 模板题——前缀和与差分
  10. 利用MapShaper将.shp文件转换成JSON文件