python - 使用 tf.train.batch 时形状错误

标签 python tensorflow

我的数据形状是(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 is False, 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 is True, tensors is assumed to represent a batch of examples, where the first dimension is indexed by example, and all members of tensors 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]. The capacity argument controls the how long the prefetching is allowed to grow the queues.

因此,要回答您的问题,您必须将 enqueue_many 参数设置为 True 并且第一个维度将被丢弃,或者如果您让 enqueue_manyFalse 您必须迭代数组的第一个维度。

要回答你的第二个问题,tensors 输入在内部通过 convert_to_tensor 方法进行,因此 numpy 数组将转换为 TensorFlow 张量

关于python - 使用 tf.train.batch 时形状错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48605331/

相关文章:

python 输入设备fflush

python - ValueError : Cannot feed value of shape (64, ) 对于张量 'x:0' ,其形状为 '(?, 100, 100, 3)'

python - 调试 Tensorflow 卡在全局变量初始化上

python - Tensorflow - 检索字符串张量中的每个字符

python - pysnmp PrettyPrint 翻译 OID 名称

python - 在没有脚本文件的情况下将命令行参数传递给 python 解释器

python 请求返回类似文件的对象以进行流式传输

python - 在 Keras 中连接三个不同维度的输入

python - TensorFlow - 为什么这个 sofmax 回归没有学到任何东西?

python - 如何将 intel-mkl 与 tensorflow 一起使用