0

这是我的代码:

W_conv1 = tf.Variable(tf.truncated_normal([3,3,3,21], stddev=0.1), name = 'W')
b_conv1 = tf.Variable(tf.constant(0.1, shape=[21]))
W_conv2 = tf.Variable(tf.truncated_normal([3,3,21,63], stddev=0.1), name = 'W')
b_conv2 = tf.Variable(tf.constant(0.1, shape=[63]))

sess = tf.Session()
init = tf.global_variables_initializer()
init_lo = tf.local_variables_initializer()
sess.run(init_lo)
sess.run(init)

h_conv1 = tf.nn.relu(conv2d(x_, W_conv1) + b_conv1) 
h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2)
sess.run(h_conv2)

错误是:

Caused by op u'Variable_2/read', defined at:
File "7.py", line 78, in <module>
b_conv2 = tf.Variable(tf.constant(0.1, shape=[63]))
File "/home/mcuee/.local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 197, in __init__
expected_shape=expected_shape)
File "/home/mcuee/.local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 316, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/home/mcuee/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1338, in identity
result = _op_def_lib.apply_op("Identity", input=input, name=name)
File "/home/mcuee/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
op_def=op_def)
File "/home/mcuee/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/mcuee/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use   uninitialized value Variable_2
 [[Node: Variable_2/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_2"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_2)]]

我运行我的代码,然后我初始化了值,但出了点问题,我不知道为什么,以及如何处理它。这是我第二次运行代码,我只是在其中添加了一些东西,但是卷积层并没有改变任何东西。是另一个代码的问题吗?

4

1 回答 1

0

我怀疑调用conv2d添加了更多变量。如果conv2d在您的代码片段中是 的简写tf.layers.conv2d,那么就是这种情况。

您想在定义它们之后初始化所有变量,例如:

W_conv1 = tf.Variable(tf.truncated_normal([3,3,3,21], stddev=0.1), name = 'W')
b_conv1 = tf.Variable(tf.constant(0.1, shape=[21]))
W_conv2 = tf.Variable(tf.truncated_normal([3,3,21,63], stddev=0.1), name = 'W')
b_conv2 = tf.Variable(tf.constant(0.1, shape=[63]))

sess = tf.Session()


h_conv1 = tf.nn.relu(conv2d(x_, W_conv1) + b_conv1) 
h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2)

init = tf.global_variables_initializer()
init_lo = tf.local_variables_initializer()
sess.run(init_lo)
sess.run(init)

sess.run(h_conv2)

有帮助的霍尔普。

于 2017-05-23T18:18:22.547 回答