转自:http://blog.topspeedsnail.com/archives/10542

RNN不像传统的神经网络-它们的输出输出是固定的,而RNN允许我们输入输出向量序列。RNN是为了对序列数据进行建模而产生的。

样本序列性:样本间存在顺序关系,每个样本和它之前的样本存在关联。比如说,在文本中,一个词和它前面的词是有关联的;在气象数据中,一天的气温和前几天的气温是有关联的。

例如本帖要使用RNN生成古诗,你给它输入一堆古诗词,它会学着生成和前面相关联的字词。如果你给它输入一堆姓名,它会学着生成姓名;给它输入一堆古典乐/歌词,它会学着生成古典乐/歌词,甚至可以给它输入源代码。

关于RNN:

  • TensorFlow练习3: RNN, Recurrent Neural Networks
  • http://karpathy.github.io/2015/05/21/rnn-effectiveness/

本帖代码移植自char-rnn,它是基于Torch的洋文模型,稍加修改即可应用于中文。char-rnn使用文本文件做为输入、训练RNN模型,然后使用它生成和训练数据类似的文本。

使用的数据集:全唐诗(43030首):https://pan.baidu.com/s/1o7QlUhO

训练:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

import collections

import numpy as np

import tensorflow as tf

#-------------------------------数据预处理---------------------------#

poetry_file ='poetry.txt'

# 诗集

poetrys = []

with open(poetry_file, "r", encoding='utf-8',) as f:

for line in f:

try:

title, content = line.strip().split(':')

content = content.replace(' ','')

if '_' in content or '(' in content or '(' in content or '《' in content or '[' in content:

continue

if len(content) < 5 or len(content) > 79:

continue

content = '[' + content + ']'

poetrys.append(content)

except Exception as e:

pass

# 按诗的字数排序

poetrys = sorted(poetrys,key=lambda line: len(line))

print('唐诗总数: ', len(poetrys))

# 统计每个字出现次数

all_words = []

for poetry in poetrys:

all_words += [word for word in poetry]

counter = collections.Counter(all_words)

count_pairs = sorted(counter.items(), key=lambda x: -x[1])

words, _ = zip(*count_pairs)

# 取前多少个常用字

words = words[:len(words)] + (' ',)

# 每个字映射为一个数字ID

word_num_map = dict(zip(words, range(len(words))))

# 把诗转换为向量形式,参考TensorFlow练习1

to_num = lambda word: word_num_map.get(word, len(words))

poetrys_vector = [ list(map(to_num, poetry)) for poetry in poetrys]

#[[314, 3199, 367, 1556, 26, 179, 680, 0, 3199, 41, 506, 40, 151, 4, 98, 1],

#[339, 3, 133, 31, 302, 653, 512, 0, 37, 148, 294, 25, 54, 833, 3, 1, 965, 1315, 377, 1700, 562, 21, 37, 0, 2, 1253, 21, 36, 264, 877, 809, 1]

#....]

# 每次取64首诗进行训练

batch_size = 64

n_chunk = len(poetrys_vector) // batch_size

x_batches = []

y_batches = []

for i in range(n_chunk):

start_index = i * batch_size

end_index = start_index + batch_size

batches = poetrys_vector[start_index:end_index]

length = max(map(len,batches))

xdata = np.full((batch_size,length), word_num_map[' '], np.int32)

for row in range(batch_size):

xdata[row,:len(batches[row])] = batches[row]

ydata = np.copy(xdata)

ydata[:,:-1] = xdata[:,1:]

"""

xdata             ydata

[6,2,4,6,9]       [2,4,6,9,9]

[1,4,2,8,5]       [4,2,8,5,5]

"""

x_batches.append(xdata)

y_batches.append(ydata)

#---------------------------------------RNN--------------------------------------#

input_data = tf.placeholder(tf.int32, [batch_size, None])

output_targets = tf.placeholder(tf.int32, [batch_size, None])

# 定义RNN

def neural_network(model='lstm', rnn_size=128, num_layers=2):

if model == 'rnn':

cell_fun = tf.nn.rnn_cell.BasicRNNCell

elif model == 'gru':

cell_fun = tf.nn.rnn_cell.GRUCell

elif model == 'lstm':

cell_fun = tf.nn.rnn_cell.BasicLSTMCell

cell = cell_fun(rnn_size, state_is_tuple=True)

cell = tf.nn.rnn_cell.MultiRNNCell([cell] * num_layers, state_is_tuple=True)

initial_state = cell.zero_state(batch_size, tf.float32)

with tf.variable_scope('rnnlm'):

softmax_w = tf.get_variable("softmax_w", [rnn_size, len(words)+1])

softmax_b = tf.get_variable("softmax_b", [len(words)+1])

with tf.device("/cpu:0"):

embedding = tf.get_variable("embedding", [len(words)+1, rnn_size])

