前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于深度学习系列笔记二(爱丽丝梦游仙境)

关于深度学习系列笔记二(爱丽丝梦游仙境)

作者头像
python与大数据分析
发布2022-03-11 13:29:11
3520
发布2022-03-11 13:29:11
举报
文章被收录于专栏:python与大数据分析

逐渐的人工智能已走入了日常生活中了,从对联生成器到古诗生成器,从智能翻译到机器写作,有时候看UC头条的新闻,感觉很多逻辑混乱的文章,有可能就是机器写作的也可能是机器翻译出来的,不管怎么样,文本写作已经成为人工智能的一个重要场景了。

在《keras深度学习实战》书籍中有关于爱丽丝梦游仙境的例子,本文不仅仅是原封不动的还原代码,也包括自己对代码的理解和总结。虽然不成熟,起码也思考了,希望下个章节能够改进一下算法,或支持一下中文写作。

代码示例

代码语言:javascript
复制
# -*- coding: utf-8 -*-
from __future__ import print_function
from keras.layers.recurrent import SimpleRNN
from keras.models import Sequential
from keras.layers import Dense, Activation
import numpy as np

#从网上获取爱丽丝梦游仙境文本
INPUT_FILE = "alice.txt"
# 抽取文件,并进行清洗
print("Extracting text from input...")
fin = open(INPUT_FILE, 'rb')
lines = []
for line in fin:
    line = line.strip().lower()
    line = line.decode("ascii", "ignore")
    if len(line) == 0:
        continue
    lines.append(line)
fin.close()
text = " ".join(lines)
print(text)
#爱丽丝梦游仙境文本格式如下:合计142773长度
#chapter i down the rabbit-hole chapter i down the rabbit-hole alice was beginning to get very tired of sitting by h
#142773

# 创建查找表
chars = set([c for c in text])
#{'h', 'u', ';', 'e', 'q', 'j', '_', '`', 'z', 'd', 'v', 'x', "'", 'n', 'k', '(', '?', 'p', 'y', 'o', ')', 'c', '*', 'r', '!', 'i', 'w', '"', 'g', 't', 's', '-', 'l', ',', 'm', ':', 'a', '.', 'f', 'b', ' '}
nb_chars = len(chars)
#41
#创建字符索引
char2index = dict((c, i) for i, c in enumerate(chars))
#{'h': 0, 'u': 1, ';': 2, 'e': 3, 'q': 4, 'j': 5, '_': 6, '`': 7, 'z': 8, 'd': 9, 'v': 10, 'x': 11, "'": 12, 'n': 13, 'k': 14, '(': 15, '?': 16, 'p': 17, 'y': 18, 'o': 19, ')': 20, 'c': 21, '*': 22, 'r': 23, '!': 24, 'i': 25, 'w': 26, '"': 27, 'g': 28, 't': 29, 's': 30, '-': 31, 'l': 32, ',': 33, 'm': 34, ':': 35, 'a': 36, '.': 37, 'f': 38, 'b': 39, ' ': 40}
index2char = dict((i, c) for i, c in enumerate(chars))
#{0: 'h', 1: 'u', 2: ';', 3: 'e', 4: 'q', 5: 'j', 6: '_', 7: '`', 8: 'z', 9: 'd', 10: 'v', 11: 'x', 12: "'", 13: 'n', 14: 'k', 15: '(', 16: '?', 17: 'p', 18: 'y', 19: 'o', 20: ')', 21: 'c', 22: '*', 23: 'r', 24: '!', 25: 'i', 26: 'w', 27: '"', 28: 'g', 29: 't', 30: 's', 31: '-', 32: 'l', 33: ',', 34: 'm', 35: ':', 36: 'a', 37: '.', 38: 'f', 39: 'b', 40: ' '}

# 创建输入和标签文本
# STEP变量给出字符数目,此处为1
# SEQLEN定义文本段,此处为10
# assuming an input text "The sky was falling", we would get the
# following sequence of input_chars and label_chars (first 5 only)
#   The sky wa -> s
#   he sky was ->
#   e sky was  -> f
#    sky was f -> a
#   sky was fa -> l
print("Creating input and label text...")
SEQLEN = 10
STEP = 1

