如何访问模型的恢复权重?

时间:2016-11-17 09:47:15

标签: python tensorflow

我希望能够访问模型的恢复权重。

我知道如何在训练后直接获得体重:

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial, name='weights')

with tf.variable_scope('conv1') as scope:
    W_conv1 = weight_variable([5, 5, 1, 32])
[train]
weights_ = W_conv1.eval()
print(weights_)

但是,以下情况不起作用:

saver.restore(sess, model_checkpoint_path)
with tf.variable_scope('conv1') as scope_conv:
    W_conv1 = tf.get_variable('weights', shape=[5, 5, 1, 32])
    weights_ = W_conv1.eval()
print(weights_)

它给出了

Model restored.
Traceback (most recent call last):
  File "./tf_mnist.py", line 122, in <module>
    weights_ = W_conv1.eval()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 445, in eval
    return self._variable.eval(session=session)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 559, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 3761, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 915, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 965, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 985, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value conv1/weights_1
     [[Node: conv1/weights_1/_52 = _Send[T=DT_FLOAT, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_4_conv1/weights_1", _device="/job:localhost/replica:0/task:0/gpu:0"](conv1/weights_1)]]
     [[Node: conv1/weights_1/_53 = _Recv[_start_time=0, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_4_conv1/weights_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

如何访问恢复的模型权重?

完整代码

执行以下两次以查看我的意思。第一次,模型将被训练并保存到磁盘(在下载数据后大约需要5分钟)。第二次运行恢复模型。

#!/usr/bin/env python

"""MNIST with Tensorflow."""

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

import os

epochs = 1000
model_checkpoint_path = 'checkpoints/mnist_tf_model.ckpt'


def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial, name='weights')


def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial, name='biases')


def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')


def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1], padding='SAME')

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

with tf.Session() as sess:
    x = tf.placeholder(tf.float32, shape=[None, 784])
    y_ = tf.placeholder(tf.float32, shape=[None, 10])
    x_image = tf.reshape(x, [-1, 28, 28, 1])

    with tf.variable_scope('conv1') as scope:
        W_conv1 = weight_variable([5, 5, 1, 32])
        b_conv1 = bias_variable([32])
        h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1, name='ReLU1')
    h_pool1 = max_pool_2x2(h_conv1)

    with tf.variable_scope('conv2') as scope:
        W_conv2 = weight_variable([5, 5, 32, 64])
        b_conv2 = bias_variable([64])
        h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2, name='ReLU2')
    h_pool2 = max_pool_2x2(h_conv2)

    with tf.variable_scope('fc1'):
        W_fc1 = weight_variable([7 * 7 * 64, 1024])
        b_fc1 = bias_variable([1024])

        h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
        h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

    with tf.variable_scope('dropout'):
        keep_prob = tf.placeholder(tf.float32)
        h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    with tf.variable_scope('softmax'):
        W_fc2 = weight_variable([1024, 10])
        b_fc2 = bias_variable([10])

        y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv),
                                                  reduction_indices=[1]))
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # Add ops to save and restore all the variables.
    saver = tf.train.Saver()

    sess.run(tf.initialize_all_variables())
    if not os.path.isfile(model_checkpoint_path):
        for i in range(epochs):
            batch = mnist.train.next_batch(50)
            train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

        # Save the variables to disk.
        save_path = saver.save(sess, model_checkpoint_path)
        print("Model saved in file: %s" % save_path)
        weights_ = W_conv1.eval()
        print(weights_)
    else:
        saver.restore(sess, model_checkpoint_path)
        print("Model restored.")
        summary_writer = tf.train.SummaryWriter('summary_dir', sess.graph)

        with tf.variable_scope('conv1') as scope_conv:
            W_conv1 = tf.get_variable('weights', shape=[5, 5, 1, 32])
            weights_ = W_conv1.eval()
            print(weights_)

1 个答案:

答案 0 :(得分:1)

您不应该混用tf.get_variabletf.Variable

您应该在开头将变量定义为:

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.get_variable('weights', initializer=initial)

然后像这样阅读:

with tf.variable_scope('conv1', reuse=True) as scope_conv:
        W_conv1 = tf.get_variable('weights', shape=[5, 5, 1, 32])
        weights_ = W_conv1.eval()
        print(weights_)

注意reuse=True参数。