inputs = tf.nn.embedding_lookup(embedding, input_data)

outputs, last_state = tf.nn.dynamic_rnn(cell, inputs, initial_state=initial_state, scope='rnnlm')

output = tf.reshape(outputs,[-1, rnn_size])

logits = tf.matmul(output, softmax_w) + softmax_b

probs = tf.nn.softmax(logits)

return logits, last_state, probs, cell, initial_state

#训练

def train_neural_network():

logits, last_state, _, _, _ = neural_network()

targets = tf.reshape(output_targets, [-1])

loss = tf.nn.seq2seq.sequence_loss_by_example([logits], [targets], [tf.ones_like(targets, dtype=tf.float32)], len(words))

cost = tf.reduce_mean(loss)

learning_rate = tf.Variable(0.0, trainable=False)

tvars = tf.trainable_variables()

grads, _ = tf.clip_by_global_norm(tf.gradients(cost, tvars), 5)

optimizer = tf.train.AdamOptimizer(learning_rate)

train_op = optimizer.apply_gradients(zip(grads, tvars))

with tf.Session() as sess:

sess.run(tf.initialize_all_variables())

saver = tf.train.Saver(tf.all_variables())

for epoch in range(50):

sess.run(tf.assign(learning_rate, 0.002 * (0.97 ** epoch)))

n = 0

for batche in range(n_chunk):

train_loss, _ , _ = sess.run([cost, last_state, train_op], feed_dict={input_data: x_batches[n], output_targets: y_batches[n]})

n += 1

print(epoch, batche, train_loss)

if epoch % 7 == 0:

saver.save(sess, 'poetry.module', global_step=epoch)

train_neural_network()

使用训练好的模型生成古诗:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

import collections

import numpy as np

import tensorflow as tf

#-------------------------------数据预处理---------------------------#

poetry_file ='poetry.txt'

# 诗集

poetrys = []

with open(poetry_file, "r", encoding='utf-8',) as f:

for line in f:

try:

title, content = line.strip().split(':')

content = content.replace(' ','')

if '_' in content or '(' in content or '(' in content or '《' in content or '[' in content:

continue

if len(content) < 5 or len(content) > 79:

continue

content = '[' + content + ']'

poetrys.append(content)

except Exception as e:

pass

# 按诗的字数排序

poetrys = sorted(poetrys,key=lambda line: len(line))

print('唐诗总数: ', len(poetrys))

# 统计每个字出现次数

all_words = []

for poetry in poetrys:

all_words += [word for word in poetry]

counter = collections.Counter(all_words)

count_pairs = sorted(counter.items(), key=lambda x: -x[1])

words, _ = zip(*count_pairs)

# 取前多少个常用字

words = words[:len(words)] + (' ',)

# 每个字映射为一个数字ID

word_num_map = dict(zip(words, range(len(words))))

# 把诗转换为向量形式,参考TensorFlow练习1

to_num = lambda word: word_num_map.get(word, len(words))

poetrys_vector = [ list(map(to_num, poetry)) for poetry in poetrys]

#[[314, 3199, 367, 1556, 26, 179, 680, 0, 3199, 41, 506, 40, 151, 4, 98, 1],

#[339, 3, 133, 31, 302, 653, 512, 0, 37, 148, 294, 25, 54, 833, 3, 1, 965, 1315, 377, 1700, 562, 21, 37, 0, 2, 1253, 21, 36, 264, 877, 809, 1]

#....]

batch_size = 1

n_chunk = len(poetrys_vector) // batch_size

x_batches = []

y_batches = []

for i in range(n_chunk):

start_index = i * batch_size

end_index = start_index + batch_size

batches = poetrys_vector[start_index:end_index]

length = max(map(len,batches))

xdata = np.full((batch_size,length), word_num_map[' '], np.int32)

for row in range(batch_size):

xdata[row,:len(batches[row])] = batches[row]

ydata = np.copy(xdata)

ydata[:,:-1] = xdata[:,1:]

"""

xdata             ydata

[6,2,4,6,9]       [2,4,6,9,9]

[1,4,2,8,5]       [4,2,8,5,5]

"""

x_batches.append(xdata)

y_batches.append(ydata)

#---------------------------------------RNN--------------------------------------#

input_data = tf.placeholder(tf.int32, [batch_size, None])

output_targets = tf.placeholder(tf.int32, [batch_size, None])

# 定义RNN

def neural_network(model='lstm', rnn_size=128, num_layers=2):

if model == 'rnn':

cell_fun = tf.nn.rnn_cell.BasicRNNCell

elif model == 'gru':

cell_fun = tf.nn.rnn_cell.GRUCell

elif model == 'lstm':

cell_fun = tf.nn.rnn_cell.BasicLSTMCell

cell = cell_fun(rnn_size, state_is_tuple=True)