input_chars = []
label_chars = []
for i in range(0, len(text) - SEQLEN, STEP):
    #print(text[i:i + SEQLEN],'~~~~~~~~~~',text[i + SEQLEN])
    #gging her  ~~~~~~~~~~ s
    #ging her s ~~~~~~~~~~ h
    #ing her sh ~~~~~~~~~~ a
    input_chars.append(text[i:i + SEQLEN])
    label_chars.append(text[i + SEQLEN])
#print('input_chars=',input_chars)
#n the danc
# the dance
#the dance?
#he dance?
#print('label_chars=',label_chars)
#d
#o
#w
#n
# 把输入和标签文本向量化
# RNN输入的每行对应前面展示的一个输入文本
# 输入中共有SEQLEN-10个字符
# 字典大小由nb_chars-41给定,把每个字符表示成one-hot编码的大小为nb_chars的向量
# 每行输入就是一个大小为(SEQLEN-10,nb_chars-41)的张量
# 输出标签是一个单个的字符,所以和输入的每个字符表示类似
# 输出标签表示为(nb_chars-41)的one-hot编码的向量
print("Vectorizing input and label text...")
X = np.zeros((len(input_chars), SEQLEN, nb_chars), dtype=np.bool)
# X形状为(142763, 10, 41),类型为bool,缺省值为False
y = np.zeros((len(input_chars), nb_chars), dtype=np.bool)
# y形状为(142763, 41),类型为bool,缺省值为False
#遍历每个单词
for i, input_char in enumerate(input_chars):
    #遍历每个字符
    for j, ch in enumerate(input_char):
        #print(i,input_char,j,ch)
        #138004 course-- " 4 s
        #138004 course-- " 5 e
        #138004 course-- " 6 -
        #把对应的单词,字符和索引索引置为1
        X[i, j, char2index[ch]] = 1
    #把对应的单词和字符索引位置置为1
    y[i, char2index[label_chars[i]]] = 1
    #print(i,label_chars[i],char2index[label_chars[i]])
    #50011 h 37
    #50012 i 10
#print('X=',X)
#X= [[[False False False ... False False False]
#  ...
#  [False False False ... False False  True]]
#
# [[False False False ... False False False]
#  ...
#  [False False False ... False False  True]]
# [[ True False False ... False False False]
#  ...
#  [False False  True ... False False False]]]
#print('y=',y)
#y= [[False False False ... False False False]
# [False False False ... False False False]
# [False False False ... False False False]
# ...
# [False False False ...  True False False]
# [False False  True ... False False False]
# [False False False ... False False False]]

#构建模型
HIDDEN_SIZE = 128
#BATCH_SIZE = 128
#NUM_ITERATIONS = 25
BATCH_SIZE = 128
NUM_ITERATIONS = 50
NUM_EPOCHS_PER_ITERATION = 1
NUM_PREDS_PER_EPOCH = 100

model = Sequential()
#将RNN输出维度大小定义为128
#选择值太小,模型不具有生成较好文本的有效容量,会看到重复字符或重复词组的长时运行
#选择值太大,模型参数过多,需要很多数据才能有效训练
model.add(SimpleRNN(HIDDEN_SIZE, return_sequences=False,
                    input_shape=(SEQLEN, nb_chars),
                    unroll=True))
#创建一个全连接dense层,dense层有nb_chars个单元,为字典中每个字符发出评分
model.add(Dense(nb_chars))
#全连接层激活函数是softmax,把分数标准化为概率,概率最高的字符即成为预测字符。
model.add(Activation("softmax"))
#用分类输出中出色的分类交叉熵函数作为损失函数
model.compile(loss="categorical_crossentropy", optimizer="rmsprop")

