确定修改TensorFlow图的内容

时间:2017-06-26 20:08:24

标签: tensorflow

我试图使用tf.train.Supervisor(),但我收到以下错误:RuntimeError: Graph is finalized and cannot be modified.

问题显然是我在实例化主管后修改了我的图表(并由this other StackOverflow question确认),但它并没有告诉我如何查找在哪里我修改我的图表。虽然在我发布的另一个问题中,我发现.minimize()改变了图表,但我可能犯了类似的错误。我已经通过了我的代码并且看起来没有什么是明显的罪魁祸首。 / p>

这是我的代码。然而,虽然我有兴趣找到我的错误,但我也有兴趣学习如何解决这个问题。

tf.set_random_seed(seed=0)
supervisor = tf.train.Supervisor(logdir=tf.flags.FLAGS.log_dir,
                                 global_step=model.global_step)

# create coordinator to handle threading
coord = tf.train.Coordinator()

supervisor = tf.train.Supervisor(logdir=tf.flags.FLAGS.log_dir,
                                 global_step=global_step)

with supervisor.managed_session() as sess:

    # start threads to enqueue input minibatches for training
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    # initialize all variables and ops
    sess.run(tf.global_variables_initializer())

    start_time = time.time()

    # train
    for step in range(1, tf.flags.FLAGS.training_steps + 1):

        # check for any raised exceptions
        if supervisor.should_stop():
            break

        train_op(sess, model, x_train, y_train, y_lengths_train)

        # every 100 steps, evaluate model metrics and write summaries to disk
        if step % 10 == 0 or step == tf.flags.FLAGS.training_steps:
            eval_op(sess, model, x_valid, y_valid, y_lengths_valid, start_time)
            start_time = time.time()

    # when done, ask the threads to stop
    coord.request_stop()

    # wait for threads to finish
    coord.join(threads)

这是我的`train_op:

的代码
def train_op(sess, model, x_minibatch, y_minibatch, y_lengths_minibatch):
    """
    Training operation(s) for model.
    """

    # dequeue training data
    x_values, y_values, y_lengths_values = sess.run([x_minibatch, y_minibatch, y_lengths_minibatch])

    # permute training data for robustness
    x_values, y_values = permute_values(x_values, y_values)

    # minimize loss
    sess.run([model.train_op], feed_dict={model.x: x_values,
                                      model.y_actual: y_values,
                                      model.y_actual_lengths: y_lengths_values})

以下是我eval_op的代码:

def eval_op(sess, model, x_minibatch, y_minibatch, y_lengths_minibatch, start_time):
    """
    Evaluation operation(s) for model.
    """

    x_values, y_values, y_lengths_values = sess.run([x_minibatch, y_minibatch, y_lengths_minibatch])

    loss, metrics, step = sess.run([model.loss, model.metrics, model.global_step],
                                   feed_dict={model.x: x_values,
                                          model.y_actual: y_values,
                                          model.y_actual_lengths: y_lengths_values})

    print('Step Number: {}\tLength of Y: {}'.format(step + 1, y_lengths_values))

    print('Elapsed time: {:.2f}\tLoss: {:.4f}\tAccuracy: {:.4f}'.format(time.time() - start_time, loss,
                                                                    metrics['accuracy']))

1 个答案:

答案 0 :(得分:0)

使用RuntimeError打印的堆栈跟踪应该指示错误发生时调用的函数。

但是,根据您的代码,我怀疑是tf.global_variables_initializer()的调用是在您的图表中添加新操作。由于您使用tf.train.Supervisor,因此不需要调用它,因为主管(默认情况下)负责初始化变量。因此,您可以删除第sess.run(tf.global_variables_initializer())行。