Creates batches of tensors in `tensors`. The argument `tensors` can be a list or a dictionary of tensors. The value returned by the function will be of the same type as `tensors`. This function is implemented using a queue. A `QueueRunner` for the queue is added to the current `Graph`'s
(tensors, batch_size, num_threads=1, capacity=32,
enqueue_many=False, shapes=None, dynamic_pad=False,
allow_smaller_final_batch=False, shared_name=None, name=None)
| 927 | "`tf.data.Dataset.batch(batch_size)` (or `padded_batch(...)` if " |
| 928 | "`dynamic_pad=True`).") |
| 929 | def batch(tensors, batch_size, num_threads=1, capacity=32, |
| 930 | enqueue_many=False, shapes=None, dynamic_pad=False, |
| 931 | allow_smaller_final_batch=False, shared_name=None, name=None): |
| 932 | """Creates batches of tensors in `tensors`. |
| 933 | |
| 934 | The argument `tensors` can be a list or a dictionary of tensors. |
| 935 | The value returned by the function will be of the same type |
| 936 | as `tensors`. |
| 937 | |
| 938 | This function is implemented using a queue. A `QueueRunner` for the |
| 939 | queue is added to the current `Graph`'s `QUEUE_RUNNER` collection. |
| 940 | |
| 941 | If `enqueue_many` is `False`, `tensors` is assumed to represent a single |
| 942 | example. An input tensor with shape `[x, y, z]` will be output as a tensor |
| 943 | with shape `[batch_size, x, y, z]`. |
| 944 | |
| 945 | If `enqueue_many` is `True`, `tensors` is assumed to represent a batch of |
| 946 | examples, where the first dimension is indexed by example, and all members of |
| 947 | `tensors` should have the same size in the first dimension. If an input |
| 948 | tensor has shape `[*, x, y, z]`, the output will have shape `[batch_size, x, |
| 949 | y, z]`. The `capacity` argument controls the how long the prefetching is |
| 950 | allowed to grow the queues. |
| 951 | |
| 952 | The returned operation is a dequeue operation and will throw |
| 953 | `tf.errors.OutOfRangeError` if the input queue is exhausted. If this |
| 954 | operation is feeding another input queue, its queue runner will catch |
| 955 | this exception, however, if this operation is used in your main thread |
| 956 | you are responsible for catching this yourself. |
| 957 | |
| 958 | *N.B.:* If `dynamic_pad` is `False`, you must ensure that either |
| 959 | (i) the `shapes` argument is passed, or (ii) all of the tensors in |
| 960 | `tensors` must have fully-defined shapes. `ValueError` will be |
| 961 | raised if neither of these conditions holds. |
| 962 | |
| 963 | If `dynamic_pad` is `True`, it is sufficient that the *rank* of the |
| 964 | tensors is known, but individual dimensions may have shape `None`. |
| 965 | In this case, for each enqueue the dimensions with value `None` |
| 966 | may have a variable length; upon dequeue, the output tensors will be padded |
| 967 | on the right to the maximum shape of the tensors in the current minibatch. |
| 968 | For numbers, this padding takes value 0. For strings, this padding is |
| 969 | the empty string. See `PaddingFIFOQueue` for more info. |
| 970 | |
| 971 | If `allow_smaller_final_batch` is `True`, a smaller batch value than |
| 972 | `batch_size` is returned when the queue is closed and there are not enough |
| 973 | elements to fill the batch, otherwise the pending elements are discarded. |
| 974 | In addition, all output tensors' static shapes, as accessed via the |
| 975 | `shape` property will have a first `Dimension` value of `None`, and |
| 976 | operations that depend on fixed batch_size would fail. |
| 977 | |
| 978 | Args: |
| 979 | tensors: The list or dictionary of tensors to enqueue. |
| 980 | batch_size: The new batch size pulled from the queue. |
| 981 | num_threads: The number of threads enqueuing `tensors`. The batching will |
| 982 | be nondeterministic if `num_threads > 1`. |
| 983 | capacity: An integer. The maximum number of elements in the queue. |
| 984 | enqueue_many: Whether each tensor in `tensors` is a single example. |
| 985 | shapes: (Optional) The shapes for each example. Defaults to the |
| 986 | inferred shapes for `tensors`. |