# 一共迭代25次
for iteration in range(NUM_ITERATIONS):
    print("=" * 50)
    print("Iteration #: %d" % (iteration))
    model.fit(X, y, batch_size=BATCH_SIZE, epochs=NUM_EPOCHS_PER_ITERATION)
    #model.fit(X, y, batch_size=142763, epochs=NUM_EPOCHS_PER_ITERATION)
    # 测试模型
    # 给定一个随机的输入
    test_idx = np.random.randint(len(input_chars))
    #从模型中生成一个字符
    test_chars = input_chars[test_idx]
    #test_chars = 'alice'
    #打印随机选出的种子字符
    print("Generating from seed: %s" % (test_chars))
    print('result=',test_chars, end="")
    #持续进行100次,生成并打印结果字符串,即生成100长度的字符串
    for i in range(NUM_PREDS_PER_EPOCH):
        Xtest = np.zeros((1, SEQLEN, nb_chars))
        for i, ch in enumerate(test_chars):
            Xtest[0, i, char2index[ch]] = 1
            #print('0, i, char2index[ch]]=',i,char2index[ch])
        #print(Xtest)
        pred = model.predict(Xtest, verbose=0)[0]
        ypred = index2char[np.argmax(pred)]
        print(ypred, end="")
        # move forward with test_chars + ypred
        test_chars = test_chars[1:] + ypred
    print()

