如何使用Tensorflow的PTB模型示例?
How to use Tensorflow's PTB model example?
我正在尝试 Tensorflow's rnn example。
一开始我遇到了一些问题,我可以 运行 这个例子来训练 ptb,现在我已经训练了一个模型。
我现在如何准确地使用该模型来创建句子而不必每次都重新训练?
我正在 运行 使用像 python ptb_word_lm.py --data_path=/home/data/ --model medium --save_path=/home/medium
这样的命令
有没有关于如何使用训练模型造句的例子?
1.Add PTBModel:__init__()
函数最后一行的以下代码:
self._output_probs = tf.nn.softmax(logits)
2.Add PTBModel
中的以下函数:
@property
def output_probs(self):
return self._output_probs
3.Try到运行以下代码:
raw_data = reader.ptb_raw_data(FLAGS.data_path)
train_data, valid_data, test_data, vocabulary, word_to_id, id_to_word = raw_data
eval_config = get_config()
eval_config.batch_size = 1
eval_config.num_steps = 1
sess = tf.Session()
initializer = tf.random_uniform_initializer(-eval_config.init_scale,
eval_config.init_scale)
with tf.variable_scope("model", reuse=None, initializer=initializer):
mtest = PTBModel(is_training=False, config=eval_config)
sess.run(tf.initialize_all_variables())
saver = tf.train.Saver()
ckpt = tf.train.get_checkpoint_state('/home/medium') # __YOUR__MODEL__SAVE__PATH__
if ckpt and gfile.Exists(ckpt.model_checkpoint_path):
msg = 'Reading model parameters from %s' % ckpt.model_checkpoint_path
print(msg)
saver.restore(sess, ckpt.model_checkpoint_path)
def pick_from_weight(weight, pows=1.0):
weight = weight**pows
t = np.cumsum(weight)
s = np.sum(weight)
return int(np.searchsorted(t, np.random.rand(1) * s))
while True:
number_of_sentences = 10 # generate 10 sentences one time
sentence_cnt = 0
text = '\n'
end_of_sentence_char = word_to_id['<eos>']
input_char = np.array([[end_of_sentence_char]])
state = sess.run(mtest.initial_state)
while sentence_cnt < number_of_sentences:
feed_dict = {mtest.input_data: input_char,
mtest.initial_state: state}
probs, state = sess.run([mtest.output_probs, mtest.final_state],
feed_dict=feed_dict)
sampled_char = pick_from_weight(probs[0])
if sampled_char == end_of_sentence_char:
text += '.\n'
sentence_cnt += 1
else:
text += ' ' + id_to_word[sampled_char]
input_char = np.array([[sampled_char]])
print(text)
raw_input('press any key to continue ...')
本网站对 PTB 文字脚本进行了修改,目前使用 Tensorflow 1.0 版
http://deeplearningathome.com/2016/10/Text-generation-using-deep-recurrent-neural-networks.html
我正在尝试 Tensorflow's rnn example。 一开始我遇到了一些问题,我可以 运行 这个例子来训练 ptb,现在我已经训练了一个模型。
我现在如何准确地使用该模型来创建句子而不必每次都重新训练?
我正在 运行 使用像 python ptb_word_lm.py --data_path=/home/data/ --model medium --save_path=/home/medium
有没有关于如何使用训练模型造句的例子?
1.Add PTBModel:__init__()
函数最后一行的以下代码:
self._output_probs = tf.nn.softmax(logits)
2.Add PTBModel
中的以下函数:
@property
def output_probs(self):
return self._output_probs
3.Try到运行以下代码:
raw_data = reader.ptb_raw_data(FLAGS.data_path)
train_data, valid_data, test_data, vocabulary, word_to_id, id_to_word = raw_data
eval_config = get_config()
eval_config.batch_size = 1
eval_config.num_steps = 1
sess = tf.Session()
initializer = tf.random_uniform_initializer(-eval_config.init_scale,
eval_config.init_scale)
with tf.variable_scope("model", reuse=None, initializer=initializer):
mtest = PTBModel(is_training=False, config=eval_config)
sess.run(tf.initialize_all_variables())
saver = tf.train.Saver()
ckpt = tf.train.get_checkpoint_state('/home/medium') # __YOUR__MODEL__SAVE__PATH__
if ckpt and gfile.Exists(ckpt.model_checkpoint_path):
msg = 'Reading model parameters from %s' % ckpt.model_checkpoint_path
print(msg)
saver.restore(sess, ckpt.model_checkpoint_path)
def pick_from_weight(weight, pows=1.0):
weight = weight**pows
t = np.cumsum(weight)
s = np.sum(weight)
return int(np.searchsorted(t, np.random.rand(1) * s))
while True:
number_of_sentences = 10 # generate 10 sentences one time
sentence_cnt = 0
text = '\n'
end_of_sentence_char = word_to_id['<eos>']
input_char = np.array([[end_of_sentence_char]])
state = sess.run(mtest.initial_state)
while sentence_cnt < number_of_sentences:
feed_dict = {mtest.input_data: input_char,
mtest.initial_state: state}
probs, state = sess.run([mtest.output_probs, mtest.final_state],
feed_dict=feed_dict)
sampled_char = pick_from_weight(probs[0])
if sampled_char == end_of_sentence_char:
text += '.\n'
sentence_cnt += 1
else:
text += ' ' + id_to_word[sampled_char]
input_char = np.array([[sampled_char]])
print(text)
raw_input('press any key to continue ...')
本网站对 PTB 文字脚本进行了修改,目前使用 Tensorflow 1.0 版
http://deeplearningathome.com/2016/10/Text-generation-using-deep-recurrent-neural-networks.html