我有一个使用 tensorflow 的 ptb example 为 ptb_word 训练的 rnn 模型。
波纹管我有一个代码,我试图在其中打印一些示例来测试训练的模型。在我制作 TypeError: 'Tensor' object is not callable
的行上运行此代码时,我收到错误 probs, state = sess.run([mtest.output_probs(), mtest._final_state], feed_dict=feed_dict)
究竟是什么导致了这个错误?
这是代码:
import numpy as np
import os
import tensorflow as tf
from ptb_word_lm import *
from tensorflow.models.rnn.ptb import reader
from tensorflow.python.platform import gfile
data_path = "/home/usr/simple-examples/data/"
raw_data = reader.ptb_raw_data(data_path)
train_data, valid_data, test_data, vocabulary = raw_data
test_path = os.path.join(data_path, "ptb.test.txt")
word_to_id = reader._build_vocab(test_path)
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)
test_input = PTBInput(config=eval_config, data=test_data, name="TestInput")
with tf.variable_scope("model", reuse=None, initializer=initializer):
mtest = PTBModel(is_training=False, config=eval_config, input_=test_input)
sess.run(tf.initialize_all_variables())
saver = tf.train.import_meta_graph('/home/usr/models/medium/model.ckpt-50979.meta')
ckpt = tf.train.get_checkpoint_state('/home/usr/models/medium/')
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
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)
for attr in mtest.__dict__:
print attr
print 'all attributes above'
while sentence_cnt < number_of_sentences:
feed_dict = {mtest._input: input_char,
mtest.initial_state: state}
probs, state = sess.run([mtest.output_probs(), mtest._final_state], feed_dict=feed_dict)
print 'after state'
sampled_char = pick_from_weight(probs[0])
print sampled_char
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 ...')
最佳答案
查看 GitHub 上的引用代码,我找不到 output_props
,因此版本可能有所不同。然而,由于 mtest.initial_state
是一个 @property
,我假设 mtest.output_props
也是一个。也就是说,尝试
probs, state = sess.run([mtest.output_probs, mtest.final_state], feed_dict=feed_dict)
相反,即不使用括号。
此外
mtest._final_state
是一个内部变量,不应直接使用。您可能想改用 mtest.final_state
。关于python - Tensorflow Session.Run() Tensor 对象不可调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42002083/