如何在Tensorflow运行时查看或保存一个张量?

我有一个模型:

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)

以便在你运行的上下文中获取变量的值。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注