towhee.trainer.utils.trainer_utils.recursively_apply¶
- towhee.trainer.utils.trainer_utils.recursively_apply(func: ~typing.Callable, data: ~typing.Any, *args, test_type: ~typing.Callable = <function is_torch_tensor>, error_on_other_type: bool = False, **kwargs)[source]¶
Recursively apply a function on a data structure that is a nested list/tuple/dictionary of a given base type.
- Parameters:
func (
callable
) – The function to recursively apply.data (nested list/tuple/dictionary of
main_type
) – The data on which to applyfunc
*args – Positional arguments that will be passed to
func
when applied on the unpacked data.main_type (
type
, optional, defaults totorch.Tensor
) – The base type of the objects to which applyfunc
.error_on_other_type (
bool
, optional, defaults toFalse
) – Whether to return an error or not if after unpackingdata
, we get on an object that is not of typemain_type
. IfFalse
, the function will leave objects of types different thanmain_type
unchanged.**kwargs – Keyword arguments that will be passed to
func
when applied on the unpacked data.
- Returns:
The same data structure as
data
withfunc
applied to every object of typemain_type
.