cell = tf.nn.rnn_cell.MultiRNNCell([cell] * num_layers, state_is_tuple=True)

initial_state = cell.zero_state(batch_size, tf.float32)

with tf.variable_scope('rnnlm'):

softmax_w = tf.get_variable("softmax_w", [rnn_size, len(words)+1])

softmax_b = tf.get_variable("softmax_b", [len(words)+1])

with tf.device("/cpu:0"):

embedding = tf.get_variable("embedding", [len(words)+1, rnn_size])

inputs = tf.nn.embedding_lookup(embedding, input_data)

outputs, last_state = tf.nn.dynamic_rnn(cell, inputs, initial_state=initial_state, scope='rnnlm')

output = tf.reshape(outputs,[-1, rnn_size])

logits = tf.matmul(output, softmax_w) + softmax_b

probs = tf.nn.softmax(logits)

return logits, last_state, probs, cell, initial_state

#-------------------------------生成古诗---------------------------------#

# 使用训练完成的模型

def gen_poetry():

def to_word(weights):

t = np.cumsum(weights)

s = np.sum(weights)

sample = int(np.searchsorted(t, np.random.rand(1)*s))

return words[sample]

_, last_state, probs, cell, initial_state = neural_network()

with tf.Session() as sess:

sess.run(tf.initialize_all_variables())

saver = tf.train.Saver(tf.all_variables())

saver.restore(sess, 'poetry.module-49')

state_ = sess.run(cell.zero_state(1, tf.float32))

x = np.array([list(map(word_num_map.get, '['))])

[probs_, state_] = sess.run([probs, last_state], feed_dict={input_data: x, initial_state: state_})

word = to_word(probs_)

#word = words[np.argmax(probs_)]

poem = ''

while word != ']':

poem += word

x = np.zeros((1,1))

x[0,0] = word_num_map[word]

[probs_, state_] = sess.run([probs, last_state], feed_dict={input_data: x, initial_state: state_})

word = to_word(probs_)

#word = words[np.argmax(probs_)]

return poem

print(gen_poetry())

生成的“诗词”(至少格式对上了):

1

2

3

4

5

6

7

8

9

10

11

12

13

新犬随风起,一璃迹阵悲。

浅昏罢庄哉,清插去园空。

双叶坐成鉴,王妓水正苑。

鸟声不成影,胙滩朱瓮声。

无斑红芜踏,那期日正闲。

吾燕登无士,无处得赵名。

并灭图微蒿,淮头水十荔。

晴花尚乘望,官宽留可求。

最忆青州守,英仍临阳峰。

生人隔天道,在国思山田。

登临闭石土,阵下一欢娱。

林暝今又少,孙频唯在愁。

生成藏头诗:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

def gen_poetry_with_head(head):

def to_word(weights):

t = np.cumsum(weights)

s = np.sum(weights)

sample = int(np.searchsorted(t, np.random.rand(1)*s))

return words[sample]

_, last_state, probs, cell, initial_state = neural_network()

with tf.Session() as sess:

sess.run(tf.initialize_all_variables())

saver = tf.train.Saver(tf.all_variables())

saver.restore(sess, 'poetry.module-49')

state_ = sess.run(cell.zero_state(1, tf.float32))

poem = ''

i = 0

for word in head:

while word != ',' and word != '。':

poem += word

x = np.array([list(map(word_num_map.get, word))])

[probs_, state_] = sess.run([probs, last_state], feed_dict={input_data: x, initial_state: state_})

word = to_word(probs_)

time.sleep(1)

if i % 2 == 0:

poem += ','

else:

poem += '。'

i += 1

return poem

print(gen_poetry_with_head('一二三四'))

上面使用的TensroFlow版本为0.11,0.12版本貌似不能直接运行,简单修改如下代码:

读取模型的方法:

1

2

3

module_file = tf.train.latest_checkpoint('.')

#print(module_file)

saver.restore(sess, module_file)

tf.initialize_all_variables()  deprecated,使用tf.global_variables_initializer()替代。

  • https://github.com/ryankiros/neural-storyteller(根据图片生成故事)

