gpt4 book ai didi

machine-learning - tensorflow 错误: FailedPeconditionError: attempting to use uninitialized variable

转载 作者:行者123 更新时间:2023-11-30 08:32:32 28 4
gpt4 key购买 nike

我想将立体图像放入优化器中。这是我的代码:

tf.reset_default_graph()

# config
learning_rate = 0.5
training_epochs = 5

# init
init = tf.global_variables_initializer()

def conv2d(input_layer):
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[3, 3],
padding='same',
activation=tf.tanh,
use_bias=False
)

conv2 = tf.layers.conv2d(
inputs=conv1,
filters=32,
kernel_size=[3, 3],
padding='same',
activation=tf.tanh,
use_bias=False
)

conv3 = tf.layers.conv2d(
inputs=conv2,
filters=32,
kernel_size=[3, 3],
padding='same',
activation=tf.tanh,
use_bias=False
)

conv4 = tf.layers.conv2d(
inputs=conv3,
filters=32,
kernel_size=[3, 3],
padding='same',
activation=tf.tanh,
use_bias=False
)

logits = tf.layers.conv2d(
inputs=conv4,
filters=32,
kernel_size=[3, 3],
padding='same',
activation=tf.sigmoid,
use_bias=False
)

return logits


if __name__ == '__main__':
# read images
# preprocessing: rgb converted to float, zero_mean, uni_variance
images = reading_images()
mask_tensor = images["mask"][1]

# reshape images
img0 = images["img0"][1]
img1 = images["img1"][1]
img0_rs = tf.reshape(img0, [1, int(1988 / 2), int(2880 / 2), 3])
img1_rs = tf.reshape(img1, [1, int(1988 / 2), int(2880 / 2), 3])

# define symbolic placeholders
t_im0 = tf.placeholder(tf.float32, [1, None, None, 3])
t_im1 = tf.placeholder(tf.float32, [1, None, None, 3])
t_img = tf.concat([t_im0, t_im1], axis=3)

input_layer = tf.reshape(t_img, [1, int(1988 / 2), int(2880 / 2), 6])
logits = conv2d(input_layer)

with tf.name_scope("cost_function") as scope:
mask_tensor = tf.tile(mask_tensor, [1, 1, 3])
cost_function = -tf.reduce_mean(mask_tensor * tf.log(logits) + (1. - mask_tensor) * tf.log(1. - logits))
tf.summary.scalar("cost_function", cost_function)

with tf.name_scope("train") as scope:
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)
merged_summary_op = tf.summary.merge_all()

with tf.Session() as sess:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

sess.run(init)
# summary_writer = tf.summary.FileWriter('/tmp/tensorflow_logs', graph=sess.graph)
for epoch in range(training_epochs):
print("epoch ", epoch)

avg_cost = 0.0
mask = sess.run(mask_tensor)
np_img0_rs = sess.run(img0_rs)
np_img1_rs = sess.run(img1_rs)

# res = t_img.eval(feed_dict={t_im0: img0_rs_, t_im1: img1_rs_})

sess.run([optimizer], feed_dict={t_im0: np_img0_rs, t_im1: np_img1_rs})
coord.request_stop()
coord.join(threads)

但我总是收到这个错误。我不知道我必须改变什么。我可以尝试什么来调试它?我真的做了很多尝试来修复这个错误。

epoch  0
2017-07-17 10:26:03.719539: W tensorflow/core/kernels/queue_base.cc:294] _4_input_producer: Skipping cancelled enqueue attempt with queue not closed
2017-07-17 10:26:03.719610: W tensorflow/core/kernels/queue_base.cc:294] _5_input_producer_1: Skipping cancelled enqueue attempt with queue not closed
Traceback (most recent call last):
File "/home/test/Dropbox/occlusion_thesis/occ_small/main.py", line 111, in <module>
sess.run([optimizer], feed_dict={t_im0: np_img0_rs, t_im1: np_img1_rs})
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 789, in run
run_metadata_ptr)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 997, in _run
feed_dict_string, options, run_metadata)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
target_list, options, run_metadata)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1152, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value conv2d_4/kernel
[[Node: conv2d_4/kernel/read = Identity[T=DT_FLOAT, _class=["loc:@conv2d_4/kernel"], _device="/job:localhost/replica:0/task:0/cpu:0"](conv2d_4/kernel)]]

Caused by op u'conv2d_4/kernel/read', defined at:
File "/home/test/Dropbox/occlusion_thesis/occ_small/main.py", line 84, in <module>
logits = conv2d(input_layer)
File "/home/test/Dropbox/occlusion_thesis/occ_small/main.py", line 60, in conv2d
use_bias=False
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/layers/convolutional.py", line 551, in conv2d
return layer.apply(inputs)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 492, in apply
return self.__call__(inputs, *args, **kwargs)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 434, in __call__
self.build(input_shapes[0])
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/layers/convolutional.py", line 137, in build
dtype=self.dtype)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 374, in add_variable
trainable=trainable and self.trainable)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1065, in get_variable
use_resource=use_resource, custom_getter=custom_getter)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 962, in get_variable
use_resource=use_resource, custom_getter=custom_getter)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 367, in get_variable
validate_shape=validate_shape, use_resource=use_resource)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 352, in _true_getter
use_resource=use_resource)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 725, in _get_single_variable
validate_shape=validate_shape)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 200, in __init__
expected_shape=expected_shape)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 319, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1303, in identity
result = _op_def_lib.apply_op("Identity", input=input, name=name)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/test/Programs/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
self._traceback = _extract_stack()

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

最佳答案

我不确定您的代码是否完整,但错误消息对我来说似乎很清楚:

FailedPreconditionError: Attempting to use uninitialized value conv2d_4/kernel

查看您的代码,我发现您有 sess.run(init) 但我在任何地方都找不到 init 的定义。尝试在 with tf.Session() as sess: 之前添加 init = tf.global_variables_initializer(),这应该可以修复“未初始化值”错误。

编辑:通过完整的代码,我发现问题来自:

# init
init = tf.global_variables_initializer() # <<<<<<<<< 1

def conv2d(input_layer):
## Bunch of code defining layers
return logits

if __name__ == '__main__':
## bunch of other code

logits = conv2d(input_layer) # <<<<<<<<< 2

我将您为到该点定义的所有变量定义初始化函数的点标记为1,并将该点标记为2您实际定义网络(以及其中的所有变量)的地方。 init 的定义必须完成所有变量的定义之后,否则将会有未初始化的变量。

更新:

我在这里复制我对答案所做的评论,因为它可能是一个更好的放置位置。必须在定义图表之后调用tf.global_variables_initializer()。如果您在开始时定义它,然后将层添加到网络中,添加的层的权重将不会被初始化,因为它们在您创建初始化操作时尚未定义。始终将 init 定义为 with tf.Session() ... 之前的最后一个操作,以确保您不会错过初始化中的任何内容。

关于machine-learning - tensorflow 错误: FailedPeconditionError: attempting to use uninitialized variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45139423/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com