尝试使用未初始化的值:Tensorflow

时间:2019-01-28 16:13:53

标签: python tensorflow machine-learning

我正在从训练有素的模型中恢复权重,并尝试使用Tensorflow中预先训练的模型的权重来初始化另一个级别的某些层。我正在使用session.runget_tensor_by_name从预训练模型中获取权重值。我正在使用这些权重初始化tf.Variable。这是我的代码:

checkpoint_dir = "check_point_"   #directory that contains .meta, .index, checkpoint files
checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir)

graph = tf.Graph()
with graph.as_default():
    sess = tf.Session()
    with sess.as_default():
        # Load the saved meta graph and restore variables
        saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
        saver.restore(sess, checkpoint_file)

        # load and save data from model
        #f = open('weights.txt', 'ab')
        var = tf.global_variables()
        tf.initialize_all_variables().run()

        for v in var:
            print(v.name, end="\t")
            print(v.shape)
        gn = graph.get_tensor_by_name
        sr = sess.run
        v1 = sr(gn('Variable:0'))
        v2 = sr(gn('Variable_1:0'))
        v3 = sr(gn('Variable_2:0'))
        v4 = sr(gn('Variable_3:0'))
        v5 = sr(gn('Variable_4:0'))
        v6 = sr(gn('Variable_5:0'))
        v7 = sr(gn('Variable_6:0'))
        v8 = sr(gn('Variable_7:0'))
        print(type(v8))
        conv1 = sess.run(gn('Variable:0'))

        train_data_node = tf.placeholder(tf.float32, shape=(BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))

        conv1_weights = tf.Variable(v1, dtype=tf.float32)
        conv1_biases = tf.Variable(v2, dtype=tf.float32)
        # conv2_weights = tf.Variable(gn('Variable_2:0'))
        conv2_weights = tf.Variable(tf.truncated_normal(
            [5, 5, 32, 64], stddev=0.1,
            seed=SEED, dtype=tf.float32))
        conv2_biases = tf.Variable(v4, dtype=tf.float32)
        fc1_weights = tf.Variable(v5, dtype=tf.float32)
        fc1_biases = tf.Variable(v6, dtype=tf.float32)
        fc2_weights = tf.Variable(v7, dtype=tf.float32)
        fc2_biases = tf.Variable(v8, dtype=tf.float32)
        # fc2_biases = tf.Variable(tf.constant(
        #     0.1, shape=[NUM_LABELS], dtype=tf.float32))

        conv1 = tf.nn.conv2d(train_data_node, conv1_weights,
                                strides=[1, 1, 1, 1],
                                padding='SAME')
        # Bias and rectified linear non-linearity.
        relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))

        pool1 = tf.nn.max_pool(relu1,
                              ksize=[1, 2, 2, 1],
                              strides=[1, 2, 2, 1],
                              padding='SAME')
        conv2 = tf.nn.conv2d(pool1,
                            conv2_weights,
                            strides=[1, 1, 1, 1],
                            padding='SAME')
        relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
        pool2 = tf.nn.max_pool(relu2,
                              ksize=[1, 2, 2, 1],
                              strides=[1, 2, 2, 1],
                              padding='SAME')
        pool_shape = pool2.get_shape().as_list()
        reshape = tf.reshape(
            pool2,
            [pool_shape[0], pool_shape[1] * pool_shape[2] * pool_shape[3]])
        # Fully connected layer. Note that the '+' operation automatically
        # broadcasts the biases.
        hidden = tf.nn.relu(tf.matmul(reshape, fc1_weights) + fc1_biases)
        print(sr(conv1_weights))
        out = tf.matmul(hidden, fc2_weights) + fc2_biases

        print(sess.run(out, feed_dict={train_data_node: numpy.random.randn(200, 28, 28, 1)}))

我遇到以下错误:'Attempting to use uninitialized value Variable_9 [[{{node _retval_Variable_9_0_0}} = _Retval[T=DT_FLOAT, index=0, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Variable_9)]]'我在做什么错了?

1 个答案:

答案 0 :(得分:1)

1)如果我不得不猜测是因为您在做

tf.initialize_all_variables().run()

如果您从保护程序实例加载时我没错,则无需执行initialize_all_variables

2)如果您正在执行initialize_all_variables并且正在定义自己的自定义变量,则在定义了所有变量之后,您将要调用initialize_all_variables