Tensorflow Session.Run()Tensor对象不可调用

时间:2017-02-02 12:17:55

标签: python tensorflow neural-network deep-learning recurrent-neural-network

我有一个针对ptb example和张量流ptb_word训练的rnn模型。 Bellow我有一个代码,我试图打印一些例子来测试训练的模型。在我生成的代码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 ...')

1 个答案:

答案 0 :(得分:2)

查看GitHub上的引用代码,我找不到output_props,因此版本可能不同。但是,由于mtest.initial_state@property,我认为mtest.output_props也是{1}}。也就是说,试试

probs, state = sess.run([mtest.output_probs, mtest.final_state], feed_dict=feed_dict)

相反,即不使用括号。

mtest._final_state也是内部变量,不应直接使用。您可能希望改为使用mtest.final_state