【问题标题】:How can I see or save one tensor at runtime in Tensorflow?如何在 Tensorflow 运行时查看或保存一个张量?
【发布时间】:2016-04-04 22:22:58
【问题描述】:

我有一个模型:

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) (does __init__, loads checkpoint)
    inputs = ...
    outputs = model.step(sess, inputs)

如何打印/保存/查看 rnn.rnn() 返回的“状态”是什么?

我已经尝试过 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   

我也尝试将 var 添加到模型中以返回它,但这不起作用:

 def __init__():
     ...
     self.state = state 

 def step():
     output_feed.append(self.state)
     result = session.run(output_feed, input_feed)
     return result

【问题讨论】:

    标签: python machine-learning tensorflow


    【解决方案1】:

    为了在 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)
    

    在您运行它的上下文中获取变量的值。

    【讨论】:

    • 无效参数列表长约 40 项,我不确定“encoder1”仅通过堆栈跟踪指的是什么。
    • 你在我上次编辑之前运行过它吗?提供的示例中只有 2 个 args(包括 self 3 个)?
    • session.run() 行抛出错误:TypeError: Fetch argument [tensor, tensor, tensor....] has invalid type 'list], can't convert list into tensor
    • 谢谢,这个解决方案经过反复试验后奏效了!
    • 酷,很高兴为您提供帮助 :) 需要对我的示例进行任何编辑吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多