gpt4 book ai didi

python - 在使用 TensorFlow 实现的 AlexNet 中输入数据时出现维度错误

转载 作者:行者123 更新时间:2023-11-30 09:53:36 34 4
gpt4 key购买 nike

我正在尝试在 AlexNet 中使用 TensorFlow 提供自己的数据。我在训练时使用 (227 x 227) rgb 图像,BATCH_SIZE 为 50。以下是代码的一部分。我总是在 train_accuracy = precision.eval( ... )

行中收到错误
x = tf.placeholder(tf.float32, shape=[None, 227, 227, 3])
x_image = tf.reshape(x, [1, 227, 227, 3])
y_ = tf.placeholder(tf.float32, shape=[None, 5])


train_image_batch, train_label_batch = tf.train.batch([train_image, train_label], batch_size=BATCH_SIZE)
test_image_batch, test_label_batch = tf.train.batch([test_image, test_label], batch_size=BATCH_SIZE)


print train_label_batch.get_shape()
print y_.get_shape()
print "input pipeline ready"




cross_entropy = tf.reduce_mean(-tf.reduce_sum(train_y * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(train_y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))




init = tf.initialize_all_variables()

with tf.Session() as sess:

sess.run(init)

# initialize the queue threads to start to shovel data
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)


for i in range(2):
train_batch_image = sess.run(train_image_batch)
train_batch_label = sess.run(train_label_batch)

#if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: train_batch_image, y_: train_batch_label, keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: train_batch_image, y_: train_batch_label, keep_prob: 0.5})

test_batch_image = sess.run(train_image_batch)
test_batch_label = sess.run(train_label_batch)

print("test accuracy %g"%accuracy.eval(feed_dict={x: test_batch_image, y_: test_batch_label, keep_prob: 1.0}))



coord.request_stop()
coord.join(threads)
sess.close()

当前错误是:

Traceback (most recent call last):
File "tf_alexnet.py", line 294, in <module>
train_accuracy = accuracy.eval(feed_dict={x: train_batch_image, y_: train_batch_label, keep_prob: 1.0})
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 556, 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 3649, 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 382, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 655, in _run
feed_dict_string, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 723, in _do_run
target_list, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 743, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 7729350 values, but the requested shape has 154587
[[Node: Reshape = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_0, Reshape/shape)]]
Caused by op u'Reshape', defined at:
File "tf_alexnet.py", line 79, in <module>
x_image = tf.reshape(x, [1, 227, 227, 3])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1750, in reshape
name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2310, 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 1232, in __init__
self._traceback = _extract_stack()

最佳答案

问题是您将batch_size设置为50,但尝试将x reshape 为以下形式,就好像您的批量大小等于1一样。要解决该问题,请将形状中的 1 更改为 -1,它将保留输入的总大小。

关于python - 在使用 TensorFlow 实现的 AlexNet 中输入数据时出现维度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39947484/

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