我有一个模型:
def __init__(params): seq2seq() { outputs, states = rnn.rnn(...) } def step() ... session.run(output_feed, input_feed)
模型的调用方式如下:
with tf.Session as sess: model = create_model(sess) (执行__init__,加载检查点) inputs = ... outputs = model.step(sess, inputs)
如何打印/保存/查看由rnn.rnn()返回的”states”的值?
我尝试过tf.Print(states[-1], [states[-1]]),它给我返回了张量的形状。
Tensor("model/seq2seq/Print:0", shape=TensorShape([Dimension(None), Dimension(4096)]), dtype=float32)
我尝试过states[-1].eval(),但这引发了一系列错误,例如:
Compute status: Invalid argument: You must feed a value for placeholder tensor 'encoder1' with dtype int32
我还尝试过将变量添加到模型中以返回它,但这不起作用:
def __init__(): ... self.state = state def step(): output_feed.append(self.state) result = session.run(output_feed, input_feed) return result
回答:
为了在eval方法中查看张量的值,你不能让图中的任何占位符产生依赖。在这种情况下,错误消息告诉你states[-1]
依赖于'encoder1'
。
你可以调用seesion.run并传入占位符的值,如下所示:
session.run(states[-1], feed_dict={encoder1:[#values for encoder1 here ]})
其中encoder1
是占位符对象。这应该会返回states[-1]
的值,然后你可以序列化保存它。
在你的具体情况下,encoder1
可能是rnn
内部的占位符,所以你可能需要运行类似于:
_, state_values = session.run([output_feed, states[-1]], input_feed)
以便在你运行的上下文中获取变量的值。