我的数据形状是(1920,60,2)。
假设批量大小为128,预计批量数据形状为(128,60,2)。
但是在使用tf.train.batch
时,我得到了(128,1920,60,2),
这是否意味着我必须先 reshape 数据?
tf_X_train = tf.constant(X_train) # type(X_train):numpy.cdarray
tf_Y_train = tf.constant(Y_train)
tf_batch_xs, tf_batch_ys = tf.train.batch([tf_X_train, tf_Y_train], batch_size = 128, capacity = 5000)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
batch_xs, batch_ys = sess.run([tf_batch_xs, tf_batch_ys])
print (batch_xs.shape )
得到 (128,1920,60,2) 作为输出。
另一个问题,tf.train.batch
应该输入张量,但为什么当我输入numpy数组时它仍然有效?
最佳答案
根据 tf.train.batch
方法的默认为 False
enqueue_many
参数的字符串文档:
If
enqueue_many
isFalse
,tensors
is assumed to represent a single example. An input tensor with shape[x, y, z]
will be output as a tensor with shape[batch_size, x, y, z]
.If
enqueue_many
isTrue
,tensors
is assumed to represent a batch of examples, where the first dimension is indexed by example, and all members oftensors
should have the same size in the first dimension. If an input tensor has shape[*, x, y, z]
, the output will have shape[batch_size, x, y, z]
. Thecapacity
argument controls the how long the prefetching is allowed to grow the queues.
因此,要回答您的问题,您必须将 enqueue_many
参数设置为 True
并且第一个维度将被丢弃,或者如果您让 enqueue_many
为 False
您必须迭代数组的第一个维度。
要回答你的第二个问题,tensors
输入在内部通过 convert_to_tensor
方法进行,因此 numpy
数组将转换为 TensorFlow 张量
。
关于python - 使用 tf.train.batch 时形状错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48605331/