#BATCH_SIZE = 10000
#NUM_ITERATIONS = 100
#test_chars=random char
# he lory, w
# ck turtle, ao  ho  he  oo  ho  he  oo  ho  he  oo  ho  he  oo  ho  he  oo  ho  he  oo  ho  he  oo  ho  he  oo
# ust begun the  ao  ae  ao  ae  ao  ae  ao  ae  ao  ae  ao  ae  ao  ae  ao  ae  ao  ae  ao  ae  ao  ae  ao  ae
#  next peep ao the  ao the  ao the  ao the  ao the  ao the  ao the  ao the  ao the  ao the  ao the  ao the  ao
# g. `and ju the  ao  he  ao  he  ao  he  ao  he  ao  he  ao  he  ao  he  ao  he  ao  he  ao  he  ao  he  ao  he
# den you ev the  ao  he  ao  ao  he  ao  ao  he  ao  ao  he  ao  ao  he  ao  ao  he  ao  ao  he  ao  ao  he  ao
# gerly, for the the the the the the the the the the the the the the the the the the the the the the the the the
#  the rabbit an the he the the the the the the the the the the the the the the the the the the the the the the
#  queer ind the  aid the sard the sard the sard the sard the sard the sard the sard the sard the sard the sard
# ; but she said the said the said the said the said the said the said the said the said the said the said the s
# y until it and the sare the sare the sare the sare the sare the sare the sare the sare the sare the sare the s
# wn the lithe sout and the sout the was ing the  are the sout the was ing the  are the sout the was ing the  ar
# and quietling the said the said the said the said the said the said the said the said the said the said the sa
# ers in the wast and the sare the sare the sare the sare the sare the sare the sare the sare the sare the sare
#  here, bill the said the sard the sar said the sard the sar said the sard the sar said the sard the sar said t
# tering ove the southe sald the sare the sare the sare the sare the sare the sare the sare the sare the sare th
#  said alice sas ing the was in the was in the was in the was in the was in the was in the was in the was in th
#  swam near and the sare the sar she cat se the was in the said the said the said the said the said the said th
# `of course for the the said the more the said the more the said the more the said the more the said the more t
#  its head the wast ou the the said the wast ou the the said the wast ou the the said the wast ou the the said
# d the moon the said the sallere the said the sallere the said the sallere the said the sallere the said the sa
# d'; and she said the said the said the said the said the said the said the said the said the said the said the
# sure whethe wast and the wast oure and the wast oure and the wast oure and the wast oure and the wast oure and
# to her usus the said the sald the said the sald the said the sald the said the sald the said the sald the said
# n to look the she her alice sad the growe the said the dore the she said the dore the she said the dore the sh
# s the stuped an the coust of at ing tore the mouthe said the more the said the more the said the more the said
# as going of the southe said the more the said the more the said the more the said the more the said the more t
#  list of she sound he sere the said the mack to the was in the was in the was in the was in the was in the was
#  poor alice sad the coust on the the harded the harded the harded the harded the harded the harded the harded
# ner!' cried an the said the mack ture said the mack ture said the mack ture said the mack ture said the mack t
#  be shutting tore the mouthe said the mack turele the was the was the was the was the was the was the was the
# her, about the growe the the grithe the grithe the grithe the grithe the grithe the grithe the grithe the grit
# y over alice sad the d at she sad she don the d at she sad she don the d at she sad she don the d at she sad s
# self, `if the said the donge for and the donge for and the donge for and the donge for and the donge for and t
# me that fire the mast the said the mack turele the said the mack turele the said the mack turele the said the
# ome, there the said the dore the she the donge said the dore the she the donge said the dore the she the donge
#  as an unuthe sad the dored so she was so the king the was so the king the was so the king the was so the king
# ' `so they wish as all to the marte the marte the marte the marte the marte the marte the marte the marte the
# ke to piece to the rither all the dong the said the dore the mouthe the said the dore the mouthe the said the
# f i shall the said the harded and alice said the harded and alice said the harded and alice said the harded an
# ell, at and the donge said the donge said the donge said the donge said the donge said the donge said the dong
# e queen! the wast of the harded and the growe the growe the growe the growe the growe the growe the growe the
#  reason of the said the mack turele the was ous the was ous the was ous the was ous the was ous the was ous th
# d still whe the har she was the way her alice sad the cares in the harded the har her all the was of the har s
# le. i wond and the was of the said the could and the was of the said the could and the was of the said the cou
# ' said alice said the mouthe said the mouthe said the mouthe said the mouthe said the mouthe said the mouthe s
# th, and it and the wast of the harded and the moust of the harded and the moust of the harded and the moust of
# ny rate, the was of the mous the mous the mous the mous the mous the mous the mous the mous the mous the mous
#  cheered, and the growe the growe the growe the growe the growe the growe the growe the growe the growe the gr
# g merrily an the could be the said the dored she had the dored she had the dored she had the dored she had the
# uld go thry har said the cat out the was of the had and the gat has look to the donde say the way har alice sa
# d the grean the could the could the could the could the could the could the could the could the could the coul
# thought alice said the gryphon inge the gring the said the gryphon inge the gring the said the gryphon inge th
# und an oppeat it on the could be the could be the could be the could be the could be the could be the could be
# as in the said to the rabbet it all the was of the said to the rabbet it all the was of the said to the rabbet
# shock of be the said the donge said the donge said the donge said the donge said the donge said the donge said
# phon. `we the there the mast the cate the mouse fore the madd the mack to the rabbet at it and the wast of the
# hness?' `realice and all as it a the har her alice all at it a the har her alice all at it a the har her alice
# nk it was, the came the marte to the dored and the dore the mores in the said to the dored and the dore the mo
#  no time she had all the harded and the growe the hat her the harded and the growe the hat her the harded and
# n minutes the was of the said the mack to the rouse she was of the said the mack to the rouse she was of the s
# d ordered. `in the say said to the donge wat on the madde was a ling on the said the mack to the roush she was
#  the cook the marth the mart the mad the mack turtle the dond on the donge the gring the was of the dorse the
# ou are all the mouth and the doon the mad the mouse sa done with the king the mouse sa done with the king the
# sidering her alice the mouse said the mack turtle the was of the moust on the was of the moust on the was of t
# e of the donge wat the way so the don't and the doon the way so the don't and the doon the way so the don't an
#  thought alice, `in's a down the gat hourd and the gryphon it of chast her was of the harded to her all the wa
#  it tricksen the gat hourden the was of the could be the cat out the was of the could be the cat out the was o
# read-knifer the said the dores it was the dores it was the dores it was the dores it was the dores it was the
#  way up as it as and the say the was erought the harte for the said the dore the mores it a could her to the d
# `but i mustle to the dorse the cat of the sare the could and the dorse the cat of the sare the could and the d
# tle girl of the soof hing to see the hard the could be the could be the could be the could be the could be the
# ed at it grown the was of the said the doon the marther said the doon the marther said the doon the marther sa
# rot away quite and all as and the catere the could be the catere the could be the catere the could be the cate
# vered his mouse be the say the way or the could and the soof anowe to the rouse said to herself, `i don't the
# t by the erought the harter the har she sad the dore the could bet mo the har her the har she sad the dore the
# by producing the dorse for the was of the sare the dorse for the was of the sare the dorse for the was of the
#  at the nuther a done said the dock turtle the harded the harded the harded the harded the harded the harded t
# nother snat it all the catt at the say said the mack to the rabbet mast the cat of the could and the say said
# trial's be the say the was of the mores it said the dorme the har she was not leat do she her for the was the
# d see, whe the king to the dore the could be the catt reat her the harde for the mack to the dore the could be
# nocking,' said the could be the said the could be the said the could be the said the could be the said the cou
# ok his head the could at and the say the was the was the was the was the was the was the was the was the was t
# ll you, you know she last down the say so for and the say so for and the say so for and the say so for and the
# their curly had for the harded to the dorme the harded to the dorme the harded to the dorme the harded to the
# l whether said to herself to the dore of the marth urse the marte said to herself to the dore of the marth urs
# y wrote do goon the mouse betone of the could be the cat of and the king to the king to the king to the king t
# nt to stay she was not mean she said the gryphon the say she was not mean she said the gryphon the say she was
#  more puzzled the mouse so gat in a the said the mouse so gat in a the said the mouse so gat in a the said the
# d better not of the some to the say she was a look an the could all was the harter and her alice the gat here
# low with it the dormouse the could be the said the dormouse the could be the said the dormouse the could be th
# ily on slack of the mouse say the gat it and the dores it was the door as the could an the came the marth a do
# fter them!' said the cat of the har she said the cat of the har she said the cat of the har she said the cat o
# nd she, oh the the could all whing to she cat of chart which was the could all whing to she cat of chart which
# t, being mo ding the mouse of the mouse of the mouse of the mouse of the mouse of the mouse of the mouse of th
#  much conter of the soof a atile the parce to the dormouse the could at the could at the could at the could at
# ing up in a long the mares it was the was the mack turtle the sabe to the mares it was the was the mack turtle
# y it ran and whing to she said the mouse beto doon the marth a dong on the sare the could be the say the way t
# with one extly soon the marth a mone that it was the mart the marth a mone that it was the mart the marth a mo
# han't! you know the mouse sad the was the growh her her had for the harde was the growh her her had for the ha

