【问题标题】:Tensorflow Estimator API: SummariesTensorFlow Estimator API:总结
【发布时间】:2017-02-10 17:00:55
【问题描述】:

我无法使用 Tensorflow 的 Estimator API 进行摘要。

Estimator 类非常有用,原因有很多:我已经实现了自己的类,它们非常相似,但我正在尝试切换到这个类。

这是代码示例:

import tensorflow as tf
import tensorflow.contrib.layers as layers
import tensorflow.contrib.learn as learn
import numpy as np

 # To reproduce the error: docker run --rm -w /algo -v $(pwd):/algo tensorflow/tensorflow bash -c "python sample.py"

def model_fn(x, y, mode):
    logits = layers.fully_connected(x, 12, scope="dense-1")
    logits = layers.fully_connected(logits, 56, scope="dense-2")
    logits = layers.fully_connected(logits, 4, scope="dense-3")

    loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y), name="xentropy")

    return {"predictions":logits}, loss, tf.train.AdamOptimizer(0.001).minimize(loss)


def input_fun():
    """ To be completed for a 4 classes classification problem """

    feature = tf.constant(np.random.rand(100,10))
    labels = tf.constant(np.random.random_integers(0,3, size=(100,)))

    return feature, labels

estimator = learn.Estimator(model_fn=model_fn, )

trainingConfig = tf.contrib.learn.RunConfig(save_checkpoints_secs=60)

estimator = learn.Estimator(model_fn=model_fn, model_dir="./tmp", config=trainingConfig)

# Works
estimator.fit(input_fn=input_fun, steps=2)

# The following code does not work

# Can't initialize saver

# saver = tf.train.Saver(max_to_keep=10) # Error: No variables to save

# The following fails because I am missing a saver... :(

hooks=[
        tf.train.LoggingTensorHook(["xentropy"], every_n_iter=100),
        tf.train.CheckpointSaverHook("./tmp", save_steps=1000, checkpoint_basename='model.ckpt'),
        tf.train.StepCounterHook(every_n_steps=100, output_dir="./tmp"),
        tf.train.SummarySaverHook(save_steps=100, output_dir="./tmp"),
]

estimator.fit(input_fn=input_fun, steps=2, monitors=hooks)

如您所见,我可以创建一个 Estimator 并使用它,但我可以在拟合过程中添加挂钩。

日志钩子可以正常工作,但其他的需要 tensorssaver,我无法提供。

张量是在模型函数中定义的,因此我无法将它们传递给 SummaryHook 并且 Saver 无法初始化,因为没有张量可用于保存...

我的问题有解决方案吗? (我猜是的,但是 tensorflow 文档中缺少这部分的文档)

  • 如何初始化我的保护程序?或者我应该使用其他对象,例如 Scaffold
  • 如何将 summaries 传递给 SummaryHook,因为它们是在我的模型函数中定义的?

提前致谢。

PS:我看过 DNNClassifier API,但我想将估计器 API 用于卷积网络和其他方法。我需要为任何估算器创建摘要。

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    预期的用例是让 Estimator 为您保存摘要。 RunConfig 中有用于配置摘要编写的选项。当constructing the Estimator 时,RunConfigs 会被传递。

    【讨论】:

    • 好的,我明白了。但是如何定义要保存的摘要呢?我应该在模型函数中使用标准的标量汇总函数吗?
    • 是的,这些应该被添加到摘要集合中并自动保存。
    • @Allen Lavoie 我在 tensorflow 快速入门指南/使用估算器上找不到此信息,你们应该明确说明:“将您的摘要添加到集合中 - 就是这样”
    • @Pietrko 感谢您的反馈。你介意opening a Github issue(或者如果你有特定的改变,可以提出拉取请求)?
    • @Allen Lavoie 我想到的更改更多地与记录 tensorflow API 和 HOW-TO 文档上的现有行为有关,而不是代码更改。如果我找到文档后,我很乐意为文档打开一个 Github 问题。
    【解决方案2】:

    只需在model_fn 中添加tf.summary.scalar("loss", loss),然后运行没有summary_hook 的代码。损失被记录并显示在张量板上。


    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      相关资源
      最近更新 更多