欢迎来到TensorFlow入门实操课程的学习

MOOC TensorFlow入门实操课程代码回顾总结(一)
MOOC TensorFlow入门实操课程代码回顾总结(二)

用于表示python代码

粘贴运行结果

目录

  • 10 文本生成——循环神经网络设计
    • 10.1 导入库
    • 10.2 将歌词中的数据进行词条化
    • 10.3 创建神经网络的输入序列
    • 10.4 建立神经网路训练数据
    • 10.5 绘制准确率曲线
    • 10.6 模型评估
    • 10(综) 项目实战——生成优美的诗歌

10 文本生成——循环神经网络设计

10.1 导入库

import tensorflow as tffrom tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Embedding, LSTM, Dense, Bidirectional
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
import numpy as np
import time

10.2 将歌词中的数据进行词条化

#print(time.time())
tokenizer = Tokenizer()data="In the town of Athy one Jeremy Lanigan \n Battered away til he hadnt a pound. \nHis father died and made him a man again \n Left him a farm and ten acres of ground. \nHe gave a grand party for friends and relations \nWho didnt forget him when come to the wall, \nAnd if youll but listen Ill make your eyes glisten \nOf the rows and the ructions of Lanigans Ball. \nMyself to be sure got free invitation, \nFor all the nice girls and boys I might ask, \nAnd just in a minute both friends and relations \nWere dancing round merry as bees round a cask. \nJudy ODaly, that nice little milliner, \nShe tipped me a wink for to give her a call, \nAnd I soon arrived with Peggy McGilligan \nJust in time for Lanigans Ball. \nThere were lashings of punch and wine for the ladies, \nPotatoes and cakes; there was bacon and tea, \nThere were the Nolans, Dolans, OGradys \nCourting the girls and dancing away. \nSongs they went round as plenty as water, \nThe harp that once sounded in Taras old hall,\nSweet Nelly Gray and The Rat Catchers Daughter,\nAll singing together at Lanigans Ball. \nThey were doing all kinds of nonsensical polkas \nAll round the room in a whirligig. \nJulia and I, we banished their nonsense \nAnd tipped them the twist of a reel and a jig. \nAch mavrone, how the girls got all mad at me \nDanced til youd think the ceiling would fall. \nFor I spent three weeks at Brooks Academy \nLearning new steps for Lanigans Ball. \nThree long weeks I spent up in Dublin, \nThree long weeks to learn nothing at all,\n Three long weeks I spent up in Dublin, \nLearning new steps for Lanigans Ball. \nShe stepped out and I stepped in again, \nI stepped out and she stepped in again, \nShe stepped out and I stepped in again, \nLearning new steps for Lanigans Ball. \nBoys were all merry and the girls they were hearty \nAnd danced all around in couples and groups, \nTil an accident happened, young Terrance McCarthy \nPut his right leg through miss Finnertys hoops. \nPoor creature fainted and cried Meelia murther, \nCalled for her brothers and gathered them all. \nCarmody swore that hed go no further \nTil he had satisfaction at Lanigans Ball. \nIn the midst of the row miss Kerrigan fainted, \nHer cheeks at the same time as red as a rose. \nSome of the lads declared she was painted, \nShe took a small drop too much, I suppose. \nHer sweetheart, Ned Morgan, so powerful and able, \nWhen he saw his fair colleen stretched out by the wall, \nTore the left leg from under the table \nAnd smashed all the Chaneys at Lanigans Ball. \nBoys, oh boys, twas then there were runctions. \nMyself got a lick from big Phelim McHugh. \nI soon replied to his introduction \nAnd kicked up a terrible hullabaloo. \nOld Casey, the piper, was near being strangled. \nThey squeezed up his pipes, bellows, chanters and all. \nThe girls, in their ribbons, they got all entangled \nAnd that put an end to Lanigans Ball."corpus = data.lower().split("\n")tokenizer.fit_on_texts(corpus)
total_words = len(tokenizer.word_index) + 1print(tokenizer.word_index)
print(total_words)

10.3 创建神经网络的输入序列

input_sequences = []for line in corpus:token_list = tokenizer.texts_to_sequences([line])[0]for i in range(1, len(token_list)):n_gram_sequence = token_list[:i+1]input_sequences.append(n_gram_sequence)# pad sequences
max_sequence_len = max([len(x) for x in input_sequences])
input_sequences = np.array(pad_sequences(input_sequences, maxlen=max_sequence_len, padding='pre'))# create predictors and label
xs, labels = input_sequences[:,:-1],input_sequences[:,-1]ys = tf.keras.utils.to_categorical(labels, num_classes=total_words)
## 查看歌词第一句话的对应编码
print(tokenizer.word_index['in'])
print(tokenizer.word_index['the'])
print(tokenizer.word_index['town'])
print(tokenizer.word_index['of'])
print(tokenizer.word_index['athy'])
print(tokenizer.word_index['one'])
print(tokenizer.word_index['jeremy'])
print(tokenizer.word_index['lanigan'])

