扩大CNN的输出

时间:2016-09-28 11:45:48

标签: tensorflow

我正在尝试将网络的输出从11输出扩展到12输出,我已经恢复了已经在11个输出上重新训练的先前检查点。我从here找到答案。使用它我发现如何更改输出图层的形状,扩展它以适应另一行权重,但我不知道我是否正确初始化重量和偏差。实际上我没有得到任何编译或运行时错误,但测试精度从95%下降到9%。可能是代码中某处出现了问题。这是代码:

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()

1 个答案:

答案 0 :(得分: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 ))  

为:

w_b_new_saver = tf.train.Saver()     
w_b_saver.restore(sess, ckpt)
print("restore complete")
sess.run(tf.initialize_variables( list( tf.get_variable(name) for name in sess.run( tf.report_uninitialized_variables( tf.all_variables( ) ) ) ) )) 
# Only initialize the unitialize variables
# That really long line will go through the variables and check which ones are not initialized
# If you already know which ones aren't initialized just pass those in directly
w_b_new_saver2 = tf.train.Saver() # Now you can make a new saver 
# in case you want to save this changed model with new weights
print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval() , test_labels,  force = False ))  

您重新初始化了导致精确度降低到9%的变量,因为您正在重置所有权重。