yang
yang

Reputation: 45

got wrong shape while using tf.train.batch

The shape of my data is (1920,60,2).
Assume the batch size is 128, expected the batch data shape will be (128,60,2).
but while using tf.train.batch, I got (128,1920,60,2),
does that mean I have to reshape the data first?

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 )

got (128,1920,60,2) as output.

Another question, tf.train.batch should input tensor, but why does it still work while I input numpy array?

Upvotes: 1

Views: 318

Answers (1)

pfm
pfm

Reputation: 6328

Ar per the string documentation for the default-to-False enqueue_many argument of the tf.train.batch method:

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.

So to answer your question, you'll have to set the enqueue_many argument to True and the first dimension will be discarded or if you let enqueue_many to False you'll have to iterate over the first dimension of your array.

To answer your second question, tensors input goes internally through the convert_to_tensor method and therefore numpy array will be converted into TensorFlow tensor.

Upvotes: 1

Related Questions