4
2
66
8
67
68
69
70

# 查看创建的数据
print(xs[6])

[ 0 0 0 4 2 66 8 67 68 69]

# 查看数据的标签
print(ys[6])         # 使用独热码对其进行编码

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

print(xs[5])
print(ys[5])

[ 0 0 0 0 4 2 66 8 67 68]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

10.4 建立神经网路训练数据

model = Sequential()
model.add(Embedding(total_words, 64, input_length=max_sequence_len-1))
model.add(Bidirectional(LSTM(20)))
model.add(Dense(total_words, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(xs, ys, epochs=500, verbose=1)

10.5 绘制准确率曲线

import matplotlib.pyplot as pltdef plot_graphs(history, string):plt.plot(history.history[string])plt.xlabel("Epochs")plt.ylabel(string)plt.show()
plot_graphs(history, 'accuracy')

10.6 模型评估

seed_text = "Laurence went to dublin"
next_words = 100for _ in range(next_words):token_list = tokenizer.texts_to_sequences([seed_text])[0]token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')predicted = model.predict_classes(token_list, verbose=0)output_word = ""for word, index in tokenizer.word_index.items():if index == predicted:output_word = wordbreakseed_text += " " + output_word
print(seed_text)
print(time.time())

10(综) 项目实战——生成优美的诗歌

import tensorflow as tffrom tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Embedding, LSTM, Dense, Bidirectional
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
import numpy as np
import time
print(time.time())
# !wget --no-check-certificate \
#     https://storage.googleapis.com/laurencemoroney-blog.appspot.com/irish-lyrics-eof.txt \
#     -O irish-lyrics-eof.txt

1612176669.77111

tokenizer = Tokenizer()data = open('/home/zzr/data/irish-lyrics-eof.txt').read()corpus = data.lower().split("\n")tokenizer.fit_on_texts(corpus)
total_words = len(tokenizer.word_index) + 1print(tokenizer.word_index)
print(total_words)

input_sequences = []
for line in corpus:token_list = tokenizer.texts_to_sequences([line])[0]for i in range(1, len(token_list)):n_gram_sequence = token_list[:i+1]input_sequences.append(n_gram_sequence)# pad sequences
max_sequence_len = max([len(x) for x in input_sequences])
input_sequences = np.array(pad_sequences(input_sequences, maxlen=max_sequence_len, padding='pre'))# create predictors and label
xs, labels = input_sequences[:,:-1],input_sequences[:,-1]ys = tf.keras.utils.to_categorical(labels, num_classes=total_words)print(tokenizer.word_index['in'])
print(tokenizer.word_index['the'])
print(tokenizer.word_index['town'])
print(tokenizer.word_index['of'])
print(tokenizer.word_index['athy'])
print(tokenizer.word_index['one'])
print(tokenizer.word_index['jeremy'])
print(tokenizer.word_index['lanigan'])

8
1
71
6
713
39
1790
1791

print(xs[6])
print(ys[6])

[0 0 0 0 0 0 0 0 0 0 0 0 0 0 2]
[0. 0. 0. … 0. 0. 0.]

print(xs[5])
print(ys[5])

[ 0 0 0 0 0 0 0 0 0 51 12 96 1217 48
2] [0. 0. 0. … 0. 0. 0.]

print(tokenizer.word_index)

model = Sequential()
model.add(Embedding(total_words, 100, input_length=max_sequence_len-1))
model.add(Bidirectional(LSTM(150)))
model.add(Dense(total_words, activation='softmax'))
adam = Adam(lr=0.01)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
#earlystop = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=0, mode='auto')
history = model.fit(xs, ys, epochs=10, verbose=1)
#print model.summary()
print(model)

import matplotlib.pyplot as pltdef plot_graphs(history, string):plt.plot(history.history[string])plt.xlabel("Epochs")plt.ylabel(string)plt.show()
plot_graphs(history, 'accuracy')

next_words = 100for _ in range(next_words):token_list = tokenizer.texts_to_sequences([seed_text])[0]token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')predicted = model.predict_classes(token_list, verbose=0)output_word = ""for word, index in tokenizer.word_index.items():if index == predicted:output_word = wordbreakseed_text += " " + output_word
print(seed_text)
print(time.time())

MOOC TensorFlow入门实操课程代码回顾总结(三)相关推荐

  1. TensorFlow官方入门实操课程-一个神经元的网络(线性曲线预测)

    基于如下的课程进行的学习记录 TensorFlow官方入门实操课程 #设置显卡内存使用率,根据使用率占用 import os os.environ["TF_FORCE_GPU_ALLOW_G ...

  2. MOOC网TensoroFlow入门实操课程1——tensorflow简介、计算机视觉、卷积简介、人马分类识别

    课程地址:https://www.icourse163.org/learn/youdao-1460578162?tid=1461280442#/learn/content?type=detail&am ...

  3. TensorFlow官方入门实操课程-全连接神经网络分类

    #设置显卡内存使用率,根据使用率占用 import os os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true" #引 ...

  4. TensorFlow官方入门实操课程-卷积神经网络

    知识点 卷积:用原始像素数据与过滤器中的值相乘,以后加起来. 如下是增强水平特征的过滤器. MaxPooling:每次卷积结束以后用一个MaxPooling用来增强图像的特征. 可以看出经过MaxPo ...

  5. MOOC网TensoroFlow入门实操课程7——TensoroFlow Lite的Android部署

    Android的猫与狗分类 Android的图像分类 Android的物体检测

  6. 【直播回顾及资料下载】小程序云应用入门实操系列课程第一讲...

    直播详情:小程序云应用入门实操系列课程第一讲:https://yq.aliyun.com/articles/698244 直播时间:2019年4月16日 20:30 直播专家: 白宦成 - Linux ...

  7. 微生物组入门必读+宏基因组实操课程=新老司机赶快上车

    声明:本文转载自宏基因组公众号,原作者朱微金,己获作者授权. 写在前面 作为纯wet遗传学博士,转行微生物组领域已经有两年.目睹微生物组文章中分析所占比重之大,让我痛下决心苦学dry技能.目前感觉对宏 ...

  8. 切图案例实操课程二-姜威-专题视频课程

    切图案例实操课程二-199人已学习 课程介绍         本课程以主要目的是引导初入前端的小白,了解前端是如何工作的,通过正确建立构建环境,解构任务, 课程收益      讲师介绍     姜威 ...

  9. 数字化风控全流程 实操课程V2.0 第三期

    一:课程主题 数字化风控全流程 实操训练营V2.0 第三期 二:时间 7月中旬-8月中旬,课后还会有录制回放视频. 三:活动方式 1:线上远程直播培训 2:线下现场课程培训 四:简介 五:老师简介 此 ...

最新文章

  1. Spring AOP源码分析(七)ProxyFactoryBean介绍
  2. web.xml中的所有配置,Listener和Filter的加载顺序
  3. 【解决bug之路】JAVA 之 static
  4. div和div之间画横线_javascript – jQuery – 使用画布在div之间绘制线条
  5. Vue (响应式原理-模拟-3-Compiler)
  6. ngx-echarts 图表数据动态更新
  7. 没有装Express版Sql Server 2005就不能用WebPart ?
  8. linux下载搜狗命令,搜狗输入法linux安装 以及 12个依赖包下载链接分享(示例代码)...
  9. HDFS之namenode文件解析
  10. 佐藤hiroko-爱拯救了我(步之物语)
  11. ngx_lua arg类型_使用模式,Arg和类型微调Drupal主题
  12. 微信小游戏制作大厅里的排行榜(跟游戏内的排行榜有区别)
  13. ERROR Deployer not found: git
  14. 虚拟摄像头之三: 重构android8.1 的 v4l2_camera_HAL 支持虚拟摄像头
  15. Object-C使用ICE
  16. 明明有空单元格,Ctrl+G定位空值报未找到单元格
  17. vscode打开文件方式小结
  18. JavaScript进阶(4)-dom查询
  19. 人工智能:语音识别技术介绍
  20. 计算机分栏过程,计算机基础与程序设计-要点分栏.docx

热门文章

  1. 记录一些之前学的APPUI设计知识
  2. 什么是SYN Flood?
  3. 45个有用的JavaScript技巧,窍门和最佳实践
  4. java中rank函数_SQL中的排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
  5. android控制板
  6. 《Python数据科学入门》之阅读笔记(第2章)
  7. 反射镜镀膜与波长关系
  8. H3C设备配置wifi
  9. Linux系统中用命令行清空垃圾箱Trash
  10. Java的字符串转int算法