基于RNN生成古诗词相关推荐

  1. TensorFlow练习7: 基于RNN生成古诗词

    RNN不像传统的神经网络-它们的输出输出是固定的,而RNN允许我们输入输出向量序列.RNN是为了对序列数据进行建模而产生的. 样本序列性:样本间存在顺序关系,每个样本和它之前的样本存在关联.比如说,在 ...

  2. java rnn生成古诗_Tensorflow 基于RNN生成古诗词 自己的实践

    在网上看到一篇利用Tensorflow+RNN模型生成古诗词的练习,觉得挺有意思的便自己来试了下,算是熟悉下Tensorflow+NLP的基本操作流程 首先pip 安装NLTK 在你的Tensorfl ...

  3. python输出古诗词_TensorFlow:基于RNN生成古诗词

    时间:2017-07-26 发布人:SHX 浏览次数:3806 评论:0 实例来源于网络,但是以前的代码都是基于TensorFlow1.0以前的版本写的,实际运行时会报错,对于小白来说是一头雾水.这里 ...

  4. TensorFlow2学习:RNN生成古诗词

    本文转自 AI科技大本营 TensorFlow2学习:RNN生成古诗词 文章不见了可以参考这位博主的文章 公众号的文章写得挺好的,这里简单介绍下思路及值得学习的地方 模型简介 模型不算多么复杂高大上, ...

  5. 基于RNN实现古诗词生成模型

    我们知道,RNN(循环神经网络)模型是基于当前的状态和当前的输入来对下一时刻做出预判.而LSTM(长短时记忆网络)模型则可以记忆距离当前位置较远的上下文信息. 在此,我们根据上述预判模型来进行 古诗词 ...

  6. TensorFlow教程使用RNN生成唐诗

    本教程转载至:TensorFlow练习7: 基于RNN生成古诗词 使用的数据集是全唐诗,首先提供一下数据集的下载链接:https://pan.baidu.com/s/13pNWfffr5HSN79WN ...

  7. Tensorflow:基于LSTM生成藏头诗

    Tensorflow:基于LSTM生成藏头诗 最近在学习TensorFlow,学习到了RNN这一块,相关的资料不是很多,了解到使用RNN可以生成藏头诗之后,我就决定拿这个下手啦! 本文不介绍RNN以及 ...

  8. 基于RNN的中文古诗词生成神经网络实现

    一.前言 1.图解RNN 2.Tensorflow中RNN实现的正确打开方式 二.实战 1.训练数据处理 (1)文字转为向量 def _get_poetry(self):with open(self. ...

  9. java rnn生成古诗_Tensorflow:基于LSTM轻松生成各种古诗

    原标题:Tensorflow:基于LSTM轻松生成各种古诗 本文代码在公众号 datadw 里 回复古诗即可获取. RNN不像传统的神经网络-它们的输出输出是固定的,而RNN允许我们输入输出向量序列. ...

  10. 基于RNN的文本生成算法的代码运转

    目录(?)[+] "什么时候能自动生成博客?" 前言 跳过废话,直接看正文 RNN相对于传统的神经网络来说对于把握上下文之间的关系更为擅长,因此现在被大量用在自然语言处理的相关任务 ...

最新文章

  1. vue打包后element-ui部分样式(图标)异常问题
  2. java创建node类型数据类型_[Java教程]js DOM Node类型
  3. 【数据结构与算法】之深入解析“二叉树的序列化与反序列化”的求解思路与算法示例
  4. java mqtt客户端_java 实现mqtt发送和接收消息客户端具体用法及测试代码
  5. 51单片机扩展io口实验c语言,【51单片机】普通I/O口模拟SPI口C语言程序
  6. 《团队作业第三、第四周》五小福团队作业--Scrum 冲刺阶段--Day6
  7. 13 款高逼格且实用的 Linux 运维必备工具
  8. 力扣93. 复原 IP 地址(JavaScript)
  9. 晶振两端的谐振电容有特殊要求吗_“吃瓜群众”也能秒懂的晶振电路原理
  10. 第四范式完成超10亿元C轮融资,估值12亿美元
  11. 凑算式——第七届蓝桥杯C语言B组(省赛)第三题
  12. Linux Shell脚本文本三剑客之AWK
  13. html修改全局字体,最好CSS全局字体写法
  14. 【云和恩墨大讲堂】视频课程震撼来袭-SQL优化精选案例
  15. php 将汉字转为拼音,PHP汉字转换为拼音字头原理
  16. 数值微分25 - Poisson 泊松方程(一维、二维):已知一些自定义的二阶导数点,得到一条曲线,即原方程平滑地通过它们)
  17. aur打包linux微信,ArchLinux微信、TIM安装与乱码解决方法
  18. IT牛们的bat面试心得与经验总结
  19. 美格信-理解串扰Crosstalk
  20. 用计算机录音并播放教学设计,八年级信息技术《录制声音》说课稿

热门文章

  1. 【Java基础】Java网络编程基础
  2. 图像处理珍藏版本汇总
  3. matlab一个m文件定义多个函数,matlab怎么在一个m文件中写多个函数?
  4. excel空值排查快捷键
  5. DCDC开关电源的阶跃响应和动态响应(Load Transient)的区别
  6. 吉他入门教程之如何练习演奏技巧—即兴演奏(下)
  7. 使用函数求余弦函数的近似值
  8. 今天把中国建设银行APP4.2.1版iOS客户端里所有的功能都点了一遍
  9. windows怎样运行wmi服务器,windows系统wmi服务器
  10. isilon 时间设置