基于RNN的自动文本生成

一.基本思路
1.假设有一段文本:the apple is red,首先对在句子中的各个字母用set进行收集,并利用index生成(index, alpha)形式的dict

2.将文本利用dict中的key进行转化,如t对应index值为1,h为2,e为3,则the可转化为1 2 3,因此文本转化为array[1 2 3 …]

3.对每一个数字进行编码转换
方式1:手动进行one-hot编码
假设len(dict)=10,则一个数字转化的vendor长度为10,如2中的1可转化为$[1,0,0…,0]^T$
方式2:利用Embedding层进行自动转换
假设将shape=(1,10)输入Embedding(a,b)层,则将输出shape=(1,10,b),即利用embedding对输入矩阵的每一个值进行向量转化

4.构建模型
(1)建立SimpleRNN层,输入shape=(array_len,dict_len),输出shape=(1,dict_len),其中 array_len 表示文本转化的array的长度,dict_len表示dict的长度,由于输入一个句子输出一个字母,因此输出值shape即一个字母的数值表示,shape=(1,dict_len)
(2)建立Dense层,设置units=dict_len,使用softmax激活函数将输出值进行压缩,并利用 categorical_crossentropy 作为loss function。即将问题转化为classification,利用全连接层进行训练,计算其与label的loss并进行反向传播调整参数,从而完成训练

参考链接:用Keras实现SimpleRNN:生成文本

Related post

  1. AOP reading notes

    2022-10-10

  2. Swift Learning Log

    2023-08-31

  3. 网址收藏夹

    2020-07-16

  4. 设计模式-工厂模式

    2020-09-09

There are no comment yet.

COMMENT

Take a Coffee Break

Recommend post

  1. 常用工具指令

    2022-09-18

Category list

ABOUT

Welcome to FullStar, a captivating online destination where the realms of software development and personal reflections intertwine.

April 2025
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
282930  

Life Logs

  1. 回首

    2023-07-14

Return Top