我正在使用以下代码来训练和保存我的模型:
def _random_selection(test, target, batch_size):
examples, labels = zip(*random.sample(list(zip(test, target)), batch_size))
return (np.array(examples), labels)
#Input pipeline is to get random images from
#test or train data.
def input_pipeline(batch_size, test):
if test:
return _random_selection(X_test, y_test, batch_size)
return _random_selection(X_train, y_train, batch_size)
#train and test data are read from files and stored in array.
def cnn_model(input_images, batch_size):
def truncated_normal_var(name, shape, dtype):
return (tf.get_variable(name=name, shape=shape, dtype=dtype,
initializer=tf.truncated_normal_initializer(stddev=0.05)))
def zero_var(name, shape, dtype):
return (tf.get_variable(name=name, shape=shape, dtype=dtype, initializer=tf.constant_initializer(0.0)))
# First Convolutional Layer
with tf.variable_scope('conv1') as scope:
# Conv_kernel is 5x5 for all 1 channel and we will create 64 features
conv1_kernel = truncated_normal_var(name='conv_kernel1', shape=[5, 5, 3, 64], dtype=tf.float32)
# We convolve across the image with a stride size of 1
conv1 = tf.nn.conv2d(input_images, conv1_kernel, [1, 1, 1, 1], padding='SAME')
# Initialize and add the bias term
conv1_bias = zero_var(name='conv_bias1', shape=[64], dtype=tf.float32)
conv1_add_bias = tf.nn.bias_add(conv1, conv1_bias)
# ReLU element wise
relu_conv1 = tf.nn.relu(conv1_add_bias)
# Max Pooling
pool1 = tf.nn.max_pool(relu_conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool_layer1')
# Local Response Normalization
norm1 = tf.nn.lrn(pool1, depth_radius=5, bias=2.0, alpha=1e-3, beta=0.75, name='norm1')
# Second Convolutional Layer
with tf.variable_scope('conv2') as scope:
# Conv kernel is 5x5, across all prior 64 features and we create 64 more features
conv2_kernel = truncated_normal_var(name='conv_kernel2', shape=[5, 5, 64, 64], dtype=tf.float32)
# Convolve filter across prior output with stride size of 1
conv2 = tf.nn.conv2d(norm1, conv2_kernel, [1, 1, 1, 1], padding='SAME')
# Initialize and add the bias
conv2_bias = zero_var(name='conv_bias2', shape=[64], dtype=tf.float32)
conv2_add_bias = tf.nn.bias_add(conv2, conv2_bias)
# ReLU element wise
relu_conv2 = tf.nn.relu(conv2_add_bias)
print("relu_conv2 shape " + `relu_conv2.get_shape()`)
# Max Pooling
pool2 = tf.nn.max_pool(relu_conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool_layer2')
# Local Response Normalization (parameters from paper)
norm2 = tf.nn.lrn(pool2, depth_radius=5, bias=2.0, alpha=1e-3, beta=0.75, name='norm2')
# Reshape output into a single matrix for multiplication for the fully connected layers
reshaped_output = tf.reshape(norm2, [batch_size, -1])
reshaped_dim = reshaped_output.get_shape()[1].value
# First Fully Connected Layer
with tf.variable_scope('full1') as scope:
# Fully connected layer will have 384 outputs.
full_weight1 = truncated_normal_var(name='full_mult1', shape=[reshaped_dim, 384], dtype=tf.float32)
full_bias1 = zero_var(name='full_bias1', shape=[384], dtype=tf.float32)
full_layer1 = tf.nn.relu(tf.add(tf.matmul(reshaped_output, full_weight1), full_bias1))
# Second Fully Connected Layer
with tf.variable_scope('full2') as scope:
# Second fully connected layer has 192 outputs.
full_weight2 = truncated_normal_var(name='full_mult2', shape=[384, 192], dtype=tf.float32)
full_bias2 = zero_var(name='full_bias2', shape=[192], dtype=tf.float32)
full_layer2 = tf.nn.relu(tf.add(tf.matmul(full_layer1, full_weight2), full_bias2))
# Final Fully Connected Layer -> 10 categories for output (num_targets)
with tf.variable_scope('full3') as scope:
# Final fully connected layer has 10 (num_targets) outputs.
full_weight3 = truncated_normal_var(name='full_mult3', shape=[192, num_targets], dtype=tf.float32)
full_bias3 = zero_var(name='full_bias3', shape=[num_targets], dtype=tf.float32)
final_output = tf.add(tf.matmul(full_layer2, full_weight3), full_bias3)
return (final_output)
def training_loss(logits, targets):
# Get rid of extra dimensions and cast targets into integers
targets = tf.squeeze(tf.cast(targets, tf.int32))
# Calculate cross entropy from logits and targets
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=targets)
# Take the average loss across batch size
cross_entropy_mean = tf.reduce_mean(cross_entropy)
return (cross_entropy_mean)
def train_step(loss_value, generation_num):
# Our learning rate is an exponential decay (stepped down)
model_learning_rate = tf.train.exponential_decay(learning_rate, generation_num, num_gens_to_wait, lr_decay,
staircase=True)
# Create optimizer
my_optimizer = tf.train.GradientDescentOptimizer(model_learning_rate)
# Initialize train step
train_step = my_optimizer.minimize(loss_value)
return (train_step)
def accuracy_of_batch(logits, targets):
# Make sure targets are integers and drop extra dimensions
targets = tf.squeeze(tf.cast(targets, tf.int32))
# Get predicted values by finding which logit is the greatest
batch_predictions = tf.cast(tf.argmax(logits, 1), tf.int32)
# Check if they are equal across the batch
predicted_correctly = tf.equal(batch_predictions, targets)
# Average the 1's and 0's (True's and False's) across the batch size
accuracy = tf.reduce_mean(tf.cast(predicted_correctly, tf.float32))
return (accuracy)
images, targets = input_pipeline(batch_size, test=False)
test_images, test_targets = input_pipeline(batch_size, test=True)
使用tf.variable_scope(' model_definition')作为范围: #声明训练网络模型 model_output = cnn_model(images,batch_size) scope.reuse_variables() #声明测试模型输出 test_output = cnn_model(test_images,batch_size)
loss = training_loss(model_output, targets)
accuracy = accuracy_of_batch(test_output, test_targets)
generation_num = tf.Variable(0, trainable=False)
train_op = train_step(loss, generation_num)
init = tf.initialize_all_variables()
sess.run(init)
tf.train.start_queue_runners(sess=sess)
train_loss = []
test_accuracy = []
for i in range(generations):
sess.run([train_op, loss])
saver = tf.train.Saver()
save_path = saver.save(sess, "./tf.train.save")
在以下代码中,我正在恢复它
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, "tf.train.save")
init = tf.initialize_all_variables()
sess.run(init)
tf.initialize_all_variables()
with tf.variable_scope('model_definition') as scope:
output = cnn_model(imgs, batch_size)
#To get variable value.
sess.run(output)
当我运行代码恢复模型时,出现以下错误:
File "restore_tf.py", line 154, in <module>
sess.run(output)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 905, in run
run_metadata_ptr)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1137, in _run
feed_dict_tensor, options, run_metadata)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1355, in _do_run
options, run_metadata)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1374, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value model_definition/full3/full_bias3_1
[[Node: model_definition/full3/full_bias3_1/read = Identity[T=DT_FLOAT, _class=["loc:@model_definition/full3/full_bias3_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](model_definition/full3/full_bias3_1)]]
Caused by op u'model_definition/full3/full_bias3_1/read', defined at:
File "restore_tf.py", line 151, in <module>
output = cnn_model(imgs, batch_size)
File "restore_tf.py", line 127, in cnn_model
full_bias3 = zero_var(name='full_bias3', shape=[num_targets], dtype=tf.float32)
File "restore_tf.py", line 64, in zero_var
return (tf.get_variable(name=name, shape=shape, dtype=dtype, initializer=tf.constant_initializer(0.0)))
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1297, in get_variable
constraint=constraint)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1093, in get_variable
constraint=constraint)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 439, in get_variable
constraint=constraint)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 408, in _true_getter
use_resource=use_resource, constraint=constraint)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 800, in _get_single_variable
use_resource=use_resource)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 2157, in variable
use_resource=use_resource)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 2147, in <lambda>
previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 2130, in default_variable_creator
constraint=constraint)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 233, in __init__
constraint=constraint)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 381, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 131, in identity
return gen_array_ops.identity(input, name=name)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2051, in identity
"Identity", input=input, name=name)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3271, in create_op
op_def=op_def)
File "/home/devender/code/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1650, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value model_definition/full3/full_bias3_1
[[Node: model_definition/full3/full_bias3_1/read = Identity[T=DT_FLOAT, _class=["loc:@model_definition/full3/full_bias3_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](model_definition/full3/full_bias3_1)]]
这里缺少什么来从文件中恢复变量?