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

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

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

关于RNN:

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

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

训练:

import os
import collections
import numpyas np
import tensorflowas tf
from tensorflow.python.opsimport rnn_cell
from tensorflow.python.opsimport seq2seq
import time
 
#-------------------------------数据预处理---------------------------#
 
poetry_file ='poetry.txt'
 
# 诗集
poetrys = []
with open(poetry_file, "r", encoding='utf-8',) as f:
for linein f:
try:
title, content = line.strip().split(':')
content = content.replace(' ','')
if '_' in contentor '(' in contentor '(' in contentor '《' in contentor '[' 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 poetryin poetrys:
all_words += [wordfor wordin 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 poetryin 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 rowin 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 = rnn_cell.BasicRNNCell
elif model == 'gru':
cell_fun = rnn_cell.GRUCell
elif model == 'lstm':
cell_fun = rnn_cell.BasicLSTMCell
 
cell = cell_fun(rnn_size, state_is_tuple=True)
cell = 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 = 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 epochin range(50):
sess.run(tf.assign(learning_rate, 0.002 * (0.97 ** epoch)))
n = 0
for batchein 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()

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

import os
import collections
import numpyas np
import tensorflowas tf
from tensorflow.python.opsimport rnn_cell
from tensorflow.python.opsimport seq2seq
import time
 
#-------------------------------数据预处理---------------------------#
 
poetry_file ='poetry.txt'
 
# 诗集
poetrys = []
with open(poetry_file, "r", encoding='utf-8',) as f:
for linein f:
try:
title, content = line.strip().split(':')
content = content.replace(' ','')
if '_' in contentor '(' in contentor '(' in contentor '《' in contentor '[' 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 poetryin poetrys:
all_words += [wordfor wordin 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 poetryin 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 = 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 rowin 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 = rnn_cell.BasicRNNCell
elif model == 'gru':
cell_fun = rnn_cell.GRUCell
elif model == 'lstm':
cell_fun = rnn_cell.BasicLSTMCell
 
cell = cell_fun(rnn_size, state_is_tuple=True)
cell = 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())

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

新犬随风起,一璃迹阵悲。
浅昏罢庄哉,清插去园空。
双叶坐成鉴,王妓水正苑。
鸟声不成影,胙滩朱瓮声。
无斑红芜踏,那期日正闲。
吾燕登无士,无处得赵名。

生成藏头诗:

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-7')
 
state_ = sess.run(cell.zero_state(1, tf.float32))
poem = ''
i = 0
for wordin 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('一二三四'))

Share the post "TensorFlow练习7: 基于RNN生成古诗词"

TensorFlow练习7: 基于RNN生成古诗词的更多相关文章

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

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

  2. 基于TensorFlow的循环神经网络(RNN)

    RNN适用场景 循环神经网络(Recurrent Neural Network)适合处理和预测时序数据 RNN的特点 RNN的隐藏层之间的节点是有连接的,他的输入是输入层的输出向量.extend(上一 ...

  3. “你什么意思”之基于RNN的语义槽填充(Pytorch实现)

    1. 概况 1.1 任务 口语理解(Spoken Language Understanding, SLU)作为语音识别与自然语言处理之间的一个新兴领域,其目的是为了让计算机从用户的讲话中理解他们的意图 ...

  4. 学习Tensorflow的LSTM的RNN例子

    学习Tensorflow的LSTM的RNN例子 基于TensorFlow一次简单的RNN实现 极客学院-递归神经网络 如何使用TensorFlow构建.训练和改进循环神经网络

  5. 深度学习实战篇-基于RNN的中文分词探索

    深度学习实战篇-基于RNN的中文分词探索 近年来,深度学习在人工智能的多个领域取得了显著成绩.微软使用的152层深度神经网络在ImageNet的比赛上斩获多项第一,同时在图像识别中超过了人类的识别水平 ...

  6. Linux下c基于openssl生成MD5的函数

    Linux下openssl提供了一系列哈希及加密的函数,如果调用openssl提供的MD5函数生成任意字符串的MD5呢?下面提供了一段代码实现Linux下c字符串生成md5的函数. 具体代码: 1 2 ...

  7. 基于RNN的音频降噪算法 (附完整C代码)

    前几天无意间看到一个项目rnnoise. 项目地址: https://github.com/xiph/rnnoise 基于RNN的音频降噪算法. 采用的是 GRU/LSTM 模型. 阅读下训练代码,可 ...

  8. kbmMW基于硬件生成随机数

    按作者的说法,Delphi提供的生成随机数不是真正随机的,因为他是根据种子计算的,即种子+算法生成的随机数,如果被人知道原始种子值和算法的调用次数,则可以重现随机数,因此在安全领域,这是不安全的.同时 ...

  9. 根据wsdl,基于wsimport生成代码的客户端

    根据wsdl,基于wsimport生成代码的客户端 wsimport是jdk自带的命令,可以根据wsdl文档生成客户端中间代码,基于生成的代码编写客户端,可以省很多麻烦. 局限性:wsimport   ...

随机推荐

  1. git 撤销本地修改

    git checkout file 例如:git checkout app/views/carts/_index_m.html.erb 可以先用 git status 查看差异 然后 git chec ...

  2. CentOS 7 yum 安装subversion

    yum list installed | grep subversionyum install subversioncd /var/wwwsvn -hsvn co svn://121.196.226. ...

  3. tomcat配置问题:访问http://localhost:8080/ 遇到 Access Error: 404

    win7: 8080端口已经被其他应用使用,比如nixxxxxxxxxxxxx When I had an error Access Error: 404 -- Not Found I fixed i ...

  4. Codeforces Round #371 (Div. 2) A. Meeting of Old Friends 水题

    A. Meeting of Old Friends 题目连接: http://codeforces.com/contest/714/problem/A Description Today an out ...

  5. Windows 10官方原版ISO制作方法

    其实市面上的ISO原版都是这样的方法制作成光盘,然后再打包出来供人们下载的. 1.下载Windows 10安装程序工具: https://www.microsoft.com/zh-cn/softwar ...

  6. 设置java.library.path的值(Mac/Linux/Windows)

    说明:网上基本针对这个值的设置分为两面,Windows派和Linux派,Windows的不说,Linux下只会说设置LD_LIBRARY_PATH即可,但这种方式在Java 8是一个错误的设置,尤其是 ...

  7. 【来龙去脉系列】深入理解DIP、IoC、DI以及IoC容器

    摘要 面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.其中,OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.DI以及Ioc容器等概念.通过本文我们将一起学 ...

  8. [vs2013]远程服务器调试

    摘要 有时遇到比较奇葩的问题,比如本地程序正常运行,在服务器上不可以,如果日志也不起作用,那么远程调试就非常必要了,在服务器上安装vs,是比较费力费时的. 步骤 1.找到vs安装目录,一般默认安装在c ...

  9. Xamarin.iOS,AOT,JIT,Limitations

    Since applications on the iPhone using Xamarin.iOS are compiled to static code, it is not possible t ...

  10. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(3)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...