#BATCH_SIZE = 128
#NUM_ITERATIONS = 50
#test_chars=random char
#id alice; `ind he wall the wall the wall the wall the wall the wall the wall the wall the wall the wall the wa
#at all. `be the was the was the was the was the was the was the was the was the was the was the was the was th
# on each so the rould all the tore the rould all the tore the rould all the tore the rould all the tore the ro
#id so, and was in the had her said the mouthe to the say her same the mouthe to the say her same the mouthe to
#hapter v and it a a mance said the gryphon in a and the sait the mack the gryphon in a and the sait the mack t
#ant to go see the mock turtle sear the rabbed and the satter a marce the was she was not in the was she was no
#the hatter the read the gryphon said the gryphon said the gryphon said the gryphon said the gryphon said the g
#t, jumping the dore with the gryphon a little so mack to the gryphon a little so mack to the gryphon a little
#, that alice a don't be a little she was nother the rabbit was the ery, and the mouse was she said the caterpi
#ously replied in a little and the sore of the sore of the sore of the sore of the sore of the sore of the sore
#ked, with her head a soup of a little got the dormouse for a little got the dormouse for a little got the dorm
#could, if a to har she said the mock turtle said to the gryphon, and the mock turtle said to the gryphon, and
#ad said the mors the dormouse the mors the dormouse the mors the dormouse the mors the dormouse the mors the d
#ossed the rabbit haster a with the rabbit haster a with the rabbit haster a with the rabbit haster a with the
#seven jogg not in a was not in a was not in a was not in a was not in a was not in a was not in a was not in a
#o my boy, and the mock turtle some of the errout for the mock turtle some of the errout for the mock turtle so
#l, it must be a mame the hatter was a look a marter the rabbit and the mouse the mouse the mouse the mouse the
#ed alice. `it's a little she was the read the round. `they were the erreat of the words all reading her all th
#t about it was not in the rabbit was the rabbit was the rabbit was the rabbit was the rabbit was the rabbit wa
#their face in the look the looked at the mock turtle the last the tone, and the dormouse the dormouse the dorm
# `i know what it was a great one on the way in a very the jury and the white rabbit and said to the plose to t
# direction in the dormouse don't be a little be the cat on the court to be a marking that the dormouse don't b
#d of mine--on it must be off the right in a very some of the dormouse the mock turtle in a little she went on
#g,' said the caterpillar the caterpillar the caterpillar the caterpillar the caterpillar the caterpillar the c
#te much accuritule the way the wase that she was no door of the hatter with the hatter with the hatter with th
# like it, and she was to see of the long and alice a can in a little shrilking about the sout very good to the
#nt,' and she seant which she her fand him all the rabbit here all the stood here with a don't be a marked and
#into the cook and the words with the rabbit alice to the room, and they were the mock turtle and the words wit
#deal frightened to her hands, and was not a mant with the white rabbit some the hatter with the white rabbit s
# memory, and she said to herself in a mort to be a little sharing down and great such a can't the room, and sh
# the duchess were like the poor as the door as the door as the door as the door as the door as the door as the
#with. alice thought the dormouse the door the rabbit was of the table. `now it was a little that the rabbit wa
#ls in its down to herself to her hands and was very croquetting in the wase that her eyes a little got to the
#tone. `praided to here and the words with the gryphon, and the words with the gryphon, and the words with the
#nly know her lifting a tone on the end of the tires in the right all the right all the right all the right all
#y it too: she was not as she said to herself, `i don't be a little sharppean wall be trees would be a minute o
#le for it, i don't be a minute on the door as mouth a down the commoute of the tame off and the cook again, an
# very sorrout for a great had the rabbit has hearts in a large all have had not a large all have had not a lar
#ds; `so not to the door a looking and shouted the door a looking and shouted the door a looking and shouted th
#igently to go and the gryphon ander with the gryphon ander with the gryphon ander with the gryphon ander with
#rink me' beauting of the sort as she car and the white rabbit had been that she had not the bolkn't be so mark
#know all the king out of the stalles were said to herself, `on the story, and she had for the hatter, and the
#get into the words of the mouse they were all round a dear face and the caterpillar. `i'm to the roots and she
#se?' said the door as she spoke that the looked at the things it was a door what you have to the tame the door
#ar off to the things it was a little shrough of the were to the right and she seaning the white rabbit asked i
#ed very poor she little got to the first to be a marted to the footman the mouse to the mock turtle said, `it
# anything the gryphon with a she was sore and she was sore and she was sore and she was sore and she was sore
#y, it fillow the king, and the white rabbit had been to herself, and the white rabbit had been to herself, and
# herself had and the dormouse in a very curious to see hor she was some of the bolkn't me sire of the gryphon,
#nches high it was the dormouse in the mock turtle in a low the least to the mock turtle in a low the least to
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-04-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 python与大数据分析 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码示例
相关产品与服务
机器翻译
机器翻译(Tencent Machine Translation,TMT)结合了神经机器翻译和统计机器翻译的优点,从大规模双语语料库自动学习翻译知识,实现从源语言文本到目标语言文本的自动翻译,目前可支持十余种语言的互译。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档