batch_set#

composer.utils.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] | 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.