composer.utils.batch_helpers#
Helpers to get items and set items in a batch.
Functions
Indexes into the batch given the key. |
|
Indexes into the batch given the key and sets the element at that index to value. |
- composer.utils.batch_helpers.batch_get(batch, key)[source]#
Indexes into the batch given the key.
>>> from composer.utils.batch_helpers import batch_get >>> batch_get([1,2,3], 1) 2 >>> batch_get({'a':1, 'b':7}, 'b') 7 >>> batch_get([{'a':1, 'b':7},{'c':5}], lambda x: x[1]['c']) 5 >>> batch_get([{'a':1, 'b':7},{'c':5}], (lambda x: x[1]['c'], lambda x: 10)) 5
- Parameters
batch (Any) โ An object that contains the input and label of the items in the batch. Can be any abritrary type that user creates, but we assume some sort of sequence (list, tuple, tensor, array), mapping (dictionary), or attribute store (object with data members, namedtuple).
key (str | int | Tuple[Callable, Callable] | Any, optional) โ A key to index into the batch or a user-specified function to do the extracting. A pair of callables is also supported for cases where a get and set function pair are both passed (like in Algorithms). The getter is assumed to be the first of the pair.
- Returns
The part of the batch specified by the key. This could be any type โ depending on what the batch is composed of.
- composer.utils.batch_helpers.batch_set(batch, key, value)[source]#
Indexes into the batch given the key and sets the element at that index to value.
This is not an in-place operation for batches of type tuple as tuples are not mutable.
>>> from composer.utils.batch_helpers import batch_set >>> batch_set([1,2,3], key=1, value=8) [1, 8, 3] >>> batch_set({'a':1, 'b':7}, key='b', value=11) {'a': 1, 'b': 11} >>> def setter(batch, value): ... batch[1]['d'] = value ... return batch ... >>> batch_set([{'a':1, 'b':7},{'d':3}], key=setter, value=20) [{'a': 1, 'b': 7}, {'d': 20}] >>> batch_set([{'a':1, 'b':7},{'d':3}], key=(lambda x: x[0]['b'], setter), value=20) [{'a': 1, 'b': 7}, {'d': 20}]
- Parameters
batch (Any) โ An object that contains the input and label of the items in the batch. Can be any abritrary type that user creates, but we assume some sort of sequence (list, tuple, tensor, array), mapping (dictionary), or attribute store (object with data members, namedtuple).
key (str | int | Tuple[Callable, Callable] | Any, optional) โ A key to index into the batch or a user-specified function to do the setting. A pair of callables is also supported for cases where a get and set function pair are both passed (like in Algorithms). The setter is assumed to be the second of the pair.
value (Any) โ The value that batch[key] or batch.key gets set to.
- Returns
batch (Any) โ updated batch with value set at key.