jacinle.nd.batch#

Functions

batchify(inputs)

Recursively combine a list of inputs into a batch.

unbatchify(inputs)

Recursively split a batch into a list of inputs.

Functions

batchify(inputs)[source]#

Recursively combine a list of inputs into a batch. This function handles tuples, lists, dicts, and numpy arrays.

Examples

>>> batchify([np.array([1, 2, 3]), np.array([4, 5, 6])])
array([[1, 2, 3],
        [4, 5, 6]])
>>> batchify([
...     {'a': np.array([1, 2, 3]), 'b': np.array([4, 5, 6])},
...     {'a': np.array([7, 8, 9]), 'b': np.array([10, 11, 12])},
... ])
{'a': array([[1, 2, 3],
            [7, 8, 9]]),
'b': array([[ 4,  5,  6],
            [10, 11, 12]])}
Parameters:

inputs (Sequence[Any]) – a list of inputs to be batched.

Returns:

a batched input.

Return type:

Any

unbatchify(inputs)[source]#

Recursively split a batch into a list of inputs. This function handles tuples, lists, dicts, and numpy arrays. This function is the inverse of batchify().

Example

>>> unbatchify(np.array([[1, 2, 3], [4, 5, 6]]))
[array([1, 2, 3]), array([4, 5, 6])]
>>> unbatchify({'a': np.array([[1, 2, 3], [4, 5, 6]]), 'b': np.array([[7, 8, 9], [10, 11, 12]])})
[{'a': array([1, 2, 3]), 'b': array([7, 8, 9])}, {'a': array([4, 5, 6]), 'b': array([10, 11, 12])}]
Parameters:

inputs (Any) – a batched input.

Returns:

a list of inputs.

Return type:

List[Any]