gpt4 book ai didi

python - Tensorflow Session.Run() 张量对象不可调用

转载 作者:太空宇宙 更新时间:2023-11-03 11:21:35 26 4
gpt4 key购买 nike

我有一个为 ptb example 训练的 rnn 模型使用 tensorflow 的 ptb_word .下面我有一个代码,我试图在其中打印一些示例来测试经过训练的模型。当我在 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() 张量对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42002083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com