- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试 Tensorflow's rnn example .开始时遇到一些问题,我可以运行该示例以训练 ptb,现在我已经训练了一个模型。
我现在如何准确地使用该模型来创建句子,而不必每次都重新训练?
我正在使用像 python ptb_word_lm.py --data_path=/home/data/--model medium --save_path=/home/medium
这样的命令运行它
是否有关于如何使用经过训练的模型造句的示例?
最佳答案
1.在PTBModel:__init__()
函数的最后一行添加如下代码:
self._output_probs = tf.nn.softmax(logits)
2.在PTBModel
中添加如下函数:
@property
def output_probs(self):
return self._output_probs
3.尝试运行以下代码:
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 ...')
关于python - 如何使用Tensorflow的PTB模型示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40786771/
如何将字符串数据(例如推文)转换为 PTB 格式以训练斯坦福情感分析工具? 最佳答案 这不是简单地从一种格式转换为另一种格式的问题。正如 @lenz 提到的,PTB 是解析器的输出格式——这意味着至少
我有一个 CoNLL-X 格式树库和每个句子对应的二叉解析树,我想将它转换成 PTB 格式。是否有任何转换器或任何人都可以阐明 PTB 格式? 最佳答案 为了将依赖关系(以 CoNLL-X 格式表示)
在 PTB 语言模型教程中 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/rnn/ptb/ptb_wor
我正在尝试使用 tensorflow LSTM model进行下一个单词预测。 如本 related question 中所述(没有可接受的答案)该示例包含用于提取下一个单词概率的伪代码: lstm
我处理 penn tree bank v2 树并经常遇到像这样的“服务”子树(以及其他几种类型) 我可以手动添加很多规则来优化我实际使用的节点(使用标签和标记进行解析,没有“哦,看那里”链接或“这里一
python :3.6.6 python-电报机器人:10.0.2 我有这个问题:我定义了一个带有 SMTPHandler、TimedRotatingFileHandler 和 StreamHandl
我是一名优秀的程序员,十分优秀!