简体   繁体   English

Tensorflow-除非我重新创建“ saver”对象,否则无法初始化保存的变量。 为什么?

[英]Tensorflow - can't initialize saved variables unless I recreate the “saver” object. Why?

I'm pretty sure I'm missing something about how tensorflow works because my solution doesn't make any sense. 我很确定我缺少关于tensorflow如何工作的信息,因为我的解决方案没有任何意义。

I'm trying to train a neural network (from scratch, without using Estimators or other abstractions), save it, and load a simplified version of it for inference. 我正在尝试训练神经网络(从头开始,不使用Estimators或其他抽象方法),保存它,并加载其简化版本以进行推理。

The following code trains but gives me the error: FailedPreconditionError (see above for traceback): Attempting to use uninitialized value hidden0/biases/Variable [[Node: hidden0/biases/Variable/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](hidden0/biases/Variable)]] . 以下代码训练了错误,但给了我错误: FailedPreconditionError (see above for traceback): Attempting to use uninitialized value hidden0/biases/Variable [[Node: hidden0/biases/Variable/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](hidden0/biases/Variable)]] If I add the commented line - if I recreate the saver obect that I'm not going to use nor return - the code works just fine. 如果添加注释行-如果我重新创建了既不想使用也不返回的保护程序对象则代码可以正常工作。

Why do I need to create a (useless) saver object in order to restore the saved weights? 为什么需要创建一个(无用的)保护程序对象以恢复保存的权重?

import tensorflow as tf
import numpy as np

def add_fc_layer(input_tensor, input_dimensions, output_dimensions, layer_name, activation=None):
    with tf.variable_scope(layer_name):
        with tf.variable_scope('weights'):
            weights = tf.Variable(tf.truncated_normal([input_dimensions, output_dimensions]))
        with tf.variable_scope('biases'):
            biases = tf.Variable(tf.zeros([output_dimensions]))
        with tf.variable_scope('Wx_plus_b'):
            preactivate = tf.matmul(input_tensor, weights) + biases
        if activation is None:
            return preactivate
        with tf.variable_scope('activation'):
            activations = activation(preactivate)
        return activations


def make_network(model_phase):
    if model_phase not in {"train", "test"}:
        raise ValueError("invalid type")

    hidden0_units = 25
    hidden1_units = 15
    hidden2_units = 10
    input_size = 10
    output_size = 4

    with tf.variable_scope('InputVector'):
        inputs = tf.placeholder(shape=[1, input_size], dtype=tf.float32)

    hidden0_out = add_fc_layer(inputs, input_size, hidden0_units, "hidden0", activation=tf.nn.sigmoid)
    hidden1_out = add_fc_layer(hidden0_out, hidden0_units, hidden1_units, "hidden1", activation=tf.nn.sigmoid)
    hidden2_out = add_fc_layer(hidden1_out, hidden1_units, hidden2_units, "hidden2", activation=tf.nn.sigmoid)

    out =  add_fc_layer(hidden2_out, hidden2_units, output_size, "regression")

    if model_phase == "test":
        # UNCOMMENTIN THIS LINE MAKES THE SCRIPT WORK
        # saver = tf.train.Saver(var_list=tf.trainable_variables())
        return inputs, out

    saver = tf.train.Saver(var_list=tf.trainable_variables())

    with tf.variable_scope('training'):
        with tf.variable_scope('groundTruth'):
            ground_truth = tf.placeholder(shape=[1, output_size], dtype=tf.float32)
        with tf.variable_scope('loss'):
            loss = tf.reduce_sum(tf.square(ground_truth - out))
            tf.summary.scalar('loss', loss)
        with tf.variable_scope('optimizer'):
            trainer = tf.train.AdamOptimizer(learning_rate=0.001)
        with tf.variable_scope('gradient'):
            updateModel = trainer.minimize(loss)


    with tf.variable_scope('predict'):
        predict = tf.random_shuffle(tf.boolean_mask(out, tf.equal(out, tf.reduce_max(out, axis=None))))[0]


    writer = tf.summary.FileWriter('/tmp/test', tf.get_default_graph())


    return inputs, out, ground_truth, updateModel, writer, saver


train_graph = tf.Graph()
with tf.Session(graph=train_graph) as sess:
    tf.set_random_seed(42)
    inputs, out, ground_truth, updateModel, writer, saver = make_network(model_phase='train')
    init = tf.initialize_all_variables()
    sess.run(init)
    print('\nLearning...')
    for _ in range(10):
        sess.run([updateModel], feed_dict={inputs:np.arange(10)+np.random.random((1,10)), ground_truth:np.arange(4).reshape(1, 4)})
    saver.save(sess,'./tensorflowModel.ckpt')

new_graph = tf.Graph()
with tf.Session(graph=new_graph) as sess:
    inputs, out = make_network(model_phase='test')
    saver = tf.train.import_meta_graph('./tensorflowModel.ckpt.meta')
    saver.restore(sess, tf.train.latest_checkpoint('./'))

    # evaluation
    print('\nEvaluation...')
    for _ in range(10):
        _ = sess.run(out, feed_dict={inputs:np.arange(10).reshape(1,10)})

I don't know why creating an unused Saver makes the problem go away, but the code betrays a misunderstanding. 我不知道为什么创建一个未使用的Saver可以解决问题,但是代码出了误解。

When you are restoring, you are creating the model graph twice. 还原时,您将创建两次模型图。 First, you call make_network() which creates the computation graph and variables. 首先,调用make_network()来创建计算图和变量。 You then also call import_meta_graph which also creates a graph and variables. 然后,您还调用import_meta_graph ,它也会创建一个图形和变量。 You should create a saver with simple saver = tf.train.Saver() instead of saver = tf.train.import_meta_graph('./tensorflowModel.ckpt.meta') 您应该使用简单的saver = tf.train.Saver()创建一个saver而不是saver = tf.train.import_meta_graph('./tensorflowModel.ckpt.meta')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我需要在 TensorFlow 中初始化变量? - Why do I need to initialize variables in TensorFlow? 为什么我不能在Tensorflow中初始化或评估变量? - Why can't I initialize or evaluate my variable in Tensorflow? 无法使用 Saver 恢复 tensorflow model - Can't restore tensorflow model with Saver Tensorflow:为什么必须在声明变量后声明`saver = tf.train.Saver()`? - Tensorflow: Why must `saver = tf.train.Saver()` be declared after variables are declared? 无论我指定了哪个变量,Tensorflow Saver都会还原所有变量 - Tensorflow Saver restores all variables no matter which ones I specified 如何初始化除tf.global_variables_initializer()之外未保存的tensorflow变量 - How to initialize tensorflow variable that wasn't saved other than with tf.global_variables_initializer() Tensorflow Saver.save无法写入Docker共享卷 - Tensorflow Saver.save can't write to docker shared volume Tensorflow错误? 无法将单个保存的变量还原到使用所有变量保存的体系结构 - Tensorflow bug? Can't restore single saved variable to a architecture saved with all the variables 为什么Tensorflow初始化一个类中失败的变量? - Why tensorflow initialize the variables failed in a class? 为什么不能将一个变量初始化为对字符串起作用的其他两个变量的索引? - Why can't I initialize a variable to the indices of two other variables working on a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM