我想将我的网络输出从11个输出扩展到12个输出,我恢复了之前已在11个输出上重新训练的检查点。我从here找到了答案。利用这一点,我发现如何改变输出图层的形状,将其展开以适应另一排权重,但我不知道是否我正确初始化了重量和偏差。实际上,我没有收到任何编译或运行时错误,但测试精度从95%下降到9%。代码中的某处可能有问题。下面的代码:扩大CNN的输出
w_b_not = {
'weight_4': tf.Variable(tf.random_normal([num_hidden, num_labels], stddev=0.1)),
'bias_4' : tf.Variable(tf.constant(1.0, shape=[num_labels])),}
w_b = {
'wc1_0': tf.Variable(tf.random_normal([patch_size_1, patch_size_1, num_channels, depth],stddev=0.1)),
.....
'bc1_0' : tf.Variable(tf.zeros([depth]))}
.... #here is the networks model
num_steps = 1001
with tf.Session(graph=graph) as sess:
ckpt = ('path_of_checkpoint.ckpt')
if os.path.isfile(ckpt) :
layer6_weights = tf.Variable(tf.random_normal([num_hidden, num_labels], stddev=0.1))
layer6_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]))
n_w_b = {
'new_layer_weights' : tf.concat(0,[w_b_not['weight_4'], layer6_weights]),
'new_layer_biases' : tf.concat(0,[w_b_not['bias_4'], layer6_biases])}
resize_var_1 = tf.assign(w_b_not['weight_4'], n_w_b['new_layer_weights'], validate_shape=False)
resize_var_2 = tf.assign(w_b_not['bias_4'], n_w_b['new_layer_biases'], validate_shape=False)
logits = tf.get_collection('logits')[0]
w_b_new_saver = tf.train.Saver()
init_op = tf.initialize_all_variables()
w_b_saver.restore(sess, ckpt)
print("restore complete")
for step in xrange(num_steps):
sess.run(init_op)
print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval() , test_labels, force = False))
删除后for循环我得到这个错误:
Traceback (most recent call last):
File "/home/owner/tensorflow/tensorflow/models/image/mnist/new_dataset/Nets.py", line 237, in <module>
print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval() , test_labels, force = False))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 502, 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 3334, 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 340, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 564, in _run
feed_dict_string, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 637, in _do_run
target_list, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 659, in _do_call
e.code)
FailedPreconditionError: Attempting to use uninitialized value Variable
[[Node: Variable/read = Identity[T=DT_FLOAT, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/gpu:0"](Variable)]]
[[Node: Softmax_2/_19 = _Recv[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_715_Softmax_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'Variable/read', defined at:
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/idlelib/run.py", line 116, in main
ret = method(*args, **kwargs)
File "/usr/lib/python2.7/idlelib/run.py", line 324, in runcode
exec code in self.locals
File "/home/owner/tensorflow/tensorflow/models/image/mnist/new_dataset/Nets.py", line 155, in <module>
'weight_4': tf.Variable(tf.random_normal([num_hidden, num_labels], stddev=0.1)),
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 206, in __init__
dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 275, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 609, in identity
return _op_def_lib.apply_op("Identity", input=input, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 655, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2154, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1154, in __init__
self._traceback = _extract_stack()
任何想法可以帮助我吗? – peter