我可以在会话之外或在创建一个会话之间使用钩子/回调吗? [英] Can I have hooks / callbacks outside a session or between the creation of one?

查看:21
本文介绍了我可以在会话之外或在创建一个会话之间使用钩子/回调吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 train_and_evaluate() 可以执行一个计划,根据我传递的规范训练和评估模型.我可以在 EvalSpecTrainSpec 但有限制.

With train_and_evaluate() it is possible to execute a schedule which trains and evaluates a model according to the specifications I am passing down. There are hooks I can register with EvalSpec and TrainSpec but there are limits.

问题是我可以有一个SessionRunHook,它用作回调,但总是 一个会话.

The problem is that I can only have a SessionRunHook, which will work as a callback, but always only within a session.

我的问题是我有一个更复杂的时间表.在评估期间,我还想量化模型并进一步评估该量化模型.这里的问题是,如前所述,如果我使用 SessionRunHook 之类的对象,我总是会话中.

My problem is that I am having a more complicated schedule. During evaluation I also want to quantize the model and further evaluate that quantized model. The problem here is, as mentioned, I am always in a session if I am using SessionRunHook-like objects.

所以问题是是否有办法使用 train_and_evaluate() 并在两者之间注册一些回调:

So the question is whether there is a way so use train_and_evaluate() and register some callbacks in between:

train_and_evaluate(..):

  # .. deep down ..

  while <condition>:
    with tf.Session() as train_sess:
      # Do training ..

    if the_callback_i_want:
      the_callback_i_want()

    with tf.Session() as eval_sess:
      # Do evaluation ..

这可能吗?

推荐答案

我猜你可以实现你自己的 SessionHook 子类的 begin 方法.

I guess you could implement the begin method of your own SessionHook subclass.

为了举例,我使用了 虹膜代码(参见本文档).

For the sake of the example I used the iris code (see this doc).

import tensorflow as tf

def the_callback_i_want():
    # You need to work in a new graph so let's create a new one
    g = tf.Graph()
    with g.as_default():
        x = tf.get_variable("x", ())
        x = tf.assign_add(x, 1)
        init = tf.global_variables_initializer()
        with tf.Session() as sess:   
            sess.run(init)
            print("I'm here !", sess.run(x))


class MyHook(tf.train.SessionRunHook):

  def begin(self):
    """Called once before using the session.

    When called, the default graph is the one that will be launched in the
    session.  The hook can modify the graph by adding new operations to it.
    After the `begin()` call the graph will be finalized and the other callbacks
    can not modify the graph anymore. Second call of `begin()` on the same
    graph, should not change the graph.
    """
    the_callback_i_want()


import iris_data
# Fetch the data
(train_x, train_y), (test_x, test_y) = iris_data.load_data()

# Feature columns describe how to use the input.
my_feature_columns = []
for key in train_x.keys():
    my_feature_columns.append(tf.feature_column.numeric_column(key=key))

# Build 2 hidden layer DNN with 10, 10 units respectively.
classifier = tf.estimator.DNNClassifier(
    feature_columns=my_feature_columns, hidden_units=[10, 10],  n_classes=3)

# Fetch the data
(train_x, train_y), (test_x, test_y) = iris_data.load_data()

# Feature columns describe how to use the input.
my_feature_columns = []
for key in train_x.keys():
    my_feature_columns.append(tf.feature_column.numeric_column(key=key))

train_spec = tf.estimator.TrainSpec(input_fn=lambda:iris_data.train_input_fn(train_x, train_y,
                                                 10), max_steps=100)
eval_spec = tf.estimator.EvalSpec(input_fn=lambda:iris_data.eval_input_fn(test_x, test_y,
                                                10), hooks=[MyHook()])
tf.estimator.train_and_evaluate(classifier, train_spec, eval_spec)

它打印:

INFO:tensorflow:Saving checkpoints for 100 into /var/folders/***/model.ckpt.
INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Starting evaluation at 2018-10-18-20:19:28
I'm here ! 1.9386581
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /var/folders/***/model.ckpt-100
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Finished evaluation at 2018-10-18-20:19:28

这篇关于我可以在会话之外或在创建一个会话之间使用钩子/回调吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